1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.*;
7 import java.lang.reflect.*;
8 import java.lang.ref.*;
10 /** currently this class exports only static methods to create repetitions; there are no public instance methods or constructors */
11 public class Repeat extends Union {
13 /** repeat zero or one times */
14 public static Repeat maybe(Element e) { return new Repeat(e, true, false); }
15 /** repeat zero or more times */
16 public static Repeat many0(Element e) { return new Repeat(e, true, true); }
17 /** repeat zero or more times, separated by <tt>sep</tt> */
18 public static Repeat many0(Element e, Element sep) { return new Repeat(e, true, true, sep); }
19 /** repeat one or more times */
20 public static Repeat many1(Element e) { return new Repeat(e, false, true); }
21 /** repeat one or more times, separated by <tt>sep</tt> */
22 public static Repeat many1(Element e, Element sep) { return new Repeat(e, false, true, sep); }
24 /** repeat zero or more times, matching a maximal sequence of atoms */
25 public static Repeat maximal0(Element e) { return new Repeat(e, true, true, null, true); }
26 /** repeat one or more times, matching a maximal sequence of atoms */
27 public static Repeat maximal1(Element e) { return new Repeat(e, false, true, null, true); }
28 /** repeat one or more times, separated by an atom <tt>sep</tt>, matching a maximal sequence */
29 public static Repeat maximal1(Element e, Element sep) { return new Repeat(e, false, true, sep, true); }
31 private Repeat(final Element e, boolean zeroOkay, boolean manyOkay) { this(e, zeroOkay, manyOkay, null); }
32 private Repeat(final Element e, boolean zeroOkay, boolean manyOkay, Element separator) { this(e, zeroOkay, manyOkay, separator, false); }
33 private Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal) {
34 super(e+(!manyOkay ? "?" : (zeroOkay ? (maximal ? "**" : "*") : (maximal ? "++" : "+")))+(separator==null?"":("/"+separator)), true);
35 if (maximal && zeroOkay && separator != null)
36 throw new RuntimeException("cannot create a maximal repetition of zero or more items with a separator (yet): " + this);
38 add(new Sequence.Constant.Empty());
39 if (manyOkay) add(new Sequence.Singleton(many1(e, separator)));
40 else add(new Sequence.Singleton(e));
42 add(new Sequence.RewritingSequence(null, new Element[] { e }));
44 add(new Sequence.Unwrap(new Element[] { e, Repeat.this }));
46 add(new Sequence.Unwrap(new Element[] { e, separator, Repeat.this }, new boolean[] { false, true, false }));
48 if (maximal) for(Sequence s : this) s.noFollow = separator==null ? e : separator;