+ public String toString() {
+ String ret = (tag==null ? "" : (tag+":: ")) + (negated ? "~" : "");
+ if (elements.length > 1) ret += "(";
+ for(MetaClause mc : elements) ret += (" " + mc + " ");
+ if (separator != null) ret += " /" + separator;
+ if (followedBy != null) ret += " -> " + followedBy;
+ if (elements.length > 1) ret += ")";
+ return ret;
+ }
+ public static MetaConjunct make(Tree<String> t) {
+ //System.err.println("MetaConjunct.make("+t+")");
+ if ("/".equals(t.head())) {
+ MetaConjunct ret = make(t.child(0));
+ ret.separator = MetaClause.make(t.child(1), ret);
+ return ret;
+ }
+ if ("->".equals(t.head())) {
+ MetaConjunct ret = make(t.child(0));
+ ret.followedBy = MetaClause.make(t.child(1), ret);
+ return ret;
+ }
+ if ("::".equals(t.head())) {
+ MetaConjunct ret = make(t.child(1));
+ ret.tag = string(t.child(0));
+ return ret;
+ }
+ if ("ps".equals(t.head())) {
+ return new MetaConjunct(t.child(0));
+ }
+ 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+")");
+ 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 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("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("~")) 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("^")) { c.tag = string(t.child(0)); return new MetaStringLiteral(t.child(0)); }
+ if (t.head().equals("^^")) throw new Error("carets: " + t);
+ throw new Error("unknown: " + t);
+ }
+ public static class MetaRepeat extends MetaClause {
+ public MetaClause element, separator;
+ public boolean maximal, zero, many;
+ public Element build(BuildContext bc) {
+ return new Repeat(element.build(bc), zero, many, separator==null?null:separator.build(bc), maximal);
+ }
+ public MetaRepeat(MetaClause element, boolean maximal, MetaClause separator, boolean zero, boolean many) {
+ this.separator = separator;
+ this.element = element;
+ this.maximal = maximal;
+ this.zero = zero;
+ this.many = many;
+ }
+ public String toString() {
+ return element+
+ ((zero&&!many)?"?":zero?"*":"+")+
+ (!maximal?"":zero?"*":"+")+
+ (separator==null?"":(" /"+separator));
+ }
+ }
+ public static class MetaParens extends MetaClause {
+ public MetaUnion body;
+ public MetaParens(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 = MetaConjunct.make(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 {
+ Range.Set range = new Range.Set();
+ public String toString() { return range.toString(); }
+ public Element build(BuildContext bc) { return set(range); }
+ public MetaRange(Tree<String> t) {
+ for(Tree<String> tt : t) {
+ if (tt.head().equals("range")) {
+ range.add(tt.child(0).head().charAt(0));
+ } else if (tt.head().equals("-")) {
+ range.add(new Range(string(tt.child(0)).charAt(0),
+ string(tt.child(1)).charAt(0)));
+ }