int _row = 0;
int _col = 0;
+ public Token.Location getLocation() { return new CharToken.CartesianLocation(_row, _col); }
public CharToken next() throws IOException {
if (cur==null) return null;
if (s != null) {
char c = s.charAt(spos++);
if (c=='\n') { _row++; _col = 0; }
else _col++;
- return new CharToken(c, _row, _col);
+ return new CharToken(c);
}
s = null;
}
pos = cur.iip+1;
cur = cur.parent;
if (cur==null) return null;
- return CharToken.right(_row, _col);
+ return CharToken.right;
}
Object o = cur.child(pos++);
if (o instanceof String) {
}
cur = (Block)o;
pos = 0;
- return CharToken.left(_row, _col);
+ return CharToken.left;
}
public static Block parse(BufferedReader br) throws Invalid, IOException {
public static class Grammar extends MetaGrammar {
private int anon = 0;
+ private final Element ws = Repeat.maximal0(nonTerminal("w"));
+ public Grammar() { dropAll.add(ws); }
public Object walk(Tree<String> tree) {
String head = tree.head();
if (tree.numChildren()==0) return super.walk(tree);
- if ("{".equals(head))
- return nonTerminal("braced"+(anon++),
+ if ("{".equals(head)) {
+ String s = "braced"+(anon++);
+ Union u = nonTerminal(s);
+ Union u2 = ((PreSequence)walk(tree, 0)).sparse(ws).buildUnion();
+ u2.add(Sequence.singleton(new Element[] { u }, 0, null, null));
+ return nonTerminal(s,
new PreSequence[][] {
new PreSequence[] {
new PreSequence(new Element[] { CharToken.leftBrace,
- Repeat.maximal(Repeat.many0(nonTerminal("w"))),
- ((PreSequence)walk(tree, 0)).buildUnion(),
- Repeat.maximal(Repeat.many0(nonTerminal("w"))),
+ ws,
+ u2,
+ ws,
CharToken.rightBrace
})
}
},
false,
false);
+ }
return super.walk(tree);
}
}