checkpoint
authoradam <adam@megacz.com>
Fri, 21 Jul 2006 07:42:22 +0000 (03:42 -0400)
committeradam <adam@megacz.com>
Fri, 21 Jul 2006 07:42:22 +0000 (03:42 -0400)
darcs-hash:20060721074222-5007d-12be09a59354cb2c028e6f916e2752bb0be69b09.gz

src/edu/berkeley/sbp/Sequence.java
src/edu/berkeley/sbp/chr/CharAtom.java
src/edu/berkeley/sbp/meta/Grammar.java
src/edu/berkeley/sbp/meta/MetaGrammarBindings.java
src/edu/berkeley/sbp/meta/Production.java
src/edu/berkeley/sbp/meta/Repeat.java

index bb0ba48..2530a21 100644 (file)
@@ -32,30 +32,33 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         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);
+    }
 
     ////////////////////////////////////////////////////////////////////////////////
 
index 920381b..cb1ed80 100644 (file)
@@ -50,7 +50,7 @@ public class CharAtom extends Atom<Character> {
     }
 
     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(); }
index ad96a5f..7b4d315 100644 (file)
@@ -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);
         }
     }
     
index 0bad0b2..7a18bab 100644 (file)
@@ -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<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];
@@ -141,16 +141,17 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
                 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));
             }
         }
     }
@@ -245,9 +246,9 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
                 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));
@@ -268,7 +269,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
     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;
@@ -325,7 +326,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
             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,
index 36ad8b0..bcd2407 100644 (file)
@@ -111,9 +111,9 @@ public  class Production {
         */
 
         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);
     }
 
 }
index 680b636..0ea201d 100644 (file)
@@ -18,21 +18,21 @@ public class Repeat extends Union {
     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));
         }
     }