X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FMetaGrammar.java;h=3420ad5ca328f4c2b9692489313a1c39304a43e0;hp=285016bc8c3a6d097cefcd4c5fb6ccb85ffd7ada;hb=98ae7592861f7e7d3bf5744b5db84ffdefc6011d;hpb=c48a9498daf7604e41cf4aea7f6f7110388fd659 diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index 285016b..3420ad5 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammar.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammar.java @@ -9,44 +9,19 @@ public class MetaGrammar extends StringWalker { public static Object repeatTag = null; - /** an atom which tracks the possible tokenset of some element, provided that element can only match single-token sequences */ - public static class Infer extends Atom { - private final Element e; - public Infer(Element e) { this.e = e; } - public Topology top() { return (Topology)toAtom(e); } - public String toString() { return e.toString(); } - } - - public static Atom infer(Element e) { return new CharRange((Topology)Atom.toAtom(e)); } - - /** an atom which tracks the inverse of some other atom */ - public static class Invert extends Atom { - private final Atom a; - public Invert(Atom a) { this.a = a; } - public Topology top() { return a.complement(); } - public String toString() { return "~"+a; } - } - - public static class Hack extends Atom { - private final Atom a; - static final Topology leftright = CharRange.rightBrace.union(CharRange.leftBrace); - public Hack(Atom a) { this.a = a; } - public Topology top() { return a.minus(leftright); } - public String toString() { return a.toString(); } - } - public static Union make() throws Exception { return make(MetaGrammarTree.meta, "s"); } public static Union make(Tree 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 boolean strings; - private static Element set(Range.Set r) { if (strings) throw new Error(); return CharRange.set(r); } - private static Element string(String s) { return strings ? StringInput.string(s) : CharRange.string(s); } + 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((Topology)Atom.toAtom(e)); } + private static Atom infer(Topology t) { return new CharRange(new CharTopology(t)); } private MetaGrammar() { } @@ -64,49 +39,61 @@ public class MetaGrammar extends StringWalker { public static class BuildContext extends HashMap { 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 static class MetaGrammarFile extends HashMap { + 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 { public MetaGrammarFile(Tree tree) { if (!tree.head().equals("grammar")) throw new Error(); for(Tree 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 static class MetaNonterminal { + public class NonTerminal { public String name; public MetaUnion rhs; - public MetaNonterminal(Tree tree) { + public NonTerminal(Tree 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 static MetaUnion rhs(Tree t) { + public MetaUnion rhs(Tree 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) { @@ -138,7 +125,6 @@ public class MetaGrammar extends StringWalker { return u; } public MetaUnion(Tree t, boolean prioritized) { - //System.err.println("metaunion: " + t); this.prioritized = prioritized; int i = 0; this.sequences = new MetaSequence[t.numChildren()]; @@ -162,14 +148,14 @@ public class MetaGrammar extends StringWalker { public abstract Sequence buildSequence(BuildContext bc); } - public static MetaSequence makeMetaSequence(Tree 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 t) { + 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 static class MetaAnd implements MetaSequence { + public class MetaAnd implements MetaSequence { boolean not; MetaSequence left; MetaSequence right; @@ -192,15 +178,20 @@ public class MetaGrammar extends StringWalker { public String toString() { return left + " &"+(not?"~":"")+" "+right; } } - public static class MetaConjunct implements MetaSequence { + public class Conjunct implements MetaSequence { public boolean negated = false; public MetaClause[] elements; + public HashMap labelMap = new HashMap(); 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; @@ -208,6 +199,7 @@ public class MetaGrammar extends StringWalker { for(int i=0; i t) { - //System.err.println("MetaConjunct("+t+")"); + private Conjunct(Tree t) { elements = new MetaClause[t.numChildren()]; int i = 0; for(Tree tt : t) - elements[i++] = MetaClause.make(tt, this); + elements[i++] = makeMetaClause(tt, this); } public String toString() { String ret = (tag==null ? "" : (tag+":: ")) + (negated ? "~" : ""); @@ -245,70 +238,80 @@ public class MetaGrammar extends StringWalker { if (elements.length > 1) ret += ")"; return ret; } - public static MetaConjunct make(Tree t) { - //System.err.println("MetaConjunct.make("+t+")"); + } + public Conjunct makeConjunct(Tree t) { + //System.err.println("makeConjunct("+t+")"); if ("/".equals(t.head())) { - MetaConjunct ret = make(t.child(0)); - ret.separator = MetaClause.make(t.child(1), ret); + Conjunct ret = makeConjunct(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); + Conjunct ret = makeConjunct(t.child(0)); + ret.followedBy = makeMetaClause(t.child(1), ret); return ret; } if ("::".equals(t.head())) { - MetaConjunct ret = make(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 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 t, MetaConjunct c) { - //System.err.println("MetaClause.make("+t+")"); - if (t==null) return new MetaEpsilon(); - if (t.head()==null) return new MetaEpsilon(); + public MetaClause makeMetaClause(Tree t, Conjunct c) { + //System.err.println("MetaClause.makeMetaClause("+t+")"); + 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(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 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 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 = 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("!")) { MetaClause mc = makeMetaClause(t.child(0), c); mc.drop = true; return mc; } + 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)); + 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) { return !maximal - ? new Repeat(element.build(bc), zero, many, separator==null?null:separator.build(bc), null) - : separator==null - ? new Repeat.Maximal(infer(element.build(bc)), zero, many, null) - : new Repeat.Maximal(element.build(bc), zero, many, infer(separator.build(bc)), null); + ? (separator==null + ? Sequence.repeat(element.build(bc), zero, many, null, null) + : Sequence.repeat(element.build(bc), zero, many, separator.build(bc), null)) + : (separator==null + ? Sequence.repeatMaximal(infer(element.build(bc)), zero, many, null) + : Sequence.repeatMaximal(element.build(bc), zero, many, infer(separator.build(bc)), null)); } public MetaRepeat(MetaClause element, boolean maximal, MetaClause separator, boolean zero, boolean many) { this.separator = separator; @@ -324,27 +327,27 @@ public class MetaGrammar extends StringWalker { (separator==null?"":(" /"+separator)); } } - public static class MetaParens extends MetaClause { + public class Epsilon extends MetaClause { + public String toString() { return "()"; } + public Element build(BuildContext bc) { return Union.epsilon; } + } + public class Parens extends MetaClause { public MetaUnion body; - public MetaParens(Tree t) { this.body = rhs(t); } + public Parens(Tree 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 t) { this.body = MetaConjunct.make(t); } + public Conjunct body; + public MetaTree(Tree t) { this.body = makeConjunct(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); } @@ -359,29 +362,28 @@ public class MetaGrammar extends StringWalker { } } } - public static class MetaStringLiteral extends MetaClause { + public class StringLiteral extends MetaClause { public String literal; public Element build(BuildContext bc) { return string(literal); } - public MetaStringLiteral(Tree literal) { this.literal = string(literal); this.drop = true; } + public StringLiteral(Tree 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 NonTerminalReference extends MetaClause { public String name; - public MetaNonterminalReference(Tree name) { this.name = string(name); } + public NonTerminalReference(Tree 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 SelfReference 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 t, MetaConjunct c) { this.element = make(t, c); } + public MetaInvert(Tree t, Conjunct c) { this.element = makeMetaClause(t, c); } public String toString() { return "~"+element; } - public Element build(BuildContext bc) { return new Hack(new Invert(infer(element.build(bc)))); } + public Element build(BuildContext bc) { return infer((Topology)Atom.toAtom(element.build(bc)).complement()); } } - } } @@ -431,4 +433,5 @@ public class MetaGrammar extends StringWalker { p.flush(); os.close(); } + }