import edu.berkeley.sbp.*;
/** an implementation of Token for streams of Java <tt>char</tt> values */
-public class CharToken implements Token, IntegerTopology.IntegerMappable {
+public class CharToken implements Token, IntegerMappable {
// Public //////////////////////////////////////////////////////////////////////////////
+ public static class CharToStringParser extends Parser<CharToken,String> {
+ public CharToStringParser(Union u) { super(u, new IntegerTopology<CharToken>()); }
+ public Forest<String> shiftedToken(CharToken ct, Token.Location loc) {
+ return Forest.create(loc, ct.result(), null, false, false);
+ }
+ }
+
public static class CharRange extends Atom<CharToken> {
private String esc(char c) { return StringUtil.escapify(c+"", "[]-~\\\"\'"); }
private Topology<CharToken> t;
return new CharRange(new IntegerTopology<CharToken>(new Range.Set(new Range((int)start, (int)end)).complement().intersect(all)));
}
- public static CharToken left(int row, int col) { return new CharToken((char)9998, 0, 0) { public String toString() { return "{"; } }; }
- public static CharToken right(int row, int col) { return new CharToken((char)9999, 0, 0) { public String toString() { return "}"; } }; }
public static final Atom leftBrace = new CharRange(new IntegerTopology<CharToken>(9998)) { public String toString() { return "{"; } };
public static final Atom rightBrace = new CharRange(new IntegerTopology<CharToken>(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<CharToken>(all));
// 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+"")+"\'"; }
//////////////////////////////////////////////////////////////////////////////////////////
// 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 */
}
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();