hoisted getLocation() out of Token and into Token.Stream
[sbp.git] / src / edu / berkeley / sbp / misc / CharToken.java
index 02c67e9..8804fee 100644 (file)
@@ -13,9 +13,19 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
 
     // Public //////////////////////////////////////////////////////////////////////////////
 
+    public static class CharToStringParser extends Parser<CharToken,String> {
+        public CharToStringParser(Union u) { super(u); }
+        public Topology<CharToken> top() { return new IntegerTopology<CharToken>(); }
+        public Forest<String> shiftedToken(CharToken ct, Token.Location loc) {
+            return Forest.create(loc, ct.result(), null, null, false, false);
+        }
+    }
+
     public static class CharRange extends Atom<CharToken> {
         private String esc(char c) { return StringUtil.escapify(c+"", "[]-~\\\"\'"); }
-        public CharRange(Topology<CharToken> t) { super(t); }
+        private Topology<CharToken> t;
+        public CharRange(Topology<CharToken> t) { this.t = t; }
+        public Topology<CharToken> 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<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 Atom(new IntegerTopology<CharToken>(9998)) { public String toString() { return "{"; } };
-    public static final Atom rightBrace = new Atom(new IntegerTopology<CharToken>(9999)) { 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));
@@ -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,11 +152,14 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
         }
 
         long then = 0;
+        private Token.Location location = new LocWrap(1, 1);
+        public Token.Location getLocation() { return location; }
         public Token next() 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;