public static class BuildContext extends HashMap<String,Union> {
private final Meta.MetaGrammarFile mgf;
+ public Meta.NonTerminal currentNonTerminal;
public BuildContext(Meta.MetaGrammarFile mgf) { this.mgf = mgf; }
public Union build(String s) {
Union ret = get(s);
if (ret != null) return ret;
- Meta.MetaNonterminal mnt = mgf.get(s);
+ Meta.NonTerminal mnt = mgf.get(s);
if (mnt==null) throw new Error("undeclared nonterminal \""+s+"\"");
return mnt.build(this);
}
}
public static class Meta {
- public class MetaGrammarFile extends HashMap<String,MetaNonterminal> {
+ public Sequence resolveTag(String s, String nonTerminalName, Element[] els, Object[] labels, boolean [] drops) {
+ return Sequence.rewritingSequence(s, els, labels, drops);
+ }
+ public class MetaGrammarFile extends HashMap<String,NonTerminal> {
public MetaGrammarFile(Tree<String> tree) {
if (!tree.head().equals("grammar")) throw new Error();
for(Tree<String> nt : tree.child(0))
- add(new MetaNonterminal(nt));
+ add(new NonTerminal(nt));
}
- private void add(MetaNonterminal mnt) {
+ private void add(NonTerminal mnt) {
if (this.get(mnt.name)!=null) throw new Error("duplicate definition of nonterminal \""+mnt.name+"\"");
this.put(mnt.name, mnt);
}
public String toString() {
String ret = "";
- for(MetaNonterminal mnt : this.values()) ret += mnt + "\n";
+ for(NonTerminal mnt : this.values()) ret += mnt + "\n";
return ret;
}
}
- public class MetaNonterminal {
+ public class NonTerminal {
public String name;
public MetaUnion rhs;
- public MetaNonterminal(Tree<String> tree) {
+ public NonTerminal(Tree<String> tree) {
name = string(tree.child(0));
rhs = rhs(tree.child(1));
}
public String toString() { return name + " = " + rhs; }
- public Union build(BuildContext bc) { return rhs.build(bc, name); }
+ public Union build(BuildContext bc) {
+ NonTerminal ont = bc.currentNonTerminal;
+ bc.currentNonTerminal = this;
+ try {
+ return rhs.build(bc, name);
+ } finally {
+ bc.currentNonTerminal = ont;
+ }
+ }
}
public MetaUnion rhs(Tree<String> t) {
return t.numChildren()==1
}
public MetaSequence makeMetaSequence(Tree<String> t) {
- if ("psx".equals(t.head())) return makeMetaConjunct(t.child(0));
- if (t.head().equals("&")) return new MetaAnd(makeMetaSequence(t.child(0)), makeMetaConjunct(t.child(1)), false);
- if (t.head().equals("&~")) return new MetaAnd(makeMetaSequence(t.child(0)), makeMetaConjunct(t.child(1)), true);
+ if ("psx".equals(t.head())) return makeConjunct(t.child(0));
+ if (t.head().equals("&")) return new MetaAnd(makeMetaSequence(t.child(0)), makeConjunct(t.child(1)), false);
+ if (t.head().equals("&~")) return new MetaAnd(makeMetaSequence(t.child(0)), makeConjunct(t.child(1)), true);
return null;
}
public String toString() { return left + " &"+(not?"~":"")+" "+right; }
}
- public class MetaConjunct implements MetaSequence {
+ public class Conjunct implements MetaSequence {
public boolean negated = false;
public MetaClause[] elements;
public HashMap<MetaClause,String> labelMap = new HashMap<MetaClause,String>();
if (dropAll) ret = Sequence.drop(els, false);
else if (unwrap) ret = Sequence.unwrap(els, /*repeatTag FIXME*/null, drops);
else if (tag!=null) {
- ret = Sequence.rewritingSequence(tag, els, labels, drops);
+ ret = resolveTag(tag, bc.currentNonTerminal==null ? null : bc.currentNonTerminal.name, els, labels, drops);
} else {
int idx = -1;
for(int i=0; i<els.length; i++)
ret.follow = infer(this.followedBy.build(bc));
return ret;
}
- private MetaConjunct(Tree<String> t) {
+ private Conjunct(Tree<String> t) {
elements = new MetaClause[t.numChildren()];
int i = 0; for(Tree<String> tt : t)
elements[i++] = makeMetaClause(tt, this);
return ret;
}
}
- public MetaConjunct makeMetaConjunct(Tree<String> t) {
- //System.err.println("makeMetaConjunct("+t+")");
+ public Conjunct makeConjunct(Tree<String> t) {
+ //System.err.println("makeConjunct("+t+")");
if ("/".equals(t.head())) {
- MetaConjunct ret = makeMetaConjunct(t.child(0));
+ Conjunct ret = makeConjunct(t.child(0));
ret.separator = makeMetaClause(t.child(1), ret);
return ret;
}
if ("->".equals(t.head())) {
- MetaConjunct ret = makeMetaConjunct(t.child(0));
+ Conjunct ret = makeConjunct(t.child(0));
ret.followedBy = makeMetaClause(t.child(1), ret);
return ret;
}
if ("::".equals(t.head())) {
- MetaConjunct ret = makeMetaConjunct(t.child(1));
+ Conjunct ret = makeConjunct(t.child(1));
ret.tag = string(t.child(0));
return ret;
}
if ("ps".equals(t.head())) {
- return new MetaConjunct(t.child(0));
+ return new Conjunct(t.child(0));
}
- return new MetaConjunct(t);
+ return new Conjunct(t);
}
- public MetaClause makeMetaClause(Tree<String> t, MetaConjunct c) {
+ public MetaClause makeMetaClause(Tree<String> t, Conjunct c) {
//System.err.println("MetaClause.makeMetaClause("+t+")");
- if (t==null) return new MetaEpsilon();
- if (t.head()==null) return new MetaEpsilon();
+ if (t==null) return new Epsilon();
+ if (t.head()==null) return new Epsilon();
if (t.head().equals("{")) throw new Error("metatree: " + t);
if (t.head().equals("*")) return new MetaRepeat(makeMetaClause(t.child(0), c), false, null, true, true);
if (t.head().equals("+")) return new MetaRepeat(makeMetaClause(t.child(0), c), false, null, false, true);
if (t.head().equals("+/")) return new MetaRepeat(makeMetaClause(t.child(0), c), false, makeMetaClause(t.child(1), c), false, true);
if (t.head().equals("**/")) return new MetaRepeat(makeMetaClause(t.child(0), c), true, makeMetaClause(t.child(1), c), true, true);
if (t.head().equals("++/")) return new MetaRepeat(makeMetaClause(t.child(0), c), true, makeMetaClause(t.child(1), c), false, true);
- if (t.head().equals("()")) return new MetaEpsilon();
+ if (t.head().equals("()")) return new Epsilon();
if (t.head().equals("[")) return new MetaRange(t.child(0));
- if (t.head().equals("literal")) return new MetaStringLiteral(t.child(0));
- if (t.head().equals("nonTerminal")) return new MetaNonterminalReference(t.child(0));
- if (t.head().equals(")")) return new MetaSelfReference();
- if (t.head().equals("(")) return new MetaParens(t.child(0));
+ if (t.head().equals("literal")) return new StringLiteral(t.child(0));
+ if (t.head().equals("nonTerminal")) return new NonTerminalReference(t.child(0));
+ if (t.head().equals(")")) return new SelfReference();
+ if (t.head().equals("(")) return new Parens(t.child(0));
if (t.head().equals("~")) return new MetaInvert(t.child(0), c);
if (t.head().equals("!")) { MetaClause mc = makeMetaClause(t.child(0), c); mc.drop = true; return mc; }
- if (t.head().equals("^")) { c.tag = string(t.child(0)); return new MetaStringLiteral(t.child(0)); }
+ if (t.head().equals("^")) { c.tag = string(t.child(0)); return new StringLiteral(t.child(0)); }
if (t.head().equals("^^")) throw new Error("carets: " + t);
if (t.head().equals(":")) {
String name = string(t.child(0));
(separator==null?"":(" /"+separator));
}
}
- public class MetaEpsilon extends MetaClause {
+ public class Epsilon extends MetaClause {
public String toString() { return "()"; }
public Element build(BuildContext bc) { return Union.epsilon; }
}
- public class MetaParens extends MetaClause {
+ public class Parens extends MetaClause {
public MetaUnion body;
- public MetaParens(Tree<String> t) { this.body = rhs(t); }
+ public Parens(Tree<String> t) { this.body = rhs(t); }
public String toString() { return "( " + body + " )"; }
public Element build(BuildContext bc) { return body.buildAnon(bc); }
}
/*
public static class MetaTree extends MetaClause {
- public MetaConjunct body;
- public MetaTree(Tree<String> t) { this.body = makeMetaConjunct(t); }
+ public Conjunct body;
+ public MetaTree(Tree<String> t) { this.body = makeConjunct(t); }
public String toString() { return "{ " + body + " }"; }
public Element build(BuildContext bc) {
return new Union("{}");// body.buildSequence();
}
}
}
- public class MetaStringLiteral extends MetaClause {
+ public class StringLiteral extends MetaClause {
public String literal;
public Element build(BuildContext bc) { return string(literal); }
- public MetaStringLiteral(Tree<String> literal) { this.literal = string(literal); this.drop = true; }
+ public StringLiteral(Tree<String> literal) { this.literal = string(literal); this.drop = true; }
public String toString() { return "\""+StringUtil.escapify(literal, "\"\r\n\\")+"\""; }
}
- public class MetaNonterminalReference extends MetaClause {
+ public class NonTerminalReference extends MetaClause {
public String name;
- public MetaNonterminalReference(Tree<String> name) { this.name = string(name); }
+ public NonTerminalReference(Tree<String> name) { this.name = string(name); }
public Element build(BuildContext bc) { return bc.build(name); }
public String toString() { return name; }
}
- public class MetaSelfReference extends MetaClause {
+ public class SelfReference extends MetaClause {
public String toString() { return "(*)"; }
public Element build(BuildContext bc) { return new Union("(*)"); /* FIXME */ }
}
public class MetaInvert extends MetaClause {
public MetaClause element;
- public MetaInvert(Tree<String> t, MetaConjunct c) { this.element = makeMetaClause(t, c); }
+ public MetaInvert(Tree<String> t, Conjunct c) { this.element = makeMetaClause(t, c); }
public String toString() { return "~"+element; }
public Element build(BuildContext bc) { return infer((Topology<Character>)Atom.toAtom(element.build(bc)).complement()); }
}