public class MetaGrammarBindings {
/** A grammar (a set of nonterminals) */
- public static class GrammarNode extends HashMap<String,NonTerminalNode> {
- public @bind.as("Grammar") GrammarNode(NonTerminalNode[] nonterminals) {
- for(NonTerminalNode nt : nonterminals) this.put(nt.name, nt); }
+ public static class GrammarNode extends HashMap<String,NonTerminalNode> 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 + ", ";
}
}
- public static class NonTerminalNode extends UnionNode {
+ public static @bind.as("#import") GrammarNode poundimport(String fileName) {
+ System.err.println("#import " + fileName);
+ try {
+ Tree t = new CharParser(MetaGrammar.make()).parse(new FileInputStream(fileName)).expand1();
+ Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
+ return (MetaGrammarBindings.GrammarNode)red.invoke(t);
+ } 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) {
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<elements.length; i++) {
- labels[i] = elements[i].getLabel();
drops[i] = elements[i].drop();
els[i] = elements[i].build(cx, cnt);
if (elements[i].getOwnerTag() != null)
Sequence ret = null;
if (dropAll) ret = Sequence.drop(els, false);
else {
- ret = cx.rm.tryResolveTag(tag, cnt==null?null:cnt.name, els, labels, drops);
+ ret = cx.rm.tryResolveTag(tag, cnt==null?null:cnt.name, els, drops);
if (ret == null) {
int idx = -1;
for(int i=0; i<els.length; i++)
public static @bind.as("~") ElementNode tilde(final ElementNode e) {
return new PostProcess(e) {
public Element postProcess(Element e) {
- return infer((Topology<Character>)Atom.toAtom(e).complement());
+ return infer((Topology<Character>)Atom.toAtom(e).complement().minus(CharRange.braces));
} }; }
public static @bind.as("Word") String word(String s) { return s; }
public Context(Tree t, GrammarBindingResolver rm) {
this.rm = rm;
Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)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); }