X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmeta%2FMetaGrammarBindings.java;h=742918be276d99ebe85ae3157ceaf93f423e57af;hp=dc35dfc3180c5ecf84e1ffaee147a4652d8784a9;hb=1e8506b9723a432c2c010b0f91b17cd0b68f6f68;hpb=babae49de7b777a5847f277bb74c88e77c67568a diff --git a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java index dc35dfc..742918b 100644 --- a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java +++ b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java @@ -12,10 +12,38 @@ 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 { - public @bind.as("Grammar") GrammarNode(NonTerminalNode[] nonterminals) { - for(NonTerminalNode nt : nonterminals) this.put(nt.name, nt); } + public static class GrammarNode extends HashMap 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 + ", "; @@ -36,14 +64,14 @@ public class MetaGrammarBindings { public abstract static class UnionNode extends ElementNode { public Seq[][] sequences; - public void build(Context cx, Union u) { + public void build(Context cx, Union u, NonTerminalNode cnt) { HashSet bad2 = new HashSet(); for(int i=0; i red = (Tree.TreeFunctor)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) { return cx.get(name); } - public void build(Context cx, Union u) { - if (!rep) { super.build(cx, u); return; } + public Element build(Context cx, NonTerminalNode cnt) { return cx.get(name); } + public void build(Context cx, Union u, NonTerminalNode cnt) { + if (!rep) { super.build(cx, u, this); return; } HashSet bad2 = new HashSet(); Union urep = new Union(); @@ -84,7 +134,7 @@ public class MetaGrammarBindings { if (sequences.length==1) u2 = u; for(int j=0; j)Atom.toAtom(e).complement()); + return infer((Topology)Atom.toAtom(e).complement().minus(CharRange.braces)); } }; } public static @bind.as("Word") String word(String s) { return s; } @@ -359,7 +425,7 @@ public class MetaGrammarBindings { public Context(Tree t, GrammarBindingResolver rm) { this.rm = rm; Tree.TreeFunctor red = (Tree.TreeFunctor)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); } @@ -370,11 +436,12 @@ 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; - nt.build(this, ret); + nt.build(this, ret, nt); cnt = old; } return ret;