checkpoint
[sbp.git] / src / edu / berkeley / sbp / chr / CharParser.java
diff --git a/src/edu/berkeley/sbp/chr/CharParser.java b/src/edu/berkeley/sbp/chr/CharParser.java
new file mode 100644 (file)
index 0000000..db235cd
--- /dev/null
@@ -0,0 +1,47 @@
+package edu.berkeley.sbp.chr;
+import java.io.*;
+import java.util.*;
+import java.lang.reflect.*;
+import java.lang.ref.*;
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.misc.*;
+import edu.berkeley.sbp.Input.Location;
+
+public class CharParser extends Parser<Character,String> {
+    public Forest<String> parse(InputStream is) throws IOException, ParseFailed {
+        return super.parse(new Stream(is));
+    }
+    public Forest<String> parse(Reader r) throws IOException, ParseFailed {
+        return super.parse(new Stream(r));
+    }
+
+    public CharParser(Union u) {
+        super(u, new CharTopology());
+        pt.optimize(new CharTopology());
+    }
+    public Forest<String> shiftToken(Character ct, Location loc) {
+        return Forest.create(loc, ct.toString(), null, false, false);
+    }
+
+    private static class Stream extends CartesianInput<Character> {
+        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 Character next() throws IOException {
+            cr = false;
+            int i = r.read();
+            if (i==-1) return null;
+            char c = (char)i;
+            cr = c=='\n';
+            return c;
+        }
+    }
+}