- 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 String string(Tree<String> tree) {
+ String ret = "";
+ if (tree.head()!=null) ret += tree.head();
+ for(Tree<String> t : tree.children()) ret += string(t);
+ return ret;
+ }
+
+ public Object walk(Tree<String> 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<String> 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 ("[".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);