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 <tt>idx</tt>th 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 <tt>e</tt> whose corresponding <tt>boolean</tt> in <tt>drops</tt>
- * is <i>false</i> 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 <tt>e</tt> whose corresponding <tt>boolean</tt> in <tt>drops</tt>
+ * is <i>false</i> 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);
+ }
////////////////////////////////////////////////////////////////////////////////
}
private static Union epsilon = new Union("()");
- static { epsilon.add(Sequence.newEmptySequence()); }
+ static { epsilon.add(Sequence.create()); }
public Topology<Atom<Character>> unwrap() { return this; }
public Topology<Atom<Character>> empty() { return new CharAtom(); }
public Object repeatTag() { return null; }
public Sequence createSequence(Production p) {
if (p.tag==null) return null;
- return Sequence.newRewritingSequence(p.tag, p.elements, p.drops);
+ return Sequence.create(p.tag, p.elements, p.drops, false);
}
}
group[j].build(cx, u2, cnt);
}
if (sequences.length==1) break;
- Sequence seq = Sequence.newSingletonSequence(u2);
+ Sequence seq = Sequence.create(u2);
for(Sequence s : bad2) seq = seq.not(s);
u.add(seq);
- bad2.add(Sequence.newSingletonSequence(u2));
+ bad2.add(Sequence.create(u2));
}
}
}
HashSet<Sequence> bad2 = new HashSet<Sequence>();
Union urep = new Union(null, false);
- urep.add(Sequence.newEmptySequence());
+ urep.add(Sequence.create());
if (sep != null)
- urep.add(Sequence.newSingletonSequence(new Element[] { cx.get(sep), u }, 1));
+ urep.add(Sequence.create(new Element[] { cx.get(sep), u }, 1));
else
- urep.add(Sequence.newSingletonSequence(new Element[] { u }, 0));
+ urep.add(Sequence.create(new Element[] { u }, 0));
for(int i=0; i<sequences.length; i++) {
Seq[] group = sequences[i];
for(int j=0; j<group.length; j++) {
Union u3 = new Union(null, false);
group[j].build(cx, u3, this);
- Sequence s = Sequence.newUnwrapSequence(new Element[] { u3, urep },
- cx.rm.repeatTag(),
- new boolean[] { false, false });
+ Sequence s = Sequence.create(cx.rm.repeatTag(),
+ new Element[] { u3, urep },
+ new boolean[] { false, false },
+ true);
u2.add(s);
}
if (sequences.length==1) break;
- Sequence seq = Sequence.newSingletonSequence(u2);
+ Sequence seq = Sequence.create(u2);
for(Sequence s : bad2) seq = seq.not(s);
u.add(seq);
- bad2.add(Sequence.newSingletonSequence(u2));
+ bad2.add(Sequence.create(u2));
}
}
}
for(int i=0; i<els.length; i++)
if (!drops[i])
if (idx==-1) idx = i;
- else throw new Error("multiple non-dropped elements in sequence: " + Sequence.newDropSequence(els));
- if (idx != -1) ret = Sequence.newSingletonSequence(els, idx);
- else ret = Sequence.newDropSequence(els);
+ else throw new Error("multiple non-dropped elements in sequence: " + Sequence.newConstantSequence(els, null));
+ if (idx != -1) ret = Sequence.create(els, idx);
+ else ret = Sequence.newConstantSequence(els, null);
}
if (this.follow != null)
ret = ret.followedBy(this.follow.toAtom(cx));
public static @bind.as("()") ElementNode epsilon() { return new Constant(epsilon); }
private static Union epsilon = new Union("()");
- static { epsilon.add(Sequence.newEmptySequence()); }
+ static { epsilon.add(Sequence.create()); }
public static class NonTerminalReferenceNode extends ElementNode {
public String nonTerminal;
Union u = new Union(null, false);
Sequence s = body.build(cx, u, null);
Union u2 = new Union(null, false);
- u2.add(Sequence.newSingletonSequence(new Element[] {
+ u2.add(Sequence.create(new Element[] {
CharAtom.leftBrace,
cx.get("ws"),
u,
*/
if (_bindable.isAnnotationPresent(bind.raw.class))
- return Sequence.newRewritingSequence(new RawBindingFunctor(tag(), _bindable.createBinding()), elements, drops);
+ return Sequence.create(new RawBindingFunctor(tag(), _bindable.createBinding()), elements, drops, false);
int[] map = buildSequence(_bindable);
- return Sequence.newRewritingSequence(new BindingFunctor(tag(), _bindable.createBinding()), elements, drops);
+ return Sequence.create(new BindingFunctor(tag(), _bindable.createBinding()), elements, drops, false);
}
}
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.newEmptySequence().followedBy(follow));
- add(Sequence.newSingletonSequence(e).followedBy(follow));
+ add(Sequence.create().followedBy(follow));
+ add(Sequence.create(e).followedBy(follow));
return;
}
if (zeroOkay) {
- add(Sequence.newRewritingSequence(tag, new Element[] { }, null).followedBy(follow));
+ add(Sequence.create(tag, new Element[] { }, null, false).followedBy(follow));
//add(new Sequence.Constant.Empty());
// FUGLY
- add(Sequence.newSingletonSequence(many1(e, separator, tag)).followedBy(follow));
+ add(Sequence.create(many1(e, separator, tag)).followedBy(follow));
} else {
- add(Sequence.newRewritingSequence(tag, new Element[] { e }, null).followedBy(follow));
+ add(Sequence.create(tag, new Element[] { e }, null, false).followedBy(follow));
if (separator==null)
- add(Sequence.newUnwrapSequence(new Element[] { e, Repeat.this }, tag, new boolean[] { false, false }).followedBy(follow));
+ add(Sequence.create(tag, new Element[] { e, Repeat.this }, new boolean[] { false, false }, true).followedBy(follow));
else
- add(Sequence.newUnwrapSequence(new Element[] { e, separator, Repeat.this }, tag, new boolean[] { false, true, false }).followedBy(follow));
+ add(Sequence.create(tag, new Element[] { e, separator, Repeat.this }, new boolean[] { false, true, false }, true).followedBy(follow));
}
}