- /*
- public CharToken next(int numstates) throws IOException {
- if (cur==null) return null;
- if (s != null) {
- if (spos < s.length()) {
- char c = s.charAt(spos++);
- if (c=='\n') { _row++; _col = 0; }
- else _col++;
- return new CharToken(c);
- }
- s = null;
- }
- if (pos >= cur.size()) {
- pos = cur.iip+1;
- _row = cur.endrow;
- _col = cur.endcol;
- cur = cur.parent;
- if (cur==null) return null;
- return CharToken.right;
- }
- Object o = cur.child(pos++);
- if (o instanceof String) {
- spos = 0;
- s = (String)o;
- return next(numstates);
- }
- if (o instanceof Block) {
- Block b = (Block)o;
- _row = b.row;
- _col = b.col;
- }
- if (((Block)o).isLiteral()) {
- spos = 0;
- s = ((Block.Literal)o).text();
- return next(numstates);
- }
- cur = (Block)o;
- pos = 0;
- return CharToken.left;
- }
- */
- public static Block parse(BufferedReader br) throws Invalid, IOException {
- int row=0, col=0;
- try {
- boolean blankLine = false;
- Block top = new Block.Root();
- for(String s = br.readLine(); s != null; s = br.readLine()) {
- row++;
- col=0;
- while (s.length() > 0 &&
- s.charAt(0) == ' ' &&
- (!(top instanceof Block.Literal) || col < top.col)) { col++; s = s.substring(1); }
- if ((top instanceof Block.Literal) && col >= top.col) { top.add(s); continue; }
- if (s.length()==0) { blankLine = true; continue; }
- while (col < top.col) {
- if (s.startsWith("{}") && top instanceof Block.Literal && ((Block.Literal)top).braceCol == col) break;
- blankLine = false;
- top.endrow = row;
- top.endcol = col;
- top = top.closeIndent();
- }
- if (s.startsWith("{}")) {
- int bc = col;
- boolean append = top instanceof Block.Literal && ((Block.Literal)top).braceCol == bc;
- s = s.substring(2);
- col += 2;
- while (s.length() > 0 && s.charAt(0) == ' ' && !(append && col >= top.col) ) { col++; s = s.substring(1); }
- if (append) top.add(s); else (top = new Block.Literal(top, row, col, bc)).add(s);
- continue;
- }
- while (s.length() > 0 && s.charAt(s.length()-1)==' ') { s = s.substring(0, s.length()-1); }
- if (col > top.col) top = new Block.Indent(top, row, col);
- else if (blankLine) { top.endrow=row; top.endcol=col; top = top.closeIndent(); top = new Block.Indent(top, row, col); }
- blankLine = false;
- for(int i=0; i<s.length(); i++) {
- top.add(s.charAt(i));
- switch(s.charAt(i)) {
- case '{': top = new Block.Brace(top, row, col); break;
- case '}': top.endrow=row; top.endcol=col; top = top.closeBrace(); break;
- }
- }
- top.add('\n');
- top.finishWord();
- }
- // FIXME
- Block ret = top;
- while (ret.parent != null) ret = ret.parent;
- return ret;
- } catch (InternalException ie) {
- throw new Invalid(ie, row, col);
- }
- }