// Subclasses //////////////////////////////////////////////////////////////////////////////
- public static class Infer<T extends Token> extends Atom<T> {
+ public static class Infer<T extends Input> extends Atom<T> {
private final Element e;
public Infer(Element e) { this.e = e; }
public Topology<T> top() { return (Topology<T>)e.toAtom(); }
public String toString() { return e.toString(); /* FIXME should be toAtom() */ }
}
- public static class Invert<T extends Token> extends Atom<T> {
+ public static class Invert<T extends Input> extends Atom<T> {
private final Atom<T> a;
public Invert(Atom<T> a) { this.a = a; }
public Topology<T> top() { return ((Topology<T>)a.top()).complement(); }
public String toString() { return "~"+a; }
}
-
- public static class Hack<T extends Token> extends Atom<T> {
- private final Atom<T> a;
- static final Topology leftright =
- edu.berkeley.sbp.misc.CharRange.rightBrace.union(edu.berkeley.sbp.misc.CharRange.leftBrace);
- public Hack(Atom<T> a) { this.a = a; }
- public Topology<T> top() { return ((Topology<T>)a.top()).minus(leftright); }
- public String toString() { return "~"+a; }
- }
-
}
/** expand this forest into a set of trees */
public abstract HashSet<Tree<T>> expand(boolean toss);
- static <T> Forest<T> singleton(Token.Location loc) { return create(loc, null, new Forest[] { }, false, true); }
- static <T> Forest<T> singleton(Token.Location loc, Forest<T> body) { return create(loc, null, new Forest[] { body }, false, true); }
- static <T> Forest<T> leaf(Token.Location loc, T tag) { return create(loc, tag, null, false, false); }
- public static <T> Forest<T> create(Token.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
+ static <T> Forest<T> singleton(Input.Location loc) { return create(loc, null, new Forest[] { }, false, true); }
+ static <T> Forest<T> singleton(Input.Location loc, Forest<T> body) { return create(loc, null, new Forest[] { body }, false, true); }
+ static <T> Forest<T> leaf(Input.Location loc, T tag) { return create(loc, tag, null, false, false); }
+ public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
return new MultiForest<T>(loc, tag, tokens, unwrap, singleton);
}
protected static class Body<T> {
- private final Token.Location location;
+ private final Input.Location location;
private final T tag;
private final Forest<T>[] tokens;
private final boolean unwrap;
private final boolean singleton;
- private Body(Token.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
+ private Body(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
this.location = loc;
this.tag = tag;
this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
private static class MultiForest<T> extends IterableForest<T> {
private final FastSet<Body<T>> results;
private MultiForest(FastSet<Body<T>> results) { this.results = results; }
- public MultiForest(Token.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
+ public MultiForest(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
this.results = new FastSet<Body<T>>(new Body(loc, tag, tokens, unwrap, singleton));
}
public Iterator<Body<T>> iterator() { return results.iterator(); }
private boolean good;
private Phase next = null;
private Phase prev;
- private Token.Location location;
+ private Input.Location location;
public final Parser parser;
private Forest forest;
- public Phase(Phase prev, Parser parser, Phase previous, Tok token, Token.Location location, Forest forest) {
+ public Phase(Phase prev, Parser parser, Phase previous, Tok token, Input.Location location, Forest forest) {
this.prev = prev;
this.forest = forest;
this.parser = parser;
return true;
}
- public Token.Location getLocation() { return location; }
+ public Input.Location getLocation() { return location; }
/** add a new node (merging with existing nodes if possible)
* @param parent the parent of the new node
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 Token {
+public interface Input {
/** this is declared abstract as a way of forcing subclasses to provide a thoughtful implementation */
public abstract String toString();
/** thrown when the parser arrives at a state from which it is clear that no valid parse can result */
public class ParseFailed extends RuntimeException {
- private final Token.Location location;
+ private final Input.Location location;
private final String message;
public ParseFailed() { this("", null); }
- public ParseFailed(String message, Token.Location loc) { this.location = loc; this.message = message; }
- public Token.Location getLocation() { return location; }
+ public ParseFailed(String message, Input.Location loc) { this.location = loc; this.message = message; }
+ public Input.Location getLocation() { return location; }
public String toString() { return message/* + (location==null ? "" : (" at " + location))*/; }
public static <Tok> void complain(GSS.Phase<Tok>.Node n, HashMap<String,HashSet<String>> errors, boolean force) {
protected Parser(Table<Tok> pt) { this.pt = pt; }
/** implement this method to create the output forest corresponding to a lone shifted input token */
- public abstract Forest<Result> shiftToken(Tok t, Token.Location loc);
+ public abstract Forest<Result> shiftToken(Tok t, Input.Location loc);
/** parse <tt>input</tt>, using the table <tt>pt</tt> to drive the parser */
- public Forest<Result> parse(Token.Stream<Tok> input) throws IOException, ParseFailed {
+ public Forest<Result> parse(Input.Stream<Tok> input) throws IOException, ParseFailed {
GSS gss = new GSS();
- Token.Location loc = input.getLocation();
+ Input.Location loc = input.getLocation();
GSS.Phase current = gss.new Phase<Tok>(null, this, null, input.next(1, 0, 0), loc, null);
current.newNode(null, Forest.leaf(null, null), pt.start, true);
int count = 1;
return epsilonForm;
}
- protected abstract <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args);
+ protected abstract <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args);
// Position //////////////////////////////////////////////////////////////////////////////
// Position /////////////////////////////////////////////////////////////////////////////////
- final <T> Forest<T> rewrite(Token.Location loc) {
+ final <T> Forest<T> rewrite(Input.Location loc) {
if (this==firstp()) return epsilonForm();
return rewrite2(loc);
}
- final <T> Forest<T> rewrite2(Token.Location loc) {
+ final <T> Forest<T> rewrite2(Input.Location loc) {
for(int i=0; i<pos; i++) if (holder[i]==null) throw new Error("realbad " + i);
for(int i=pos; i<elements.length; i++) {
if (holder[i]==null) holder[i] = elements[i].epsilonForm();
static class Constant extends Sequence {
private final Object result;
public Constant(Element[] e, Object result, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.result = result; }
- public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
+ public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
return (Forest<T>)Forest.leaf(loc, result);
}
static class Drop extends Constant {
private final int idx;
public Singleton(Element e, HashSet<Sequence> and, HashSet<Sequence> not) { this(new Element[] { e }, 0, and, not); }
public Singleton(Element[] e, int idx, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.idx = idx; }
- public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx]); }
+ public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx]); }
}
public static class Unwrap extends Sequence {
private boolean[] drops;
public Unwrap(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.drops = null; }
public Unwrap(Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.drops = drops; }
- public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
+ public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
for(int i=0; i<args.length; i++) if (args[i]==null) throw new Error();
if (drops==null) return Forest.create(loc, null, args, true, false);
int count = 0;
this.drops = drops == null ? new boolean[e.length] : drops;
for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
}
- public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
+ public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
Forest<T>[] args2 = new Forest[count];
int j = 0;
for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
final T head;
Tree<T>[] children;
- final Token.Location location;
+ final Input.Location location;
public T head() { return head; }
public int numChildren() { return children.length; }
public Iterator<Tree<T>> iterator() { return new ArrayIterator(children); }
public Tree<T> child(int i) { return children[i]; }
- public Token.Location getLocation() { return location; }
+ public Input.Location getLocation() { return location; }
- public Tree(Token.Location loc, T head) { this(loc, head, null); }
- public Tree(Token.Location loc, T head, Tree<T>[] children) {
+ public Tree(Input.Location loc, T head) { this(loc, head, null); }
+ public Tree(Input.Location loc, T head, Tree<T>[] children) {
this.location = loc;
this.head = head;
Tree<T>[] children2 = children==null ? new Tree[0] : new Tree[children.length];
}
- // Token-Set //////////////////////////////////////////////////////////////////////////////
+ // Input-Set //////////////////////////////////////////////////////////////////////////////
- static abstract class WalkTokenSet<Tok extends Token> extends Walk<Topology<Tok>> {
+ static abstract class WalkTokenSet<Tok extends Input> extends Walk<Topology<Tok>> {
public Topology<Tok> cs;
public WalkTokenSet(Topology<Tok> cs) { this.cs = cs; }
public WalkTokenSet(Topology<Tok> cs, Cache c) { super(c); this.cs = cs; }
public Topology<Tok> walkAtom(Atom r) { cs = cs.union(r); return cs; }
}
- static class First<Tok extends Token> extends WalkTokenSet<Tok> {
+ static class First<Tok extends Input> extends WalkTokenSet<Tok> {
public First(Topology<Tok> cs, Walk.Cache cache) { super(cs, cache); }
public Topology<Tok> sequence(Sequence seq) {
for(Position p = seq.firstp(); p!=null && !p.isLast(); p = p.next()) {
}
}
- static class Follow<Tok extends Token> extends WalkTokenSet<Tok> {
+ static class Follow<Tok extends Input> extends WalkTokenSet<Tok> {
private final Element me;
private final HashSet<Element> all;
private boolean eof = false;
public HashMap<Element,Topology> follow = new HashMap<Element,Topology>();
public HashMapBag<Element,Element> ys = new HashMapBag<Element,Element>();
public HashMap<Element,Topology> atoms = new HashMap<Element,Topology>();
- public <Tok extends Token> Topology<Tok> first(Element e, Topology<Tok> empty) {
+ public <Tok extends Input> Topology<Tok> first(Element e, Topology<Tok> empty) {
return new Walk.First<Tok>(empty, this).walk(e);
}
}
-package edu.berkeley.sbp.misc;
+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.Token.Location;
import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.misc.*;
+import edu.berkeley.sbp.Input.Location;
-public class CharToStringParser extends Parser<Character,String> {
+public class CharParser extends Parser<Character,String> {
public Forest<String> parse(InputStream is) throws IOException, ParseFailed {
return super.parse(new Stream(is));
}
return super.parse(new Stream(r));
}
- public CharToStringParser(Union u) {
+ public CharParser(Union u) {
super(u, new CharTopology());
pt.optimize(new CharTopology());
}
-package edu.berkeley.sbp.misc;
+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.Token.Location;
import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.misc.*;
+import edu.berkeley.sbp.Input.Location;
public class CharRange extends Atom<Character> {
-package edu.berkeley.sbp.misc;
+package edu.berkeley.sbp.chr;
import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.misc.*;
import edu.berkeley.sbp.util.*;
public class CharTopology extends IntegerTopology<Character> implements Functor<Character,Integer> {
import java.lang.reflect.*;
import java.lang.ref.*;
import edu.berkeley.sbp.*;
-import edu.berkeley.sbp.Token.Location;
+import edu.berkeley.sbp.Input.Location;
import edu.berkeley.sbp.util.*;
-public abstract class CartesianInput<Tok> implements Token.Stream<Tok> {
+public abstract class CartesianInput<Tok> implements Input.Stream<Tok> {
private int line = 1;
private int col = 0;
public abstract boolean isCR();
long then = 0;
- private Token.Location location = new LocWrap(line, col);
- public Token.Location getLocation() { return location; }
+ private Input.Location location = new LocWrap(line, col);
+ public Input.Location getLocation() { return location; }
public Tok next(int numstates, int resets, int waits) throws IOException {
Tok t = next();
if (t==null) return null;
return t;
}
- public static class Location implements Token.Location {
+ 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 Token.Location {
+ private class LocWrap implements Input.Location {
public final int line;
public final int col;
public String toString() { return line + ":" + col; }
package edu.berkeley.sbp.misc;
import edu.berkeley.sbp.util.*;
import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.chr.*;
import java.util.*;
import java.io.*;
public class MetaGrammar extends StringWalker {
+ public static class Hack<T extends Input> extends Atom<T> {
+ private final Atom<T> a;
+ static final Topology leftright = CharRange.rightBrace.union(CharRange.leftBrace);
+ public Hack(Atom<T> a) { this.a = a; }
+ public Topology<T> top() { return a.minus(leftright); }
+ public String toString() { return "~"+a; }
+ }
+
+
public static Union make() throws Exception {
return ((MetaGrammar)new MetaGrammar().walk(meta)).done();
}
else if ("psylr".equals(head)) throw new Error("not supported");
else if ("/".equals(head)) return ((PreSequence)walk(tree, 0)).sparse((Element)walk(tree, 1));
else if ("~".equals(head))
- return new Atom.Hack(new Atom.Invert(new Atom.Infer((Element)walk(tree, 0))));
+ return new Hack(new Atom.Invert(new Atom.Infer((Element)walk(tree, 0))));
else if ("ps".equals(head)) return new PreSequence((Element[])walk(tree, 0));
else if ("ps".equals(head)) return new PreSequence((Element[])walk(tree, 0));
else if ("qprod".equals(head)) {
}
out.append("\n // DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED\n");
- new CharToStringParser(MetaGrammar.make()).parse(new FileInputStream(args[0])).expand1().toJava(out);
+ new CharParser(MetaGrammar.make()).parse(new FileInputStream(args[0])).expand1().toJava(out);
out.append("\n // DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED\n");
for(String s = br.readLine(); s != null; s = br.readLine()) out.append(s+"\n");
import edu.berkeley.sbp.*;
import edu.berkeley.sbp.misc.*;
import edu.berkeley.sbp.tib.*;
+import edu.berkeley.sbp.chr.*;
public class RegressionTests {
//MetaGrammar mg0 = new MetaGrammar();
//mg0.walk(MetaGrammar.meta);
//System.out.println(mg0);
- Tree<String> res = new CharToStringParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
+ Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
MetaGrammar mg = (MetaGrammar)new MetaGrammar().walk(res);
//System.out.println(mg);
Union meta = mg.done();
SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
- res = new CharToStringParser(meta).parse(sis).expand1();
+ res = new CharParser(meta).parse(sis).expand1();
Union testcasegrammar = ((MetaGrammar)new MetaGrammar("ts").walk(res)).done("ts");
if (testcasegrammar==null) return;
- CharToStringParser parser = new CharToStringParser(testcasegrammar);
+ CharParser parser = new CharParser(testcasegrammar);
if (profile) {
System.out.println("\nready...");
ParseFailed pfe = null;
try {
res = tib
- ? /*new CharToStringParser(grammar).parse(new Tib(input))*/ null
- : new CharToStringParser(grammar).parse(new StringReader(input));
+ ? /*new CharParser(grammar).parse(new Tib(input))*/ null
+ : new CharParser(grammar).parse(new StringReader(input));
} catch (ParseFailed pf) {
pfe = pf;
}
import edu.berkeley.sbp.util.*;
import edu.berkeley.sbp.*;
-/** an implementation of Token for streams of Java <tt>char</tt> values */
-public class StringToken implements Token {
+/** an implementation of Input for streams of Java <tt>char</tt> values */
+public class StringToken implements Input {
public static final StringToken left = new StringToken(null, null) { public boolean equals(Object o) { return this==o; } };
public static final StringToken right = new StringToken(null, null) { public boolean equals(Object o) { return this==o; } };
package edu.berkeley.sbp.tib;
import edu.berkeley.sbp.*;
import edu.berkeley.sbp.misc.*;
+import edu.berkeley.sbp.chr.*;
import java.util.*;
import java.io.*;
* experimentation with the TIB spec. Once the spec is finalized it
* should probably be rewritten.
*/
-public class Tib implements Token.Stream<Character> {
+public class Tib implements Input.Stream<Character> {
public Tib(String s) throws IOException { this(new StringReader(s)); }
public Tib(Reader r) throws IOException { this(new BufferedReader(r)); }
int _row = 1;
int _col = 0;
- public Token.Location getLocation() { return new CartesianInput.Location(_row, _col); }
+ public Input.Location getLocation() { return new CartesianInput.Location(_row, _col); }
private BufferedReader br;
char left = CharRange.left;
// You may not use this file except in compliance with the License.
package edu.berkeley.sbp.tib;
-//import org.ibex.util.*;
-//import org.ibex.io.*;
import edu.berkeley.sbp.*;
import edu.berkeley.sbp.misc.*;
import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.chr.*;
import java.util.*;
import java.io.*;
public static void main(String[] s) throws Exception {
System.out.println("parsing " + s[0]);
- Tree<String> res = new CharToStringParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
+ Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
MetaGrammar gram = (MetaGrammar)new Tib.Grammar().walk(res);
//System.out.println(gram);
Union mg = gram.done();
System.out.println("\nparsing " + s[1]);
- Forest f = new CharToStringParser(mg).parse(new Tib(new FileInputStream(s[1])));
+ Forest f = new CharParser(mg).parse(new Tib(new FileInputStream(s[1])));
System.out.println();
System.out.println(f);