// Subclasses //////////////////////////////////////////////////////////////////////////////
- /** an atom which tracks the possible tokenset of some element, provided that element can only match single-token sequences */
- 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>)toAtom(e); }
- public String toString() { return e.toString(); }
- }
- /** an atom which tracks the inverse of some other atom */
- 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; }
- }
-
- static Topology toAtom(Element e) {
+ public static Topology toAtom(Element e) {
if (e instanceof Atom) return (Atom)e;
if (e instanceof Sequence) return ((Sequence)e).toAtom();
Topology ret = null;
public InnerAmbiguous(Forest<?> f) { this.f = f; }
}
- public static interface TreeConsumer<T> {
+ static interface TreeConsumer<T> {
public void addTree(Tree<T> t);
}
- public static class HashSetTreeConsumer<T> extends HashSet<Tree<T>> implements TreeConsumer<T> {
+ static class HashSetTreeConsumer<T> extends HashSet<Tree<T>> implements TreeConsumer<T> {
public void addTree(Tree<T> t) {
super.add(t);
}
}
- public class Waiting {
+ class Waiting {
Node parent;
Forest pending;
State state;
// Node /////////////////////////////////////////////////////////////////////////////////
/** a node in the GSS */
- public final class Node extends FastSet<Node> implements Invokable<Position, Node, Node>, IntegerMappable {
+ final class Node extends FastSet<Node> implements Invokable<Position, Node, Node>, IntegerMappable {
private Forest.Ref holder = null;
private boolean allqueued = false;
// Table //////////////////////////////////////////////////////////////////////////////
/** an SLR(1) parse table which may contain conflicts */
- public static class Table<Tok> extends Walk.Cache {
+ static class Table<Tok> extends Walk.Cache {
public final Walk.Cache cache = this;
/** a single state in the LR table and the transitions possible from it */
- public class State<Tok> implements Comparable<State<Tok>>, IntegerMappable, Iterable<Position> {
+ class State<Tok> implements Comparable<State<Tok>>, IntegerMappable, Iterable<Position> {
public final int idx = master_state_idx++;
private final HashSet<Position> hs;
import java.lang.ref.*;
/** currently this class exports only static methods to create repetitions; there are no public instance methods or constructors */
-public class Repeat extends Union {
+class Repeat extends Union {
- /** repeat zero or one times */
- public static Repeat maybe(Element e) { return new Repeat(e, true, false, null, false); }
- public static Repeat maybe(Element e, Object tag) { return new Repeat(e, true, false, null, false, tag); }
- /** repeat zero or more times */
- public static Repeat many0(Element e) { return new Repeat(e, true, true, null, false); }
- public static Repeat many0(Element e, Object tag) { return new Repeat(e, true, true, null, false, tag); }
- /** repeat zero or more times, separated by <tt>sep</tt> */
- public static Repeat many0(Element e, Element sep) { return new Repeat(e, true, true, sep, false); }
- public static Repeat many0(Element e, Element sep, Object tag) { return new Repeat(e, true, true, sep, false, tag); }
- /** repeat one or more times */
- public static Repeat many1(Element e) { return new Repeat(e, false, true, null, false); }
- public static Repeat many1(Element e, Object tag) { return new Repeat(e, false, true, null, false, tag); }
- /** repeat one or more times, separated by <tt>sep</tt> */
- public static Repeat many1(Element e, Element sep) { return new Repeat(e, false, true, sep, false); }
- public static Repeat many1(Element e, Element sep, Object tag) { return new Repeat(e, false, true, sep, false, tag); }
-
- /** repeat zero or more times, matching a maximal sequence of atoms */
- public static Repeat maximal0(Element e) { return new Repeat(e, true, true, null, true); }
- public static Repeat maximal0(Element e, Object tag) { return new Repeat(e, true, true, null, true, tag); }
- /** repeat one or more times, matching a maximal sequence of atoms */
- public static Repeat maximal1(Element e) { return new Repeat(e, false, true, null, true); }
- public static Repeat maximal1(Element e, Object tag) { return new Repeat(e, false, true, null, true, tag); }
- /** repeat one or more times, separated by an atom <tt>sep</tt>, matching a maximal sequence */
- public static Repeat maximal1(Element e, Element sep) { return new Repeat(e, false, true, sep, true); }
- public static Repeat maximal1(Element e, Element sep, Object tag) { return new Repeat(e, false, true, sep, true, tag); }
-
- private Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal) {
+ Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal) {
this(e, zeroOkay, manyOkay, separator, maximal, null); }
- private Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag) {
+ Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag) {
super(e+(!manyOkay ? "?" : (zeroOkay ? (maximal ? "**" : "*") : (maximal ? "++" : "+")))+(separator==null?"":("/"+separator)), true);
if (maximal && zeroOkay && separator != null)
throw new RuntimeException("cannot create a maximal repetition of zero or more items with a separator (yet): " + this);
if (zeroOkay) {
add(new Sequence.Constant.Empty());
- if (manyOkay) add(new Sequence.Singleton(many1(e, separator)));
+ if (manyOkay) add(new Sequence.Singleton(Sequence.many1(e, separator)));
else add(new Sequence.Singleton(e));
} else {
add(new Sequence.RewritingSequence(tag, new Element[] { e }, null));
Sequence _clone() { return new Singleton(elements,idx); }
}
- public static class Unwrap extends Sequence {
+ public static Unwrap unwrap(Element[] e, Object tag, boolean[] drops) { return new Unwrap(e, tag, drops); }
+ static class Unwrap extends Sequence {
private boolean[] drops;
private final Object tag;
public Unwrap(Element[] e, Object tag) { super(e); this.drops = null; this.tag = tag; }
return sb;
}
}
+
+ // Repeat //////////////////////////////////////////////////////////////////////////////
+
+ /** repeat zero or one times */
+ public static Repeat maybe(Element e) { return new Repeat(e, true, false, null, false); }
+ public static Repeat maybe(Element e, Object tag) { return new Repeat(e, true, false, null, false, tag); }
+ /** repeat zero or more times */
+ public static Repeat many0(Element e) { return new Repeat(e, true, true, null, false); }
+ public static Repeat many0(Element e, Object tag) { return new Repeat(e, true, true, null, false, tag); }
+ /** repeat zero or more times, separated by <tt>sep</tt> */
+ public static Repeat many0(Element e, Element sep) { return new Repeat(e, true, true, sep, false); }
+ public static Repeat many0(Element e, Element sep, Object tag) { return new Repeat(e, true, true, sep, false, tag); }
+ /** repeat one or more times */
+ public static Repeat many1(Element e) { return new Repeat(e, false, true, null, false); }
+ public static Repeat many1(Element e, Object tag) { return new Repeat(e, false, true, null, false, tag); }
+ /** repeat one or more times, separated by <tt>sep</tt> */
+ public static Repeat many1(Element e, Element sep) { return new Repeat(e, false, true, sep, false); }
+ public static Repeat many1(Element e, Element sep, Object tag) { return new Repeat(e, false, true, sep, false, tag); }
+
+ /** repeat zero or more times, matching a maximal sequence of atoms */
+ public static Repeat maximal0(Element e) { return new Repeat(e, true, true, null, true); }
+ public static Repeat maximal0(Element e, Object tag) { return new Repeat(e, true, true, null, true, tag); }
+ /** repeat one or more times, matching a maximal sequence of atoms */
+ public static Repeat maximal1(Element e) { return new Repeat(e, false, true, null, true); }
+ public static Repeat maximal1(Element e, Object tag) { return new Repeat(e, false, true, null, true, tag); }
+ /** repeat one or more times, separated by an atom <tt>sep</tt>, matching a maximal sequence */
+ public static Repeat maximal1(Element e, Element sep) { return new Repeat(e, false, true, sep, true); }
+ public static Repeat maximal1(Element e, Element sep, Object tag) { return new Repeat(e, false, true, sep, true, tag); }
+
+
}
public class MetaGrammar extends StringWalker {
+ /** an atom which tracks the possible tokenset of some element, provided that element can only match single-token sequences */
+ 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>)toAtom(e); }
+ public String toString() { return e.toString(); }
+ }
+
+ /** an atom which tracks the inverse of some other atom */
+ 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 a.complement(); }
+ public String toString() { return "~"+a; }
+ }
+
public static class Hack<T extends Input> extends Atom<T> {
private final Atom<T> a;
static final Topology leftright = CharRange.rightBrace.union(CharRange.leftBrace);
if ("\\n".equals(head)) return new Character('\n');
else if ("\\r".equals(head)) return new Character('\r');
else if ("grammar".equals(head)) { for(Tree<String> t : tree.children()) walk(t); return this; }
- else if ("*".equals(head)) return Repeat.many0((Element)walk(tree.child(0)), repeatTag());
- else if ("+".equals(head)) return Repeat.many1((Element)walk(tree.child(0)), repeatTag());
- else if ("+/".equals(head)) return Repeat.many1((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag());
- else if ("*/".equals(head)) return Repeat.many0((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag());
- else if ("++/".equals(head)) return Repeat.maximal1((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag());
- else if ("**".equals(head)) return Repeat.maximal0((Element)walk(tree.child(0)), repeatTag());
- else if ("++".equals(head)) return Repeat.maximal1((Element)walk(tree.child(0)), repeatTag());
- else if ("?".equals(head)) return Repeat.maybe((Element)walk(tree.child(0)), repeatTag());
+ else if ("*".equals(head)) return Sequence.many0((Element)walk(tree.child(0)), repeatTag());
+ else if ("+".equals(head)) return Sequence.many1((Element)walk(tree.child(0)), repeatTag());
+ else if ("+/".equals(head)) return Sequence.many1((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag());
+ else if ("*/".equals(head)) return Sequence.many0((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag());
+ else if ("++/".equals(head)) return Sequence.maximal1((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag());
+ else if ("**".equals(head)) return Sequence.maximal0((Element)walk(tree.child(0)), repeatTag());
+ else if ("++".equals(head)) return Sequence.maximal1((Element)walk(tree.child(0)), repeatTag());
+ else if ("?".equals(head)) return Sequence.maybe((Element)walk(tree.child(0)), repeatTag());
else if ("!".equals(head)) { Element e = (Element)walk(tree.child(0)); dropAll.add(e); return e; }
else if ("&".equals(head)) return ((Sequence)walk(tree,0)).and(sequence(walk(tree, 1), true));
else if ("and".equals(head)) return ((Sequence)walk(tree,0)).and(sequence(walk(tree, 1), true));
else if ("->".equals(head)) { PreSequence p = (PreSequence)walk(tree, 0); p.noFollow = (Element)walk(tree, 1); return p; }
else if ("/".equals(head)) return ((PreSequence)walk(tree, 0)).sparse((Element)walk(tree, 1));
else if (" /".equals(head)) return ((PreSequence)walk(tree, 0)).sparse((Element)walk(tree, 1));
- else if ("~".equals(head)) return new Hack(new Atom.Invert(new Atom.Infer((Element)walk(tree, 0))));
+ else if ("~".equals(head)) return new Hack(new Invert(new Infer((Element)walk(tree, 0))));
else if ("ps".equals(head)) return new PreSequence((Object[])walk(tree,0), null);
else if (":".equals(head)) {
String s = string(tree.child(0));
Element[] expansion = o2;
Sequence ret = null;
if (dropAll || lame) ret = Sequence.drop(expansion, lame);
- else if (unwrap) ret = new Sequence.Unwrap(expansion, repeatTag(), drops);
+ else if (unwrap) ret = Sequence.unwrap(expansion, repeatTag(), drops);
else if (keeping || tag!=null) ret = Sequence.rewritingSequence(tag, expansion, labels, drops);
else {
int idx = -1;
for(Sequence s : and) ret = ret.and(s);
for(Sequence s : not) ret = ret.not(s);
set.add(ret);
- if (this.noFollow != null) ret.noFollow = new Atom.Invert(new Atom.Infer(this.noFollow));
+ if (this.noFollow != null) ret.noFollow = new Invert(new Infer(this.noFollow));
return ret;
}
}
+
// DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED
new edu.berkeley.sbp.Tree(null, "grammar", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "=", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "s", new edu.berkeley.sbp.Tree[] { })}),
new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "psx", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "ps", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "!", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "nonTerminal", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "w", new edu.berkeley.sbp.Tree[] { }),
+
public static class Grammar extends ReflectiveGrammar {
private int anon = 0;
- private final Element ws = Repeat.maximal0(getNonTerminal("w"));
+ private final Element ws = Sequence.maximal0(getNonTerminal("w"));
public Grammar(Class c) { super(c); dropAll.add(ws); }
public Object walk(Tree<String> tree) {
String head = tree.head();