checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / MetaGrammarBindings.java
index ca3b7fe..2372273 100644 (file)
@@ -9,20 +9,20 @@ import java.lang.annotation.*;
 import java.lang.reflect.*;
 import java.io.*;
 
+/** The java classes typically used to represent a parsed grammar AST */
 public class MetaGrammarBindings {
-    public static @bind class Grammar {
-        public NonTerminal get(String s) {
-            for(NonTerminal nt : nonterminals)
-                if (nt.name.equals(s)) return nt;
-            return null;
-        }
-        public @bind.arg NonTerminal[] nonterminals;
+
+    /** A grammar (a set of nonterminals) */
+    public static class Grammar extends HashMap<String,NonTerminal> {
+        public @bind Grammar(NonTerminal[] nonterminals) {
+            for(NonTerminal nt : nonterminals) this.put(nt.name, nt); }
         public String toString() {
             String ret = "[ ";
-            for(NonTerminal nt : nonterminals) ret += nt + ", ";
+            for(NonTerminal nt : values()) ret += nt + ", ";
             return ret + " ]";
         }
     }
+
     public abstract static class Un extends El {
         public Seq[][] sequences;
         public void build(MetaGrammar.Context cx, Union u) {
@@ -45,14 +45,54 @@ public class MetaGrammarBindings {
             }
         }
     }
+
     public static class NonTerminal extends Un {
+        public boolean rep;
         public String  name = null;
-        public @bind NonTerminal(@bind.arg String name,
-                                 @bind.arg Seq[][] sequences) {
+        public String sep = null;
+        public @bind NonTerminal(@bind.arg String name, @bind.arg Seq[][] sequences) { this(name, sequences, false); }
+        public NonTerminal(String name, Seq[][] sequences, boolean rep) { this(name, sequences, rep, null); }
+        public NonTerminal(String name, Seq[][] sequences, boolean rep, String sep) {
             this.name = name;
             this.sequences = sequences;
+            this.rep = rep;
+            this.sep = sep;
         }
         public Element build(MetaGrammar.Context cx) { return cx.get(name); }
+        public void build(MetaGrammar.Context cx, Union u) {
+            if (!rep) { super.build(cx, u); return; }
+            HashSet<Sequence> bad2 = new HashSet<Sequence>();
+
+            Union urep = new Union();
+            urep.add(Sequence.empty);
+            urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
+
+            for(int i=0; i<sequences.length; i++) {
+                Seq[] group = sequences[i];
+                Union u2 = new Union();
+                if (sequences.length==1) u2 = u;
+                for(int j=0; j<group.length; j++) {
+                    Union u3 = new Union();
+                    group[j].build(cx, u3, false);
+                    Sequence s = Sequence.unwrap(new Element[] { u3, urep },
+                                                 cx.rm.repeatTag(),
+                                                 new boolean[] { false, false });
+                    u2.add(s);
+                }
+                if (sequences.length==1) break;
+                Sequence seq = Sequence.singleton(u2);
+                for(Sequence s : bad2) {
+                    s.lame = true;
+                    seq = seq.not(s);
+                }
+                u.add(seq);
+                bad2.add(Sequence.singleton(u2));
+            }
+        }
+    }
+    public static @bind.as("=") NonTerminal go(@bind.arg String name, @bind.arg Seq[][] sequences) { return new NonTerminal(name, sequences, true); }
+    public static @bind.as("=") NonTerminal go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
+        return new NonTerminal(name, sequences, true, sep);
     }
 
     public static class AnonUn extends Un {
@@ -66,10 +106,9 @@ public class MetaGrammarBindings {
         }
     }
 
-    //public static @bind.as void range(char c) { }
     public static class Range {
-        public @bind.as("range") Range(char only) { first = only; last = only; }
-        public @bind.as("-")     Range(char first, char last) { this.first = first; this.last = last; }
+        public @bind Range(char only) { first = only; last = only; }
+        public @bind Range(char first, char last) { this.first = first; this.last = last; }
         public char first;
         public char last;
     }
@@ -102,6 +141,12 @@ public class MetaGrammarBindings {
         El follow;
         String tag = null;
         boolean lame;
+        public void append(El e) {
+            El[] elements = new El[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 tag(String tag) { this.tag = tag; return this; }
@@ -152,10 +197,9 @@ public class MetaGrammarBindings {
             Sequence ret = null;
             if (dropAll)     ret = Sequence.drop(els, false);
             else if (unwrap) ret = Sequence.unwrap(els, cx.rm.repeatTag(), drops);
-            else if (tag!=null) {
-                ret = cx.rm.resolveTag(tag, cx.cnt, els, labels, drops);
-            } else {
-                ret = cx.rm.tryResolveTag(tag, cx.cnt, els, labels, drops);
+            else {
+                if (tag != null) ret = cx.rm.resolveTag(tag, cx.cnt, els, labels, drops);
+                else ret = cx.rm.tryResolveTag(tag, cx.cnt, els, labels, drops);
                 if (ret == null) {
                     int idx = -1;
                     for(int i=0; i<els.length; i++)
@@ -172,27 +216,26 @@ public class MetaGrammarBindings {
             return ret;
         }
     }
-    public static @bind.as("&")   Seq  and(Seq s,         El[] elements) { return s.and(seq(elements)); }
-    public static @bind.as("&~")  Seq  andnot(Seq s,      El[] elements) { return s.andnot(seq(elements)); }
+    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  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("ps")  Seq  seq(El[] elements)                { return new Seq(elements); }
+    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 @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(")")   void close(String foo)                 { throw new Error("not supported"); }
     public static @bind.as("()")  El   epsilon()                         { return new Constant(Union.epsilon); }
 
-    public static @bind.as("nonTerminal") class NonTerminalReference extends El {
+    public static @bind class NonTerminalReference extends El {
         public @bind.arg String nonTerminal;
-        public Element build(MetaGrammar.Context cx) {
-            return cx.get(nonTerminal);
-        }
+        public Element build(MetaGrammar.Context cx) { return cx.get(nonTerminal); }
     }
 
-    public static class StringLiteral        extends Constant {
-        public @bind.as("literal") StringLiteral(String string) { super(CharRange.string(string)); }
+    public static class Literal extends Constant {
+        public @bind Literal(@bind.arg String string) { super(CharRange.string(string)); }
         public boolean drop() { return true; }
     }
 
@@ -210,7 +253,17 @@ public class MetaGrammarBindings {
     public static @bind.as("{")           class XTree                 extends El {
         public @bind.arg Seq body;
         public Element build(MetaGrammar.Context cx) {
-            throw new Error();
+            Union u = new Union();
+            Sequence s = body.build(cx, u, false);
+            Union u2 = new Union();
+            u2.add(Sequence.singleton(new Element[] {
+                CharRange.leftBrace,
+                cx.get("ws"),
+                u,
+                cx.get("ws"),
+                CharRange.rightBrace
+            }, 2));
+            return u2;
         }
     }
 
@@ -268,12 +321,12 @@ public class MetaGrammarBindings {
 
     //public static @bind.as("(")   El 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; }
-    public static @bind.as("escaped") String c(char c) { return c+""; }
-    public static @bind.as("\"\"")    String emptystring() { return ""; }
-    public static @bind.as("\n")      String retur() { return "\n"; }
-    public static @bind.as("\r")      String lf() { return "\r"; }
+    public static @bind.as("Word")        String word(String s) { return s; }
+    public static @bind.as("Quoted")      String quoted(String s) { return s; }
+    public static @bind.as("escaped")     String c(char c) { return c+""; }
+    public static @bind.as("EmptyString") String emptystring() { return ""; }
+    public static @bind.as("\n")          String retur() { return "\n"; }
+    public static @bind.as("\r")          String lf() { return "\r"; }
 
     static Atom infer(Element e)  { return infer((Topology<Character>)Atom.toAtom(e)); }
     static Atom infer(Topology<Character> t) { return new CharRange(new CharTopology(t)); }