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 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();
22 Range.Set ranges = ((IntegerTopology)top()).getRanges();
23 if (ranges.size() == -1 || ranges.size() > Character.MAX_VALUE/2) {
25 ranges = ranges.complement();
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()));
33 sb.append(esc((char)r.getMin()));
35 sb.append(esc((char)r.getMax()));
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))));
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)));
53 private static final Range.Set all = new Range.Set(new Range(0, Character.MAX_VALUE));
54 public static final Atom any = new CharRange(new IntegerTopology<CharToken>(all));
55 public static final Atom none = new CharRange(new IntegerTopology<CharToken>());
56 public static IntegerTopology<CharToken> range(Range r) { return new IntegerTopology<CharToken>(r); }
57 public static Atom set(Range.Set r) { return new CharRange(new IntegerTopology<CharToken>(r)); }
59 /** returns an element which exactly matches the string given */
60 public static Element string(String s) {
61 if (s.length() == 0) return MetaGrammar.epsilon;
62 final String escapified = "\""+StringUtil.escapify(s, "\"\r\n\\")+"\"";
64 if (s.length() == 1) {
66 new CharRange(new IntegerTopology<CharToken>((int)s.charAt(0))) {
67 public String toString() { return escapified; } };
69 Union ret2 = new Union("\""+s+"\"_str", true) {
70 public String toString() { return escapified; } };
71 Element[] refs = new Element[s.length()];
72 for(int i=0; i<refs.length; i++) refs[i] = new CharRange(new IntegerTopology<CharToken>((int)s.charAt(i)));
73 ret2.add(Sequence.constant(refs, s, null, null));
80 public static Topology<CharToken> top() { return new IntegerTopology<CharToken>(); }
81 public static Topology<CharToken> top(String s) throws java.text.ParseException {
82 return new IntegerTopology<CharToken>(Range.Set.parse(s));
85 // Private //////////////////////////////////////////////////////////////////////////////
88 public final Location location;
89 private CharToken(char c, int line, int col) { this(c, new CartesianLocation(line, col)); }
90 private CharToken(char c, Location loc) { this.c = c; this.location = loc; }
91 public String result() { return c+""; }
92 public Location getLocation() { return location; }
93 public String toString() { return "\'"+StringUtil.escapify(c+"")+"\'"; }
95 //////////////////////////////////////////////////////////////////////////////////////////
97 public int toInt() { return (int)c; }
99 // Statics //////////////////////////////////////////////////////////////////////////////
101 static class CartesianLocation implements Location {
102 public final int line;
103 public final int col;
104 public String toString() { return line + ":" + col; }
105 public CartesianLocation(int line, int col) { this.line = line; this.col = col; }
106 public String getContext() { return ""; }
109 /** an implementation of Token.Stream for sequences of characters */
110 public static class Stream implements Token.Stream {
111 private final String message;
112 private final Reader r;
113 private int line = 1;
116 public Stream(String s) { this(new StringReader(s)); }
118 public Stream(Reader r) { this(r, null); }
119 public Stream(Reader r, String s) { this.r = r; this.message = s; }
121 public Stream(InputStream i) { this(i, null); }
122 public Stream(InputStream i, String s) { this(new InputStreamReader(i), s); }
124 private Line currentLine = new Line();
126 public StringBuffer line = new StringBuffer();
129 private class LocWrap implements Location {
130 Line myline = Stream.this.currentLine;
131 public final int line;
132 public final int col;
133 public String toString() { return line + ":" + col; }
134 public LocWrap(int line, int col) { this.line = line; this.col = col; }
135 public String getContext() {
136 StringBuffer spaces = new StringBuffer();
137 for(int i=0; i<col-1; i++) spaces.append(' ');
139 return " " + myline.line.toString() + "\n " + spaces.toString();
144 public Token next() throws IOException {
146 if (i==-1) return null;
148 Token ret = new CharToken(c, new LocWrap(line, col));
149 String s = line + "";
150 while(s.length() < 4) s = " " + s;
151 s = "line "+s+", col " + col;
152 long now = System.currentTimeMillis();
155 System.out.print(" "+(message==null?"":message)+" " + s + " \r");
158 currentLine = new Line();
162 currentLine.line.append(c);