-
- // Exceptions //////////////////////////////////////////////////////////////////////////////
-
- private static class InternalException extends RuntimeException { public InternalException(String s) { super(s); } }
- public static class Invalid extends /*IOException*/RuntimeException {
- public Invalid(InternalException ie, int row, int col) {
- super(ie.getMessage() + " at " + row + ":" + col);
- }
- }
-
- // Testing //////////////////////////////////////////////////////////////////////////////
-
- public static void main(String[] s) throws Exception {
- System.out.println(parse(new BufferedReader(new InputStreamReader(System.in))).toString(-1)); }
-
- // Utilities //////////////////////////////////////////////////////////////////////////////
-
- public static String spaces(int i) { if (i<=0) return ""; return " " + spaces(i-1); }
-
- private static String justify(String s, int indent, int justificationLimit) {
- if (s.length() == 0) return "";
- if (justificationLimit==-1) return s;
- StringBuffer ret = new StringBuffer();
- while(s.length() > 0) {
- if (s.charAt(0) == ' ') { s = s.substring(1); continue; }
- ret.append(spaces(indent));
- int i = s.indexOf(' ');
- if (i==-1) i = s.length();
- while(s.indexOf(' ', i+1) != -1 && s.indexOf(' ', i+1) < justificationLimit-indent) i = s.indexOf(' ', i+1);
- if (s.length() + indent < justificationLimit) i = s.length();
- ret.append(s.substring(0, i));
- s = s.substring(i);
- ret.append('\n');
- }
- return ret.toString();
- }
-
- // 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[] { CharToken.leftBrace,
- ws,
- u2,
- ws,
- CharToken.rightBrace
- })
- }
- },
- false,
- false);
- }
- return super.walk(tree);