X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTib.java;h=eabc9eea863f799604972b4a0cb3d1f135979988;hp=167ad8b60deacb81085cd4662b8f456b230d8e36;hb=6bee7f519f499a92a92a459fd2c903d85de0759c;hpb=2724e3d1df836f6ce40bc4227f1509e777de2e48 diff --git a/src/edu/berkeley/sbp/tib/Tib.java b/src/edu/berkeley/sbp/tib/Tib.java index 167ad8b..eabc9ee 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,25 +21,64 @@ 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 final String str; - - 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) { str = parse(br).toString(0,-1); } - - public Token next() throws IOException { - if (pos >= str.length()) return Atom.EOF; - return Atom.fromChar(str.charAt(pos++)); +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) + "\""); } - public static Block parse(BufferedReader br) throws Invalid { + 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; + if (cur==null) return null; + return CharToken.right(_row, _col); + } + Object o = cur.child(pos++); + 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 CharToken.left(_row, _col); + } + + public static Block parse(BufferedReader br) throws Invalid, IOException { int row=0, col=0; try { boolean blankLine = false; @@ -88,26 +127,30 @@ 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; - private final Vec children = new Vec(); + public final int iip; + 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; } - protected Block(int row, int col) { this.row=row; this.col=col; parent = null; } - public Block(Block parent, int row, int col) { this.row=row; this.col=col; (this.parent = parent).add(this); } + protected Block(int row, int col) { + this.row=row; + this.col=col; + this.iip = -1; + parent = null; + } + public Block(Block parent, int row, int col) { + this.row=row; + this.col=col; + this.iip = parent.size(); + (this.parent = parent).add(this); + } public void add(String s) { children.addElement(s); } public void add(char c) { @@ -188,6 +231,7 @@ public class Tib /*implements Token.Stream>*/ { public Block close() { return parent; } public Block closeIndent() { return close(); } public void add(String s) { if (content.length()>0) content.append('\n'); content.append(s); } + public String text() { return content.toString(); } protected String toString(int indent, int justificationLimit) { StringBuffer ret = new StringBuffer(); String s = content.toString(); @@ -209,7 +253,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); } @@ -217,7 +261,7 @@ 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 ////////////////////////////////////////////////////////////////////////////// @@ -240,6 +284,35 @@ 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.maximal(Repeat.many0(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++), + new PreSequence[][] { + new PreSequence[] { + new PreSequence(new Element[] { CharToken.leftBrace, + ws, + ((PreSequence)walk(tree, 0)).sparse(ws).buildUnion(), + ws, + CharToken.rightBrace + }) + } + }, + false, + false); + return super.walk(tree); + } + } + }