+
+ public static class BuildContext extends HashMap<String,Union> {
+ private final Meta.MetaGrammarFile mgf;
+ public BuildContext(Meta.MetaGrammarFile mgf) { this.mgf = mgf; }
+ public Union build(String s) {
+ Union ret = get(s);
+ if (ret != null) return ret;
+ return mgf.get(s).build(this);
+ }
+ }
+
+ public static class Meta {
+ public static 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))
+ add(new MetaNonterminal(nt));
+ }
+ private void add(MetaNonterminal mnt) {
+ this.put(mnt.name, mnt);
+ }
+ public String toString() {
+ String ret = "";
+ for(MetaNonterminal mnt : this.values()) ret += mnt + "\n";
+ return ret;
+ }
+ }
+ public static class MetaNonterminal {
+ public String name;
+ public MetaUnion rhs;
+ public MetaNonterminal(Tree<String> tree) {
+ System.err.println("MetaNonterminal("+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 static 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 boolean prioritized;
+ public MetaSequence[] sequences;
+ public Sequence buildSequence(BuildContext bc) {
+ return Sequence.singleton(new Element[] { buildAnon(bc) }, 0);
+ }
+ public Union buildAnon(BuildContext bc) {
+ String s = "";
+ for(int i=0; i<sequences.length; i++)
+ s += (i>0?"\n "+(prioritized?">":"|")+" ":"")+sequences[i].buildSequence(bc);
+ return build(bc, s);
+ }
+ public Union build(BuildContext bc, String name) {
+ Union u = bc.get(name);
+ if (u != null) return u;
+ u = new Union(name);
+ bc.put(name, u);
+ HashSet<Sequence> seqs = new HashSet<Sequence>();
+ for(MetaSequence s : sequences) {
+ Sequence seq = s.buildSequence(bc);
+ if (seq != null) {
+ Sequence oseq = seq;
+ if (prioritized)
+ for(Sequence seqprev : seqs)
+ seq = seq.not(seqprev);
+ u.add(seq);
+ seqs.add(oseq);
+ }
+ }
+ return u;
+ }
+ public MetaUnion(Tree<String> t, boolean prioritized) {
+ System.err.println("MetaUnion("+t+")");
+ //System.err.println("metaunion: " + t);
+ this.prioritized = prioritized;
+ int i = 0;
+ this.sequences = new MetaSequence[t.numChildren()];
+ for(Tree<String> tt : t)
+ sequences[i++] = prioritized
+ ? new MetaUnion(tt, false)
+ : makeMetaSequence(tt);
+ }
+ public String toString() {
+ String ret = "\n ";
+ for(int i=0; i<sequences.length; i++) {
+ ret += sequences[i];
+ if (i<sequences.length-1)
+ ret += (prioritized ? "\n > " : "\n | ");
+ }
+ return ret;
+ }
+ }
+
+ public interface MetaSequence {
+ public abstract Sequence buildSequence(BuildContext bc);
+ }
+
+ public static MetaSequence makeMetaSequence(Tree<String> t) {
+ System.err.println("MetaSequence("+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);
+ return null;
+ }
+
+ public static class MetaAnd implements MetaSequence {
+ boolean not;
+ MetaSequence left;
+ MetaSequence right;
+ public Sequence buildSequence(BuildContext bc) {
+ Sequence ret = left.buildSequence(bc);
+ if (not) ret.not(right.buildSequence(bc)); else ret.and(right.buildSequence(bc));
+ return ret;
+ }
+ public MetaAnd(MetaSequence left, MetaSequence right, boolean not) {
+ this.left = left;
+ this.right = right;
+ this.not = not;
+ }
+ public String toString() { return left + " &"+(not?"~":"")+" " /* FIXME */; }
+ }
+
+ public static class MetaConjunct implements MetaSequence {
+ public boolean negated = false;
+ public MetaClause[] elements;
+ public MetaClause followedBy = null;
+ public String tag;
+ public MetaClause separator;
+ 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))];
+ boolean unwrap = false;
+ boolean dropAll = false;
+ if (tag!=null && tag.equals("[]")) unwrap = true;
+ if (tag!=null && "()".equals(tag)) dropAll=true;
+ for(int i=0; i<elements.length; i++) {
+ int j = separator==null ? i : i*2;
+ els[j] = elements[i].build(bc);
+ drops[j] = elements[i].drop;
+ if (separator!=null && i<elements.length-1) {
+ els[j+1] = separator.build(bc);
+ drops[j+1] = true;
+ }
+ }
+ 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 {
+ int idx = -1;
+ for(int i=0; i<els.length; i++)
+ if (!drops[i])
+ if (idx==-1) idx = i;
+ else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
+ if (idx != -1) ret = Sequence.singleton(els, idx);
+ else ret = Sequence.drop(els, false);
+ }
+ if (this.followedBy != null) ret.follow = new Hack(new Infer(this.followedBy.build(bc)));
+ 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);
+ }
+ 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)));
+ }
+ }
+ //set = set(ret);
+ }
+ }
+ 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 */ }
+ }
+ public static class MetaInvert extends MetaClause {
+ public MetaClause element;
+ public MetaInvert(Tree<String> t, MetaConjunct c) { this.element = make(t, c); }
+ public String toString() { return "~"+element; }
+ public Element build(BuildContext bc) { return new Hack(new Invert(new Infer(element.build(bc)))); }
+ }
+ }
+
+ }
+