- // Grammar //////////////////////////////////////////////////////////////////////////////
-
- public static class Grammar extends MetaGrammar {
- private int anon = 0;
- private final Element ws = Repeat.maximal0(nonTerminal("w"));
- public Grammar() { dropAll.add(ws); }
- public Object walk(Tree<String> tree) {
- String head = tree.head();
- if (tree.numChildren()==0) return super.walk(tree);
- if ("{".equals(head)) {
- String s = "braced"+(anon++);
- Union u = nonTerminal(s);
- Union u2 = ((PreSequence)walk(tree, 0)).sparse(ws).buildUnion();
- u2.add(Sequence.singleton(new Element[] { u }, 0, null, null));
- return nonTerminal(s,
- new PreSequence[][] {
- new PreSequence[] {
- new PreSequence(new Element[] { CharRange.leftBrace,
- ws,
- u2,
- ws,
- CharRange.rightBrace
- })
- }
- },
- false,
- false);
- }
- return super.walk(tree);
- }
- }
-
- /*
-public class Braces extends Union {
-
- private static final Element left = string("{");
- private static final Element right = string("}");
-
- public static String join(Object[] e) {
- StringBuffer ret = new StringBuffer();
- for(int i=0; i<e.length; i++) {
- if (i>0) ret.append(" ");
- ret.append(e[i]);
- }
- return ret.toString();
- }
-
- public Braces(Element[] e, Element sep) {
- super("{"+join(e)+"}");
- Element[] e2 = new Element[sep == null ? e.length+2 : e.length + 4];
- e2[0] = left;
- e2[e2.length-1] = right;
- if (sep != null) {
- e2[1] = sep;
- e2[e2.length-2] = sep;
- }
- for(int i=0; i<e.length; i++) e2[i+(sep==null ? 1 : 2)] = e[i];
- addAlternative(new Sequence.Singleton(e2));
- addAlternative(new Sequence.Singleton(sep==null ? new Element[] { left, this, right} : new Element[] { left, sep, this, sep, right }));
- }
-}
- */
-