1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp.meta;
4 import edu.berkeley.sbp.util.*;
5 import edu.berkeley.sbp.*;
8 import java.lang.reflect.*;
9 import java.lang.ref.*;
11 /** currently this class exports only static methods to create repetitions; there are no public instance methods or constructors */
12 public class Repeat extends Union {
14 public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, Object tag) {
15 this(e, zeroOkay, manyOkay, null, false, tag); }
16 public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, Object tag) {
17 this(e, zeroOkay, manyOkay, separator, false, tag); }
18 protected Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag) {
19 this(e, zeroOkay, manyOkay, separator, maximal, tag, null); }
20 protected Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag, Atom follow) {
21 super(e+(!manyOkay ? "?" : (zeroOkay ? (maximal ? "**" : "*") : (maximal ? "++" : "+")))+(separator==null?"":("/"+separator)), true);
22 if (zeroOkay && !manyOkay) {
23 add(Sequence.create().followedBy(follow));
24 add(Sequence.create(e).followedBy(follow));
28 add(Sequence.create(tag, new Element[] { }, null, false).followedBy(follow));
29 //add(new Sequence.Constant.Empty());
31 add(Sequence.create(many1(e, separator, tag)).followedBy(follow));
33 add(Sequence.create(tag, new Element[] { e }, null, false).followedBy(follow));
35 add(Sequence.create(tag, new Element[] { e, Repeat.this }, new boolean[] { false, false }, true).followedBy(follow));
37 add(Sequence.create(tag, new Element[] { e, separator, Repeat.this }, new boolean[] { false, true, false }, true).followedBy(follow));
41 public static class Maximal extends Repeat {
42 public Maximal(final Element e, boolean zeroOkay, boolean manyOkay, final Atom separator, Object tag) {
43 super(e, zeroOkay, manyOkay, separator, true, tag, (Atom)separator.complement());
44 if (zeroOkay && separator != null)
45 throw new RuntimeException("cannot create a maximal repetition of zero or more items with a separator (yet): " + this);
47 public Maximal(final Atom e, boolean zeroOkay, boolean manyOkay, Object tag) {
48 super(e, zeroOkay, manyOkay, null, true, tag, (Atom)e.complement());
52 /** repeat zero or one times */
53 public static Element maybe(Element e) { return new Repeat(e, true, false, null, null); }
54 public static Element maybe(Element e, Object tag) { return new Repeat(e, true, false, null, tag); }
55 /** repeat zero or more times */
56 public static Element many0(Element e) { return new Repeat(e, true, true, null, null); }
57 public static Element many0(Element e, Object tag) { return new Repeat(e, true, true, null, tag); }
58 /** repeat zero or more times, separated by <tt>sep</tt> */
59 public static Element many0(Element e, Element sep) { return new Repeat(e, true, true, sep, null); }
60 public static Element many0(Element e, Element sep, Object tag) { return new Repeat(e, true, true, sep, tag); }
61 /** repeat one or more times */
62 public static Element many1(Element e) { return new Repeat(e, false, true, null, null); }
63 public static Element many1(Element e, Object tag) { return new Repeat(e, false, true, null, tag); }
64 /** repeat one or more times, separated by <tt>sep</tt> */
65 public static Element many1(Element e, Element sep) { return new Repeat(e, false, true, sep, null); }
66 public static Element many1(Element e, Element sep, Object tag) { return new Repeat(e, false, true, sep, tag); }
68 /** repeat zero or more times, matching a maximal sequence of atoms */
69 public static Element maximal0(Atom e) { return new Repeat.Maximal(e, true, true, null); }
70 public static Element maximal0(Atom e, Object tag) { return new Repeat.Maximal(e, true, true, tag); }
71 /** repeat one or more times, matching a maximal sequence of atoms */
72 public static Element maximal1(Atom e) { return new Repeat.Maximal(e, false, true, null); }
73 public static Element maximal1(Atom e, Object tag) { return new Repeat.Maximal(e, false, true, tag); }
74 /** repeat one or more times, separated by an atom <tt>sep</tt>, matching a maximal sequence */
75 public static Element maximal1(Element e, Atom sep) { return new Repeat.Maximal(e, false, true, sep, null); }
76 public static Element maximal1(Element e, Atom sep, Object tag) { return new Repeat.Maximal(e, false, true, sep, tag); }
78 public static Element repeatMaximal(Atom e, boolean zero, boolean many, Object tag) {
79 return new Repeat.Maximal(e, zero, many, tag); }
80 public static Element repeatMaximal(Element e, boolean zero, boolean many, Atom sep, Object tag) {
81 return new Repeat.Maximal(e, zero, many, sep, tag); }
82 public static Element repeat(Element e, boolean zero, boolean many, Object tag) {
83 return new Repeat(e, zero, many, tag); }
84 public static Element repeat(Element e, boolean zero, boolean many, Element sep, Object tag) {
85 return new Repeat(e, zero, many, sep, tag); }