X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTib.java;h=9a546e9d149978da05c19ae6eadbc574b9b1864b;hp=167ad8b60deacb81085cd4662b8f456b230d8e36;hb=c130bd41b47c14693a9c4980b39bcddb1b7198e7;hpb=2724e3d1df836f6ce40bc4227f1509e777de2e48 diff --git a/src/edu/berkeley/sbp/tib/Tib.java b/src/edu/berkeley/sbp/tib/Tib.java index 167ad8b..9a546e9 100644 --- a/src/edu/berkeley/sbp/tib/Tib.java +++ b/src/edu/berkeley/sbp/tib/Tib.java @@ -25,18 +25,34 @@ import java.io.*; * possible (it is unclear whether or not the associated grammar is * context-free). */ -public class Tib /*implements Token.Stream>*/ { +public class Tib /*implements Token.Stream*/ { /* - 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 { @@ -92,6 +108,7 @@ public class Tib /*implements Token.Stream>*/ { Block parent; public final int row; public final int col; + public final int iip; private final Vec children = new Vec(); private String pending = ""; @@ -106,8 +123,18 @@ public class Tib /*implements Token.Stream>*/ { 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) { @@ -188,6 +215,7 @@ public class Tib /*implements Token.Stream>*/ { 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();