X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FCharToken.java;h=0793d803355db27e095f021870a12b7c2a1a257c;hb=8a9fc9f2357e54052374cf2ef003630a486a6c0a;hp=e73ccddc26cc9bf17c3ec28bb99cae6565a97048;hpb=695b95cf0b6140177098a5a2d7117aec4017e470;p=sbp.git
diff --git a/src/edu/berkeley/sbp/misc/CharToken.java b/src/edu/berkeley/sbp/misc/CharToken.java
index e73ccdd..0793d80 100644
--- a/src/edu/berkeley/sbp/misc/CharToken.java
+++ b/src/edu/berkeley/sbp/misc/CharToken.java
@@ -9,13 +9,23 @@ import edu.berkeley.sbp.util.*;
import edu.berkeley.sbp.*;
/** an implementation of Token for streams of Java char values */
-public class CharToken implements Token, IntegerTopology.IntegerMappable {
+public class CharToken implements Token, IntegerMappable {
// Public //////////////////////////////////////////////////////////////////////////////
+ public static class CharToStringParser extends Parser {
+ public CharToStringParser(Union u) { super(u); }
+ public Topology top() { return new IntegerTopology(); }
+ public Forest shiftedToken(CharToken ct, Token.Location loc) {
+ return Forest.create(loc, ct.result(), null, false, false);
+ }
+ }
+
public static class CharRange extends Atom {
private String esc(char c) { return StringUtil.escapify(c+"", "[]-~\\\"\'"); }
- public CharRange(Topology t) { super(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('[');
@@ -50,10 +60,10 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
return new CharRange(new IntegerTopology(new Range.Set(new Range((int)start, (int)end)).complement().intersect(all)));
}
- public static final CharToken left = new CharToken((char)-3, 0, 0);
- public static final CharToken right = new CharToken((char)-4, 0, 0);
- public static final Atom leftBrace = new Atom(new IntegerTopology(-3)) { public String toString() { return "{"; } };
- public static final Atom rightBrace = new Atom(new IntegerTopology(-4)) { public String toString() { return "}"; } };
+ public static final Atom leftBrace = new CharRange(new IntegerTopology(9998)) { public String toString() { return "{"; } };
+ public static final Atom rightBrace = new CharRange(new IntegerTopology(9999)) { public String toString() { return "}"; } };
+ public static final CharToken left = new CharToken((char)9998);
+ public static final CharToken right = new CharToken((char)9999);
private static final Range.Set all = new Range.Set(new Range(0, Character.MAX_VALUE));
public static final Atom any = new CharRange(new IntegerTopology(all));
@@ -63,7 +73,7 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
/** returns an element which exactly matches the string given */
public static Element string(String s) {
- if (s.length() == 0) return MetaGrammar.epsilon;
+ if (s.length() == 0) return Union.epsilon;
final String escapified = "\""+StringUtil.escapify(s, "\"\r\n\\")+"\"";
Element ret;
if (s.length() == 1) {
@@ -90,12 +100,9 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
// Private //////////////////////////////////////////////////////////////////////////////
public final char c;
- public final Location location;
- public CharToken(char c, int line, int col) { this(c, new CartesianLocation(line, col)); }
- private CharToken(char c, Location loc) { this.c = c; this.location = loc; }
- public String result() { return c+""; }
- public Location getLocation() { return location; }
- public String toString() { return "\'"+StringUtil.escapify(c+"")+"\'"; }
+ public CharToken(char c) { this.c = c; }
+ public String result() { return c+""; }
+ public String toString() { return "\'"+StringUtil.escapify(c+"")+"\'"; }
//////////////////////////////////////////////////////////////////////////////////////////
@@ -103,12 +110,11 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
// Statics //////////////////////////////////////////////////////////////////////////////
- static class CartesianLocation implements Location {
+ public static class CartesianLocation implements Location {
public final int line;
public final int col;
public String toString() { return line + ":" + col; }
public CartesianLocation(int line, int col) { this.line = line; this.col = col; }
- public String getContext() { return ""; }
}
/** an implementation of Token.Stream for sequences of characters */
@@ -146,18 +152,23 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
}
long then = 0;
- public Token next() throws IOException {
+ private Token.Location location = new LocWrap(1, 1);
+ public Token.Location getLocation() { return location; }
+ public Token next(int numstates, int resets, int waits) throws IOException {
int i = r.read();
if (i==-1) return null;
char c = (char)i;
- Token ret = new CharToken(c, new LocWrap(line, col));
+ location = new LocWrap(line, col);
+ Token ret = new CharToken(c);
String s = line + "";
while(s.length() < 4) s = " " + s;
s = "line "+s+", col " + col;
+ while(s.length() < 20) s += " ";
+ s += "[ambiguity level: " + (numstates-1) + "] [resets: " + resets + "] [waits: " + waits + "]";
long now = System.currentTimeMillis();
if (now-then > 10) {
then = now;
- System.out.print(" "+(message==null?"":message)+" " + s + " \r");
+ System.out.print(" "+(message==null?"":message)+" " + s + " \r");
}
if (c=='\n') {
currentLine = new Line();