c68120e8c6a63988a98986b5a9a041d6c1a73505
[sbp.git] / src / edu / berkeley / sbp / Repeat.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8 import java.lang.ref.*;
9
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 {
12
13     /** repeat zero or one times */
14     public  static Repeat maybe(Element e)              { return new Repeat(e, true, false, null, false); }
15     public  static Repeat maybe(Element e, Object tag)              { return new Repeat(e, true, false, null, false, tag); }
16     /** repeat zero or more times */
17     public  static Repeat many0(Element e)              { return new Repeat(e, true, true, null, false); }
18     public  static Repeat many0(Element e, Object tag)              { return new Repeat(e, true, true, null, false, tag); }
19     /** repeat zero or more times, separated by <tt>sep</tt> */
20     public  static Repeat many0(Element e, Element sep) { return new Repeat(e, true, true, sep, false); }
21     public  static Repeat many0(Element e, Element sep, Object tag) { return new Repeat(e, true, true, sep, false, tag); }
22     /** repeat one or more times */
23     public  static Repeat many1(Element e)              { return new Repeat(e, false, true, null, false); }
24     public  static Repeat many1(Element e, Object tag)              { return new Repeat(e, false, true, null, false, tag); }
25     /** repeat one or more times, separated by <tt>sep</tt> */
26     public  static Repeat many1(Element e, Element sep) { return new Repeat(e, false, true, sep, false); }
27     public  static Repeat many1(Element e, Element sep, Object tag) { return new Repeat(e, false, true, sep, false, tag); }
28
29     /** repeat zero or more times, matching a maximal sequence of atoms */
30     public  static Repeat maximal0(Element e)              { return new Repeat(e, true, true, null, true); }
31     public  static Repeat maximal0(Element e, Object tag)              { return new Repeat(e, true, true, null, true, tag); }
32     /** repeat one or more times, matching a maximal sequence of atoms */
33     public  static Repeat maximal1(Element e)              { return new Repeat(e, false, true, null, true); }
34     public  static Repeat maximal1(Element e, Object tag)              { return new Repeat(e, false, true, null, true, tag); }
35     /** repeat one or more times, separated by an atom <tt>sep</tt>, matching a maximal sequence */
36     public  static Repeat maximal1(Element e, Element sep) { return new Repeat(e, false, true, sep, true); }
37     public  static Repeat maximal1(Element e, Element sep, Object tag) { return new Repeat(e, false, true, sep, true, tag); }
38
39     private Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal) {
40         this(e, zeroOkay, manyOkay, separator, maximal, null); }
41     private Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag) {
42         super(e+(!manyOkay ? "?" : (zeroOkay ? (maximal ? "**" : "*") : (maximal ? "++" : "+")))+(separator==null?"":("/"+separator)), true);
43         if (maximal && zeroOkay && separator != null)
44             throw new RuntimeException("cannot create a maximal repetition of zero or more items with a separator (yet): " + this);
45         if (zeroOkay) {
46             add(new Sequence.Constant.Empty());
47             if (manyOkay) add(new Sequence.Singleton(many1(e, separator)));
48             else          add(new Sequence.Singleton(e));
49         } else {
50             add(new Sequence.RewritingSequence(tag, new Element[] { e }, null));
51             if (separator==null)
52                 add(new Sequence.Unwrap(new Element[] { e,                 Repeat.this }, tag));
53             else
54                 add(new Sequence.Unwrap(new Element[] { e, separator,      Repeat.this }, tag, new boolean[] { false, true, false }));
55         }
56         if (maximal) for(Sequence s : this) s.noFollow = separator==null ? e : separator;
57     }
58 }