import edu.berkeley.sbp.*;
import edu.berkeley.sbp.util.*;
-/** a token of input -- note that this represents an <i>actual input token</i> rather than an <tt>Element</tt> which <i>matches</i> a token */
-public interface Input<Tok> {
+/** a stream of tokens to be parsed */
+public interface Input<Token> {
- public Tok next(int numstates, int resets, int waits) throws IOException;
- public abstract Location getLocation();
+ /** returns the token just beyond the current location and advances beyond it */
+ public Token next(int numstates, int resets, int waits) throws IOException;
+
+ /** returns the location the input stream is currently at */
+ public Location getLocation();
/** a location *between tokens* in the input stream */
public static interface Location {
public final Walk.Cache cache = this;
- public void optimize(Functor<Tok,Integer> f) {
- for(State<Tok> state : all_states.values()) {
- state.oreductions = state.reductions.optimize(f);
- state.oshifts = state.shifts.optimize(f);
- }
- }
-
private void walk(Element e, HashSet<Element> hs) {
if (e==null) return;
if (hs.contains(e)) return;
if (p.element() != null && p.element() instanceof Atom)
state.shifts.addAll(state.gotoSetTerminals.subset(((Atom)p.element())));
}
+ if (top instanceof IntegerTopology)
+ for(State<Tok> state : all_states.values()) {
+ state.oreductions = state.reductions.optimize(((IntegerTopology)top).functor());
+ state.oshifts = state.shifts.optimize(((IntegerTopology)top).functor());
+ }
}
private boolean isRightNullable(Position p) {
--- /dev/null
+package edu.berkeley.sbp.chr;
+import java.io.*;
+import java.util.*;
+import java.lang.reflect.*;
+import java.lang.ref.*;
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.misc.*;
+import edu.berkeley.sbp.Input.Location;
+
+public class CharInput extends CartesianInput<Character> {
+ private final Reader r;
+
+ public CharInput(String s) { this(new StringReader(s)); }
+ public CharInput(Reader r) { this(r, null); }
+ public CharInput(Reader r, String s) { this.r = r; }
+ public CharInput(InputStream i) { this(i, null); }
+ public CharInput(InputStream i, String s) { this(new InputStreamReader(i), s); }
+
+ boolean cr = false;
+ public boolean isCR() { return cr; }
+ public Character next() throws IOException {
+ cr = false;
+ int i = r.read();
+ if (i==-1) return null;
+ char c = (char)i;
+ cr = c=='\n';
+ return c;
+ }
+}
import edu.berkeley.sbp.Input.Location;
public class CharParser extends Parser<Character,String> {
- public Forest<String> parse(InputStream is) throws IOException, ParseFailed {
- return super.parse(new Stream(is));
- }
- public Forest<String> parse(Reader r) throws IOException, ParseFailed {
- return super.parse(new Stream(r));
- }
- public CharParser(Union u) {
- super(u, new CharTopology());
- pt.optimize(new CharTopology());
- }
+ public Forest<String> parse(InputStream is) throws IOException, ParseFailed { return super.parse(new CharInput(is)); }
+ public Forest<String> parse(Reader r) throws IOException, ParseFailed { return super.parse(new CharInput(r)); }
+
+ public CharParser(Union u) { super(u, new CharTopology()); }
+
public Forest<String> shiftToken(Character ct, Location loc) {
return Forest.create(loc, ct.toString(), null, false, false);
}
- private static class Stream extends CartesianInput<Character> {
- private final Reader r;
-
- public Stream(String s) { this(new StringReader(s)); }
- public Stream(Reader r) { this(r, null); }
- public Stream(Reader r, String s) { this.r = r; }
- public Stream(InputStream i) { this(i, null); }
- public Stream(InputStream i, String s) { this(new InputStreamReader(i), s); }
-
- boolean cr = false;
- public boolean isCR() { return cr; }
- public Character next() throws IOException {
- cr = false;
- int i = r.read();
- if (i==-1) return null;
- char c = (char)i;
- cr = c=='\n';
- return c;
- }
- }
}
return t;
}
- public static class Location implements Input.Location {
- public final int line;
- public final int col;
- public String toString() { return line + ":" + col; }
- public Location(int line, int col) { this.line = line; this.col = col; }
- }
-
private class LocWrap implements Input.Location {
public final int line;
public final int col;
--- /dev/null
+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.util.*;
+
+public class CartesianLocation implements Input.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; }
+}
int _row = 1;
int _col = 0;
- public Input.Location getLocation() { return new CartesianInput.Location(_row, _col); }
+ public Input.Location getLocation() { return new CartesianLocation(_row, _col); }
private BufferedReader br;
char left = CharRange.left;
private int toInt(V v) { return f==null?((IntegerMappable)v).toInt():f.invoke(v); }
public Range.Set getRanges() { return new Range.Set(rs); }
+ public Functor<V,Integer> functor() { return f; }
public IntegerTopology(Functor<V,Integer> f) { this(f, new Range.Set()); }
public IntegerTopology(Functor<V,Integer> f, V a) { this(f, f==null?((IntegerMappable)a).toInt():f.invoke(a)); }