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 final Atom leftBrace  = new Atom(new IntegerTopology<CharToken>(-3)) { public String toString() { return "{"; } };
54     public static final Atom rightBrace = new Atom(new IntegerTopology<CharToken>(-4)) { public String toString() { return "}"; } };
55
56     private static final Range.Set all = new Range.Set(new Range(0, Character.MAX_VALUE));
57     public  static final Atom      any = new CharRange(new IntegerTopology<CharToken>(all));
58     public  static final Atom     none = new CharRange(new IntegerTopology<CharToken>());
59     public static IntegerTopology<CharToken> range(Range r) { return new IntegerTopology<CharToken>(r); }
60     public static Atom set(Range.Set r) { return new CharRange(new IntegerTopology<CharToken>(r)); }
61
62     /** returns an element which exactly matches the string given */
63     public static Element string(String s) {
64         if (s.length() == 0) return MetaGrammar.epsilon;
65         final String escapified = "\""+StringUtil.escapify(s, "\"\r\n\\")+"\"";
66         Element ret;
67         if (s.length() == 1) {
68             ret =
69                 new CharRange(new IntegerTopology<CharToken>((int)s.charAt(0))) {
70                     public String toString() { return escapified; } };
71         } else {
72             Union ret2 = new Union("\""+s+"\"_str", true) {
73                     public String toString() { return escapified; } };
74             Element[] refs = new Element[s.length()];
75             for(int i=0; i<refs.length; i++) refs[i] = new CharRange(new IntegerTopology<CharToken>((int)s.charAt(i)));
76             ret2.add(Sequence.constant(refs, s, null, null));
77             ret = ret2;
78         }
79         return ret;
80     }
81
82     /** FIXME */
83     public static Topology<CharToken> top() { return new IntegerTopology<CharToken>(); }
84     public static Topology<CharToken> top(String s) throws java.text.ParseException {
85         return new IntegerTopology<CharToken>(Range.Set.parse(s));
86     }
87
88     // Private //////////////////////////////////////////////////////////////////////////////
89
90     public final char c;
91     public final Location location;
92     private CharToken(char c, int line, int col)   { this(c, new CartesianLocation(line, col)); }
93     private CharToken(char c, Location loc)        { this.c = c; this.location = loc; }
94     public String result()                         { return c+""; }
95     public Location getLocation()                  { return location; }
96     public String  toString()                      { return "\'"+StringUtil.escapify(c+"")+"\'"; }
97
98     //////////////////////////////////////////////////////////////////////////////////////////
99
100     public int toInt() { return (int)c; }
101
102     // Statics //////////////////////////////////////////////////////////////////////////////
103
104     static class CartesianLocation implements Location {
105         public final int line;
106         public final int col;
107         public String toString()            { return line + ":" + col; }
108         public CartesianLocation(int line, int col) { this.line = line; this.col = col; }
109         public String getContext() { return ""; }
110     }
111     
112     /** an implementation of Token.Stream for sequences of characters */
113     public static class Stream implements Token.Stream {
114         private final String message;
115         private final Reader r;
116         private int line  = 1;
117         private int col   = 1;
118
119         public Stream(String s)                { this(new StringReader(s)); }
120
121         public Stream(Reader r)                { this(r, null); }
122         public Stream(Reader r,      String s) { this.r = r; this.message = s; }
123
124         public Stream(InputStream i)           { this(i, null); }
125         public Stream(InputStream i, String s) { this(new InputStreamReader(i), s); }
126
127         private Line currentLine = new Line();
128         private class Line {
129             public StringBuffer line = new StringBuffer();
130         }
131
132         private class LocWrap implements Location {
133             Line myline = Stream.this.currentLine;
134             public final int line;
135             public final int col;
136             public String toString()            { return line + ":" + col; }
137             public LocWrap(int line, int col) { this.line = line; this.col = col; }
138             public String getContext() {
139                 StringBuffer spaces = new StringBuffer();
140                 for(int i=0; i<col-1; i++) spaces.append(' ');
141                 spaces.append('^');
142                 return "  " + myline.line.toString() + "\n  " + spaces.toString();
143             }
144         }
145
146         long then = 0;
147         public Token next() throws IOException {
148             int i = r.read();
149             if (i==-1) return null;
150             char c = (char)i;
151             Token ret = new CharToken(c, new LocWrap(line, col));
152             String s = line + "";
153             while(s.length() < 4) s = " " + s;
154             s = "line "+s+", col " + col;
155             long now = System.currentTimeMillis();
156             if (now-then > 10) {
157                 then = now;
158                 System.out.print("  "+(message==null?"":message)+" " + s + "                                                                      \r");
159             }
160             if (c=='\n') { 
161                 currentLine = new Line();
162                 line++;
163                 col = 1;
164             } else {
165                 currentLine.line.append(c);
166                 col++;
167             }
168             return ret;
169         }
170     }
171
172 }