X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FCharToken.java;h=5d897dec2a93e678ae057ad45d891a0bfe27b544;hb=1177435c53540af2247a2fbd7b6fbcf059accbb8;hp=eccac6a2949ce1ee58b84081eb9da62d0846df94;hpb=2724e3d1df836f6ce40bc4227f1509e777de2e48;p=sbp.git
diff --git a/src/edu/berkeley/sbp/misc/CharToken.java b/src/edu/berkeley/sbp/misc/CharToken.java
index eccac6a..5d897de 100644
--- a/src/edu/berkeley/sbp/misc/CharToken.java
+++ b/src/edu/berkeley/sbp/misc/CharToken.java
@@ -4,18 +4,24 @@ import java.util.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import edu.berkeley.sbp.*;
-import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.Token.Location;
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 IntegerMappable {
- // Public //////////////////////////////////////////////////////////////////////////////
+ public static class CharToStringParser extends Parser {
+ public CharToStringParser(Union u) { super(u, new IntegerTopology()); }
+ public Forest shiftToken(CharToken ct, 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,8 +56,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 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));
@@ -61,7 +69,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) {
@@ -88,85 +96,32 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
// Private //////////////////////////////////////////////////////////////////////////////
public final char c;
- public final Location location;
- private 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+"")+"\'"; }
//////////////////////////////////////////////////////////////////////////////////////////
public int toInt() { return (int)c; }
- // Statics //////////////////////////////////////////////////////////////////////////////
-
- 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 */
- public static class Stream implements Token.Stream {
- private final String message;
+ public static class Stream extends CartesianInput {
private final Reader r;
- private int line = 1;
- private int col = 1;
-
+
public Stream(String s) { this(new StringReader(s)); }
-
public Stream(Reader r) { this(r, null); }
- public Stream(Reader r, String s) { this.r = r; this.message = s; }
-
+ public Stream(Reader r, String s) { this.r = r; }
public Stream(InputStream i) { this(i, null); }
public Stream(InputStream i, String s) { this(new InputStreamReader(i), s); }
- private Line currentLine = new Line();
- private class Line {
- public StringBuffer line = new StringBuffer();
- }
-
- private class LocWrap implements Location {
- Line myline = Stream.this.currentLine;
- public final int line;
- public final int col;
- public String toString() { return line + ":" + col; }
- public LocWrap(int line, int col) { this.line = line; this.col = col; }
- public String getContext() {
- StringBuffer spaces = new StringBuffer();
- for(int i=0; i 10) {
- then = now;
- System.out.print(" "+(message==null?"":message)+" " + s + " \r");
- }
- if (c=='\n') {
- currentLine = new Line();
- line++;
- col = 1;
- } else {
- currentLine.line.append(c);
- col++;
- }
- return ret;
+ cr = c=='\n';
+ return new CharToken(c);
}
}
-
}