- } catch (InternalException ie) {
- throw new Invalid(ie, row, col);
- }
- }
-
- public static class Block implements Token {
- Block parent;
- public final int row;
- public final int col;
- public final int iip;
- private final Vec children = new Vec();
- private String pending = "";
-
- public Tree<String> result() {
- // FIXME
- }
-
- public Location getLocation() { return new Location.Cartesian(row, col); }
- public boolean isEOF() { return false; }
-
- public int size() { return children.size(); }
- public Object child(int i) { return children.elementAt(i); }
- public boolean isLiteral() { return false; }
-
- protected Block(int row, int col) {
- this.row=row;
- this.col=col;
- this.iip = -1;
- parent = null;
- }
- public Block(Block parent, int row, int col) {
- this.row=row;
- this.col=col;
- this.iip = parent.size();
- (this.parent = parent).add(this);
- }
-
- public void add(String s) { children.addElement(s); }
- public void add(char c) {
- if (c == '{' || c == '}') { finishWord(); return; }
- if (c != ' ') { pending += c; return; }
- if (pending.length() > 0) { finishWord(); add(" "); return; }
- if (size()==0) return;
- if (child(size()-1).equals(" ")) return;
- add(" ");
- return;
- }
-
- public void add(Block b) { children.addElement(b); }
- public Block promote() { parent.parent.replaceLast(this); return close(); }
- public Object lastChild() { return children.lastElement(); }
- public Block lastChildAsBlock() { return (Block)lastChild(); }
- public void replaceLast(Block b) { children.setElementAt(b, children.size()-1); b.parent = this; }
-
- public void finishWord() { if (pending.length() > 0) { add(pending); pending = ""; } }
-
- public Block closeBrace() { throw new InternalException("attempt to closeBrace() a "+getClass().getName()); }
- public Block closeIndent() { throw new InternalException("attempt to closeIndent() a "+getClass().getName()); }
- public Block close() {
- while(size() > 0 && child(size()-1).equals(" ")) children.setSize(children.size()-1);
- if (size()==0) throw new InternalException("PARSER BUG: attempt to close an empty block (should never happen)");
- if (size() > 1 || !(lastChild() instanceof Block)) return parent;
- return lastChildAsBlock().promote();