X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTib.java;h=29181a012e9c7acdc86f6cf7d2c240ee4332b0c1;hp=9a546e9d149978da05c19ae6eadbc574b9b1864b;hb=9ded11559a1b6f817e99355b1c9e2c88042e91d4;hpb=c130bd41b47c14693a9c4980b39bcddb1b7198e7 diff --git a/src/edu/berkeley/sbp/tib/Tib.java b/src/edu/berkeley/sbp/tib/Tib.java index 9a546e9..29181a0 100644 --- a/src/edu/berkeley/sbp/tib/Tib.java +++ b/src/edu/berkeley/sbp/tib/Tib.java @@ -3,8 +3,8 @@ // You may not use this file except in compliance with the License. package edu.berkeley.sbp.tib; -//import org.ibex.util.*; -//import org.ibex.io.*; +import edu.berkeley.sbp.*; +import edu.berkeley.sbp.misc.*; import java.util.*; import java.io.*; @@ -21,47 +21,74 @@ 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) { this(new StringReader(s)); } - public Tib(Reader r) { this(new BufferedReader(r)); } - public Tib(InputStream is) { this(new BufferedReader(new InputStreamReader(is))); } - public Tib(BufferedReader br) { cur = parse(br).toString(0,-1); } +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); + System.out.println("\rparsing: \"" + cur.toString(0, -1) + "\""); + } - boolean left = false; - boolean right = false; private Block cur; + private String s = null; int pos = 0; + int spos = 0; - public Token next() throws IOException { - if (left) { left = false; return new CharToken(-3); } - if (right) { right = false; return new CharToken(-4); } - + 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) { + 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; - return new CharToken(-4); + if (cur==null) return null; + return CharToken.right; } - Object o = cur.child(pos++); - if (o instanceof String) return new StringToken((String)o); - if (o.isLiteral()) return ((Block.Literal)o).text(); - cur = (Block)b; + if (o instanceof String) { + spos = 0; + 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(); + return next(); + } + cur = (Block)o; pos = 0; - return new CharToken(-3); + return CharToken.left; } - public static Block parse(BufferedReader br) throws Invalid { + 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()) { - col = 0; + row++; + col=0; while (s.length() > 0 && s.charAt(0) == ' ' && (!(top instanceof Block.Literal) || col < top.col)) { col++; s = s.substring(1); } @@ -70,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("{}")) { @@ -83,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*/ { } } - public static class Block implements Token { + public static class Block { Block parent; public final int row; public final int col; + public int endrow; + public int endcol; public final int iip; - private final Vec children = new Vec(); + private final Vector children = new Vector(); private String pending = ""; - public Tree result() { - // FIXME - } - - public Location getLocation() { return new Location.Cartesian(row, col); } - public boolean isEOF() { return false; } - public int size() { return children.size(); } public Object child(int i) { return children.elementAt(i); } public boolean isLiteral() { return false; } @@ -237,7 +261,7 @@ public class Tib /*implements Token.Stream*/ { // Exceptions ////////////////////////////////////////////////////////////////////////////// private static class InternalException extends RuntimeException { public InternalException(String s) { super(s); } } - public static class Invalid extends IOException { + public static class Invalid extends /*IOException*/RuntimeException { public Invalid(InternalException ie, int row, int col) { super(ie.getMessage() + " at " + row + ":" + col); } @@ -245,7 +269,8 @@ public class Tib /*implements Token.Stream*/ { // Testing ////////////////////////////////////////////////////////////////////////////// - public static void main(String[] s) throws Exception { System.out.println(parse(new Stream(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 ////////////////////////////////////////////////////////////////////////////// @@ -268,6 +293,38 @@ 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); + } + } + }