X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmeta%2FMetaGrammarBindings.java;h=401536f2aca726718d96982cb84f6fd463d4bac2;hb=a7ba8d8a5f0cb7fbb5bf67f1a95f1cad5226c507;hp=37635a54223235c877e29ee2827ed5e1151b40c8;hpb=256a398ff08c792eb94941b7e7d59d01bff86a94;p=sbp.git diff --git a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java index 37635a5..401536f 100644 --- a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java +++ b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java @@ -21,18 +21,29 @@ public class MetaGrammarBindings { for(NonTerminalNode nt : values()) ret += nt + ", "; return ret + " ]"; } + public Union build(String s, GrammarBindingResolver rm) { + Context cx = new Context(this,rm); + Union u = null; + for(MetaGrammarBindings.NonTerminalNode nt : values()) { + Union el = (Union)cx.get(nt.name); + StringBuffer st = new StringBuffer(); + el.toString(st); + if (nt.name.equals(s)) u = el; + } + return u; + } } public abstract static class UnionNode extends ElementNode { public Seq[][] sequences; - public void build(MetaGrammar.Context cx, Union u) { + public void build(Context cx, Union u, NonTerminalNode cnt) { HashSet bad2 = new HashSet(); for(int i=0; i bad2 = new HashSet(); Union urep = new Union(); @@ -73,7 +84,7 @@ public class MetaGrammarBindings { if (sequences.length==1) u2 = u; for(int j=0; j and = new HashSet(); HashSet not = new HashSet(); @@ -171,31 +186,29 @@ public class MetaGrammarBindings { this.elements = elements; return this; } - public Sequence build(MetaGrammar.Context cx, Union u, boolean lame) { - Sequence ret = build0(cx, lame || this.lame); - for(Seq s : and) { Sequence dork = s.build(cx, u, true); ret = ret.and(dork); } - for(Seq s : not) { Sequence dork = s.build(cx, u, true); ret = ret.not(dork); } + public Sequence build(Context cx, Union u, boolean lame, NonTerminalNode cnt) { + Sequence ret = build0(cx, lame || this.lame, cnt); + for(Seq s : and) { Sequence dork = s.build(cx, u, true, cnt); ret = ret.and(dork); } + for(Seq s : not) { Sequence dork = s.build(cx, u, true, cnt); ret = ret.not(dork); } u.add(ret); ret.lame = lame; return ret; } - public Sequence build0(MetaGrammar.Context cx, boolean lame) { + public Sequence build0(Context cx, boolean lame, NonTerminalNode cnt) { boolean dropAll = lame; if (tag!=null && "()".equals(tag)) dropAll = true; - Object[] labels = new Object[elements.length]; boolean[] drops = new boolean[elements.length]; Element[] els = new Element[elements.length]; for(int i=0; i)Atom.toAtom(e).complement()); } }; } - public static @bind.as("^^") void doublecaret(final ElementNode e) { throw new Error("not implemented"); } - - //public static @bind.as("(") ElementNode subexpression(Seq[][] rhs) { return new NonTerminalNode(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+""; } @@ -326,4 +339,49 @@ public class MetaGrammarBindings { static Atom infer(Element e) { return infer((Topology)Atom.toAtom(e)); } static Atom infer(Topology t) { return new CharRange(new CharTopology(t)); } + + public static class Context { + public HashMap map = new HashMap(); + public GrammarNode grammar; + public String cnt = null; + public GrammarBindingResolver rm; + public Context(GrammarNode g, GrammarBindingResolver rm) { + this.grammar = g; + this.rm = rm; + } + public Union build() { + Union ret = null; + for(NonTerminalNode nt : grammar.values()) { + Union u = get(nt.name); + if ("s".equals(nt.name)) + ret = u; + } + return ret; + } + public Context(Tree t, GrammarBindingResolver rm) { + this.rm = rm; + Tree.TreeFunctor red = (Tree.TreeFunctor)t.head(); + this.grammar = (GrammarNode)red.invoke(t.children()); + } + public Union peek(String name) { return map.get(name); } + public void put(String name, Union u) { map.put(name, u); } + public Union get(String name) { + Union ret = map.get(name); + if (ret != null) return ret; + ret = new Union(name); + map.put(name, ret); + NonTerminalNode nt = grammar.get(name); + if (nt==null) { + System.err.println("*** warning could not find " + name); + } else { + String old = cnt; + cnt = name; + nt.build(this, ret, nt); + cnt = old; + } + return ret; + } + + } + }