convered maximal to use character lookahead
[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); }
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); }
29
30     // Instance //////////////////////////////////////////////////////////////////////////////
31     
32     final boolean zeroOkay, manyOkay;
33     final Element e;
34     final Element separator;
35     final boolean maximal;
36
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);
41         this.e = e;
42         this.maximal = maximal;
43         this.zeroOkay = zeroOkay;
44         this.manyOkay = manyOkay;
45         this.separator = separator;
46         Union who = this;
47         if (maximal) {
48             who = new Union(this+"++");
49             add(new Sequence.Singleton(who, null, null) {
50                     public Topology noFollow() { return separator==null ? e.toAtom() : separator.toAtom(); }
51                 });
52         }
53         if (zeroOkay) {
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));
57         } else {
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));
61             } else {
62                 who.add(new Sequence.Unwrap(new Element[] { e, this.separator, Repeat.this }, new boolean[] { false, true, false }, null, null));
63             }
64         }
65     }
66 }