support for left recursion in Repeat.java
[sbp.git] / src / edu / berkeley / sbp / meta / Repeat.java
index 6308362..a8739d9 100644 (file)
@@ -18,24 +18,52 @@ public class Repeat extends Union {
     protected Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag) {
         this(e, zeroOkay, manyOkay, separator, maximal, tag, null); }
     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) {
-            // FIXME
-            add(Sequence.create(new Element[0], tag).followedBy(follow));
-            add(Sequence.create(e).followedBy(follow));
+        super(e
+              +(!manyOkay
+                ? "?"
+                : (zeroOkay
+                   ? (maximal ? "**" : "*")
+                   : (maximal ? "++" : "+")))
+              +(separator==null
+                ? ""
+                : ("/"+separator)),
+              true);
+        if (follow != null) {
+            Sequence s = Sequence.create(new Repeat(e, zeroOkay, manyOkay, separator, maximal, tag, null)).followedBy(follow);
+            add(s);
             return;
         }
-        if (zeroOkay) {
+        if (zeroOkay && !manyOkay) {
+            // FIXME
+            add(Sequence.create(tag, new Element[] { }, null, false).followedBy(follow));
+            add(Sequence.create(tag, new Element[] { e }, null, false).followedBy(follow));
+        } else if (zeroOkay) {
             add(Sequence.create(tag, new Element[] { }, null, false).followedBy(follow));
             //add(new Sequence.Constant.Empty());
             // FUGLY
             add(Sequence.create(many1(e, separator, tag)).followedBy(follow));
         } else {
             add(Sequence.create(tag, new Element[] { e }, null, false).followedBy(follow));
+            /*
             if (separator==null)
                 add(Sequence.create(tag, new Element[] { e,            Repeat.this }, new boolean[] { false, false }, true).followedBy(follow));
             else
                 add(Sequence.create(tag, new Element[] { e, separator, Repeat.this }, new boolean[] { false, true, false }, true).followedBy(follow));
+            */
+            if (separator==null)
+            /*
+                add(Sequence.create(tag, new Element[] { e,            Repeat.this }, new boolean[] { false, false }, true).followedBy(follow));
+            */
+                add(Sequence.createLeft(tag, new Element[] { Repeat.this,            e },
+                                        new boolean[] { false, false }, true));
+            else {
+                /*
+                add(Sequence.create(tag, new Element[] { e, separator, Repeat.this }, new boolean[] { false, true, false }, true).followedBy(follow));
+                */
+                add(Sequence.createLeft(tag, new Element[] { Repeat.this, separator, e },
+                                        new boolean[] { false, true, false }, true));
+            }
+
         }
     }