X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmeta%2FRepeat.java;h=800639b14f3a2afb7ea33b804be596561a7d2df1;hp=f21e8209b6b58d06592caae54f00ca1d38b71571;hb=HEAD;hpb=e6be1f03f79b16cd410991d7e64cfa7beab6de59 diff --git a/src/edu/berkeley/sbp/meta/Repeat.java b/src/edu/berkeley/sbp/meta/Repeat.java index f21e820..800639b 100644 --- a/src/edu/berkeley/sbp/meta/Repeat.java +++ b/src/edu/berkeley/sbp/meta/Repeat.java @@ -1,3 +1,5 @@ +// Copyright 2007 all rights reserved; see LICENSE file for BSD-style license + package edu.berkeley.sbp.meta; import edu.berkeley.sbp.util.*; import edu.berkeley.sbp.*; @@ -6,8 +8,11 @@ import java.util.*; import java.lang.reflect.*; import java.lang.ref.*; -/** currently this class exports only static methods to create repetitions; there are no public instance methods or constructors */ -public class Repeat extends Union { +/** + * Currently this class exports only static methods to create repetitions; + * there are no public instance methods or constructors + */ +class Repeat extends Union { public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, Object tag) { this(e, zeroOkay, manyOkay, null, false, tag); } @@ -16,23 +21,44 @@ public class Repeat extends Union { protected Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag) { this(e, zeroOkay, manyOkay, separator, maximal, tag, null); } protected Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag, Atom follow) { - super(e+(!manyOkay ? "?" : (zeroOkay ? (maximal ? "**" : "*") : (maximal ? "++" : "+")))+(separator==null?"":("/"+separator)), true); - if (zeroOkay && !manyOkay) { - add(Sequence.empty().followedBy(follow)); - add(Sequence.singleton(e).followedBy(follow)); + super(e + +(!manyOkay + ? "?" + : (zeroOkay + ? (maximal ? "**" : "*") + : (maximal ? "++" : "+"))) + +(separator==null + ? "" + : ("/"+separator)), + true); + if (follow != null) { + Sequence s = Sequence.create(new Repeat(e, zeroOkay, manyOkay, + separator, maximal, tag, null)).followedBy(follow); + add(s); return; } - if (zeroOkay) { - add(Sequence.rewritingSequence(tag, new Element[] { }, null).followedBy(follow)); - //add(new Sequence.Constant.Empty()); - // FUGLY - add(Sequence.singleton(many1(e, separator, tag)).followedBy(follow)); - } else { - add(Sequence.rewritingSequence(tag, new Element[] { e }, null).followedBy(follow)); + if (zeroOkay) + add(Sequence.create(tag, new Element[] { }, null).followedBy(follow)); + if (!(zeroOkay && manyOkay)) + add(Sequence.create(tag, new Element[] { e }, null).followedBy(follow)); + + // FEATURE: stringify ~[]* as ... + if (zeroOkay && manyOkay && separator!=null) { + add(Sequence.create(many1(e, separator, tag)).followedBy(follow)); + + } else if (manyOkay) { if (separator==null) - add(Sequence.unwrap(new Element[] { e, Repeat.this }, tag, new boolean[] { false, false }).followedBy(follow)); + add(Sequence.create(tag, + new Element[] { Repeat.this, e }, + new boolean[] { false, false }, + new boolean[] { true, false })); else - add(Sequence.unwrap(new Element[] { e, separator, Repeat.this }, tag, new boolean[] { false, true, false }).followedBy(follow)); + add(Sequence.create(tag, + new Element[] { Repeat.this, separator, e }, + new boolean[] { false, true, false }, + new boolean[] { true, false, false } + )); + } }