checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / CharToken.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.*;
8 import edu.berkeley.sbp.util.*;
9 import edu.berkeley.sbp.*;
10
11 /** an implementation of Token for streams of Java <tt>char</tt> values */
12 public class CharToken implements Token, IntegerTopology.IntegerMappable {
13
14     // Public //////////////////////////////////////////////////////////////////////////////
15
16     public static class CharRange extends Atom<CharToken> {
17         private String esc(char c) { return StringUtil.escapify(c+"", "[]-~\\\"\'"); }
18         public CharRange(Topology<CharToken> t) { super(t); }
19         public String toString() {
20             StringBuffer sb = new StringBuffer();
21             sb.append('[');
22             Range.Set ranges = ((IntegerTopology)top()).getRanges();
23             if (ranges.size() == -1 || ranges.size() > Character.MAX_VALUE/2) {
24                 sb.append('~');
25                 ranges = ranges.complement();
26             }
27             ranges = ranges.intersect(all);
28             for(Range r : ranges) {
29                 if (r.isMinNegInf() || r.isMaxPosInf()) throw new Error("should not happen");
30                 if (r.getMin()==r.getMax()) {
31                     sb.append(esc((char)r.getMin()));
32                 } else{
33                     sb.append(esc((char)r.getMin()));
34                     sb.append('-');
35                     sb.append(esc((char)r.getMax()));
36                 }
37             }
38             sb.append(']');
39             return sb.toString();
40         }
41     }
42
43     /** returns an element matching all characters between <tt>start</tt> and <tt>end</tt>, inclusive */
44     public static Atom positiveRange(char start, char end) {
45         return new CharRange(new IntegerTopology<CharToken>(new Range.Set(new Range((int)start, (int)end))));
46     }
47
48     /** returns an element matching all characters <b>not</b> between <tt>start</tt> and <tt>end</tt>, inclusive */
49     public static Atom negativeRange(char start, char end) {
50         return new CharRange(new IntegerTopology<CharToken>(new Range.Set(new Range((int)start, (int)end)).complement().intersect(all)));
51     }
52
53     public static CharToken left(int row, int col) { return new CharToken((char)9998, 0, 0) { public String toString() { return "{"; } }; }
54     public static CharToken right(int row, int col) { return new CharToken((char)9999, 0, 0) { public String toString() { return "}"; } }; }
55     public static final Atom leftBrace  = new Atom(new IntegerTopology<CharToken>(9998)) { public String toString() { return "{"; } };
56     public static final Atom rightBrace = new Atom(new IntegerTopology<CharToken>(9999)) { public String toString() { return "}"; } };
57
58     private static final Range.Set all = new Range.Set(new Range(0, Character.MAX_VALUE));
59     public  static final Atom      any = new CharRange(new IntegerTopology<CharToken>(all));
60     public  static final Atom     none = new CharRange(new IntegerTopology<CharToken>());
61     public static IntegerTopology<CharToken> range(Range r) { return new IntegerTopology<CharToken>(r); }
62     public static Atom set(Range.Set r) { return new CharRange(new IntegerTopology<CharToken>(r)); }
63
64     /** returns an element which exactly matches the string given */
65     public static Element string(String s) {
66         if (s.length() == 0) return Union.epsilon;
67         final String escapified = "\""+StringUtil.escapify(s, "\"\r\n\\")+"\"";
68         Element ret;
69         if (s.length() == 1) {
70             ret =
71                 new CharRange(new IntegerTopology<CharToken>((int)s.charAt(0))) {
72                     public String toString() { return escapified; } };
73         } else {
74             Union ret2 = new Union("\""+s+"\"_str", true) {
75                     public String toString() { return escapified; } };
76             Element[] refs = new Element[s.length()];
77             for(int i=0; i<refs.length; i++) refs[i] = new CharRange(new IntegerTopology<CharToken>((int)s.charAt(i)));
78             ret2.add(Sequence.constant(refs, s, null, null));
79             ret = ret2;
80         }
81         return ret;
82     }
83
84     /** FIXME */
85     public static Topology<CharToken> top() { return new IntegerTopology<CharToken>(); }
86     public static Topology<CharToken> top(String s) throws java.text.ParseException {
87         return new IntegerTopology<CharToken>(Range.Set.parse(s));
88     }
89
90     // Private //////////////////////////////////////////////////////////////////////////////
91
92     public final char c;
93     public final Location location;
94     public CharToken(char c, int line, int col)   { this(c, new CartesianLocation(line, col)); }
95     private CharToken(char c, Location loc)        { this.c = c; this.location = loc; }
96     public String result()                         { return c+""; }
97     public Location getLocation()                  { return location; }
98     public String  toString()                      { return "\'"+StringUtil.escapify(c+"")+"\'"; }
99
100     //////////////////////////////////////////////////////////////////////////////////////////
101
102     public int toInt() { return (int)c; }
103
104     // Statics //////////////////////////////////////////////////////////////////////////////
105
106     static class CartesianLocation implements Location {
107         public final int line;
108         public final int col;
109         public String toString()            { return line + ":" + col; }
110         public CartesianLocation(int line, int col) { this.line = line; this.col = col; }
111         public String getContext() { return ""; }
112     }
113     
114     /** an implementation of Token.Stream for sequences of characters */
115     public static class Stream implements Token.Stream {
116         private final String message;
117         private final Reader r;
118         private int line  = 1;
119         private int col   = 1;
120
121         public Stream(String s)                { this(new StringReader(s)); }
122
123         public Stream(Reader r)                { this(r, null); }
124         public Stream(Reader r,      String s) { this.r = r; this.message = s; }
125
126         public Stream(InputStream i)           { this(i, null); }
127         public Stream(InputStream i, String s) { this(new InputStreamReader(i), s); }
128
129         private Line currentLine = new Line();
130         private class Line {
131             public StringBuffer line = new StringBuffer();
132         }
133
134         private class LocWrap implements Location {
135             Line myline = Stream.this.currentLine;
136             public final int line;
137             public final int col;
138             public String toString()            { return line + ":" + col; }
139             public LocWrap(int line, int col) { this.line = line; this.col = col; }
140             public String getContext() {
141                 StringBuffer spaces = new StringBuffer();
142                 for(int i=0; i<col-1; i++) spaces.append(' ');
143                 spaces.append('^');
144                 return "  " + myline.line.toString() + "\n  " + spaces.toString();
145             }
146         }
147
148         long then = 0;
149         public Token next() throws IOException {
150             int i = r.read();
151             if (i==-1) return null;
152             char c = (char)i;
153             Token ret = new CharToken(c, new LocWrap(line, col));
154             String s = line + "";
155             while(s.length() < 4) s = " " + s;
156             s = "line "+s+", col " + col;
157             long now = System.currentTimeMillis();
158             if (now-then > 10) {
159                 then = now;
160                 System.out.print("  "+(message==null?"":message)+" " + s + "                                                                      \r");
161             }
162             if (c=='\n') { 
163                 currentLine = new Line();
164                 line++;
165                 col = 1;
166             } else {
167                 currentLine.line.append(c);
168                 col++;
169             }
170             return ret;
171         }
172     }
173
174 }