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); }
- /** repeat zero or more times */
- public static Repeat many0(Element e) { return new Repeat(e, true, true); }
- /** 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); }
- /** repeat one or more times */
- 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); }
- /** 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) { 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(); }
- });
+ public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, Object tag) {
+ this(e, zeroOkay, manyOkay, null, false, tag); }
+ public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, Object tag) {
+ this(e, zeroOkay, manyOkay, separator, false, tag); }
+ protected 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 (zeroOkay && !manyOkay) {
+ add(new Sequence.Constant.Empty());
+ add(new Sequence.Singleton(e));
+ return;
}
if (zeroOkay) {
- 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));
+ add(new Sequence.RewritingSequence(tag, new Element[] { }, null));
+ //add(new Sequence.Constant.Empty());
+ // FUGLY
+ add(new Sequence.Singleton(Sequence.many1(e, separator, tag)));
} else {
- who.add(new Sequence.RewritingSequence(null, new Element[] { e }, null, null));
- if (this.separator==null) {
- who.add(new Sequence.Unwrap(new Element[] { e, Repeat.this }, null, null));
- } else {
- who.add(new Sequence.Unwrap(new Element[] { e, this.separator, Repeat.this }, new boolean[] { false, true, false }, null, null));
- }
+ add(new Sequence.RewritingSequence(tag, new Element[] { e }, null));
+ if (separator==null)
+ add(new Sequence.Unwrap(new Element[] { e, Repeat.this }, tag));
+ else
+ add(new Sequence.Unwrap(new Element[] { e, separator, Repeat.this }, tag, new boolean[] { false, true, false }));
+ }
+ }
+
+ public static class Maximal extends Repeat {
+ public Maximal(final Element e, boolean zeroOkay, boolean manyOkay, final Atom separator, Object tag) {
+ super(e, zeroOkay, manyOkay, separator, true, tag);
+ if (zeroOkay && separator != null)
+ throw new RuntimeException("cannot create a maximal repetition of zero or more items with a separator (yet): " + this);
+ for(Sequence s : this)
+ s.follow = new Invert(separator);
}
+ public Maximal(final Atom e, boolean zeroOkay, boolean manyOkay, Object tag) {
+ super(e, zeroOkay, manyOkay, null, true, tag);
+ for(Sequence s : this)
+ s.follow = new Invert(e);
+ }
+ }
+
+
+ /** an atom which tracks the inverse of some other atom */
+ private 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; }
}
}