checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / CharToStringParser.java
index 802d689..c66ff5d 100644 (file)
@@ -8,14 +8,36 @@ import edu.berkeley.sbp.Token.Location;
 import edu.berkeley.sbp.util.*;
 
 public class CharToStringParser extends Parser<CharToken,String> {
-    public CharToStringParser(Union u) { super(u, new IntegerTopology<CharToken>()); }
     public Forest<String> parse(InputStream is) throws IOException, ParseFailed {
-        return super.parse(new CharToken.Stream(is));
+        return super.parse(new Stream(is));
     }
     public Forest<String> parse(Reader r) throws IOException, ParseFailed {
-        return super.parse(new CharToken.Stream(r));
+        return super.parse(new Stream(r));
     }
+
+    public CharToStringParser(Union u) { super(u, new IntegerTopology<CharToken>(null)); }
     public Forest<String> shiftToken(CharToken ct, Location loc) {
         return Forest.create(loc, ct.result(), null, false, false);
     }
+
+    private static class Stream extends CartesianInput<CharToken> {
+        private final Reader r;
+        
+        public Stream(String s)                { this(new StringReader(s)); }
+        public Stream(Reader r)                { this(r, null); }
+        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); }
+
+        boolean cr = false;
+        public boolean   isCR() { return cr; }
+        public CharToken next() throws IOException {
+            cr = false;
+            int i = r.read();
+            if (i==-1) return null;
+            char c = (char)i;
+            cr = c=='\n';
+            return new CharToken(c);
+        }
+    }
 }