X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmeta%2FMetaGrammarBindings.java;h=e3299cbe0f205c5cdfba36f7fbdfe7a2b3953d80;hb=f1dba2b015c3da04fc2fab45604da2e6c9ae0a1d;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..e3299cb 100644 --- a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java +++ b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java @@ -10,29 +10,68 @@ import java.lang.reflect.*; 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 { +public class MetaGrammarBindings extends AnnotationGrammarBindings { + public MetaGrammarBindings() { super(MetaGrammarBindings.class); } + + // 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 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 GrammarNode) add(((GrammarNode)o).getNonTerminals()); + } + } public String toString() { String ret = "[ "; for(NonTerminalNode nt : values()) ret += nt + ", "; return ret + " ]"; } + public Union build(String s, Grammar.Bindings 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 class NonTerminalNode extends UnionNode { public boolean rep; public String name = null; public String sep = null; - public @bind.as("NonTerminal") NonTerminalNode(@bind.arg String name, @bind.arg Seq[][] sequences) { this(name, sequences, false); } + 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 = sep==null?null:(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(); urep.add(Sequence.empty); - urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1)); + if (sep != null) + urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1)); + else + urep.add(Sequence.singleton(new Element[] { u }, 0)); for(int i=0; i and = new HashSet(); HashSet not = new HashSet(); @@ -149,14 +192,14 @@ public class MetaGrammarBindings { } public Seq(ElementNode e) { this(new ElementNode[] { e }); } public Seq(ElementNode[] elements) { this.elements = elements; } - public Seq tag(String tag) { this.tag = tag; return this; } + public Seq tag(String tag) { this.tag = prefix+tag; return this; } public Seq follow(ElementNode follow) { this.follow = follow; return this; } public Seq dup() { Seq ret = new Seq(elements); ret.and.addAll(and); ret.not.addAll(not); ret.follow = follow; - ret.tag = tag; + ret.tag = prefix+tag; return ret; } public Seq and(Seq s) { and.add(s); s.lame = true; return this; } @@ -171,31 +214,30 @@ 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]; + if (tag!=null && tag.endsWith("()")) dropAll = true; 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 +373,88 @@ 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 Grammar.Bindings rm; + public Context(GrammarNode g, Grammar.Bindings 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, Grammar.Bindings 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) { + throw new Error("warning could not find " + name); + } else { + String old = cnt; + cnt = name; + nt.build(this, ret, nt); + cnt = old; + } + return ret; + } + + } + + public static abstract class ElementNode { + public String getLabel() { return null; } + public String getOwnerTag() { return null; } + public boolean drop() { return false; } + public abstract Element build(Context cx, NonTerminalNode cnt); + } + + public static abstract class ElementNodeWrapper extends ElementNode { + protected ElementNode _e; + public ElementNodeWrapper(ElementNode e) { this._e = e; } + public String getLabel() { return _e.getLabel(); } + public String getOwnerTag() { return _e.getOwnerTag(); } + public boolean drop() { return _e.drop(); } + public Element build(Context cx, NonTerminalNode cnt) { return _e.build(cx, cnt); } + } + + public static class Constant extends ElementNode { + Element constant; + public Constant(Element constant) { this.constant = constant; } + public Element build(Context cx, NonTerminalNode cnt) { return constant; } + } + + public abstract static class PostProcess extends ElementNodeWrapper { + ElementNode e; + public PostProcess(ElementNode e) { super(e); } + public Element build(Context cx, NonTerminalNode cnt) { return postProcess(_e.build(cx, cnt)); } + public abstract Element postProcess(Element e); + } + + public static class Drop extends ElementNodeWrapper { + public Drop(ElementNode e) { super(e); } + public boolean drop() { return true; } + } + + public static class Label extends ElementNodeWrapper { + public String label; + public Label(String label, ElementNode e) { super(e); this.label = label; } + public String getLabel() { return label; } + } }