X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmeta%2FMetaGrammarBindings.java;h=742918be276d99ebe85ae3157ceaf93f423e57af;hp=37635a54223235c877e29ee2827ed5e1151b40c8;hb=1e8506b9723a432c2c010b0f91b17cd0b68f6f68;hpb=256a398ff08c792eb94941b7e7d59d01bff86a94 diff --git a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java index 37635a5..742918b 100644 --- a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java +++ b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java @@ -12,27 +12,66 @@ 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 + ", "; 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 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(MetaGrammar.Context cx) { return cx.get(name); } - public void build(MetaGrammar.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(); @@ -73,7 +134,7 @@ public class MetaGrammarBindings { if (sequences.length==1) u2 = u; for(int j=0; j and = new HashSet(); HashSet not = new HashSet(); @@ -171,31 +236,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()); + return infer((Topology)Atom.toAtom(e).complement().minus(CharRange.braces)); } }; } - 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 +403,50 @@ 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); + } + 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); + throw new Error("warning could not find " + name); + } else { + String old = cnt; + cnt = name; + nt.build(this, ret, nt); + cnt = old; + } + return ret; + } + + } + }