add messages noting that TestAstGenerator and GrammarAST.emitCode() do not work yet
[sbp.git] / src / edu / berkeley / sbp / meta / Repeat.java
1 // Copyright 2007 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp.meta;
4 import edu.berkeley.sbp.util.*;
5 import edu.berkeley.sbp.*;
6 import java.io.*;
7 import java.util.*;
8 import java.lang.reflect.*;
9 import java.lang.ref.*;
10
11 /**
12  *  Currently this class exports only static methods to create repetitions;
13  *  there are no public instance methods or constructors
14  */
15 class Repeat extends Union {
16
17     public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, Object tag) {
18         this(e, zeroOkay, manyOkay, null, false, tag); }
19     public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, Object tag) {
20         this(e, zeroOkay, manyOkay, separator, false, tag); }
21     protected Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag) {
22         this(e, zeroOkay, manyOkay, separator, maximal, tag, null); }
23     protected Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag, Atom follow) {
24         super(e
25               +(!manyOkay
26                 ? "?"
27                 : (zeroOkay
28                    ? (maximal ? "**" : "*")
29                    : (maximal ? "++" : "+")))
30               +(separator==null
31                 ? ""
32                 : ("/"+separator)),
33               true);
34         if (follow != null) {
35             Sequence s = Sequence.create(new Repeat(e, zeroOkay, manyOkay,
36                                                     separator, maximal, tag, null)).followedBy(follow);
37             add(s);
38             return;
39         }
40         if (zeroOkay) 
41             add(Sequence.create(tag, new Element[] {   }, null).followedBy(follow));
42         if (!(zeroOkay && manyOkay))
43             add(Sequence.create(tag, new Element[] { e }, null).followedBy(follow));
44
45         // FEATURE: stringify ~[]* as ...
46         if (zeroOkay && manyOkay && separator!=null) {
47             add(Sequence.create(many1(e, separator, tag)).followedBy(follow));
48
49         } else if (manyOkay) {
50             if (separator==null)
51                 add(Sequence.create(tag,
52                                     new Element[] { Repeat.this,    e },
53                                     new boolean[] { false, false },
54                                     new boolean[] { true, false }));
55             else
56                 add(Sequence.create(tag,
57                                     new Element[] { Repeat.this, separator, e },
58                                     new boolean[] { false, true,  false },
59                                     new boolean[] { true,  false, false }
60                                     ));
61
62         }
63     }
64
65     public static class Maximal extends Repeat {
66         public Maximal(final Element e, boolean zeroOkay, boolean manyOkay, final Atom separator, Object tag) {
67             super(e, zeroOkay, manyOkay, separator, true, tag, (Atom)separator.complement());
68             if (zeroOkay && separator != null)
69                 throw new RuntimeException("cannot create a maximal repetition of zero or more items with a separator (yet): " + this);
70         }
71         public Maximal(final Atom e, boolean zeroOkay, boolean manyOkay, Object tag) {
72             super(e, zeroOkay, manyOkay, null, true, tag, (Atom)e.complement());
73         }
74     }
75
76     /** repeat zero or one times */
77     public  static Element maybe(Element e)                             { return new Repeat(e, true, false, null, null); }
78     public  static Element maybe(Element e, Object tag)                 { return new Repeat(e, true, false, null, tag); }
79     /** repeat zero or more times */
80     public  static Element many0(Element e)                             { return new Repeat(e, true, true, null, null); }
81     public  static Element many0(Element e, Object tag)                 { return new Repeat(e, true, true, null, tag); }
82     /** repeat zero or more times, separated by <tt>sep</tt> */
83     public  static Element many0(Element e, Element sep)                { return new Repeat(e, true, true, sep, null); }
84     public  static Element many0(Element e, Element sep, Object tag)    { return new Repeat(e, true, true, sep, tag); }
85     /** repeat one or more times */
86     public  static Element many1(Element e)                             { return new Repeat(e, false, true, null, null); }
87     public  static Element many1(Element e, Object tag)                 { return new Repeat(e, false, true, null, tag); }
88     /** repeat one or more times, separated by <tt>sep</tt> */
89     public  static Element many1(Element e, Element sep)                { return new Repeat(e, false, true, sep, null); }
90     public  static Element many1(Element e, Element sep, Object tag)    { return new Repeat(e, false, true, sep, tag); }
91
92     /** repeat zero or more times, matching a maximal sequence of atoms */
93     public  static Element maximal0(Atom e)                             { return new Repeat.Maximal(e, true, true, null); }
94     public  static Element maximal0(Atom e, Object tag)                 { return new Repeat.Maximal(e, true, true, tag); }
95     /** repeat one or more times, matching a maximal sequence of atoms */
96     public  static Element maximal1(Atom e)                             { return new Repeat.Maximal(e, false, true, null); }
97     public  static Element maximal1(Atom e, Object tag)                 { return new Repeat.Maximal(e, false, true, tag); }
98     /** repeat one or more times, separated by an atom <tt>sep</tt>, matching a maximal sequence */
99     public  static Element maximal1(Element e, Atom sep)                { return new Repeat.Maximal(e, false, true, sep, null); }
100     public  static Element maximal1(Element e, Atom sep, Object tag)    { return new Repeat.Maximal(e, false, true, sep, tag); }
101
102     public  static Element repeatMaximal(Atom e, boolean zero, boolean many, Object tag) {
103         return new Repeat.Maximal(e, zero, many, tag); }
104     public  static Element repeatMaximal(Element e, boolean zero, boolean many, Atom sep, Object tag) {
105         return new Repeat.Maximal(e, zero, many, sep, tag); }
106     public  static Element repeat(Element e, boolean zero, boolean many, Object tag) {
107         return new Repeat(e, zero, many, tag); }
108     public  static Element repeat(Element e, boolean zero, boolean many, Element sep, Object tag) {
109         return new Repeat(e, zero, many, sep, tag); }
110 }