merge from 2_4_Release branch
[jalview.git] / src / com / stevesoft / pat / Multi_stage2.java
index 990b0e6..b18ad1c 100755 (executable)
-//\r
-// This software is now distributed according to\r
-// the Lesser Gnu Public License.  Please see\r
-// http://www.gnu.org/copyleft/lesser.txt for\r
-// the details.\r
-//    -- Happy Computing!\r
-//\r
-package com.stevesoft.pat;\r
-\r
-import java.util.*;\r
-\r
-/** If Multi were not split into a second stage, then\r
- a nested Multi would try to re-use the same count\r
- variable and the whole thing would break. */\r
-class Multi_stage2\r
-    extends PatternSub\r
-{\r
-  Pattern nextRet;\r
-  patInt count;\r
-  patInt matchMin, matchMax;\r
-  public boolean matchFewest = false;\r
-  public String toString()\r
-  {\r
-    String ret = "";\r
-    ret += sub.toString();\r
-    ret += "{" + matchMin + "," + matchMax + "}";\r
-    if (matchFewest)\r
-    {\r
-      ret += "?";\r
-    }\r
-    ret += parent.nextString();\r
-    return ret;\r
-  }\r
-\r
-  Multi_stage2(patInt a, patInt b, Pattern p)\r
-      throws RegSyntax\r
-  {\r
-    if (p == null)\r
-    {\r
-      RegSyntaxError.endItAll(\r
-          "Multiple match of Null pattern requested.");\r
-    }\r
-    sub = p;\r
-    nextRet = this;\r
-    sub.setParent(this);\r
-    matchMin = a;\r
-    matchMax = b;\r
-    count = new patInt(0);\r
-    // we must have b > a > -1 for this\r
-    // to make sense.\r
-    if (!a.lessEq(b))\r
-    {\r
-      //throw new BadMultiArgs();\r
-      RegSyntaxError.endItAll("Bad Multi Args: " + a + ">" + b);\r
-    }\r
-    patInt i = new patInt( -1);\r
-    if (a.lessEq(i))\r
-    {\r
-      //throw new BadMultiArgs();\r
-      RegSyntaxError.endItAll("Bad Multi Args: " + a + "< 0");\r
-    }\r
-  }\r
-\r
-  public Pattern getNext()\r
-  {\r
-    return nextRet;\r
-  }\r
-\r
-  int pos_old = -1;\r
-  public int matchInternal(int pos, Pthings pt)\r
-  {\r
-    sub.setParent(this);\r
-\r
-    int canUse = -1;\r
-\r
-    // check for some forms of infinite recursion...\r
-    if (pos_old >= 0 && pos == pos_old)\r
-    {\r
-      return -1;\r
-    }\r
-    pos_old = pos;\r
-\r
-    if (matchMin.lessEq(count))\r
-    {\r
-      canUse = pos;\r
-    }\r
-    if (!count.lessEq(matchMax) || pos > pt.src.length())\r
-    {\r
-      return -1;\r
-    }\r
-\r
-    if ( (matchFewest || count.equals(matchMax)) && canUse >= 0)\r
-    {\r
-      Pattern n = super.getNext();\r
-      if (n == null)\r
-      {\r
-        return canUse;\r
-      }\r
-      int ret = testMatch(n, pos, pt);\r
-      if (ret >= 0)\r
-      {\r
-        return ret;\r
-      }\r
-      else\r
-      {\r
-        canUse = -1;\r
-      }\r
-    }\r
-\r
-    count.inc();\r
-    try\r
-    {\r
-      if (count.lessEq(matchMax))\r
-      {\r
-        int r = testMatch(sub, pos, pt);\r
-        if (r >= 0)\r
-        {\r
-          return r;\r
-        }\r
-      }\r
-    }\r
-    finally\r
-    {\r
-      count.dec();\r
-    }\r
-\r
-    if (!matchFewest && canUse >= 0)\r
-    {\r
-      Pattern n = super.getNext();\r
-      if (n == null)\r
-      {\r
-        return canUse;\r
-      }\r
-      int ret = testMatch(n, pos, pt);\r
-      return ret;\r
-    }\r
-    else\r
-    {\r
-      return canUse;\r
-    }\r
-  }\r
-\r
-  public Pattern clone1(Hashtable h)\r
-  {\r
-    try\r
-    {\r
-      Multi_stage2 m = new Multi_stage2(matchMin, matchMax, sub.clone(h));\r
-      m.matchFewest = matchFewest;\r
-      return m;\r
-    }\r
-    catch (RegSyntax rs)\r
-    {\r
-      return null;\r
-    }\r
-  }\r
-};\r
+//
+// This software is now distributed according to
+// the Lesser Gnu Public License.  Please see
+// http://www.gnu.org/copyleft/lesser.txt for
+// the details.
+//    -- Happy Computing!
+//
+package com.stevesoft.pat;
+
+import java.util.*;
+
+/**
+ * If Multi were not split into a second stage, then a nested Multi would try to
+ * re-use the same count variable and the whole thing would break.
+ */
+class Multi_stage2 extends PatternSub
+{
+  Pattern nextRet;
+
+  patInt count;
+
+  patInt matchMin, matchMax;
+
+  public boolean matchFewest = false;
+
+  public String toString()
+  {
+    String ret = "";
+    ret += sub.toString();
+    ret += "{" + matchMin + "," + matchMax + "}";
+    if (matchFewest)
+    {
+      ret += "?";
+    }
+    ret += parent.nextString();
+    return ret;
+  }
+
+  Multi_stage2(patInt a, patInt b, Pattern p) throws RegSyntax
+  {
+    if (p == null)
+    {
+      RegSyntaxError.endItAll("Multiple match of Null pattern requested.");
+    }
+    sub = p;
+    nextRet = this;
+    sub.setParent(this);
+    matchMin = a;
+    matchMax = b;
+    count = new patInt(0);
+    // we must have b > a > -1 for this
+    // to make sense.
+    if (!a.lessEq(b))
+    {
+      // throw new BadMultiArgs();
+      RegSyntaxError.endItAll("Bad Multi Args: " + a + ">" + b);
+    }
+    patInt i = new patInt(-1);
+    if (a.lessEq(i))
+    {
+      // throw new BadMultiArgs();
+      RegSyntaxError.endItAll("Bad Multi Args: " + a + "< 0");
+    }
+  }
+
+  public Pattern getNext()
+  {
+    return nextRet;
+  }
+
+  int pos_old = -1;
+
+  public int matchInternal(int pos, Pthings pt)
+  {
+    sub.setParent(this);
+
+    int canUse = -1;
+
+    // check for some forms of infinite recursion...
+    if (pos_old >= 0 && pos == pos_old)
+    {
+      return -1;
+    }
+    pos_old = pos;
+
+    if (matchMin.lessEq(count))
+    {
+      canUse = pos;
+    }
+    if (!count.lessEq(matchMax) || pos > pt.src.length())
+    {
+      return -1;
+    }
+
+    if ((matchFewest || count.equals(matchMax)) && canUse >= 0)
+    {
+      Pattern n = super.getNext();
+      if (n == null)
+      {
+        return canUse;
+      }
+      int ret = testMatch(n, pos, pt);
+      if (ret >= 0)
+      {
+        return ret;
+      }
+      else
+      {
+        canUse = -1;
+      }
+    }
+
+    count.inc();
+    try
+    {
+      if (count.lessEq(matchMax))
+      {
+        int r = testMatch(sub, pos, pt);
+        if (r >= 0)
+        {
+          return r;
+        }
+      }
+    } finally
+    {
+      count.dec();
+    }
+
+    if (!matchFewest && canUse >= 0)
+    {
+      Pattern n = super.getNext();
+      if (n == null)
+      {
+        return canUse;
+      }
+      int ret = testMatch(n, pos, pt);
+      return ret;
+    }
+    else
+    {
+      return canUse;
+    }
+  }
+
+  public Pattern clone1(Hashtable h)
+  {
+    try
+    {
+      Multi_stage2 m = new Multi_stage2(matchMin, matchMax, sub.clone(h));
+      m.matchFewest = matchFewest;
+      return m;
+    } catch (RegSyntax rs)
+    {
+      return null;
+    }
+  }
+};