checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / MetaGrammarBindings.java
index b70ddb8..667b6a6 100644 (file)
@@ -10,23 +10,48 @@ import java.lang.reflect.*;
 import java.io.*;
 
 /** The java classes typically used to represent a parsed grammar AST; each inner class is a type of AST node. */
-public class MetaGrammarBindings {
+public class MetaGrammarBindings extends AnnotationGrammarBindings {
 
+    public MetaGrammarBindings() { super(MetaGrammarBindings.class); }
+
+    // FIXME ugly ugly ugly scary dangerous
+    public static String prefix = "";
+    
     /** A grammar (a set of nonterminals) */
-    public static class GrammarNode extends HashMap<String,NonTerminalNode> {
-        public @bind.as("Grammar") GrammarNode(NonTerminalNode[] nonterminals) {
+    public static class GrammarNode extends HashMap<String,NonTerminalNode> implements NonTerminalSource {
+        public NonTerminalNode[] getNonTerminals() {
+            return (NonTerminalNode[])values().toArray(new NonTerminalNode[0]);
+        }
+        public GrammarNode(NonTerminalNode[] nonterminals) {
             for(NonTerminalNode nt : nonterminals) {
+                if (nt==null) continue;
                 if (this.get(nt.name)!=null)
                     throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
                 this.put(nt.name, nt);
             }
         }
+        public @bind.as("Grammar") GrammarNode(Object[] nt) {
+            add(nt);
+        }
+        private void add(Object[] obs) {
+            for(Object o : obs) {
+                if (o==null) continue;
+                else if (o instanceof Object[]) add((Object[])o);
+                else if (o instanceof NonTerminalNode) {
+                    NonTerminalNode nt = (NonTerminalNode)o;
+                    if (this.get(nt.name)!=null)
+                        throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
+                    this.put(nt.name, nt);
+                }
+                else if (o instanceof NonTerminalSource) add(((NonTerminalSource)o).getNonTerminals());
+            }
+        }
         public String toString() {
             String ret = "[ ";
             for(NonTerminalNode nt : values()) ret += nt + ", ";
             return ret + " ]";
         }
-        public Union build(String s, GrammarBindingResolver rm) {
+        public Union build(String s, Grammar.Bindings rm) {
             Context cx = new Context(this,rm);
             Union u = null;
             for(MetaGrammarBindings.NonTerminalNode nt : values()) {
@@ -62,17 +87,42 @@ public class MetaGrammarBindings {
         }
     }
 
-    public static class NonTerminalNode extends UnionNode {
+    public static @bind.as("#import") GrammarNode poundimport(String fileName, String as) {
+        if (as==null) as = "";
+        else if ("".equals(as)) { }
+        else as = as +".";
+        
+        System.err.println("#import " + fileName + " as " + as);
+        try {
+            Tree t = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream("tests/"+fileName)).expand1();
+            Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
+            String oldprefix = prefix;
+            prefix = as;
+            GrammarNode gn = (GrammarNode)red.invoke(t);
+            prefix = oldprefix;
+            return gn;
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static interface NonTerminalSource {
+        public NonTerminalNode[] getNonTerminals();
+    }
+
+    public static class NonTerminalNode extends UnionNode implements NonTerminalSource {
         public boolean rep;
         public String  name = null;
         public String sep = null;
+        public NonTerminalNode[] getNonTerminals() { return new NonTerminalNode[] { this }; }
         public @bind.as("NonTerminal") NonTerminalNode(@bind.arg String name, @bind.arg Seq[][] sequences) { this(name, sequences, false); }
         public NonTerminalNode(String name, Seq[][] sequences, boolean rep) { this(name, sequences, rep, null); }
         public NonTerminalNode(String name, Seq[][] sequences, boolean rep, String sep) {
-            this.name = name;
+            this.name = prefix + name;
             this.sequences = sequences;
             this.rep = rep;
-            this.sep = sep;
+            this.sep = prefix + sep;
         }
         public Element build(Context cx, NonTerminalNode cnt) { return cx.get(name); }
         public void build(Context cx, Union u, NonTerminalNode cnt) {
@@ -106,10 +156,11 @@ public class MetaGrammarBindings {
             }
         }
     }
-    public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg Seq[][] sequences) { return new NonTerminalNode(name, sequences, true); }
+
+    public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg Seq[][] sequences) {
+        return new NonTerminalNode(name, sequences, true); }
     public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
-        return new NonTerminalNode(name, sequences, true, sep);
-    }
+        return new NonTerminalNode(name, sequences, true, sep); }
 
     public static class AnonUnionNode extends UnionNode {
         public @bind.as("(") AnonUnionNode(Seq[][] sequences) {
@@ -129,31 +180,6 @@ public class MetaGrammarBindings {
         public char last;
     }
 
-    public static abstract class ElementNode {
-        public String getLabel() { return null; }
-        public String getOwnerTag() { return null; }
-        public boolean drop() { return false; }
-        public abstract Element build(Context cx, NonTerminalNode cnt);
-    }
-
-    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(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); }
-    }
-
-    public static class Label extends ElementNode {
-        public String 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(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); }
-    }
-
     public static /*abstract*/ class Seq {
         HashSet<Seq> and = new HashSet<Seq>();
         HashSet<Seq> not = new HashSet<Seq>();
@@ -169,14 +195,14 @@ public class MetaGrammarBindings {
         }
         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 tag(String tag) { this.tag = prefix+tag; return this; }
         public Seq follow(ElementNode follow) { this.follow = follow; return this; }
         public Seq dup() {
             Seq ret = new Seq(elements);
             ret.and.addAll(and);
             ret.not.addAll(not);
             ret.follow = follow;
-            ret.tag = tag;
+            ret.tag = prefix+tag;
             return ret;
         }
         public Seq and(Seq s) { and.add(s); s.lame = true; return this; }
@@ -201,7 +227,7 @@ public class MetaGrammarBindings {
         }
         public Sequence build0(Context cx, boolean lame, NonTerminalNode cnt) {
             boolean dropAll = lame;
-            if (tag!=null && "()".equals(tag)) dropAll = true;
+            if (tag!=null && tag.endsWith("()")) dropAll = true;
             boolean[] drops = new boolean[elements.length];
             Element[] els = new Element[elements.length];
             for(int i=0; i<elements.length; i++) {
@@ -213,7 +239,8 @@ public class MetaGrammarBindings {
             Sequence ret = null;
             if (dropAll)     ret = Sequence.drop(els, false);
             else {
-                ret = cx.rm.tryResolveTag(tag, cnt==null?null:cnt.name, els, drops);
+                Production prod = new Production(tag, (cnt==null?null:cnt.name), els, drops);
+                ret = cx.rm.createSequence(prod);
                 if (ret == null) {
                     int idx = -1;
                     for(int i=0; i<els.length; i++)
@@ -243,11 +270,16 @@ public class MetaGrammarBindings {
     public static @bind.as(")")   void close(String foo)                 { throw new Error("not supported"); }
     public static @bind.as("()")  ElementNode   epsilon()                         { return new Constant(Union.epsilon); }
 
-    public static @bind.as("NonTerminalReference") class NonTerminalReferenceNode extends ElementNode {
-        public @bind.arg String nonTerminal;
+    public static class NonTerminalReferenceNode extends ElementNode {
+        public String nonTerminal;
+        public NonTerminalReferenceNode() { }
+        public @bind.as("NonTerminalReference") NonTerminalReferenceNode(String nonTerminal) {
+            this.nonTerminal = prefix + nonTerminal;
+        }
         public Element build(Context cx, NonTerminalNode cnt) {
+            if (!this.nonTerminal.startsWith(prefix)) nonTerminal = prefix + nonTerminal;
             Element ret = cx.get(nonTerminal);
-            if (ret == null) throw new RuntimeException("unkown nonterminal \""+nonTerminal+"\"");
+            if (ret == null) throw new RuntimeException("unknown nonterminal \""+nonTerminal+"\"");
             return ret;
         }
     }
@@ -298,34 +330,33 @@ public class MetaGrammarBindings {
                 : Sequence.repeatMaximal(e.build(cx, null),                    zero, many, infer(sep.build(cx, null)), cx.rm.repeatTag());
         }
     }
-    public static class Constant extends ElementNode {
-        Element constant;
-        public Constant(Element constant) { this.constant = constant; }
-        public Element build(Context cx, NonTerminalNode cnt) { return constant; }
-    }
-    public abstract static class PostProcess extends ElementNode {
-        ElementNode e;
-        public PostProcess(ElementNode e) { this.e = e; }
-        public Element build(Context cx, NonTerminalNode cnt) { return postProcess(e.build(cx, cnt)); }
-        public abstract Element postProcess(Element e);
-    }
 
     // FIXME: it would be nice if we could hoist this into "Rep"
-    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("!")   ElementNode bang(final ElementNode e)                        { return new Drop(e); }
+    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("!")   ElementNode bang(final ElementNode e)                        
+    { return new Drop(e); }
 
     public static @bind.as("^")   ElementNode caret(final String s) {
+        final String thePrefix = prefix;
         return new Drop(new Constant(CharRange.string(s)) {
-                public String getOwnerTag() { return s; }
+                public String getOwnerTag() { return thePrefix+s; }
             });
     }
 
@@ -349,8 +380,8 @@ public class MetaGrammarBindings {
         public HashMap<String,Union> map = new HashMap<String,Union>();
         public GrammarNode grammar;
         public String cnt = null;
-        public GrammarBindingResolver rm;
-        public Context(GrammarNode g, GrammarBindingResolver rm) {
+        public Grammar.Bindings rm;
+        public Context(GrammarNode g, Grammar.Bindings rm) {
             this.grammar = g;
             this.rm = rm;
         }
@@ -363,10 +394,10 @@ public class MetaGrammarBindings {
             }
             return ret;
         }
-        public Context(Tree t, GrammarBindingResolver rm) {
+        public Context(Tree t, Grammar.Bindings rm) {
             this.rm = rm;
             Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
-            this.grammar = (GrammarNode)red.invoke(t.children());
+            this.grammar = (GrammarNode)red.invoke(t);
         }
         public Union peek(String name) { return map.get(name); }
         public void  put(String name, Union u) { map.put(name, u); }
@@ -377,7 +408,8 @@ public class MetaGrammarBindings {
             map.put(name, ret);
             NonTerminalNode nt = grammar.get(name);
             if (nt==null) {
-                System.err.println("*** warning could not find " + name);
+                //System.err.println("*** warning could not find " + name);
+                throw new Error("warning could not find " + name);
             } else {
                 String old = cnt;
                 cnt = name;
@@ -389,4 +421,39 @@ public class MetaGrammarBindings {
 
     }
 
+    public static class Constant extends ElementNode {
+        Element constant;
+        public Constant(Element constant) { this.constant = constant; }
+        public Element build(Context cx, NonTerminalNode cnt) { return constant; }
+    }
+    public abstract static class PostProcess extends ElementNode {
+        ElementNode e;
+        public PostProcess(ElementNode e) { this.e = e; }
+        public Element build(Context cx, NonTerminalNode cnt) { return postProcess(e.build(cx, cnt)); }
+        public abstract Element postProcess(Element e);
+    }
+    public static abstract class ElementNode {
+        public String getLabel() { return null; }
+        public String getOwnerTag() { return null; }
+        public boolean drop() { return false; }
+        public abstract Element build(Context cx, NonTerminalNode cnt);
+    }
+
+    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(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); }
+    }
+
+    public static class Label extends ElementNode {
+        public String 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(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); }
+    }
 }