checkpoint
[sbp.git] / src / edu / berkeley / sbp / chr / CharParser.java
1 package edu.berkeley.sbp.chr;
2 import java.io.*;
3 import java.util.*;
4 import java.lang.reflect.*;
5 import java.lang.ref.*;
6 import edu.berkeley.sbp.*;
7 import edu.berkeley.sbp.util.*;
8 import edu.berkeley.sbp.misc.*;
9 import edu.berkeley.sbp.Input.Location;
10
11 public class CharParser extends Parser<Character,String> {
12     public Forest<String> parse(InputStream is) throws IOException, ParseFailed {
13         return super.parse(new Stream(is));
14     }
15     public Forest<String> parse(Reader r) throws IOException, ParseFailed {
16         return super.parse(new Stream(r));
17     }
18
19     public CharParser(Union u) {
20         super(u, new CharTopology());
21         pt.optimize(new CharTopology());
22     }
23     public Forest<String> shiftToken(Character ct, Location loc) {
24         return Forest.create(loc, ct.toString(), null, false, false);
25     }
26
27     private static class Stream extends CartesianInput<Character> {
28         private final Reader r;
29         
30         public Stream(String s)                { this(new StringReader(s)); }
31         public Stream(Reader r)                { this(r, null); }
32         public Stream(Reader r,      String s) { this.r = r; }
33         public Stream(InputStream i)           { this(i, null); }
34         public Stream(InputStream i, String s) { this(new InputStreamReader(i), s); }
35
36         boolean cr = false;
37         public boolean   isCR() { return cr; }
38         public Character next() throws IOException {
39             cr = false;
40             int i = r.read();
41             if (i==-1) return null;
42             char c = (char)i;
43             cr = c=='\n';
44             return c;
45         }
46     }
47 }