checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / MetaGrammarBindings.java
index bddccf5..742918b 100644 (file)
@@ -12,6 +12,9 @@ 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 {
 
+    // FIXME ugly ugly ugly scary dangerous
+    public static String prefix = "";
+
     /** A grammar (a set of nonterminals) */
     public static class GrammarNode extends HashMap<String,NonTerminalNode> implements NonTerminalSource {
         public NonTerminalNode[] getNonTerminals() {
@@ -82,12 +85,17 @@ public class MetaGrammarBindings {
         }
     }
 
-    public static @bind.as("#import") GrammarNode poundimport(String fileName) {
-        System.err.println("#import " + fileName);
+    public static @bind.as("#import") GrammarNode poundimport(String fileName, String as) {
+        if (as==null) as = "";
+        System.err.println("#import " + fileName + " as " + as);
         try {
             Tree t = new CharParser(MetaGrammar.make()).parse(new FileInputStream(fileName)).expand1();
             Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
-            return (MetaGrammarBindings.GrammarNode)red.invoke(t);
+            String oldprefix = prefix;
+            prefix = as;
+            GrammarNode gn = (GrammarNode)red.invoke(t);
+            prefix = oldprefix;
+            return gn;
         } catch (Exception e) {
             e.printStackTrace();
             throw new RuntimeException(e);
@@ -106,10 +114,10 @@ public class MetaGrammarBindings {
         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) {
@@ -280,11 +288,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;
         }
     }
@@ -348,17 +361,26 @@ public class MetaGrammarBindings {
     }
 
     // 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) {
         return new Drop(new Constant(CharRange.string(s)) {
@@ -414,7 +436,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;