public static Union make() throws Exception { return make(MetaGrammarTree.meta, "s"); }
public static Union make(Tree<String> tree, String nt) throws Exception {
- Meta.MetaGrammarFile mgf = new MetaGrammar.Meta.MetaGrammarFile(tree);
+ Meta.MetaGrammarFile mgf = new Meta().new MetaGrammarFile(tree);
BuildContext bc = new BuildContext(mgf);
return mgf.get(nt).build(bc);
}
private static Element set(Range.Set r) { return CharRange.set(r); }
private static Element string(String s) { return CharRange.string(s); }
- private static Atom infer(Element e) { return infer(Atom.toAtom(e)); }
- private static Atom infer(Topology t) { return new CharRange((Topology<Character>)t); }
+ private static Atom infer(Element e) { return infer((Topology<Character>)Atom.toAtom(e)); }
+ private static Atom infer(Topology<Character> t) { return new CharRange(new CharTopology(t)); }
private MetaGrammar() { }
}
public static class Meta {
- public static class MetaGrammarFile extends HashMap<String,MetaNonterminal> {
+ public class MetaGrammarFile extends HashMap<String,MetaNonterminal> {
public MetaGrammarFile(Tree<String> tree) {
if (!tree.head().equals("grammar")) throw new Error();
for(Tree<String> nt : tree.child(0))
return ret;
}
}
- public static class MetaNonterminal {
+ public class MetaNonterminal {
public String name;
public MetaUnion rhs;
public MetaNonterminal(Tree<String> tree) {
public String toString() { return name + " = " + rhs; }
public Union build(BuildContext bc) { return rhs.build(bc, name); }
}
- public static MetaUnion rhs(Tree<String> t) {
+ public MetaUnion rhs(Tree<String> t) {
return t.numChildren()==1
? new MetaUnion(t.child(0), false)
: new MetaUnion(t, true);
}
- public static class MetaUnion implements MetaSequence {
+ public class MetaUnion implements MetaSequence {
public boolean prioritized;
public MetaSequence[] sequences;
public Sequence buildSequence(BuildContext bc) {
return u;
}
public MetaUnion(Tree<String> t, boolean prioritized) {
- //System.err.println("metaunion: " + t);
this.prioritized = prioritized;
int i = 0;
this.sequences = new MetaSequence[t.numChildren()];
public abstract Sequence buildSequence(BuildContext bc);
}
- public static MetaSequence makeMetaSequence(Tree<String> t) {
- if ("psx".equals(t.head())) return MetaConjunct.make(t.child(0));
- if (t.head().equals("&")) return new MetaAnd(makeMetaSequence(t.child(0)), MetaConjunct.make(t.child(1)), false);
- if (t.head().equals("&~")) return new MetaAnd(makeMetaSequence(t.child(0)), MetaConjunct.make(t.child(1)), true);
+ 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);
return null;
}
- public static class MetaAnd implements MetaSequence {
+ public class MetaAnd implements MetaSequence {
boolean not;
MetaSequence left;
MetaSequence right;
public String toString() { return left + " &"+(not?"~":"")+" "+right; }
}
- public static class MetaConjunct implements MetaSequence {
+ public class MetaConjunct implements MetaSequence {
public boolean negated = false;
public MetaClause[] elements;
+ public HashMap<MetaClause,String> labelMap = new HashMap<MetaClause,String>();
public MetaClause followedBy = null;
public String tag;
public MetaClause separator;
+ public void addNamedClause(String name, MetaClause mc) {
+ labelMap.put(mc, name);
+ }
public Sequence buildSequence(BuildContext bc) {
Element[] els = new Element[elements.length + (separator==null?0:(elements.length-1))];
boolean[] drops = new boolean[elements.length + (separator==null?0:(elements.length-1))];
+ Object[] labels = new Object[els.length];
boolean unwrap = false;
boolean dropAll = false;
if (tag!=null && tag.equals("[]")) unwrap = true;
for(int i=0; i<elements.length; i++) {
int j = separator==null ? i : i*2;
els[j] = elements[i].build(bc);
+ labels[j] = labelMap.get(elements[i]);
drops[j] = elements[i].drop;
if (separator!=null && i<elements.length-1) {
els[j+1] = separator.build(bc);
Sequence ret = null;
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, new Object[els.length], drops);
- else {
+ else if (tag!=null) {
+ ret = Sequence.rewritingSequence(tag, els, labels, drops);
+ } else {
int idx = -1;
for(int i=0; i<els.length; i++)
if (!drops[i])
return ret;
}
private MetaConjunct(Tree<String> t) {
- //System.err.println("MetaConjunct("+t+")");
elements = new MetaClause[t.numChildren()];
int i = 0; for(Tree<String> tt : t)
- elements[i++] = MetaClause.make(tt, this);
+ elements[i++] = makeMetaClause(tt, this);
}
public String toString() {
String ret = (tag==null ? "" : (tag+":: ")) + (negated ? "~" : "");
if (elements.length > 1) ret += ")";
return ret;
}
- public static MetaConjunct make(Tree<String> t) {
- //System.err.println("MetaConjunct.make("+t+")");
+ }
+ public MetaConjunct makeMetaConjunct(Tree<String> t) {
+ //System.err.println("makeMetaConjunct("+t+")");
if ("/".equals(t.head())) {
- MetaConjunct ret = make(t.child(0));
- ret.separator = MetaClause.make(t.child(1), ret);
+ MetaConjunct ret = makeMetaConjunct(t.child(0));
+ ret.separator = makeMetaClause(t.child(1), ret);
return ret;
}
if ("->".equals(t.head())) {
- MetaConjunct ret = make(t.child(0));
- ret.followedBy = MetaClause.make(t.child(1), ret);
+ MetaConjunct ret = makeMetaConjunct(t.child(0));
+ ret.followedBy = makeMetaClause(t.child(1), ret);
return ret;
}
if ("::".equals(t.head())) {
- MetaConjunct ret = make(t.child(1));
+ MetaConjunct ret = makeMetaConjunct(t.child(1));
ret.tag = string(t.child(0));
return ret;
}
}
return new MetaConjunct(t);
}
- }
- public static abstract class MetaClause {
- public String label = null;
- public boolean drop = false;
- public boolean lift = false;
- public abstract Element build(BuildContext bc);
- public static MetaClause make(Tree<String> t, MetaConjunct c) {
- //System.err.println("MetaClause.make("+t+")");
+ public MetaClause makeMetaClause(Tree<String> t, MetaConjunct c) {
+ //System.err.println("MetaClause.makeMetaClause("+t+")");
if (t==null) return new MetaEpsilon();
if (t.head()==null) return new MetaEpsilon();
if (t.head().equals("{")) throw new Error("metatree: " + t);
- if (t.head().equals("*")) return new MetaRepeat(make(t.child(0), c), false, null, true, true);
- if (t.head().equals("+")) return new MetaRepeat(make(t.child(0), c), false, null, false, true);
- if (t.head().equals("?")) return new MetaRepeat(make(t.child(0), c), false, null, true, false);
- if (t.head().equals("**")) return new MetaRepeat(make(t.child(0), c), true, null, true, true);
- if (t.head().equals("++")) return new MetaRepeat(make(t.child(0), c), true, null, false, true);
- if (t.head().equals("*/")) return new MetaRepeat(make(t.child(0), c), false, make(t.child(1), c), true, true);
- if (t.head().equals("+/")) return new MetaRepeat(make(t.child(0), c), false, make(t.child(1), c), false, true);
- if (t.head().equals("**/")) return new MetaRepeat(make(t.child(0), c), true, make(t.child(1), c), true, true);
- if (t.head().equals("++/")) return new MetaRepeat(make(t.child(0), c), true, make(t.child(1), c), false, true);
+ 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, null, true, false);
+ if (t.head().equals("**")) return new MetaRepeat(makeMetaClause(t.child(0), c), true, null, true, true);
+ if (t.head().equals("++")) return new MetaRepeat(makeMetaClause(t.child(0), c), true, null, false, true);
+ if (t.head().equals("*/")) return new MetaRepeat(makeMetaClause(t.child(0), c), false, makeMetaClause(t.child(1), c), true, 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 MetaRange(t.child(0));
if (t.head().equals("literal")) return new MetaStringLiteral(t.child(0));
if (t.head().equals(")")) return new MetaSelfReference();
if (t.head().equals("(")) return new MetaParens(t.child(0));
if (t.head().equals("~")) return new MetaInvert(t.child(0), c);
- if (t.head().equals("!")) { MetaClause mc = make(t.child(0), c); mc.drop = true; return mc; }
+ 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("^^")) throw new Error("carets: " + t);
+ if (t.head().equals(":")) {
+ String name = string(t.child(0));
+ MetaClause clause = makeMetaClause(t.child(1), c);
+ c.addNamedClause(name, clause);
+ return clause;
+ }
throw new Error("unknown: " + t);
}
- public static class MetaRepeat extends MetaClause {
+
+ public abstract class MetaClause {
+ public String label = null;
+ public boolean drop = false;
+ public boolean lift = false;
+ public abstract Element build(BuildContext bc);
+ }
+ public class MetaRepeat extends MetaClause {
public MetaClause element, separator;
public boolean maximal, zero, many;
public Element build(BuildContext bc) {
(separator==null?"":(" /"+separator));
}
}
- public static class MetaParens extends MetaClause {
+ public class MetaEpsilon extends MetaClause {
+ public String toString() { return "()"; }
+ public Element build(BuildContext bc) { return Union.epsilon; }
+ }
+ public class MetaParens extends MetaClause {
public MetaUnion body;
public MetaParens(Tree<String> t) { this.body = rhs(t); }
public String toString() { return "( " + body + " )"; }
/*
public static class MetaTree extends MetaClause {
public MetaConjunct body;
- public MetaTree(Tree<String> t) { this.body = MetaConjunct.make(t); }
+ public MetaTree(Tree<String> t) { this.body = makeMetaConjunct(t); }
public String toString() { return "{ " + body + " }"; }
public Element build(BuildContext bc) {
return new Union("{}");// body.buildSequence();
}
}
*/
- public static class MetaEpsilon extends MetaClause {
- public String toString() { return "()"; }
- public Element build(BuildContext bc) { return Union.epsilon; }
- }
- public static class MetaRange extends MetaClause {
+ public class MetaRange extends MetaClause {
Range.Set range = new Range.Set();
public String toString() { return range.toString(); }
public Element build(BuildContext bc) { return set(range); }
}
}
}
- public static class MetaStringLiteral extends MetaClause {
+ public class MetaStringLiteral 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 String toString() { return "\""+StringUtil.escapify(literal, "\"\r\n\\")+"\""; }
}
- public static class MetaNonterminalReference extends MetaClause {
+ public class MetaNonterminalReference extends MetaClause {
public String name;
public MetaNonterminalReference(Tree<String> name) { this.name = string(name); }
public Element build(BuildContext bc) { return bc.build(name); }
public String toString() { return name; }
}
- public static class MetaSelfReference extends MetaClause {
+ public class MetaSelfReference extends MetaClause {
public String toString() { return "(*)"; }
public Element build(BuildContext bc) { return new Union("(*)"); /* FIXME */ }
}
- public static class MetaInvert extends MetaClause {
+ public class MetaInvert extends MetaClause {
public MetaClause element;
- public MetaInvert(Tree<String> t, MetaConjunct c) { this.element = make(t, c); }
+ public MetaInvert(Tree<String> t, MetaConjunct c) { this.element = makeMetaClause(t, c); }
public String toString() { return "~"+element; }
- public Element build(BuildContext bc) { return infer(Atom.toAtom(element.build(bc)).complement()); }
+ public Element build(BuildContext bc) { return infer((Topology<Character>)Atom.toAtom(element.build(bc)).complement()); }
}
- }
}