X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FCharToken.java;h=9067d143495f692200ed92507c0f1464ebb899ea;hb=d62f06aa2411dab1830020f731a83abeef24abd3;hp=8804feedc307f3122feb0c4d61f58f46b9d1c40c;hpb=fd97655ce34c3aff1a47bfb2f45775ac711923b1;p=sbp.git
diff --git a/src/edu/berkeley/sbp/misc/CharToken.java b/src/edu/berkeley/sbp/misc/CharToken.java
index 8804fee..9067d14 100644
--- a/src/edu/berkeley/sbp/misc/CharToken.java
+++ b/src/edu/berkeley/sbp/misc/CharToken.java
@@ -4,51 +4,11 @@ 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 //////////////////////////////////////////////////////////////////////////////
-
- 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, null, false, false);
- }
- }
-
- public static 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; }
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append('[');
- Range.Set ranges = ((IntegerTopology)top()).getRanges();
- if (ranges.size() == -1 || ranges.size() > Character.MAX_VALUE/2) {
- sb.append('~');
- ranges = ranges.complement();
- }
- ranges = ranges.intersect(all);
- for(Range r : ranges) {
- if (r.isMinNegInf() || r.isMaxPosInf()) throw new Error("should not happen");
- if (r.getMin()==r.getMax()) {
- sb.append(esc((char)r.getMin()));
- } else{
- sb.append(esc((char)r.getMin()));
- sb.append('-');
- sb.append(esc((char)r.getMax()));
- }
- }
- sb.append(']');
- return sb.toString();
- }
- }
+public class CharToken implements IntegerMappable {
/** returns an element matching all characters between start and end, inclusive */
public static Atom positiveRange(char start, char end) {
@@ -64,8 +24,8 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
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));
+
+ static final Range.Set all = new Range.Set(new Range(0, Character.MAX_VALUE));
public static final Atom any = new CharRange(new IntegerTopology(all));
public static final Atom none = new CharRange(new IntegerTopology());
public static IntegerTopology range(Range r) { return new IntegerTopology(r); }
@@ -108,76 +68,24 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
public int toInt() { return (int)c; }
- // Statics //////////////////////////////////////////////////////////////////////////////
-
- 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; }
- }
-
- /** 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);
}
}
-
}