2 // This software is now distributed according to
\r
3 // the Lesser Gnu Public License. Please see
\r
4 // http://www.gnu.org/copyleft/lesser.txt for
\r
6 // -- Happy Computing!
\r
8 package com.stevesoft.pat;
\r
9 import java.util.Hashtable;
\r
11 /** If Multi were not split into a second stage, then
\r
12 a nested Multi would try to re-use the same count
\r
13 variable and the whole thing would break. */
\r
14 class Multi_stage2 extends PatternSub {
\r
17 patInt matchMin,matchMax;
\r
18 public boolean matchFewest = false;
\r
19 public String toString() {
\r
21 ret += sub.toString();
\r
22 ret += "{"+matchMin+","+matchMax+"}";
\r
23 if(matchFewest) ret += "?";
\r
24 ret += parent.nextString();
\r
27 Multi_stage2(patInt a,patInt b,Pattern p) throws RegSyntax {
\r
28 if(p == null) RegSyntaxError.endItAll(
\r
29 "Multiple match of Null pattern requested.");
\r
32 sub.setParent(this);
\r
35 count = new patInt(0);
\r
36 // we must have b > a > -1 for this
\r
39 //throw new BadMultiArgs();
\r
40 RegSyntaxError.endItAll("Bad Multi Args: "+a+">"+b);
\r
41 patInt i = new patInt(-1);
\r
43 //throw new BadMultiArgs();
\r
44 RegSyntaxError.endItAll("Bad Multi Args: "+a+"< 0");
\r
46 public Pattern getNext() {
\r
50 public int matchInternal(int pos,Pthings pt) {
\r
51 sub.setParent(this);
\r
55 // check for some forms of infinite recursion...
\r
56 if(pos_old >= 0 && pos == pos_old) {
\r
61 if(matchMin.lessEq(count))
\r
63 if(!count.lessEq(matchMax) || pos > pt.src.length())
\r
66 if((matchFewest||count.equals(matchMax)) && canUse >= 0) {
\r
67 Pattern n = super.getNext();
\r
70 int ret = testMatch(n,pos,pt);
\r
79 if(count.lessEq(matchMax)) {
\r
80 int r = testMatch(sub,pos,pt);
\r
84 } finally { count.dec(); }
\r
86 if(!matchFewest && canUse >= 0) {
\r
87 Pattern n = super.getNext();
\r
90 int ret = testMatch(n,pos,pt);
\r
92 } else return canUse;
\r
94 public Pattern clone1(Hashtable h) {
\r
96 Multi_stage2 m = new Multi_stage2(matchMin,matchMax,sub.clone(h));
\r
97 m.matchFewest = matchFewest;
\r
99 } catch(RegSyntax rs) {
\r