public final char c;
public final Location location;
- private CharToken(char c, int line, int col) { this(c, new CartesianLocation(line, col)); }
+ CharToken(char c, int line, int col) { this(c, new CartesianLocation(line, col)); }
private CharToken(char c, Location loc) { this.c = c; this.location = loc; }
public String result() { return c+""; }
public Location getLocation() { return location; }
private HashMap<String,Union> nt;
private int anon = 0;
private String startSymbol;
+ private boolean strings;
- public MetaGrammar() { this("s"); }
- public MetaGrammar(String s) { done(s); }
+ private Element set(Range.Set r) { return CharToken.set(r); }
+ private Element string(String s) { return CharToken.string(s); }
+ private Topology top() { return CharToken.top(); }
+ private Atom leftBrace() { return CharToken.leftBrace; }
+ private Atom rightBrace() { return CharToken.rightBrace; }
+
+ public MetaGrammar() { this("s", false); }
+ public MetaGrammar(String s) { this(s, false); }
+ public MetaGrammar(String s, boolean strings) { this.strings = strings; done(s); }
public Union done() { return done("s"); }
public Union done(String str) {
Union ret = g;
return nonTerminal("braced"+(anon++),
new PreSequence[][] {
new PreSequence[] {
- new PreSequence(CharToken.leftBrace),
+ new PreSequence(leftBrace()),
p,
- new PreSequence(CharToken.rightBrace)
+ new PreSequence(rightBrace())
}
},
false,
public MetaGrammar grammar(Object o, Union[] u, Object x) { return this; }
public char _backslash_n() { return '\n'; }
public char _backslash_r() { return '\r'; }
- public Object literal(String s) { return new MyDrop(CharToken.string(s)); }
+ public Object literal(String s) { return new MyDrop(string(s)); }
public Range _minus_(char a, char b) { return new Range(a, b); }
public Range range(char a) { return new Range(a, a); }
public Element _leftbracket_ (Range[] rr) { return ranges(true, rr); }
for(Range r : rr)
if (positive) ret.add(r);
else ret.remove(r);
- return CharToken.set(ret);
+ return set(ret);
}
- public static class MyLift {
+ public class MyLift {
public final String s;
public MyLift(String s) { this.s = s; }
}
- public static class MyDrop {
+ public class MyDrop {
public final Object o;
public MyDrop(Object o) { this.o = o; }
}
- public static class PreSequence {
+ public class PreSequence {
public PreSequence not(Sequence s) { not.add(s); return this; }
public PreSequence and(Sequence s) { and.add(s); return this; }
public final HashSet<Sequence> and = new HashSet<Sequence>();
if (oi instanceof MyDrop) { oi = ((MyDrop)oi).o; drops[j] = true; }
if (oi==SELF) o2[j] = u.new Subset("(("+u+"))", set);
- else if (oi instanceof MyLift) { o2[j] = CharToken.string(tag = ((MyLift)oi).s); drops[j] = o.length>1; }
+ else if (oi instanceof MyLift) { o2[j] = string(tag = ((MyLift)oi).s); drops[j] = o.length>1; }
else o2[j] = (Element)oi;
if (MetaGrammar.dropAll.contains(o2[j])) drops[j] = true;
* possible (it is unclear whether or not the associated grammar is
* context-free).
*/
-public class Tib /*implements Token.Stream<TreeToken<String>>*/ {
+public class Tib /*implements Token.Stream<CharToken>*/ {
/*
- public final String str;
-
public Tib(String s) { this(new StringReader(s)); }
public Tib(Reader r) { this(new BufferedReader(r)); }
public Tib(InputStream is) { this(new BufferedReader(new InputStreamReader(is))); }
- public Tib(BufferedReader br) { str = parse(br).toString(0,-1); }
+ public Tib(BufferedReader br) { cur = parse(br).toString(0,-1); }
+
+ boolean left = false;
+ boolean right = false;
+ private Block cur;
+ int pos = 0;
public Token next() throws IOException {
- if (pos >= str.length()) return Atom.EOF;
- return Atom.fromChar(str.charAt(pos++));
+ if (left) { left = false; return new CharToken(-3); }
+ if (right) { right = false; return new CharToken(-4); }
+
+ if (pos >= cur.size()) {
+ pos = cur.iip+1;
+ cur = cur.parent;
+ return new CharToken(-4);
+ }
+
+ Object o = cur.child(pos++);
+ if (o instanceof String) return new StringToken((String)o);
+ if (o.isLiteral()) return ((Block.Literal)o).text();
+ cur = (Block)b;
+ pos = 0;
+ return new CharToken(-3);
}
public static Block parse(BufferedReader br) throws Invalid {
Block parent;
public final int row;
public final int col;
+ public final int iip;
private final Vec children = new Vec();
private String pending = "";
public Object child(int i) { return children.elementAt(i); }
public boolean isLiteral() { return false; }
- protected Block(int row, int col) { this.row=row; this.col=col; parent = null; }
- public Block(Block parent, int row, int col) { this.row=row; this.col=col; (this.parent = parent).add(this); }
+ protected Block(int row, int col) {
+ this.row=row;
+ this.col=col;
+ this.iip = -1;
+ parent = null;
+ }
+ public Block(Block parent, int row, int col) {
+ this.row=row;
+ this.col=col;
+ this.iip = parent.size();
+ (this.parent = parent).add(this);
+ }
public void add(String s) { children.addElement(s); }
public void add(char c) {
public Block close() { return parent; }
public Block closeIndent() { return close(); }
public void add(String s) { if (content.length()>0) content.append('\n'); content.append(s); }
+ public String text() { return content.toString(); }
protected String toString(int indent, int justificationLimit) {
StringBuffer ret = new StringBuffer();
String s = content.toString();