X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FMetaGrammar.java;h=d631e8ab0f4a6d57dd3a3302e77236378d6426bb;hp=656f878e2bb6793dc4cc8d824da81b1fbcec400b;hb=2c399ca4d7e658266c974f43f26e5a55944f9042;hpb=ea83c68e20abf2e74f901502a053b413b93e5289 diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index 656f878..d631e8a 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammar.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammar.java @@ -51,15 +51,6 @@ public class MetaGrammar extends ReflectiveWalker { // MetaGrammar ////////////////////////////////////////////////////////////////////////////// - public PreSequence _amp_(PreSequence p, Object[] o) { return p.and(new PreSequence(o, null).buildSequence(null, true, false)); } - public PreSequence _amp__tilde_(PreSequence p, Object[] o) { return p.not(new PreSequence(o, null).buildSequence(null, true, false)); } - 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 Object _leftparen_(PreSequence[][] p) { return nonTerminal("anon"+(anon++), p, 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)); @@ -111,26 +102,40 @@ public class MetaGrammar extends ReflectiveWalker { } 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 String string(Tree tree) { + String ret = ""; + if (tree.head()!=null) ret += tree.head(); + for(Tree t : tree.children()) ret += string(t); + return 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 return super.walk(tree); } - public MetaGrammar grammar(Object o, Union[] u, Object x) { return this; } - public Object gram(Object o, Object g, Object o2) { return g; } - public MetaGrammar grammar(Object[] o) { return this; } - public Element literal(String s) { Element ret = string(s); dropAll.add(ret); return ret; } + public Object gram(Object o, Object g, Object o2) { return g; } + 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); }