checkpoint
[sbp.git] / src / edu / berkeley / sbp / tib / Tib.java
index 915158b..59336df 100644 (file)
@@ -21,9 +21,7 @@ import java.io.*;
  *
  *   This was written as an ad-hoc parser to facilitate
  *   experimentation with the TIB spec.  Once the spec is finalized it
- *   should probably be rewritten using a parser-generator, if
- *   possible (it is unclear whether or not the associated grammar is
- *   context-free).
+ *   should probably be rewritten.
  */
 public class Tib implements Token.Stream<CharToken> {
 
@@ -129,7 +127,7 @@ public class Tib implements Token.Stream<CharToken> {
         }
     }
 
-    public static class Block /*implements Token*/ {
+    public static class Block {
                       Block  parent;
         public  final int    row;
         public  final int    col;
@@ -137,7 +135,6 @@ public class Tib implements Token.Stream<CharToken> {
         private final Vector children = new Vector();
         private       String pending  = "";
 
-        //public Location getLocation() { return /*new Location.Cartesian(row, col)*/null; }
         public int    size() { return children.size(); }
         public Object child(int i) { return children.elementAt(i); }
         public boolean isLiteral() {  return false; }
@@ -288,5 +285,30 @@ public class Tib implements Token.Stream<CharToken> {
         return ret.toString();
     }
 
+    // Grammar //////////////////////////////////////////////////////////////////////////////
+
+    public static class Grammar extends MetaGrammar {
+        private int anon = 0;
+        public Object walk(Tree<String> tree) {
+            String head = tree.head();
+            if (tree.numChildren()==0) return super.walk(tree);
+            if ("{".equals(head))
+                return nonTerminal("braced"+(anon++),
+                                   new PreSequence[][] {
+                                       new PreSequence[] {
+                                           new PreSequence(new Element[] { CharToken.leftBrace,
+                                                                           Repeat.maximal(Repeat.many0(nonTerminal("w"))),
+                                                                           ((PreSequence)walk(tree, 0)).buildUnion(),
+                                                                           Repeat.maximal(Repeat.many0(nonTerminal("w"))),
+                                                                           CharToken.rightBrace
+                                           })
+                                       }
+                                   },
+                                   false,
+                                   false);
+            return super.walk(tree);
+        }
+    }
+
 }