1 package edu.berkeley.sbp.misc;
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.*;
11 /** an implementation of Token for streams of Java <tt>char</tt> values */
12 public class CharToken implements Token, IntegerTopology.IntegerMappable {
14 // Public //////////////////////////////////////////////////////////////////////////////
16 public static class CharToStringParser extends Parser<CharToken,String> {
17 public CharToStringParser(Union u) { super(u); }
18 public Topology<CharToken> top() { return new IntegerTopology<CharToken>(); }
19 public Forest<String> shiftedToken(CharToken ct, Token.Location loc) {
20 return Forest.create(loc, ct.result(), null, null, false, false);
24 public static class CharRange extends Atom<CharToken> {
25 private String esc(char c) { return StringUtil.escapify(c+"", "[]-~\\\"\'"); }
26 private Topology<CharToken> t;
27 public CharRange(Topology<CharToken> t) { this.t = t; }
28 public Topology<CharToken> top() { return t; }
29 public String toString() {
30 StringBuffer sb = new StringBuffer();
32 Range.Set ranges = ((IntegerTopology)top()).getRanges();
33 if (ranges.size() == -1 || ranges.size() > Character.MAX_VALUE/2) {
35 ranges = ranges.complement();
37 ranges = ranges.intersect(all);
38 for(Range r : ranges) {
39 if (r.isMinNegInf() || r.isMaxPosInf()) throw new Error("should not happen");
40 if (r.getMin()==r.getMax()) {
41 sb.append(esc((char)r.getMin()));
43 sb.append(esc((char)r.getMin()));
45 sb.append(esc((char)r.getMax()));
53 /** returns an element matching all characters between <tt>start</tt> and <tt>end</tt>, inclusive */
54 public static Atom positiveRange(char start, char end) {
55 return new CharRange(new IntegerTopology<CharToken>(new Range.Set(new Range((int)start, (int)end))));
58 /** returns an element matching all characters <b>not</b> between <tt>start</tt> and <tt>end</tt>, inclusive */
59 public static Atom negativeRange(char start, char end) {
60 return new CharRange(new IntegerTopology<CharToken>(new Range.Set(new Range((int)start, (int)end)).complement().intersect(all)));
63 public static final Atom leftBrace = new CharRange(new IntegerTopology<CharToken>(9998)) { public String toString() { return "{"; } };
64 public static final Atom rightBrace = new CharRange(new IntegerTopology<CharToken>(9999)) { public String toString() { return "}"; } };
65 public static final CharToken left = new CharToken((char)9998);
66 public static final CharToken right = new CharToken((char)9999);
68 private static final Range.Set all = new Range.Set(new Range(0, Character.MAX_VALUE));
69 public static final Atom any = new CharRange(new IntegerTopology<CharToken>(all));
70 public static final Atom none = new CharRange(new IntegerTopology<CharToken>());
71 public static IntegerTopology<CharToken> range(Range r) { return new IntegerTopology<CharToken>(r); }
72 public static Atom set(Range.Set r) { return new CharRange(new IntegerTopology<CharToken>(r)); }
74 /** returns an element which exactly matches the string given */
75 public static Element string(String s) {
76 if (s.length() == 0) return Union.epsilon;
77 final String escapified = "\""+StringUtil.escapify(s, "\"\r\n\\")+"\"";
79 if (s.length() == 1) {
81 new CharRange(new IntegerTopology<CharToken>((int)s.charAt(0))) {
82 public String toString() { return escapified; } };
84 Union ret2 = new Union("\""+s+"\"_str", true) {
85 public String toString() { return escapified; } };
86 Element[] refs = new Element[s.length()];
87 for(int i=0; i<refs.length; i++) refs[i] = new CharRange(new IntegerTopology<CharToken>((int)s.charAt(i)));
88 ret2.add(Sequence.constant(refs, s, null, null));
95 public static Topology<CharToken> top() { return new IntegerTopology<CharToken>(); }
96 public static Topology<CharToken> top(String s) throws java.text.ParseException {
97 return new IntegerTopology<CharToken>(Range.Set.parse(s));
100 // Private //////////////////////////////////////////////////////////////////////////////
103 public CharToken(char c) { this.c = c; }
104 public String result() { return c+""; }
105 public String toString() { return "\'"+StringUtil.escapify(c+"")+"\'"; }
107 //////////////////////////////////////////////////////////////////////////////////////////
109 public int toInt() { return (int)c; }
111 // Statics //////////////////////////////////////////////////////////////////////////////
113 public static class CartesianLocation implements Location {
114 public final int line;
115 public final int col;
116 public String toString() { return line + ":" + col; }
117 public CartesianLocation(int line, int col) { this.line = line; this.col = col; }
120 /** an implementation of Token.Stream for sequences of characters */
121 public static class Stream implements Token.Stream {
122 private final String message;
123 private final Reader r;
124 private int line = 1;
127 public Stream(String s) { this(new StringReader(s)); }
129 public Stream(Reader r) { this(r, null); }
130 public Stream(Reader r, String s) { this.r = r; this.message = s; }
132 public Stream(InputStream i) { this(i, null); }
133 public Stream(InputStream i, String s) { this(new InputStreamReader(i), s); }
135 private Line currentLine = new Line();
137 public StringBuffer line = new StringBuffer();
140 private class LocWrap implements Location {
141 Line myline = Stream.this.currentLine;
142 public final int line;
143 public final int col;
144 public String toString() { return line + ":" + col; }
145 public LocWrap(int line, int col) { this.line = line; this.col = col; }
146 public String getContext() {
147 StringBuffer spaces = new StringBuffer();
148 for(int i=0; i<col-1; i++) spaces.append(' ');
150 return " " + myline.line.toString() + "\n " + spaces.toString();
155 private Token.Location location = new LocWrap(1, 1);
156 public Token.Location getLocation() { return location; }
157 public Token next() throws IOException {
159 if (i==-1) return null;
161 location = new LocWrap(line, col);
162 Token ret = new CharToken(c);
163 String s = line + "";
164 while(s.length() < 4) s = " " + s;
165 s = "line "+s+", col " + col;
166 long now = System.currentTimeMillis();
169 System.out.print(" "+(message==null?"":message)+" " + s + " \r");
172 currentLine = new Line();
176 currentLine.line.append(c);