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