abstract void reachable(HashSet<Sequence.Position> rp);
abstract Topology toAtom();
+ public Topology noFollow() { return null; }
Forest epsilonForm() { throw new Error("no epsilon form: " + this); }
final boolean possiblyEpsilon(Walk.Cache cache) {
Boolean ret = cache==null ? null : cache.possiblyEpsilon.get(this);
public static Repeat many1(Element e) { return new Repeat(e, false, true); }
/** 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); }
- public static Maximal maximal(Element e) { return new Maximal(e); }
+ /** 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); }
+ /** 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); }
+ /** repeat one or more times, separated by <tt>sep</tt>, matching a maximal sequence of atoms */
+ public static Repeat maximal1(Element e, Element sep) { return new Repeat(e, false, true, sep, true); }
// Instance //////////////////////////////////////////////////////////////////////////////
final boolean zeroOkay, manyOkay;
final Element e;
final Element separator;
+ final boolean maximal;
Repeat(final Element e, boolean zeroOkay, boolean manyOkay) { this(e, zeroOkay, manyOkay, null); }
- Repeat(final Element e, boolean zeroOkay, boolean manyOkay, Element separator) {
+ Repeat(final Element e, boolean zeroOkay, boolean manyOkay, Element separator) { this(e, zeroOkay, manyOkay, separator, false); }
+ Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal) {
super(e+(!manyOkay ? "?" : (zeroOkay ? "*" : "+"))+(separator==null?"":("/"+separator.toString())), true);
this.e = e;
+ this.maximal = maximal;
this.zeroOkay = zeroOkay;
this.manyOkay = manyOkay;
this.separator = separator;
+ Union who = this;
+ if (maximal) {
+ who = new Union(this+"++");
+ add(new Sequence.Singleton(who, null, null) {
+ public Topology noFollow() { return separator==null ? e.toAtom() : separator.toAtom(); }
+ });
+ }
if (zeroOkay) {
- add(new Sequence.Constant.Empty());
- if (manyOkay) add(new Sequence.Singleton(many1(e, separator), null, null));
- else add(new Sequence.Singleton(e, null, null));
+ who.add(new Sequence.Constant.Empty());
+ if (manyOkay) who.add(new Sequence.Singleton(many1(e, separator), null, null));
+ else who.add(new Sequence.Singleton(e, null, null));
} else {
- add(new Sequence.RewritingSequence(null, new Element[] { e }, null, null));
+ who.add(new Sequence.RewritingSequence(null, new Element[] { e }, null, null));
if (this.separator==null) {
- add(new Sequence.Unwrap(new Element[] { e, Repeat.this }, null, null));
+ who.add(new Sequence.Unwrap(new Element[] { e, Repeat.this }, null, null));
} else {
- add(new Sequence.Unwrap(new Element[] { e, this.separator, Repeat.this }, new boolean[] { false, true, false }, null, null));
+ who.add(new Sequence.Unwrap(new Element[] { e, this.separator, Repeat.this }, new boolean[] { false, true, false }, null, null));
}
}
}
-
- static class MaximalSequence extends Sequence.Singleton {
- private final Element e;
- public String toString() { return e+"@"; }
- public Topology noFollow() { return e.toAtom(); }
- public MaximalSequence(Element e) {
- super(e, null, null);
- this.e = e;
- }
- }
- static class Maximal extends Union {
- public Maximal(final Element e) {
- super(e+"+", true);
- add(new MaximalSequence(e));
- }
- }
}
public Topology noFollow() { return null; }
Topology toAtom() {
- if (elements.length>1) throw new RuntimeException("cannot invoke toAtom() on a Sequence with " + elements.length + " elements: " + this);
- if (elements.length==0) return null;
+ if (elements.length!=1) throw new RuntimeException("cannot invoke toAtom() on a Sequence with " + elements.length + " elements: " + this);
return elements[0].toAtom();
}
Topology ret = null;
for(Sequence s : this) {
Topology a = s.toAtom();
- if (a==null) continue;
if (ret==null) ret = a.dup();
- else ret.add(a.dup());
+ else ret = ret.union(a.dup());
}
+ if (ret==null) throw new RuntimeException("confusion on " + this);
return ret;
}
if (matched) walk(a);
}
- if (e instanceof Repeat.MaximalSequence || e instanceof Repeat.Maximal)
- cs.remove(new Last<Tok>(cs.fresh(), c).walk(e));
- /*
if (e instanceof Sequence) {
Sequence s = (Sequence)e;
- if (s.noFollow() != null) cs.remove(s.noFollow());
+ if (s.noFollow() != null) cs.remove(s.noFollow().dup());
}
- */
+
if (c != null && e==me) {
c.follow.put(e, cs.dup());
c.eof.put(e, eof);
else if ("+".equals(head)) return Repeat.many1((Element)walk(tree.child(0)));
else if ("+/".equals(head)) return Repeat.many1((Element)walk(tree.child(0)), (Element)walk(tree.child(1)));
else if ("*/".equals(head)) return Repeat.many0((Element)walk(tree.child(0)), (Element)walk(tree.child(1)));
- else if ("**".equals(head)) return Repeat.maximal(Repeat.many0((Element)walk(tree.child(0))));
- else if ("++".equals(head)) return Repeat.maximal(Repeat.many1((Element)walk(tree.child(0))));
+ else if ("**".equals(head)) return Repeat.maximal0((Element)walk(tree.child(0)));
+ else if ("++".equals(head)) return Repeat.maximal1((Element)walk(tree.child(0)));
else if ("?".equals(head)) return Repeat.maybe((Element)walk(tree.child(0)));
else if ("&".equals(head))
return ((PreSequence)walk(tree,0)).and(new PreSequence((Element[])Reflection.lub((Object[])walk(tree, 1)), null).buildSequence(null, true, false));
+
+
+
// DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED
new Tree(null, "gram", new Tree[] { new Tree(null, null, new Tree[] { }),
new Tree(null, "grammar", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "::=", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "s", new Tree[] { })}),
new Tree(null, "nonTerminal", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "w", new Tree[] { }),
new Tree(null, "s", new Tree[] { })})})})})})}),
new Tree(null, "!::=", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "w", new Tree[] { })}),
- new Tree(null, null, new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "qprod", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, " ", new Tree[] { })})}),
- new Tree(null, "qprod", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "\n", new Tree[] { })})}),
- new Tree(null, "qprod", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "\r", new Tree[] { })})})})})}),
+ new Tree(null, null, new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "ps", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "[", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "range", new Tree[] { new Tree(null, " ", new Tree[] { })}),
+ new Tree(null, "range", new Tree[] { new Tree(null, "\r", new Tree[] { })}),
+ new Tree(null, "range", new Tree[] { new Tree(null, "\n", new Tree[] { })})})})})})})})}),
new Tree(null, "::=", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "w", new Tree[] { }),
new Tree(null, "o", new Tree[] { }),
new Tree(null, "r", new Tree[] { }),
+
+
+
public static class Grammar extends MetaGrammar {
private int anon = 0;
- private final Element ws = Repeat.maximal(Repeat.many0(nonTerminal("w")));
- public Grammar() {
- dropAll.add(ws);
- }
+ private final Element ws = Repeat.maximal0(nonTerminal("w"));
+ public Grammar() { dropAll.add(ws); }
public Object walk(Tree<String> tree) {
String head = tree.head();
if (tree.numChildren()==0) return super.walk(tree);
public void add(Topology<K> t, V v) { put(t, v); }
public void addAll(Topology<K> k, Iterable<V> vi) { putAll(k, vi); }
- public void putAll(Topology<K> k, Iterable<V> vi) { for(V v : vi) put(k, v); }
+ public void putAll(Topology<K> k, Iterable<V> vi) { if (vi!=null) for(V v : vi) put(k, v); }
public void put(Topology<K> t, V v) {
for(Topology<K> ht : h.keySet()) {
if (t.disjoint(ht)) continue;
TopologicalBag<K,V> ret = new TopologicalBag<K,V>();
for(Topology<K> ht : h.keySet()) {
if (ht.disjoint(t)) continue;
- ret.putAll(ht.intersect(t), h.get(ht));
+ Iterable<V> it = h.get(ht);
+ Topology<K> tk = ht.intersect(t);
+ ret.putAll(tk, it);
}
return ret;
}