X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FMetaGrammar.java;h=49befee1a7c20583f49a2ef78bdc78b06131e005;hp=f1ae308b359349f28e5a391897c0a598a516bab7;hb=4c5cadfb5604449d04e2cdaaae7b9a61795c9044;hpb=c1519a9645e80f123c485a15931b193a22140184 diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index f1ae308..49befee 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammar.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammar.java @@ -4,7 +4,7 @@ import edu.berkeley.sbp.*; import java.util.*; import java.io.*; -public class MetaGrammar extends ReflectiveWalker { +public class MetaGrammar extends StringWalker { public static Union make() throws Exception { return ((MetaGrammar)new MetaGrammar().walk(meta)).done(); @@ -15,8 +15,7 @@ public class MetaGrammar extends ReflectiveWalker { return ret.toString(); } - // FIXME - private static HashSet dropAll = new HashSet(); + public /*private*/ static HashSet dropAll = new HashSet(); // Statics ////////////////////////////////////////////////////////////////////////////// @@ -52,16 +51,7 @@ public class MetaGrammar extends ReflectiveWalker { // MetaGrammar ////////////////////////////////////////////////////////////////////////////// - public PreSequence _amp_(PreSequence p, Object[] o) { return p.and(new PreSequence(o, null).buildSequence(null, true, false, true)); } - public PreSequence _amp__tilde_(PreSequence p, Object[] o) { return p.not(new PreSequence(o, null).buildSequence(null, true, false, true)); } - public Element epsilon(Object o, Object b) { return Union.epsilon; } - public Object _leftparen__rightparen_(Object o) { return Union.epsilon; } - public Element _rightparen_(Object e) { return SELF; } - public Union nonTerminal(String s) { return nonTerminal(s, null, false, false); } - public Union _colon__colon__equals_(String s, PreSequence[][] p) { return nonTerminal(s, p, false, false); } - public Union _bang__colon__colon__equals_(String s, PreSequence[][] p) { return nonTerminal(s, p, false, true); } - public Union _colon__colon__equals_(boolean q, String s, PreSequence[][] p) { return nonTerminal(s, p, false, q); } - public Object _leftparen_(PreSequence[][] p) { return nonTerminal("anon"+(anon++), p, false, false); } + public Union nonTerminal(String str) { return nonTerminal(str, null, false, false); } public Union nonTerminal(String str, PreSequence[][] s, boolean synthetic, boolean dropAll) { Union n = str.equals(startSymbol) ? g : nt.get(str); if (n == null) nt.put(str, n = new Union(str, synthetic)); @@ -73,7 +63,7 @@ public class MetaGrammar extends ReflectiveWalker { HashSet temp = new HashSet(); for(PreSequence pre : s[i]) { pre.not.addAll(seqs); - Sequence seq = pre.buildSequence(n, false, dropAll, false); + Sequence seq = pre.buildSequence(n, false, dropAll); temp.add(seq); n.add(seq); } @@ -82,72 +72,85 @@ public class MetaGrammar extends ReflectiveWalker { return n; } - public Object _backslash__leftbrace_(String s) { return SELF; } - public Object _leftbrace_(PreSequence p) { - return nonTerminal("braced"+(anon++), - new PreSequence[][] { - new PreSequence[] { - new PreSequence(new Element[] { leftBrace(), - p.buildUnion(), - rightBrace() }) - } - }, - false, - false); + public String string(Iterable> children) { + String ret = ""; + for(Tree t : children) ret += string(t); + return ret; } - - public PreSequence rewrite(Object[] o) { return new PreSequence(o, null); } - - public PreSequence seqx(PreSequence p, String tag) { return _equals__greater_(p, tag); } - public PreSequence _equals__greater_(PreSequence p, String tag) { - p.tag = tag; - return p; + public String string(Tree tree) { + String ret = ""; + if (tree.head()!=null) ret += tree.head(); + ret += string(tree.children()); + return ret; } - public PreSequence ps(Object[] o) { return new PreSequence(o); } - public PreSequence ps2(Object[] o1, String s, Object[] o2) { - if (o1==null) o1 = new Object[0]; - if (o2==null) o2 = new Object[0]; - Object[] o3 = new Object[o1.length + o2.length + 1]; - System.arraycopy(o1, 0, o3, 0, o1.length); - o3[o1.length] = new MyLift(s); - System.arraycopy(o2, 0, o3, o1.length+1, o2.length); - return new PreSequence(o3); - } - public PreSequence _slash_(PreSequence p, Object sep) { return p.sparse(sep); } - - public Object _star_(Element r) { return Repeat.many0(r); } - public Object _plus_(final Element r) { return Repeat.many1(r); } - public Object _plus__slash_(final Element r, Element s) { return Repeat.many1(r, s); } - public Object _star__slash_(final Element r, Element s) { return Repeat.many0(r, s); } - public Object _star__star_(final Element r) { return Repeat.maximal(Repeat.many0(r)); } - public Object _plus__plus_(final Element r) { return Repeat.maximal(Repeat.many1(r)); } - public Element _question_(final Element r) { return Repeat.maybe(r); } - - public MetaGrammar gram(Object o, MetaGrammar g, Object o2) { return g; } - public MetaGrammar grammar(Object[] o) { return this; } - public MetaGrammar grammar(Object o, Union[] u, Object x) { return this; } - public char _backslash_n() { return '\n'; } - public char _backslash_r() { return '\r'; } - public Element literal(String s) { Element ret = string(s); dropAll.add(ret); return ret; } - public Range _minus_(char a, char b) { return new Range(a, b); } - public Range range(char a) { return new Range(a, a); } - public Element _leftbracket_ (Range[] rr) { return ranges(true, rr); } - public Element _leftbracket__tilde_(Range[] rr) { return ranges(false, rr); } - public Element ranges(boolean positive, Range[] rr) { - Range.Set ret = positive ? new Range.Set() : new Range.Set(new Range(true, true)); - if (rr != null) - for(Range r : rr) - if (positive) ret.add(r); - else ret.remove(r); - return set(ret); + public Object walk(Tree tree) { + String head = tree.head(); + if (tree.numChildren()==0) return super.walk(tree); + if ("\\n".equals(head)) return new Character('\n'); + else if ("\\r".equals(head)) return new Character('\r'); + else if ("grammar".equals(head)) { for(Tree t : tree.children()) walk(t); return this; } + else if ("*".equals(head)) return Repeat.many0((Element)walk(tree.child(0))); + else if ("+".equals(head)) return Repeat.many1((Element)walk(tree.child(0))); + else if ("+/".equals(head)) return Repeat.many1((Element)walk(tree.child(0)), (Element)walk(tree.child(1))); + else if ("*/".equals(head)) return Repeat.many0((Element)walk(tree.child(0)), (Element)walk(tree.child(1))); + else if ("**".equals(head)) return Repeat.maximal(Repeat.many0((Element)walk(tree.child(0)))); + else if ("++".equals(head)) return Repeat.maximal(Repeat.many1((Element)walk(tree.child(0)))); + else if ("?".equals(head)) return Repeat.maybe((Element)walk(tree.child(0))); + else if ("&".equals(head)) + return ((PreSequence)walk(tree,0)).and(new PreSequence((Element[])Reflection.lub((Object[])walk(tree, 1)), null).buildSequence(null, true, false)); + else if ("&~".equals(head)) + return ((PreSequence)walk(tree,0)).not(new PreSequence((Element[])Reflection.lub((Object[])walk(tree, 1)), null).buildSequence(null, true, false)); + else if ("epsilon".equals(head)) return Union.epsilon; + else if ("()".equals(head)) return Union.epsilon; + else if (")".equals(head)) return SELF; + else if ("nonTerminal".equals(head)) return nonTerminal(string(tree.child(0)), null, false, false); + else if ("::=".equals(head)) return nonTerminal(string(tree.child(0)), (PreSequence[][])walk(tree, 1), false, false); + else if ("!::=".equals(head)) return nonTerminal(string(tree.child(0)), (PreSequence[][])walk(tree, 1), false, true); + else if ("(".equals(head)) return nonTerminal("anon"+(anon++), (PreSequence[][])walk(tree, 0), false, false); + else if ("literal".equals(head)) { Element ret = string(string(tree.child(0))); dropAll.add(ret); return ret; } + else if ("-".equals(head)) return new Range(walk(tree, 0).toString().charAt(0), walk(tree,1).toString().charAt(0)); + else if ("range".equals(head)) return new Range(walk(tree, 0).toString().charAt(0), walk(tree,0).toString().charAt(0)); + else if ("gram".equals(head)) return walk(tree, 1); + else if ("=>".equals(head)) { PreSequence p = (PreSequence)walk(tree, 0); p.tag = string(tree.child(1)); return p; } + else if ("/".equals(head)) return ((PreSequence)walk(tree, 0)).sparse((Element)walk(tree, 1)); + else if ("ps".equals(head)) return new PreSequence((Element[])walk(tree, 0)); + else if ("ps2".equals(head)) { + Object[] o1 = (Object[])walk(tree, 0); + String s = string(tree.child(1)); + Object[] o2 = (Object[])walk(tree, 2); + if (o1==null) o1 = new Object[0]; + if (o2==null) o2 = new Object[0]; + Object[] o3 = new Object[o1.length + o2.length + 1]; + System.arraycopy(o1, 0, o3, 0, o1.length); + o3[o1.length] = string(s); + System.arraycopy(o2, 0, o3, o1.length+1, o2.length); + PreSequence ret = new PreSequence(o3, s); + ret.drops[o1.length] = o3.length>1; + return ret; + } else if ("[".equals(head) || "[~".equals(head)) { + boolean positive = "[".equals(head); + Range[] rr = (Range[])walk(tree, 0); + Range.Set ret = positive ? new Range.Set() : new Range.Set(new Range(true, true)); + if (rr != null) + for(Range r : rr) + if (positive) ret.add(r); + else ret.remove(r); + return set(ret); + } + else return super.walk(tree); } - public class MyLift { - public final String s; - public MyLift(String s) { this.s = s; } + + public Object walk(String tag, Object[] argo) { + if (argo.length==0) return super.walk(tag, argo); + if (argo==null) return tag; + if (tag==null || "".equals(tag)) return argo; + return super.walk(tag, argo); } + ////////////////////////////////////////////////////////////////////////////// + public class PreSequence { public final HashSet and = new HashSet(); public final HashSet not = new HashSet(); @@ -198,8 +201,8 @@ public class MetaGrammar extends ReflectiveWalker { u.add(buildSequence(u)); return u; } - public Sequence buildSequence(Union u) { return buildSequence(u, false, false, false); } - public Sequence buildSequence(Union u, boolean lame, boolean dropAll, boolean keeper) { + public Sequence buildSequence(Union u) { return buildSequence(u, false, false); } + public Sequence buildSequence(Union u, boolean lame, boolean dropAll) { for(Sequence s : and) u.add(s); for(Sequence s : not) u.add(s); HashSet set = new HashSet(); @@ -210,7 +213,6 @@ public class MetaGrammar extends ReflectiveWalker { for(int i=0; i1; } else o2[j] = (Element)oi; if (MetaGrammar.dropAll.contains(o2[j])) drops[j] = true; @@ -220,9 +222,10 @@ public class MetaGrammar extends ReflectiveWalker { } } Element[] expansion = o2; - Sequence ret = dropAll || lame || keeper ? Sequence.drop(expansion, and, not, lame) : null; - if (ret==null && tag!=null) ret = Sequence.rewritingSequence(tag, expansion, drops, and, not); - if (ret==null) { + Sequence ret = null; + if (dropAll || lame) ret = Sequence.drop(expansion, and, not, lame); + else if (tag!=null) ret = Sequence.rewritingSequence(tag, expansion, drops, and, not); + else { int idx = -1; for(int i=0; i