X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTib.java;h=8ee70ca237ec5b9148f2ae3e29f82e0f2ba9bbc4;hp=915158bc8139a99a5a831cb1b8400be9fbc46143;hb=fd97655ce34c3aff1a47bfb2f45775ac711923b1;hpb=0907ad8065e0d123e46fd00d87189a2fa9f10bf4 diff --git a/src/edu/berkeley/sbp/tib/Tib.java b/src/edu/berkeley/sbp/tib/Tib.java index 915158b..8ee70ca 100644 --- a/src/edu/berkeley/sbp/tib/Tib.java +++ b/src/edu/berkeley/sbp/tib/Tib.java @@ -21,9 +21,7 @@ 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 { @@ -42,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) { @@ -49,7 +48,7 @@ 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; } @@ -57,7 +56,7 @@ public class Tib implements Token.Stream { pos = cur.iip+1; 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) { @@ -77,7 +76,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 { @@ -129,7 +128,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; @@ -137,7 +136,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; } @@ -288,5 +286,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); + } + } + }