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); }
23 /** repeat zero or more times, matching a maximal sequence of atoms */
24 public static Repeat maximal0(Element e) { return new Repeat(e, true, true, null, true); }
25 /** repeat one or more times, matching a maximal sequence of atoms */
26 public static Repeat maximal1(Element e) { return new Repeat(e, false, true, null, true); }
27 /** repeat one or more times, separated by <tt>sep</tt>, matching a maximal sequence of atoms */
28 public static Repeat maximal1(Element e, Element sep) { return new Repeat(e, false, true, sep, true); }
30 // Instance //////////////////////////////////////////////////////////////////////////////
32 final boolean zeroOkay, manyOkay;
34 final Element separator;
35 final boolean maximal;
37 Repeat(final Element e, boolean zeroOkay, boolean manyOkay) { this(e, zeroOkay, manyOkay, null); }
38 Repeat(final Element e, boolean zeroOkay, boolean manyOkay, Element separator) { this(e, zeroOkay, manyOkay, separator, false); }
39 Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal) {
40 super(e+(!manyOkay ? "?" : (zeroOkay ? "*" : "+"))+(separator==null?"":("/"+separator.toString())), true);
42 this.maximal = maximal;
43 this.zeroOkay = zeroOkay;
44 this.manyOkay = manyOkay;
45 this.separator = separator;
48 who = new Union(this+"++");
49 add(new Sequence.Singleton(who, null, null) {
50 public Topology noFollow() { return separator==null ? e.toAtom() : separator.toAtom(); }
54 who.add(new Sequence.Constant.Empty());
55 if (manyOkay) who.add(new Sequence.Singleton(many1(e, separator), null, null));
56 else who.add(new Sequence.Singleton(e, null, null));
58 who.add(new Sequence.RewritingSequence(null, new Element[] { e }, null, null));
59 if (this.separator==null) {
60 who.add(new Sequence.Unwrap(new Element[] { e, Repeat.this }, null, null));
62 who.add(new Sequence.Unwrap(new Element[] { e, this.separator, Repeat.this }, new boolean[] { false, true, false }, null, null));