GrammarAST.BacktickNode
[sbp.git] / src / edu / berkeley / sbp / meta / GrammarAST.java
index 05c7b39..297886b 100644 (file)
@@ -21,7 +21,7 @@ public class GrammarAST {
      *  parsing with this Union should be provided to <tt>buildFromAST</tt>
      */
     public static Union getMetaGrammar() {
-        return MetaGrammar.newInstance();
+        return buildFromAST(MetaGrammar.meta, "s", new File[0]);
     }
 
     /**
@@ -107,7 +107,7 @@ public class GrammarAST {
 
         if (head.equals("!"))   return new DropNode(walkElement(t.child(0)));
         if (head.equals("^"))   return new LiteralNode(walkString(t.child(0)), true);
-        if (head.equals("`"))   return walkElement(t.child(0)).lifted();
+        if (head.equals("`"))   return new BacktickNode(walkElement(t.child(0)));
         if (head.equals("Quoted")) return stringifyChildren(t);
         if (head.equals("Literal")) return new LiteralNode(walkString(t.child(0)));
         if (head.equals("->")) return walkSeq(t.child(0)).follow(walkElement(t.child(1)));
@@ -154,7 +154,7 @@ public class GrammarAST {
                 try {
                     String newPrefix = t.size()<2 ? "" : (walkString(t.child(1))+".");
                     FileInputStream fis = new FileInputStream(file);
-                    Tree tr = new CharParser(MetaGrammar.newInstance()).parse(fis).expand1();
+                    Tree tr = new CharParser(getMetaGrammar()).parse(fis).expand1();
                     return (GrammarNode)new GrammarAST(includes, newPrefix).walk(tr);
                 } catch (Exception e) {
                     throw new RuntimeException("while parsing " + file, e);
@@ -207,10 +207,9 @@ public class GrammarAST {
         }
     }
 
+    /** a node in the AST which is resolved into an Element */
     private abstract class ElementNode {
-        public boolean lifted = false;
-        public Seq ownerSeq = null;
-        public ElementNode lifted() { this.lifted = true; return this; }
+        public boolean isLifted() { return false; }
         public boolean drop(Context cx) { return false; }
         public Atom toAtom(Context cx) { throw new Error("can't convert a " + this.getClass().getName() + " to an atom: " + this); }
         public abstract Element build(Context cx, NonTerminalNode cnt, boolean dropall);
@@ -314,7 +313,6 @@ public class GrammarAST {
             for(int i=0; i<elements.length; i++) {
                 if (elements[i]==null)
                     throw new RuntimeException();
-                elements[i].ownerSeq = this;
             }
             // FIXME: this whole mechanism is sketchy
             if (check)
@@ -392,7 +390,7 @@ public class GrammarAST {
                 throw new RuntimeException("multiple non-dropped elements in sequence: " + Sequence.create("", els));
             boolean[] lifts = new boolean[elements.length];
             for(int i=0; i<elements.length; i++)
-                lifts[i] = elements[i].lifted;
+                lifts[i] = elements[i].isLifted();
             if (!multiNonDrop) {
                 if (idx == -1) 
                     ret = tag==null
@@ -504,6 +502,11 @@ public class GrammarAST {
         public Element build(Context cx, NonTerminalNode cnt, boolean dropall) { return _e.build(cx, cnt, dropall); }
     }
 
+    private class BacktickNode extends ElementNodeWrapper {
+        public BacktickNode(ElementNode e) { super(e); }
+        public boolean isLifted() { return true; }
+    }
+
     private class TildeNode extends ElementNodeWrapper {
         public TildeNode(ElementNode e) { super(e); }
         public Atom toAtom(Context cx) { return (Atom)((Topology<Character>)_e.toAtom(cx).complement()); }