package edu.berkeley.sbp.misc;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import edu.berkeley.sbp.*;
import edu.berkeley.sbp.*;
import edu.berkeley.sbp.util.*;
import edu.berkeley.sbp.*;
/** an implementation of Token for streams of Java char values */
public class CharToken implements Token, IntegerTopology.IntegerMappable {
// Public //////////////////////////////////////////////////////////////////////////////
public static class CharRange extends Atom {
private String esc(char c) { return StringUtil.escapify(c+"", "[]-~\\\"\'"); }
public CharRange(Topology t) { super(t); }
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append('[');
Range.Set ranges = ((IntegerTopology)top()).getRanges();
if (ranges.size() == -1 || ranges.size() > Character.MAX_VALUE/2) {
sb.append('~');
ranges = ranges.complement();
}
ranges = ranges.intersect(all);
for(Range r : ranges) {
if (r.isMinNegInf() || r.isMaxPosInf()) throw new Error("should not happen");
if (r.getMin()==r.getMax()) {
sb.append(esc((char)r.getMin()));
} else{
sb.append(esc((char)r.getMin()));
sb.append('-');
sb.append(esc((char)r.getMax()));
}
}
sb.append(']');
return sb.toString();
}
}
/** returns an element matching all characters between start and end, inclusive */
public static Atom positiveRange(char start, char end) {
return new CharRange(new IntegerTopology(new Range.Set(new Range((int)start, (int)end))));
}
/** returns an element matching all characters not between start and end, inclusive */
public static Atom negativeRange(char start, char end) {
return new CharRange(new IntegerTopology(new Range.Set(new Range((int)start, (int)end)).complement().intersect(all)));
}
public static final Atom leftBrace = new Atom(new IntegerTopology(-3)) { public String toString() { return "{"; } };
public static final Atom rightBrace = new Atom(new IntegerTopology(-4)) { public String toString() { return "}"; } };
private static final Range.Set all = new Range.Set(new Range(0, Character.MAX_VALUE));
public static final Atom any = new CharRange(new IntegerTopology(all));
public static final Atom none = new CharRange(new IntegerTopology());
public static IntegerTopology range(Range r) { return new IntegerTopology(r); }
public static Atom set(Range.Set r) { return new CharRange(new IntegerTopology(r)); }
/** returns an element which exactly matches the string given */
public static Element string(String s) {
if (s.length() == 0) return MetaGrammar.epsilon;
final String escapified = "\""+StringUtil.escapify(s, "\"\r\n\\")+"\"";
Element ret;
if (s.length() == 1) {
ret =
new CharRange(new IntegerTopology((int)s.charAt(0))) {
public String toString() { return escapified; } };
} else {
Union ret2 = new Union("\""+s+"\"_str", true) {
public String toString() { return escapified; } };
Element[] refs = new Element[s.length()];
for(int i=0; i((int)s.charAt(i)));
ret2.add(Sequence.constant(refs, s, null, null));
ret = ret2;
}
return ret;
}
/** FIXME */
public static Topology top() { return new IntegerTopology(); }
public static Topology top(String s) throws java.text.ParseException {
return new IntegerTopology(Range.Set.parse(s));
}
// Private //////////////////////////////////////////////////////////////////////////////
public final char c;
public final Location location;
CharToken(char c, int line, int col) { this(c, new CartesianLocation(line, col)); }
private CharToken(char c, Location loc) { this.c = c; this.location = loc; }
public String result() { return c+""; }
public Location getLocation() { return location; }
public String toString() { return "\'"+StringUtil.escapify(c+"")+"\'"; }
//////////////////////////////////////////////////////////////////////////////////////////
public int toInt() { return (int)c; }
// Statics //////////////////////////////////////////////////////////////////////////////
static class CartesianLocation implements Location {
public final int line;
public final int col;
public String toString() { return line + ":" + col; }
public CartesianLocation(int line, int col) { this.line = line; this.col = col; }
public String getContext() { return ""; }
}
/** an implementation of Token.Stream for sequences of characters */
public static class Stream implements Token.Stream {
private final String message;
private final Reader r;
private int line = 1;
private int col = 1;
public Stream(String s) { this(new StringReader(s)); }
public Stream(Reader r) { this(r, null); }
public Stream(Reader r, String s) { this.r = r; this.message = s; }
public Stream(InputStream i) { this(i, null); }
public Stream(InputStream i, String s) { this(new InputStreamReader(i), s); }
private Line currentLine = new Line();
private class Line {
public StringBuffer line = new StringBuffer();
}
private class LocWrap implements Location {
Line myline = Stream.this.currentLine;
public final int line;
public final int col;
public String toString() { return line + ":" + col; }
public LocWrap(int line, int col) { this.line = line; this.col = col; }
public String getContext() {
StringBuffer spaces = new StringBuffer();
for(int i=0; i 10) {
then = now;
System.out.print(" "+(message==null?"":message)+" " + s + " \r");
}
if (c=='\n') {
currentLine = new Line();
line++;
col = 1;
} else {
currentLine.line.append(c);
col++;
}
return ret;
}
}
}