X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTib.java;h=29181a012e9c7acdc86f6cf7d2c240ee4332b0c1;hp=200544aa5e4a53140c40a216e04195bee24d51ed;hb=9ded11559a1b6f817e99355b1c9e2c88042e91d4;hpb=f7dc68eeb40878c12463fc1ed2b4351b2a9fe261 diff --git a/src/edu/berkeley/sbp/tib/Tib.java b/src/edu/berkeley/sbp/tib/Tib.java index 200544a..29181a0 100644 --- a/src/edu/berkeley/sbp/tib/Tib.java +++ b/src/edu/berkeley/sbp/tib/Tib.java @@ -40,6 +40,7 @@ public class Tib implements Token.Stream { int _row = 0; int _col = 0; + public Token.Location getLocation() { return new CharToken.CartesianLocation(_row, _col); } public CharToken next() throws IOException { if (cur==null) return null; if (s != null) { @@ -47,15 +48,17 @@ public class Tib implements Token.Stream { char c = s.charAt(spos++); if (c=='\n') { _row++; _col = 0; } else _col++; - return new CharToken(c, _row, _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(_row, _col); + return CharToken.right; } Object o = cur.child(pos++); if (o instanceof String) { @@ -75,7 +78,7 @@ public class Tib implements Token.Stream { } cur = (Block)o; pos = 0; - return CharToken.left(_row, _col); + return CharToken.left; } public static Block parse(BufferedReader br) throws Invalid, IOException { @@ -84,7 +87,8 @@ public class Tib implements Token.Stream { boolean blankLine = false; Block top = new Block.Root(); for(String s = br.readLine(); s != null; s = br.readLine()) { - col = 0; + row++; + col=0; while (s.length() > 0 && s.charAt(0) == ' ' && (!(top instanceof Block.Literal) || col < top.col)) { col++; s = s.substring(1); } @@ -93,6 +97,8 @@ public class Tib implements Token.Stream { 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("{}")) { @@ -106,16 +112,16 @@ public class Tib implements Token.Stream { } 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 = top.closeIndent(); 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 { Block parent; public final int row; public final int col; + public int endrow; + public int endcol; public final int iip; private final Vector children = new Vector(); private String pending = ""; @@ -261,7 +269,8 @@ public class Tib implements Token.Stream { // Testing ////////////////////////////////////////////////////////////////////////////// - public static void main(String[] s) throws Exception { System.out.println(parse(new BufferedReader(new InputStreamReader(System.in))).toString(-1)); } + public static void main(String[] s) throws Exception { + System.out.println(parse(new BufferedReader(new InputStreamReader(System.in))).toString(-1)); } // Utilities ////////////////////////////////////////////////////////////////////////////// @@ -289,21 +298,30 @@ public class Tib implements Token.Stream { 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 tree) { String head = tree.head(); if (tree.numChildren()==0) return super.walk(tree); - if ("{".equals(head)) - return nonTerminal("braced"+(anon++), + 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, - ((PreSequence)walk(tree, 0)).buildUnion(), + ws, + u2, + ws, CharToken.rightBrace }) } }, false, false); + } return super.walk(tree); } }