c85bfc6497dd140ab22b891e0e00a37e47a8c151
[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 class Repeat extends Union {
12
13     public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, Object tag) {
14         this(e, zeroOkay, manyOkay, null, false, tag); }
15     public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, Object tag) {
16         this(e, zeroOkay, manyOkay, separator, false, tag); }
17     protected Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag) {
18         super(e+(!manyOkay ? "?" : (zeroOkay ? (maximal ? "**" : "*") : (maximal ? "++" : "+")))+(separator==null?"":("/"+separator)), true);
19         if (zeroOkay && !manyOkay) {
20             add(new Sequence.Constant.Empty());
21             add(new Sequence.Singleton(e));
22             return;
23         }
24         if (zeroOkay) {
25             add(new Sequence.RewritingSequence(tag, new Element[] { }, null));
26             //add(new Sequence.Constant.Empty());
27             // FUGLY
28             add(new Sequence.Singleton(Sequence.many1(e, separator, tag)));
29         } else {
30             add(new Sequence.RewritingSequence(tag, new Element[] { e }, null));
31             if (separator==null)
32                 add(new Sequence.Unwrap(new Element[] { e,                 Repeat.this }, tag));
33             else
34                 add(new Sequence.Unwrap(new Element[] { e, separator,      Repeat.this }, tag, new boolean[] { false, true, false }));
35         }
36     }
37
38     public static class Maximal extends Repeat {
39         public Maximal(final Element e, boolean zeroOkay, boolean manyOkay, final Atom separator, Object tag) {
40             super(e, zeroOkay, manyOkay, separator, true, tag);
41             if (zeroOkay && separator != null)
42                 throw new RuntimeException("cannot create a maximal repetition of zero or more items with a separator (yet): " + this);
43             for(Sequence s : this)
44                 s.follow = new Invert(separator);
45         }
46         public Maximal(final Atom e, boolean zeroOkay, boolean manyOkay, Object tag) {
47             super(e, zeroOkay, manyOkay, null, true, tag);
48             for(Sequence s : this)
49                 s.follow = new Invert(e);
50         }
51     }
52
53
54     /** an atom which tracks the inverse of some other atom */
55     private static class Invert<T extends Input> extends Atom<T> {
56         private final Atom<T> a;
57         public Invert(Atom<T> a) { this.a = a; }
58         public Topology<T> top() { return a.complement(); }
59         public String toString() { return "~"+a; }
60     }
61 }