X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FSequence.java;h=1c76aefadda764a09bf478877d764ecb108c0675;hp=c41be4ff9d62da6ba0537477b13c3db22aaed42d;hb=584cef55d8811e3215858fde22e708d2a3d1cf70;hpb=2afdfe14e78fa0597186614937c679a09d74ecdf diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index c41be4f..1c76aef 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -34,10 +34,18 @@ public abstract class Sequence implements Iterable, SequenceOrElement { 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 which) { return new Singleton(e, which); } + public static Sequence create(Element[] e, int which) { + return new Singleton(e, which); } /** create a sequence which always evaluates to a constant result */ - public static Sequence create(Element[] e, Object result) { return new Constant(e, result); } + public static Sequence create(Object result, Element[] e) { + return new RewritingSequence(result, e, trues(e.length)); } + + private static boolean[] trues(int length) { + boolean[] ret = new boolean[length]; + for(int i=0; i, SequenceOrElement { * @param e the elements to match * @param drop only elements of e whose corresponding boolean in drops * is false will be included in the output tree - * @param foster if true, all children of the last child (ie - * grandchildren) are promoted to children of this - * node; this is very useful for matching repetitions + * @param lifts which (if any) child trees to lift **/ - public static Sequence create(Object head, Element[] e, boolean[] drop, boolean foster) { - return foster - ? new Unwrap(e, head, drop) - : new RewritingSequence(head, e, drop); - } - public static Sequence createLeft(Object head, Element[] e, boolean[] drop, boolean foster) { - return foster - ? new UnwrapLeft(e, head, drop) - : new RewritingSequence(head, e, drop); + public static Sequence create(Object head, Element[] e, boolean[] drop) { + return create(head, e, drop, new boolean[e.length]); } + public static Sequence create(Object head, Element[] e, boolean[] drop, boolean[] lifts) { + if (lifts==null) lifts = new boolean[e.length]; + return new RewritingSequence(head, e, drop, lifts); } /** return a new sequence identical to this one, but with a positive conjunct s */ @@ -240,66 +242,31 @@ public abstract class Sequence implements Iterable, SequenceOrElement { } } - static class Unwrap extends Sequence { - private boolean[] drops; - private final Object tag; - public Unwrap(Element[] e, Object tag) { this(e, tag, null); } - public Unwrap(Element[] e, Object tag, boolean[] drops) { super(e); this.drops = drops; this.tag = tag; } - Sequence _clone() { return new Unwrap(elements, tag, drops); } - public Forest postReduce(Input.Region loc, Forest[] args, Position p) { - for(int i=0; i[] args2 = new Forest[count]; - int j = 0; - for(int i=0; i Forest postReduce(Input.Region loc, Forest[] args, Position p) { - for(int i=0; i[] args2 = new Forest[count]; - int j = 0; - for(int i=0; i Forest postReduce(Input.Region loc, Forest[] args, Position p) { - Forest[] args2 = new Forest[count]; + Forest[] args2 = new Forest[lifts.length]; int j = 0; for(int i=0; i, SequenceOrElement { return sb; } Forest epsilonForm(Input.Region loc, Grammar cache) { - return Forest.create(loc, tag, new Forest[0], false); + return Forest.create(loc, tag, new Forest[0], lifts); } } - }