JAL-1807 still testing
[jalviewjs.git] / unused / 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 \r
10 import java.util.*;\r
11 \r
12 /**\r
13  * If Multi were not split into a second stage, then a nested Multi would try to\r
14  * re-use the same count variable and the whole thing would break.\r
15  */\r
16 class Multi_stage2 extends PatternSub\r
17 {\r
18   Pattern nextRet;\r
19 \r
20   patInt count;\r
21 \r
22   patInt matchMin, matchMax;\r
23 \r
24   public boolean matchFewest = false;\r
25 \r
26   public String toString()\r
27   {\r
28     String ret = "";\r
29     ret += sub.toString();\r
30     ret += "{" + matchMin + "," + matchMax + "}";\r
31     if (matchFewest)\r
32     {\r
33       ret += "?";\r
34     }\r
35     ret += parent.nextString();\r
36     return ret;\r
37   }\r
38 \r
39   Multi_stage2(patInt a, patInt b, Pattern p) throws RegSyntax\r
40   {\r
41     if (p == null)\r
42     {\r
43       RegSyntaxError.endItAll("Multiple match of Null pattern requested.");\r
44     }\r
45     sub = p;\r
46     nextRet = this;\r
47     sub.setParent(this);\r
48     matchMin = a;\r
49     matchMax = b;\r
50     count = new patInt(0);\r
51     // we must have b > a > -1 for this\r
52     // to make sense.\r
53     if (!a.lessEq(b))\r
54     {\r
55       // throw new BadMultiArgs();\r
56       RegSyntaxError.endItAll("Bad Multi Args: " + a + ">" + b);\r
57     }\r
58     patInt i = new patInt(-1);\r
59     if (a.lessEq(i))\r
60     {\r
61       // throw new BadMultiArgs();\r
62       RegSyntaxError.endItAll("Bad Multi Args: " + a + "< 0");\r
63     }\r
64   }\r
65 \r
66   public Pattern getNext()\r
67   {\r
68     return nextRet;\r
69   }\r
70 \r
71   int pos_old = -1;\r
72 \r
73   public int matchInternal(int pos, Pthings pt)\r
74   {\r
75     sub.setParent(this);\r
76 \r
77     int canUse = -1;\r
78 \r
79     // check for some forms of infinite recursion...\r
80     if (pos_old >= 0 && pos == pos_old)\r
81     {\r
82       return -1;\r
83     }\r
84     pos_old = pos;\r
85 \r
86     if (matchMin.lessEq(count))\r
87     {\r
88       canUse = pos;\r
89     }\r
90     if (!count.lessEq(matchMax) || pos > pt.src.length())\r
91     {\r
92       return -1;\r
93     }\r
94 \r
95     if ((matchFewest || count.equals(matchMax)) && canUse >= 0)\r
96     {\r
97       Pattern n = super.getNext();\r
98       if (n == null)\r
99       {\r
100         return canUse;\r
101       }\r
102       int ret = testMatch(n, pos, pt);\r
103       if (ret >= 0)\r
104       {\r
105         return ret;\r
106       }\r
107       else\r
108       {\r
109         canUse = -1;\r
110       }\r
111     }\r
112 \r
113     count.inc();\r
114     try\r
115     {\r
116       if (count.lessEq(matchMax))\r
117       {\r
118         int r = testMatch(sub, pos, pt);\r
119         if (r >= 0)\r
120         {\r
121           return r;\r
122         }\r
123       }\r
124     } finally\r
125     {\r
126       count.dec();\r
127     }\r
128 \r
129     if (!matchFewest && canUse >= 0)\r
130     {\r
131       Pattern n = super.getNext();\r
132       if (n == null)\r
133       {\r
134         return canUse;\r
135       }\r
136       int ret = testMatch(n, pos, pt);\r
137       return ret;\r
138     }\r
139     else\r
140     {\r
141       return canUse;\r
142     }\r
143   }\r
144 \r
145   public Pattern clone1(Hashtable h)\r
146   {\r
147     try\r
148     {\r
149       Multi_stage2 m = new Multi_stage2(matchMin, matchMax, sub.clone(h));\r
150       m.matchFewest = matchFewest;\r
151       return m;\r
152     } catch (RegSyntax rs)\r
153     {\r
154       return null;\r
155     }\r
156   }\r
157 };\r