spelling err
[jalview.git] / src / com / stevesoft / pat / Multi_stage2.java
1 //\r
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
5 // the details.\r
6 //    -- Happy Computing!\r
7 //\r
8 package com.stevesoft.pat;\r
9 import java.util.Hashtable;\r
10 \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
15     Pattern nextRet;\r
16     patInt count;\r
17     patInt matchMin,matchMax;\r
18     public boolean matchFewest = false;\r
19     public String toString() {\r
20         String ret = "";\r
21         ret += sub.toString();\r
22         ret += "{"+matchMin+","+matchMax+"}";\r
23         if(matchFewest) ret += "?";\r
24         ret += parent.nextString();\r
25         return ret;\r
26     }\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
30         sub = p;\r
31         nextRet = this;\r
32         sub.setParent(this);\r
33         matchMin = a;\r
34         matchMax = b;\r
35         count = new patInt(0);\r
36         // we must have b > a > -1 for this\r
37         // to make sense.\r
38         if(!a.lessEq(b))\r
39             //throw new BadMultiArgs();\r
40             RegSyntaxError.endItAll("Bad Multi Args: "+a+">"+b);\r
41         patInt i = new patInt(-1);\r
42         if(a.lessEq(i))\r
43             //throw new BadMultiArgs();\r
44             RegSyntaxError.endItAll("Bad Multi Args: "+a+"< 0");\r
45     }\r
46     public Pattern getNext() {\r
47         return nextRet;\r
48     }\r
49     int pos_old = -1;\r
50     public int matchInternal(int pos,Pthings pt) {\r
51         sub.setParent(this);\r
52 \r
53         int canUse = -1;\r
54 \r
55         // check for some forms of infinite recursion...\r
56         if(pos_old >= 0 && pos == pos_old) {\r
57             return -1;\r
58         }\r
59         pos_old = pos;\r
60 \r
61         if(matchMin.lessEq(count))\r
62             canUse = pos;\r
63         if(!count.lessEq(matchMax) || pos > pt.src.length())\r
64             return -1;\r
65 \r
66         if((matchFewest||count.equals(matchMax)) && canUse >= 0) {\r
67             Pattern n = super.getNext();\r
68             if(n == null)\r
69                 return canUse;\r
70             int ret = testMatch(n,pos,pt);\r
71             if(ret >= 0) {\r
72                return ret;\r
73             }\r
74             else canUse = -1;\r
75         }\r
76 \r
77         count.inc();\r
78         try {\r
79             if(count.lessEq(matchMax)) {\r
80                 int r = testMatch(sub,pos,pt);\r
81                 if(r >= 0)\r
82                     return r;\r
83             }\r
84         } finally { count.dec(); }\r
85 \r
86         if(!matchFewest && canUse >= 0) {\r
87             Pattern n = super.getNext();\r
88             if(n == null)\r
89                 return canUse;\r
90             int ret = testMatch(n,pos,pt);\r
91             return ret;\r
92         } else return canUse;\r
93     }\r
94     public Pattern clone1(Hashtable h) {\r
95         try {\r
96             Multi_stage2 m = new Multi_stage2(matchMin,matchMax,sub.clone(h));\r
97             m.matchFewest = matchFewest;\r
98             return m;\r
99         } catch(RegSyntax rs) {\r
100             return null;\r
101         }\r
102     }\r
103 };\r