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