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 /* FIXME make private again */ public class Repeat extends Union {
13 public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, Object tag) {
14 this(e, zeroOkay, manyOkay, separator, false, tag);
16 protected Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag) {
17 super(e+(!manyOkay ? "?" : (zeroOkay ? (maximal ? "**" : "*") : (maximal ? "++" : "+")))+(separator==null?"":("/"+separator)), true);
18 if (maximal && zeroOkay && separator != null)
19 throw new RuntimeException("cannot create a maximal repetition of zero or more items with a separator (yet): " + this);
21 add(new Sequence.Constant.Empty());
22 if (manyOkay) add(new Sequence.Singleton(Sequence.many1(e, separator)));
23 else add(new Sequence.Singleton(e));
25 add(new Sequence.RewritingSequence(tag, new Element[] { e }, null));
27 add(new Sequence.Unwrap(new Element[] { e, Repeat.this }, tag));
29 add(new Sequence.Unwrap(new Element[] { e, separator, Repeat.this }, tag, new boolean[] { false, true, false }));
33 for(Sequence s : this)
34 s.follow = new edu.berkeley.sbp.misc.MetaGrammar.Invert(new edu.berkeley.sbp.misc.MetaGrammar.Infer(separator==null ? e : separator));
37 public static class Maximal extends Repeat {
38 public Maximal(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, Object tag) {
39 super(e, zeroOkay, manyOkay, separator, true, tag);