checkpoint
authoradam <adam@megacz.com>
Thu, 15 Dec 2005 06:58:25 +0000 (01:58 -0500)
committeradam <adam@megacz.com>
Thu, 15 Dec 2005 06:58:25 +0000 (01:58 -0500)
darcs-hash:20051215065825-5007d-776a113458c1330fd5aefef8c8b4dc2433c144e4.gz

src/edu/berkeley/sbp/misc/CharToken.java
src/edu/berkeley/sbp/misc/MetaGrammar.java
src/edu/berkeley/sbp/misc/RegressionTests.java
src/edu/berkeley/sbp/tib/Tib.java
tests/regression.tc

index e73ccdd..72578dc 100644 (file)
@@ -50,10 +50,10 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
         return new CharRange(new IntegerTopology<CharToken>(new Range.Set(new Range((int)start, (int)end)).complement().intersect(all)));
     }
 
-    public static final CharToken left  = new CharToken((char)-3, 0, 0);
-    public static final CharToken right = new CharToken((char)-4, 0, 0);
-    public static final Atom leftBrace  = new Atom(new IntegerTopology<CharToken>(-3)) { public String toString() { return "{"; } };
-    public static final Atom rightBrace = new Atom(new IntegerTopology<CharToken>(-4)) { public String toString() { return "}"; } };
+    public static CharToken left(int row, int col) { return new CharToken((char)9998, 0, 0) { public String toString() { return "{"; } }; }
+    public static CharToken right(int row, int col) { return new CharToken((char)9999, 0, 0) { public String toString() { return "}"; } }; }
+    public static final Atom leftBrace  = new Atom(new IntegerTopology<CharToken>(9998)) { public String toString() { return "{"; } };
+    public static final Atom rightBrace = new Atom(new IntegerTopology<CharToken>(9999)) { public String toString() { return "}"; } };
 
     private static final Range.Set all = new Range.Set(new Range(0, Character.MAX_VALUE));
     public  static final Atom      any = new CharRange(new IntegerTopology<CharToken>(all));
index 889e2bc..18803cf 100644 (file)
@@ -93,9 +93,9 @@ public class MetaGrammar extends ReflectiveWalker {
         return nonTerminal("braced"+(anon++),
                            new PreSequence[][] {
                                new PreSequence[] {
-                                   new PreSequence(leftBrace()),
-                                   p,
-                                   new PreSequence(rightBrace())
+                                   new PreSequence(new Element[] { leftBrace(),
+                                                                   p.buildUnion(),
+                                                                   rightBrace() })
                                }
                            },
                            false,
index 717c784..8d165ff 100644 (file)
@@ -130,10 +130,13 @@ public class RegressionTests {
         public TestCase testcase(String input,                  Union grammar) throws IOException {
             return new TestCase(input, new String[0], grammar, false); }
         public TestCase  tibcase(String input,                  Union grammar) throws IOException {
-            return new TestCase(input, new String[0], grammar, false); }
+            return new TestCase(input, new String[0], grammar, true); }
         public MetaGrammar grammar(Object[] o) { return this; }
         public Object walk(String tag, Object[] args) {
-            if ("grammar".equals(tag)) return done("s");
+            if ("grammar".equals(tag)) {
+                System.out.println("\n" + this + "\n");
+                return done("s");
+            }
             else return super.walk(tag, args);
         }
     }
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 {
index 3204e6a..90dd6ee 100644 (file)
 //    s ::= ()       => s0
 //}
 
-//tibcase {
-//    input "
-//  hello 
-//    there
-//    how
-//     { are }
-//";
-//
-//    s        ::= ws S ws
-//    ws      !::= w*
-//    S        ::= { Hello }
-//    Hello    ::= "hello" { "there" "how" { "are" } }
-//}
+tibcase {
+    input 
+"
+  hello 
+    there
+    how
+     { are }
+";
+
+    s        ::= { Hello }
+    w        ::= " " | "\n"
+    ws      !::= w**
+    Hello    ::= "hello" ws { "there" ws "how" ws { { "are" } ws } }
+}
 
 testcase {
     input "ab c";