X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fcom%2Fstevesoft%2Fpat%2FMulti_stage2.java;h=b18ad1c6842c4f4e8f62a6ed689185850bd35905;hb=506d60f0e188723ddc91c26824b41ac7034df3fe;hp=990b0e61e43e95b7c3884fd6357f8a1f7cd7e31d;hpb=60f2d6c034560415fd0139c8bc7df0c19cae1186;p=jalview.git diff --git a/src/com/stevesoft/pat/Multi_stage2.java b/src/com/stevesoft/pat/Multi_stage2.java index 990b0e6..b18ad1c 100755 --- a/src/com/stevesoft/pat/Multi_stage2.java +++ b/src/com/stevesoft/pat/Multi_stage2.java @@ -1,156 +1,157 @@ -// -// 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; - } - } -}; +// +// 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; + } + } +};