X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTib.java;h=39f6071d01a576fa6539f62240125d40b822f052;hp=7b3270f933dab95fd91659035765b1cb802aa900;hb=0516ea34996c86664928ef948013b749876b87ec;hpb=695b95cf0b6140177098a5a2d7117aec4017e470 diff --git a/src/edu/berkeley/sbp/tib/Tib.java b/src/edu/berkeley/sbp/tib/Tib.java index 7b3270f..39f6071 100644 --- a/src/edu/berkeley/sbp/tib/Tib.java +++ b/src/edu/berkeley/sbp/tib/Tib.java @@ -21,33 +21,41 @@ import java.io.*; * * This was written as an ad-hoc parser to facilitate * experimentation with the TIB spec. Once the spec is finalized it - * should probably be rewritten using a parser-generator, if - * possible (it is unclear whether or not the associated grammar is - * context-free). + * should probably be rewritten. */ public class Tib implements Token.Stream { public Tib(String s) throws IOException, Invalid { this(new StringReader(s)); } public Tib(Reader r) throws IOException, Invalid { this(new BufferedReader(r)); } public Tib(InputStream is) throws IOException, Invalid { this(new BufferedReader(new InputStreamReader(is))); } - public Tib(BufferedReader br) throws IOException, Invalid { cur = parse(br); } + public Tib(BufferedReader br) throws IOException, Invalid { + cur = parse(br); + System.out.println("\rparsing: \"" + cur.toString(0, -1) + "\""); + } private Block cur; private String s = null; int pos = 0; int spos = 0; + int _row = 0; + int _col = 0; public CharToken next() 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, _row, _col); + } + s = null; + } if (pos >= cur.size()) { pos = cur.iip+1; cur = cur.parent; - return CharToken.right; - } - - if (s != null) { - if (spos < s.length()) - return new CharToken(s.charAt(spos++), 0, 0); - s = null; + if (cur==null) return null; + return CharToken.right(_row, _col); } Object o = cur.child(pos++); if (o instanceof String) { @@ -55,6 +63,11 @@ public class Tib implements Token.Stream { s = (String)o; return next(); } + 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(); @@ -62,7 +75,7 @@ public class Tib implements Token.Stream { } cur = (Block)o; pos = 0; - return CharToken.left; + return CharToken.left(_row, _col); } public static Block parse(BufferedReader br) throws Invalid, IOException { @@ -114,7 +127,7 @@ public class Tib implements Token.Stream { } } - public static class Block /*implements Token*/ { + public static class Block { Block parent; public final int row; public final int col; @@ -122,7 +135,6 @@ public class Tib implements Token.Stream { 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; } @@ -273,5 +285,37 @@ public class Tib implements Token.Stream { 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 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); + } + } + }