checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / MetaGrammarBindings.java
index 15cb0d6..cae7f13 100644 (file)
@@ -9,7 +9,7 @@ import java.lang.annotation.*;
 import java.lang.reflect.*;
 import java.io.*;
 
-/** The java classes typically used to represent a parsed grammar AST */
+/** The java classes typically used to represent a parsed grammar AST; each inner class is a type of AST node. */
 public class MetaGrammarBindings {
 
     /** A grammar (a set of nonterminals) */
@@ -23,7 +23,7 @@ public class MetaGrammarBindings {
         }
     }
 
-    public abstract static class Un extends El {
+    public abstract static class UnionNode extends ElementNode {
         public Seq[][] sequences;
         public void build(MetaGrammar.Context cx, Union u) {
             HashSet<Sequence> bad2 = new HashSet<Sequence>();
@@ -46,7 +46,7 @@ public class MetaGrammarBindings {
         }
     }
 
-    public static class NonTerminal extends Un {
+    public static class NonTerminal extends UnionNode {
         public boolean rep;
         public String  name = null;
         public String sep = null;
@@ -95,8 +95,8 @@ public class MetaGrammarBindings {
         return new NonTerminal(name, sequences, true, sep);
     }
 
-    public static class AnonUn extends Un {
-        public @bind.as("(") AnonUn(Seq[][] sequences) {
+    public static class AnonUnionNode extends UnionNode {
+        public @bind.as("(") AnonUnionNode(Seq[][] sequences) {
             this.sequences = sequences;
         }
         public Element build(MetaGrammar.Context cx) {
@@ -112,24 +112,24 @@ public class MetaGrammarBindings {
         public char first;
         public char last;
     }
-    public static abstract class El {
+    public static abstract class ElementNode {
         public String getLabel() { return null; }
         public String getOwnerTag() { return null; }
         public boolean drop() { return false; }
         public abstract Element build(MetaGrammar.Context cx);
     }
-    public static class Drop extends El {
-        public El e;
-        public Drop(El e) { this.e = e; }
+    public static class Drop extends ElementNode {
+        public ElementNode e;
+        public Drop(ElementNode e) { this.e = e; }
         public String getLabel() { return null; }
         public boolean drop() { return true; }
         public String getOwnerTag() { return e.getOwnerTag(); }
         public Element build(MetaGrammar.Context cx) { return e.build(cx); }
     }
-    public static class Label extends El {
+    public static class Label extends ElementNode {
         public String label;
-        public El e;
-        public Label(String label, El e) { this.e = e; this.label = label; }
+        public ElementNode e;
+        public Label(String label, ElementNode e) { this.e = e; this.label = label; }
         public String getLabel() { return label; }
         public String getOwnerTag() { return e.getOwnerTag(); }
         public Element build(MetaGrammar.Context cx) { return e.build(cx); }
@@ -137,20 +137,20 @@ public class MetaGrammarBindings {
     public static /*abstract*/ class Seq {
         HashSet<Seq> and = new HashSet<Seq>();
         HashSet<Seq> not = new HashSet<Seq>();
-        El[] elements;
-        El follow;
+        ElementNode[] elements;
+        ElementNode follow;
         String tag = null;
         boolean lame;
-        public void append(El e) {
-            El[] elements = new El[this.elements.length+1];
+        public void append(ElementNode e) {
+            ElementNode[] elements = new ElementNode[this.elements.length+1];
             System.arraycopy(this.elements, 0, elements, 0, this.elements.length);
             this.elements = elements;
             elements[elements.length-1] = e;
         }
-        public Seq(El e) { this(new El[] { e }); }
-        public Seq(El[] elements) { this.elements = elements; }
+        public Seq(ElementNode e) { this(new ElementNode[] { e }); }
+        public Seq(ElementNode[] elements) { this.elements = elements; }
         public Seq tag(String tag) { this.tag = tag; return this; }
-        public Seq follow(El follow) { this.follow = follow; return this; }
+        public Seq follow(ElementNode follow) { this.follow = follow; return this; }
         public Seq dup() {
             Seq ret = new Seq(elements);
             ret.and.addAll(and);
@@ -161,8 +161,8 @@ public class MetaGrammarBindings {
         }
         public Seq and(Seq s) { and.add(s); s.lame = true; return this; }
         public Seq andnot(Seq s) { not.add(s); s.lame = true; return this; }
-        public Seq separate(El sep) {
-            El[] elements = new El[this.elements.length * 2 - 1];
+        public Seq separate(ElementNode sep) {
+            ElementNode[] elements = new ElementNode[this.elements.length * 2 - 1];
             for(int i=0; i<this.elements.length; i++) {
                 elements[i*2]   = this.elements[i];
                 if (i<this.elements.length-1)
@@ -214,18 +214,18 @@ public class MetaGrammarBindings {
     }
     public static @bind.as("&")   Seq  and2(Seq s,        Seq a) { return s.and(a); }
     public static @bind.as("&~")  Seq  andnot2(Seq s,     Seq a) { return s.andnot(a); }
-    public static @bind.as("->")  Seq  arrow(Seq s, El e)                { return s.follow(e); }
+    public static @bind.as("->")  Seq  arrow(Seq s, ElementNode e)                { return s.follow(e); }
     public static @bind.as("::")  Seq  tag(String tagname, Seq s)        { return s.tag(tagname); }
-    public static @bind.as("/")   Seq  slash(Seq s, El e)                { return s.separate(e); }
+    public static @bind.as("/")   Seq  slash(Seq s, ElementNode e)                { return s.separate(e); }
 
-    public static Seq  seq(El[] elements)               { return new Seq(elements); }
-    public static @bind.as("Elements")  Seq  seq2(El[] elements)               { return new Seq(elements); }
+    public static Seq  seq(ElementNode[] elements)               { return new Seq(elements); }
+    public static @bind.as("Elements")  Seq  seq2(ElementNode[] elements)               { return new Seq(elements); }
     public static @bind.as        Seq  psx(Seq s)                        { return s; }
-    public static @bind.as(":")   El   colon(String s, El e)             { return new Label(s, e); }
+    public static @bind.as(":")   ElementNode   colon(String s, ElementNode e)             { return new Label(s, e); }
     public static @bind.as(")")   void close(String foo)                 { throw new Error("not supported"); }
-    public static @bind.as("()")  El   epsilon()                         { return new Constant(Union.epsilon); }
+    public static @bind.as("()")  ElementNode   epsilon()                         { return new Constant(Union.epsilon); }
 
-    public static @bind class NonTerminalReference extends El {
+    public static @bind class NonTerminalReference extends ElementNode {
         public @bind.arg String nonTerminal;
         public Element build(MetaGrammar.Context cx) { return cx.get(nonTerminal); }
     }
@@ -235,7 +235,7 @@ public class MetaGrammarBindings {
         public boolean drop() { return true; }
     }
 
-    public static                     class CharClass            extends El {
+    public static                     class CharClass            extends ElementNode {
         Range[] ranges;
         public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; }
         public Element build(MetaGrammar.Context cx) {
@@ -246,7 +246,7 @@ public class MetaGrammarBindings {
         }
     }
 
-    public static @bind.as("{")           class XTree                 extends El {
+    public static @bind.as("{")           class XTree                 extends ElementNode {
         public @bind.arg Seq body;
         public Element build(MetaGrammar.Context cx) {
             Union u = new Union();
@@ -263,10 +263,10 @@ public class MetaGrammarBindings {
         }
     }
 
-    public static class Rep extends El {
-        public El e, sep;
+    public static class Rep extends ElementNode {
+        public ElementNode e, sep;
         public boolean zero, many, max;
-        public Rep(El e, El sep, boolean zero, boolean many, boolean max) {
+        public Rep(ElementNode e, ElementNode sep, boolean zero, boolean many, boolean max) {
             this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
         public Element build(MetaGrammar.Context cx) {
             return (!max)
@@ -276,46 +276,46 @@ public class MetaGrammarBindings {
                 : Sequence.repeatMaximal(e.build(cx),                    zero, many, infer(sep.build(cx)), cx.rm.repeatTag());
         }
     }
-    public static class Constant extends El {
+    public static class Constant extends ElementNode {
         Element constant;
         public Constant(Element constant) { this.constant = constant; }
         public Element build(MetaGrammar.Context cx) { return constant; }
     }
-    public abstract static class PostProcess extends El {
-        El e;
-        public PostProcess(El e) { this.e = e; }
+    public abstract static class PostProcess extends ElementNode {
+        ElementNode e;
+        public PostProcess(ElementNode e) { this.e = e; }
         public Element build(MetaGrammar.Context cx) { return postProcess(e.build(cx)); }
         public abstract Element postProcess(Element e);
     }
 
     // FIXME: it would be nice if we could hoist this into "Rep"
-    public static @bind.as("++")  El plusmax(final El e)                     { return new Rep(e, null, false, true, true); }
-    public static @bind.as("+")   El plus(final El e)                        { return new Rep(e, null, false, true, false); }
-    public static @bind.as("++/") El plusmaxfollow(final El e, final El sep) { return new Rep(e, sep,  false, true, true); }
-    public static @bind.as("+/")  El plusfollow(final El e, final El sep)    { return new Rep(e, sep,  false, true, false); }
-    public static @bind.as("**")  El starmax(final El e)                     { return new Rep(e, null, true,  true, true); }
-    public static @bind.as("*")   El star(final El e)                        { return new Rep(e, null, true,  true, false); }
-    public static @bind.as("**/") El starmaxfollow(final El e, final El sep) { return new Rep(e, sep,  true,  true, true); }
-    public static @bind.as("*/")  El starfollow(final El e, final El sep)    { return new Rep(e, sep,  true,  true, false); }
-    public static @bind.as("?")   El question(final El e)                    { return new Rep(e, null, true,  true, false); }
+    public static @bind.as("++")  ElementNode plusmax(final ElementNode e)                     { return new Rep(e, null, false, true, true); }
+    public static @bind.as("+")   ElementNode plus(final ElementNode e)                        { return new Rep(e, null, false, true, false); }
+    public static @bind.as("++/") ElementNode plusmaxfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep,  false, true, true); }
+    public static @bind.as("+/")  ElementNode plusfollow(final ElementNode e, final ElementNode sep)    { return new Rep(e, sep,  false, true, false); }
+    public static @bind.as("**")  ElementNode starmax(final ElementNode e)                     { return new Rep(e, null, true,  true, true); }
+    public static @bind.as("*")   ElementNode star(final ElementNode e)                        { return new Rep(e, null, true,  true, false); }
+    public static @bind.as("**/") ElementNode starmaxfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep,  true,  true, true); }
+    public static @bind.as("*/")  ElementNode starfollow(final ElementNode e, final ElementNode sep)    { return new Rep(e, sep,  true,  true, false); }
+    public static @bind.as("?")   ElementNode question(final ElementNode e)                    { return new Rep(e, null, true,  true, false); }
 
-    public static @bind.as("!")   El bang(final El e)                        { return new Drop(e); }
+    public static @bind.as("!")   ElementNode bang(final ElementNode e)                        { return new Drop(e); }
 
-    public static @bind.as("^")   El caret(final String s) {
+    public static @bind.as("^")   ElementNode caret(final String s) {
         return new Drop(new Constant(CharRange.string(s)) {
                 public String getOwnerTag() { return s; }
             });
     }
 
-    public static @bind.as("~")   El tilde(final El e) {
+    public static @bind.as("~")   ElementNode tilde(final ElementNode e) {
         return new PostProcess(e) {
                 public Element postProcess(Element e) {
                     return infer((Topology<Character>)Atom.toAtom(e).complement()); 
                 } }; }
 
-    public static @bind.as("^^")  void doublecaret(final El e)                 { throw new Error("not implemented"); }
+    public static @bind.as("^^")  void doublecaret(final ElementNode e)                 { throw new Error("not implemented"); }
 
-    //public static @bind.as("(")   El subexpression(Seq[][] rhs)                { return new NonTerminal(rhs); }
+    //public static @bind.as("(")   ElementNode subexpression(Seq[][] rhs)                { return new NonTerminal(rhs); }
 
     public static @bind.as("Word")        String word(String s) { return s; }
     public static @bind.as("Quoted")      String quoted(String s) { return s; }