- 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==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 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 maximal
- ? new Repeat.Maximal(element.build(bc), zero, many, separator==null?null:separator.build(bc), null)
- : new Repeat(element.build(bc), zero, many, separator==null?null:separator.build(bc), null);
- }
- 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)));
- }
- }
- }
- }
- public static 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 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 String toString() { return "(*)"; }
- public Element build(BuildContext bc) { return new Union("(*)"); /* FIXME */ }