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<CharToken,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) { super(u, new IntegerTopology<CharToken>(null)); }
19     public Forest<String> shiftToken(CharToken ct, Location loc) {
20         return Forest.create(loc, ct.result(), null, false, false);
21     }
22
23     private static class Stream extends CartesianInput<CharToken> {
24         private final Reader r;
25         
26         public Stream(String s)                { this(new StringReader(s)); }
27         public Stream(Reader r)                { this(r, null); }
28         public Stream(Reader r,      String s) { this.r = r; }
29         public Stream(InputStream i)           { this(i, null); }
30         public Stream(InputStream i, String s) { this(new InputStreamReader(i), s); }
31
32         boolean cr = false;
33         public boolean   isCR() { return cr; }
34         public CharToken next() throws IOException {
35             cr = false;
36             int i = r.read();
37             if (i==-1) return null;
38             char c = (char)i;
39             cr = c=='\n';
40             return new CharToken(c);
41         }
42     }
43 }