checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / Repeat.java
1 package edu.berkeley.sbp.meta;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import java.io.*;
5 import java.util.*;
6 import java.lang.reflect.*;
7 import java.lang.ref.*;
8
9 /** currently this class exports only static methods to create repetitions; there are no public instance methods or constructors */
10 public class Repeat extends Union {
11
12     public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, Object tag) {
13         this(e, zeroOkay, manyOkay, null, false, tag); }
14     public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, Object tag) {
15         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         this(e, zeroOkay, manyOkay, separator, maximal, tag, null); }
18     protected Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag, Atom follow) {
19         super(e+(!manyOkay ? "?" : (zeroOkay ? (maximal ? "**" : "*") : (maximal ? "++" : "+")))+(separator==null?"":("/"+separator)), true);
20         if (zeroOkay && !manyOkay) {
21             add(Sequence.empty().followedBy(follow));
22             add(Sequence.singleton(e).followedBy(follow));
23             return;
24         }
25         if (zeroOkay) {
26             add(Sequence.rewritingSequence(tag, new Element[] { }, null).followedBy(follow));
27             //add(new Sequence.Constant.Empty());
28             // FUGLY
29             add(Sequence.singleton(many1(e, separator, tag)).followedBy(follow));
30         } else {
31             add(Sequence.rewritingSequence(tag, new Element[] { e }, null).followedBy(follow));
32             if (separator==null)
33                 add(Sequence.unwrap(new Element[] { e,                 Repeat.this }, tag, new boolean[] { false, false }).followedBy(follow));
34             else
35                 add(Sequence.unwrap(new Element[] { e, separator,      Repeat.this }, tag, new boolean[] { false, true, false }).followedBy(follow));
36         }
37     }
38
39     public static class Maximal extends Repeat {
40         public Maximal(final Element e, boolean zeroOkay, boolean manyOkay, final Atom separator, Object tag) {
41             super(e, zeroOkay, manyOkay, separator, true, tag, (Atom)separator.complement());
42             if (zeroOkay && separator != null)
43                 throw new RuntimeException("cannot create a maximal repetition of zero or more items with a separator (yet): " + this);
44         }
45         public Maximal(final Atom e, boolean zeroOkay, boolean manyOkay, Object tag) {
46             super(e, zeroOkay, manyOkay, null, true, tag, (Atom)e.complement());
47         }
48     }
49
50     /** repeat zero or one times */
51     public  static Element maybe(Element e)                             { return new Repeat(e, true, false, null, null); }
52     public  static Element maybe(Element e, Object tag)                 { return new Repeat(e, true, false, null, tag); }
53     /** repeat zero or more times */
54     public  static Element many0(Element e)                             { return new Repeat(e, true, true, null, null); }
55     public  static Element many0(Element e, Object tag)                 { return new Repeat(e, true, true, null, tag); }
56     /** repeat zero or more times, separated by <tt>sep</tt> */
57     public  static Element many0(Element e, Element sep)                { return new Repeat(e, true, true, sep, null); }
58     public  static Element many0(Element e, Element sep, Object tag)    { return new Repeat(e, true, true, sep, tag); }
59     /** repeat one or more times */
60     public  static Element many1(Element e)                             { return new Repeat(e, false, true, null, null); }
61     public  static Element many1(Element e, Object tag)                 { return new Repeat(e, false, true, null, tag); }
62     /** repeat one or more times, separated by <tt>sep</tt> */
63     public  static Element many1(Element e, Element sep)                { return new Repeat(e, false, true, sep, null); }
64     public  static Element many1(Element e, Element sep, Object tag)    { return new Repeat(e, false, true, sep, tag); }
65
66     /** repeat zero or more times, matching a maximal sequence of atoms */
67     public  static Element maximal0(Atom e)                             { return new Repeat.Maximal(e, true, true, null); }
68     public  static Element maximal0(Atom e, Object tag)                 { return new Repeat.Maximal(e, true, true, tag); }
69     /** repeat one or more times, matching a maximal sequence of atoms */
70     public  static Element maximal1(Atom e)                             { return new Repeat.Maximal(e, false, true, null); }
71     public  static Element maximal1(Atom e, Object tag)                 { return new Repeat.Maximal(e, false, true, tag); }
72     /** repeat one or more times, separated by an atom <tt>sep</tt>, matching a maximal sequence */
73     public  static Element maximal1(Element e, Atom sep)                { return new Repeat.Maximal(e, false, true, sep, null); }
74     public  static Element maximal1(Element e, Atom sep, Object tag)    { return new Repeat.Maximal(e, false, true, sep, tag); }
75
76     public  static Element repeatMaximal(Atom e, boolean zero, boolean many, Object tag) {
77         return new Repeat.Maximal(e, zero, many, tag); }
78     public  static Element repeatMaximal(Element e, boolean zero, boolean many, Atom sep, Object tag) {
79         return new Repeat.Maximal(e, zero, many, sep, tag); }
80     public  static Element repeat(Element e, boolean zero, boolean many, Object tag) {
81         return new Repeat(e, zero, many, tag); }
82     public  static Element repeat(Element e, boolean zero, boolean many, Element sep, Object tag) {
83         return new Repeat(e, zero, many, sep, tag); }
84 }