From ad3d28238e215a2dee9224c146a1928671c2ba89 Mon Sep 17 00:00:00 2001 From: adam Date: Sun, 15 Jan 2006 04:35:50 -0500 Subject: [PATCH] checkpoint darcs-hash:20060115093550-5007d-60f36d225a3c831114fc431aaab7f73fb4cbd75c.gz --- src/edu/berkeley/sbp/misc/CharRange.java | 14 +- src/edu/berkeley/sbp/misc/CharToStringParser.java | 14 +- src/edu/berkeley/sbp/misc/CharToken.java | 23 +- src/edu/berkeley/sbp/misc/RegressionTests.java | 2 +- src/edu/berkeley/sbp/tib/Tib.java | 282 +-------------------- src/edu/berkeley/sbp/tib/TibDoc.java | 2 +- 6 files changed, 36 insertions(+), 301 deletions(-) diff --git a/src/edu/berkeley/sbp/misc/CharRange.java b/src/edu/berkeley/sbp/misc/CharRange.java index 5a1c24c..18f89a0 100644 --- a/src/edu/berkeley/sbp/misc/CharRange.java +++ b/src/edu/berkeley/sbp/misc/CharRange.java @@ -7,11 +7,11 @@ import edu.berkeley.sbp.*; import edu.berkeley.sbp.Token.Location; import edu.berkeley.sbp.util.*; -public class CharRange extends Atom { +public class CharRange extends Atom { private String esc(char c) { return StringUtil.escapify(c+"", "[]-~\\\"\'"); } - private Topology t; - public CharRange(Topology t) { this.t = t; } - public Topology top() { return t; } + private Topology t; + public CharRange(Topology t) { this.t = t; } + public Topology top() { return t; } public String toString() { StringBuffer sb = new StringBuffer(); sb.append('['); @@ -36,7 +36,7 @@ public class CharRange extends Atom { } public static final Atom leftBrace = CharToken.leftBrace; public static final Atom rightBrace = CharToken.rightBrace; - public static Atom set(Range.Set r) { return new CharRange(new IntegerTopology(CharToken.c2i, r)); } + public static Atom set(Range.Set r) { return new CharRange(new IntegerTopology(CharToken.c2i, r)); } private static final Range.Set all = new Range.Set(new Range(0, Character.MAX_VALUE)); /** returns an element which exactly matches the string given */ @@ -46,13 +46,13 @@ public class CharRange extends Atom { Element ret; if (s.length() == 1) { ret = - new CharRange(new IntegerTopology(CharToken.c2i, (int)s.charAt(0))) { + new CharRange(new IntegerTopology(CharToken.c2i, (int)s.charAt(0))) { public String toString() { return escapified; } }; } else { Union ret2 = new Union("\""+s+"\"_str", true) { public String toString() { return escapified; } }; Element[] refs = new Element[s.length()]; - for(int i=0; i(CharToken.c2i, (int)s.charAt(i))); + for(int i=0; i(CharToken.c2i, (int)s.charAt(i))); ret2.add(Sequence.constant(refs, s, null, null)); ret = ret2; } diff --git a/src/edu/berkeley/sbp/misc/CharToStringParser.java b/src/edu/berkeley/sbp/misc/CharToStringParser.java index f4395b9..bba0234 100644 --- a/src/edu/berkeley/sbp/misc/CharToStringParser.java +++ b/src/edu/berkeley/sbp/misc/CharToStringParser.java @@ -7,7 +7,7 @@ import edu.berkeley.sbp.*; import edu.berkeley.sbp.Token.Location; import edu.berkeley.sbp.util.*; -public class CharToStringParser extends Parser { +public class CharToStringParser extends Parser { public Forest parse(InputStream is) throws IOException, ParseFailed { return super.parse(new Stream(is)); } @@ -16,14 +16,14 @@ public class CharToStringParser extends Parser { } public CharToStringParser(Union u) { - super(u, new IntegerTopology(CharToken.c2i)); + super(u, new IntegerTopology(CharToken.c2i)); pt.optimize(CharToken.c2i); } - public Forest shiftToken(CharToken ct, Location loc) { - return Forest.create(loc, ct.result(), null, false, false); + public Forest shiftToken(Character ct, Location loc) { + return Forest.create(loc, ct.toString(), null, false, false); } - private static class Stream extends CartesianInput { + private static class Stream extends CartesianInput { private final Reader r; public Stream(String s) { this(new StringReader(s)); } @@ -34,13 +34,13 @@ public class CharToStringParser extends Parser { boolean cr = false; public boolean isCR() { return cr; } - public CharToken next() throws IOException { + public Character next() throws IOException { cr = false; int i = r.read(); if (i==-1) return null; char c = (char)i; cr = c=='\n'; - return new CharToken(c); + return c; } } } diff --git a/src/edu/berkeley/sbp/misc/CharToken.java b/src/edu/berkeley/sbp/misc/CharToken.java index 580e851..b0ac8e3 100644 --- a/src/edu/berkeley/sbp/misc/CharToken.java +++ b/src/edu/berkeley/sbp/misc/CharToken.java @@ -10,24 +10,13 @@ import edu.berkeley.sbp.util.*; /** an implementation of Token for streams of Java char values */ public class CharToken { - public static final Functor c2i = new Functor() { - public Integer invoke(CharToken c) { return new Integer(c.c); } + public static final Functor c2i = new Functor() { + public Integer invoke(Character c) { return (int)c.charValue(); } }; - public static final Atom leftBrace = new CharRange(new IntegerTopology(c2i, 9998)) { public String toString() { return "{"; } }; - public static final Atom rightBrace = new CharRange(new IntegerTopology(c2i, 9999)) { public String toString() { return "}"; } }; - public static final CharToken left = new CharToken((char)9998); - public static final CharToken right = new CharToken((char)9999); + public static final Atom leftBrace = new CharRange(new IntegerTopology(c2i, 9998)) { public String toString() { return "{"; } }; + public static final Atom rightBrace = new CharRange(new IntegerTopology(c2i, 9999)) { public String toString() { return "}"; } }; + public static final Character left = new Character((char)9998); + public static final Character right = new Character((char)9999); - // Private ////////////////////////////////////////////////////////////////////////////// - - public final char c; - public CharToken(char c) { this.c = c; } - public String result() { return c+""; } - public String toString() { return "\'"+StringUtil.escapify(c+"")+"\'"; } - - ////////////////////////////////////////////////////////////////////////////////////////// - - public int toInt() { return (int)c; } - } diff --git a/src/edu/berkeley/sbp/misc/RegressionTests.java b/src/edu/berkeley/sbp/misc/RegressionTests.java index b972564..af92d0b 100644 --- a/src/edu/berkeley/sbp/misc/RegressionTests.java +++ b/src/edu/berkeley/sbp/misc/RegressionTests.java @@ -79,7 +79,7 @@ public class RegressionTests { ParseFailed pfe = null; try { res = tib - ? new CharToStringParser(grammar).parse(new Tib(input)) + ? /*new CharToStringParser(grammar).parse(new Tib(input))*/ null : new CharToStringParser(grammar).parse(new StringReader(input)); } catch (ParseFailed pf) { pfe = pf; diff --git a/src/edu/berkeley/sbp/tib/Tib.java b/src/edu/berkeley/sbp/tib/Tib.java index ae8c6e0..e4123fc 100644 --- a/src/edu/berkeley/sbp/tib/Tib.java +++ b/src/edu/berkeley/sbp/tib/Tib.java @@ -23,19 +23,18 @@ import java.io.*; * experimentation with the TIB spec. Once the spec is finalized it * should probably be rewritten. */ -public class Tib implements Token.Stream { +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 { + public Tib(String s) throws IOException { this(new StringReader(s)); } + public Tib(Reader r) throws IOException { this(new BufferedReader(r)); } + public Tib(InputStream is) throws IOException { this(new BufferedReader(new InputStreamReader(is))); } + public Tib(BufferedReader br) throws IOException { this.br = br; istack.add(-1); //cur = parse(br); //System.out.println("\rparsing: \"" + cur.toString(0, -1) + "\""); } - private Block cur; private String s = ""; int pos = 0; int spos = 0; @@ -50,20 +49,20 @@ public class Tib implements Token.Stream { boolean indenting = true; int indentation = 0; private ArrayList istack = new ArrayList(); - public CharToken next(int numstates, int resets, int waits) throws IOException { - CharToken ret = nextc(numstates, resets); + public Character next(int numstates, int resets, int waits) throws IOException { + Character ret = nextc(numstates, resets); if (ret==CharToken.left) System.out.print("\033[31m{\033[0m"); else if (ret==CharToken.right) System.out.print("\033[31m}\033[0m"); else if (ret==null) return null; - else System.out.print(ret.c); + else System.out.print(ret); return ret; } - CharToken waitingBrace = null; - public CharToken nextc(int numstates, int resets) throws IOException { + Character waitingBrace = null; + public Character nextc(int numstates, int resets) throws IOException { char c; if (waitingBrace != null) { - CharToken ret = waitingBrace; + Character ret = waitingBrace; waitingBrace = null; return ret; } @@ -85,7 +84,7 @@ public class Tib implements Token.Stream { else _col++; if (indenting) { if (c==' ') { indentation++; return done(c); } - if (c=='\n') { indentation = 0; if (blank) return nextc(numstates, resets); blank = true; waiting = true; waitingChar='\n'; return new CharToken('\n'); } + if (c=='\n') { indentation = 0; if (blank) return nextc(numstates, resets); blank = true; waiting = true; waitingChar='\n'; return '\n'; } int last = istack.size()==0 ? -1 : istack.get(istack.size()-1); if (indentation==last) { if (blank) { @@ -121,267 +120,14 @@ public class Tib implements Token.Stream { } } - public CharToken done(char c) { + public Character done(char c) { switch(c) { case '{': return CharToken.left; case '}': return CharToken.right; - default: return new CharToken(c); + default: return c; } } boolean blank = false; - /* - public CharToken next(int numstates) 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; - if (cur==null) return null; - return CharToken.right; - } - Object o = cur.child(pos++); - if (o instanceof String) { - spos = 0; - s = (String)o; - return next(numstates); - } - 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(numstates); - } - cur = (Block)o; - pos = 0; - return CharToken.left; - } - */ - 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()) { - row++; - col=0; - while (s.length() > 0 && - s.charAt(0) == ' ' && - (!(top instanceof Block.Literal) || col < top.col)) { col++; s = s.substring(1); } - if ((top instanceof Block.Literal) && col >= top.col) { top.add(s); continue; } - if (s.length()==0) { blankLine = true; continue; } - 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("{}")) { - int bc = col; - boolean append = top instanceof Block.Literal && ((Block.Literal)top).braceCol == bc; - s = s.substring(2); - col += 2; - while (s.length() > 0 && s.charAt(0) == ' ' && !(append && col >= top.col) ) { col++; s = s.substring(1); } - if (append) top.add(s); else (top = new Block.Literal(top, row, col, bc)).add(s); - continue; - } - 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.endrow=row; top.endcol=col; top = top.closeIndent(); top = new Block.Indent(top, row, col); } - blankLine = false; - for(int i=0; i 0) { finishWord(); add(" "); return; } - if (size()==0) return; - if (child(size()-1).equals(" ")) return; - add(" "); - return; - } - - public void add(Block b) { children.addElement(b); } - public Block promote() { parent.parent.replaceLast(this); return close(); } - public Object lastChild() { return children.lastElement(); } - public Block lastChildAsBlock() { return (Block)lastChild(); } - public void replaceLast(Block b) { children.setElementAt(b, children.size()-1); b.parent = this; } - - public void finishWord() { if (pending.length() > 0) { add(pending); pending = ""; } } - - public Block closeBrace() { throw new InternalException("attempt to closeBrace() a "+getClass().getName()); } - public Block closeIndent() { throw new InternalException("attempt to closeIndent() a "+getClass().getName()); } - public Block close() { - while(size() > 0 && child(size()-1).equals(" ")) children.setSize(children.size()-1); - if (size()==0) throw new InternalException("PARSER BUG: attempt to close an empty block (should never happen)"); - if (size() > 1 || !(lastChild() instanceof Block)) return parent; - return lastChildAsBlock().promote(); - } - public String toString() { return toString(80); } - public String toString(int justificationLimit) { return toString(0, 80); } - protected String toString(int indent, int justificationLimit) { - StringBuffer ret = new StringBuffer(); - StringBuffer line = new StringBuffer(); - for(int i=0; i0 && children.elementAt(i-1) instanceof Block && justificationLimit!=-1) ret.append("\n"); - if (o instanceof Block) { - ret.append(justify(line.toString(), indent, justificationLimit)); - line.setLength(0); - if (justificationLimit==-1) { - ret.append("{"); - ret.append(((Block)o).toString(indent+2, justificationLimit)); - ret.append("}"); - } else { - ret.append(((Block)o).toString(indent+2, justificationLimit)); - } - } else { - line.append(o.toString()); - } - } - ret.append(justify(line.toString(), indent, justificationLimit)); - return ret.toString(); - } - - private static class Root extends Block { - public Root() { super(0, Integer.MIN_VALUE); } - public Block close() { throw new InternalException("attempted to close top block"); } - public String toString(int justificationLimit) { return toString(-2, justificationLimit); } - } - - private static class Brace extends Block { - public Brace(Block parent, int row, int col) { super(parent, row, col); } - public Block closeBrace() { return super.close(); } - } - - private static class Indent extends Block { - public Indent(Block parent, int row, int col) { super(parent, row, col); } - public Block closeIndent() { return super.close(); } - } - - private static class Literal extends Block { - private StringBuffer content = new StringBuffer(); - public final int braceCol; - public Literal(Block parent, int row, int col, int braceCol) { super(parent,row,col); this.braceCol = braceCol; } - public boolean isLiteral() { return true; } - public int size() { return 1; } - public Object child(int i) { return i==0 ? content.toString() : null; } - 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(); - while(s.length() > 0) { - int nl = s.indexOf('\n'); - if (nl==-1) nl = s.length(); - ret.append(spaces(indent)); - ret.append("{} "); - ret.append(s.substring(0, nl)); - s = s.substring(Math.min(s.length(),nl+1)); - ret.append('\n'); - } - return ret.toString(); - } - } - } - - - // Exceptions ////////////////////////////////////////////////////////////////////////////// - - private static class InternalException extends RuntimeException { public InternalException(String s) { super(s); } } - public static class Invalid extends /*IOException*/RuntimeException { - public Invalid(InternalException ie, int row, int col) { - super(ie.getMessage() + " at " + row + ":" + col); - } - } - - // Testing ////////////////////////////////////////////////////////////////////////////// - - public static void main(String[] s) throws Exception { - System.out.println(parse(new BufferedReader(new InputStreamReader(System.in))).toString(-1)); } - - // Utilities ////////////////////////////////////////////////////////////////////////////// - - public static String spaces(int i) { if (i<=0) return ""; return " " + spaces(i-1); } - - private static String justify(String s, int indent, int justificationLimit) { - if (s.length() == 0) return ""; - if (justificationLimit==-1) return s; - StringBuffer ret = new StringBuffer(); - while(s.length() > 0) { - if (s.charAt(0) == ' ') { s = s.substring(1); continue; } - ret.append(spaces(indent)); - int i = s.indexOf(' '); - if (i==-1) i = s.length(); - while(s.indexOf(' ', i+1) != -1 && s.indexOf(' ', i+1) < justificationLimit-indent) i = s.indexOf(' ', i+1); - if (s.length() + indent < justificationLimit) i = s.length(); - ret.append(s.substring(0, i)); - s = s.substring(i); - ret.append('\n'); - } - return ret.toString(); - } // Grammar ////////////////////////////////////////////////////////////////////////////// diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index c1a803c..2883508 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -12,7 +12,7 @@ import java.util.*; import java.io.*; public class TibDoc { - + public static void main(String[] s) throws Exception { System.out.println("parsing " + s[0]); Tree res = new CharToStringParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1(); -- 1.7.10.4