- }
-
- public static class Block /*implements Token*/ {
- Block parent;
- public final int row;
- public final int col;
- public final int iip;
- private final Vector children = new Vector();
- private String pending = "";
-
- //public Location getLocation() { return /*new Location.Cartesian(row, col)*/null; }
- 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();
- }
- public String toString() { return toString(80); }
- public String toString(int justificationLimit) { return toString(0, 80); }
- protected String toString(int indent, int justificationLimit) {
- StringBuffer ret = new StringBuffer();
- StringBuffer line = new StringBuffer();
- for(int i=0; i<children.size(); i++) {
- Object o = children.elementAt(i);
- if (i>0 && children.elementAt(i-1) instanceof Block && justificationLimit!=-1) ret.append("\n");
- if (o instanceof Block) {
- ret.append(justify(line.toString(), indent, justificationLimit));
- line.setLength(0);
- if (justificationLimit==-1) {
- ret.append("{");
- ret.append(((Block)o).toString(indent+2, justificationLimit));
- ret.append("}");
- } else {
- ret.append(((Block)o).toString(indent+2, justificationLimit));
- }
- } else {
- line.append(o.toString());