X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FSequence.java;h=2530a211cf0a996591a4248f6ab83e447fa66579;hp=bb0ba4815a6212744ae9c0c27efed77172548ff3;hb=9b031db4cefc550c82a8bb6da3b061d3790c2cdc;hpb=4378d20232cd88fb2a57edc5a5114f952ab58a18 diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index bb0ba48..2530a21 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -32,30 +32,33 @@ public abstract class Sequence extends Element implements Iterable { return ret; } - /** the empty sequence (matches the empty string) */ - public static Sequence newEmptySequence() { return new Sequence.Constant.Empty(); } + /** create an empty sequence (matches the empty string) */ + public static Sequence create() { return new Sequence.Constant.Empty(); } - /** after matching the sequence, do not add anything to the output tree */ - public static Sequence newDropSequence(Element[] e) { return new Constant.Drop(e); } + /** create a sequence of one element */ + public static Sequence create(Element e) { return create(new Element[] { e }, 0); } + + /** create a sequence which drops the result of all but one of its element */ + public static Sequence create(Element[] e, int idx) { return new Singleton(e, idx); } /** after matching the sequence, insert a constant into the output tree */ public static Sequence newConstantSequence(Element[] e, Object o) { return new Constant(e, o); } - /** after matching the sequence, place the result of the idxth match in the output tree */ - public static Sequence newSingletonSequence(Element[] e, int idx) { return new Singleton(e, idx); } - public static Sequence newSingletonSequence(Element e) { return newSingletonSequence(new Element[] { e }, 0); } /** * after matching the sequence, create the specified output tree - * @param tag the tag for the output tree - * @param e the elements to match - * @param drops only elements of e whose corresponding boolean in drops - * is false will be included in the output tree + * @param tag the tag for the output tree + * @param e the elements to match + * @param drops only elements of e whose corresponding boolean in drops + * is false will be included in the output tree + * @param unwrap if true, all children of the last child (ie + * grandchildren) are promoted to children of this node; this is very useful for matching repetitions **/ - public static Sequence newRewritingSequence(Object tag, Element[] e, boolean[] drops) { - return new RewritingSequence(tag, e, drops); } - - public static Sequence newUnwrapSequence(Element[] e, Object tag, boolean[] drops) { return new Unwrap(e, tag, drops); } + public static Sequence create(Object tag, Element[] e, boolean[] drops, boolean unwrap) { + return unwrap + ? new Unwrap(e, tag, drops) + : new RewritingSequence(tag, e, drops); + } ////////////////////////////////////////////////////////////////////////////////