checkpoint
[sbp.git] / src / edu / berkeley / sbp / tib / Tib.java
index 7b3270f..915158b 100644 (file)
@@ -30,24 +30,34 @@ public class Tib implements Token.Stream<CharToken> {
     public Tib(String s) throws IOException, Invalid { this(new StringReader(s)); }
     public Tib(Reader r) throws IOException, Invalid { this(new BufferedReader(r)); }
     public Tib(InputStream is) throws IOException, Invalid { this(new BufferedReader(new InputStreamReader(is))); }
-    public Tib(BufferedReader br) throws IOException, Invalid { cur = parse(br); }
+    public Tib(BufferedReader br) throws IOException, Invalid {
+        cur = parse(br);
+        System.out.println("\rparsing: \"" + cur.toString(0, -1) + "\"");
+    }
 
     private Block cur;
     private String s = null;
     int pos = 0;
     int spos = 0;
 
+    int _row = 0;
+    int _col = 0;
     public CharToken next() throws IOException {
+        if (cur==null) return null;
+        if (s != null) {
+            if (spos < s.length()) {
+                char c = s.charAt(spos++);
+                if (c=='\n') { _row++; _col = 0; }
+                else _col++;
+                return new CharToken(c, _row, _col);
+            }
+            s = null;
+        }
         if (pos >= cur.size()) {
             pos = cur.iip+1;
             cur = cur.parent;
-            return CharToken.right;
-        }
-
-        if (s != null) {
-            if (spos < s.length())
-                return new CharToken(s.charAt(spos++), 0, 0);
-            s = null;
+            if (cur==null) return null;
+            return CharToken.right(_row, _col);
         }
         Object o = cur.child(pos++);
         if (o instanceof String) {
@@ -55,6 +65,11 @@ public class Tib implements Token.Stream<CharToken> {
             s = (String)o;
             return next();
         }
+        if (o instanceof Block) {
+            Block b = (Block)o;
+            _row = b.row;
+            _col = b.col;
+        }
         if (((Block)o).isLiteral()) {
             spos = 0;
             s = ((Block.Literal)o).text();
@@ -62,7 +77,7 @@ public class Tib implements Token.Stream<CharToken> {
         }
         cur = (Block)o;
         pos = 0;
-        return CharToken.left;
+        return CharToken.left(_row, _col);
     }
 
     public static Block parse(BufferedReader br) throws Invalid, IOException {