checkpoint
[sbp.git] / src / edu / berkeley / sbp / tib / Tib.java
index 167ad8b..9a546e9 100644 (file)
@@ -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<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 {
@@ -92,6 +108,7 @@ public class Tib /*implements Token.Stream<TreeToken<String>>*/ {
                       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<TreeToken<String>>*/ {
         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<TreeToken<String>>*/ {
             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();