From 9b031db4cefc550c82a8bb6da3b061d3790c2cdc Mon Sep 17 00:00:00 2001 From: adam Date: Fri, 21 Jul 2006 03:42:22 -0400 Subject: [PATCH] checkpoint darcs-hash:20060721074222-5007d-12be09a59354cb2c028e6f916e2752bb0be69b09.gz --- src/edu/berkeley/sbp/Sequence.java | 33 +++++++++++--------- src/edu/berkeley/sbp/chr/CharAtom.java | 2 +- src/edu/berkeley/sbp/meta/Grammar.java | 2 +- src/edu/berkeley/sbp/meta/MetaGrammarBindings.java | 31 +++++++++--------- src/edu/berkeley/sbp/meta/Production.java | 4 +-- src/edu/berkeley/sbp/meta/Repeat.java | 14 ++++----- 6 files changed, 45 insertions(+), 41 deletions(-) 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); + } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/edu/berkeley/sbp/chr/CharAtom.java b/src/edu/berkeley/sbp/chr/CharAtom.java index 920381b..cb1ed80 100644 --- a/src/edu/berkeley/sbp/chr/CharAtom.java +++ b/src/edu/berkeley/sbp/chr/CharAtom.java @@ -50,7 +50,7 @@ public class CharAtom extends Atom { } private static Union epsilon = new Union("()"); - static { epsilon.add(Sequence.newEmptySequence()); } + static { epsilon.add(Sequence.create()); } public Topology> unwrap() { return this; } public Topology> empty() { return new CharAtom(); } diff --git a/src/edu/berkeley/sbp/meta/Grammar.java b/src/edu/berkeley/sbp/meta/Grammar.java index ad96a5f..7b4d315 100644 --- a/src/edu/berkeley/sbp/meta/Grammar.java +++ b/src/edu/berkeley/sbp/meta/Grammar.java @@ -28,7 +28,7 @@ public class Grammar { 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); } } diff --git a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java index 0bad0b2..7a18bab 100644 --- a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java +++ b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java @@ -81,10 +81,10 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings { 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)); } } } @@ -128,11 +128,11 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings { HashSet bad2 = new HashSet(); 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