X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmeta%2FMetaGrammarBindings.java;h=742918be276d99ebe85ae3157ceaf93f423e57af;hp=9dc7ab34acb67c83814ee625e951a7dc13622ae9;hb=1e8506b9723a432c2c010b0f91b17cd0b68f6f68;hpb=41b74cd8469abb0bdff811f26594a7ef87cb26a6 diff --git a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java index 9dc7ab3..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 + ", "; @@ -57,17 +85,39 @@ public class MetaGrammarBindings { } } - public static class NonTerminalNode extends UnionNode { + public static @bind.as("#import") GrammarNode poundimport(String fileName, String as) { + if (as==null) as = ""; + System.err.println("#import " + fileName + " as " + as); + try { + Tree t = new CharParser(MetaGrammar.make()).parse(new FileInputStream(fileName)).expand1(); + Tree.TreeFunctor 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, NonTerminalNode cnt) { return cx.get(name); } public void build(Context cx, Union u, NonTerminalNode cnt) { @@ -197,11 +247,9 @@ public class MetaGrammarBindings { 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("Word") String word(String s) { return s; } @@ -363,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); } @@ -374,7 +436,8 @@ 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;