Source code: org/apache/xerces/utils/regex/Op.java
1 /*
2 * The Apache Software License, Version 1.1
3 *
4 *
5 * Copyright (c) 1999,2000 The Apache Software Foundation. All rights
6 * reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The end-user documentation included with the redistribution,
21 * if any, must include the following acknowledgment:
22 * "This product includes software developed by the
23 * Apache Software Foundation (http://www.apache.org/)."
24 * Alternately, this acknowledgment may appear in the software itself,
25 * if and wherever such third-party acknowledgments normally appear.
26 *
27 * 4. The names "Xerces" and "Apache Software Foundation" must
28 * not be used to endorse or promote products derived from this
29 * software without prior written permission. For written
30 * permission, please contact apache@apache.org.
31 *
32 * 5. Products derived from this software may not be called "Apache",
33 * nor may "Apache" appear in their name, without prior written
34 * permission of the Apache Software Foundation.
35 *
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This software consists of voluntary contributions made by many
51 * individuals on behalf of the Apache Software Foundation and was
52 * originally based on software copyright (c) 1999, International
53 * Business Machines, Inc., http://www.apache.org. For more
54 * information on the Apache Software Foundation, please see
55 * <http://www.apache.org/>.
56 */
57
58 package org.apache.xerces.utils.regex;
59
60
61 import java.util.Vector;
62
63 /**
64 */
65 class Op {
66 static final int DOT = 0;
67 static final int CHAR = 1; // Single character
68 static final int RANGE = 3; // [a-zA-Z]
69 static final int NRANGE = 4; // [^a-zA-Z]
70 static final int ANCHOR = 5; // ^ $ ...
71 static final int STRING = 6; // literal String
72 static final int CLOSURE = 7; // X*
73 static final int NONGREEDYCLOSURE = 8; // X*?
74 static final int QUESTION = 9; // X?
75 static final int NONGREEDYQUESTION = 10; // X??
76 static final int UNION = 11; // X|Y
77 static final int CAPTURE = 15; // ( and )
78 static final int BACKREFERENCE = 16; // \1 \2 ...
79 static final int LOOKAHEAD = 20; // (?=...)
80 static final int NEGATIVELOOKAHEAD = 21; // (?!...)
81 static final int LOOKBEHIND = 22; // (?<=...)
82 static final int NEGATIVELOOKBEHIND = 23; // (?<!...)
83 static final int INDEPENDENT = 24; // (?>...)
84 static final int MODIFIER = 25; // (?ims-ims:...)
85 static final int CONDITION = 26; // (?(..)yes|no)
86
87 static int nofinstances = 0;
88 static final boolean COUNT = false;
89
90 static Op createDot() {
91 if (Op.COUNT) Op.nofinstances ++;
92 return new Op(Op.DOT);
93 }
94 static CharOp createChar(int data) {
95 if (Op.COUNT) Op.nofinstances ++;
96 return new CharOp(Op.CHAR, data);
97 }
98 static CharOp createAnchor(int data) {
99 if (Op.COUNT) Op.nofinstances ++;
100 return new CharOp(Op.ANCHOR, data);
101 }
102 static CharOp createCapture(int number, Op next) {
103 if (Op.COUNT) Op.nofinstances ++;
104 CharOp op = new CharOp(Op.CAPTURE, number);
105 op.next = next;
106 return op;
107 }
108 static UnionOp createUnion(int size) {
109 if (Op.COUNT) Op.nofinstances ++;
110 //System.err.println("Creates UnionOp");
111 return new UnionOp(Op.UNION, size);
112 }
113 static ChildOp createClosure(int id) {
114 if (Op.COUNT) Op.nofinstances ++;
115 return new ModifierOp(Op.CLOSURE, id, -1);
116 }
117 static ChildOp createNonGreedyClosure() {
118 if (Op.COUNT) Op.nofinstances ++;
119 return new ChildOp(Op.NONGREEDYCLOSURE);
120 }
121 static ChildOp createQuestion(boolean nongreedy) {
122 if (Op.COUNT) Op.nofinstances ++;
123 return new ChildOp(nongreedy ? Op.NONGREEDYQUESTION : Op.QUESTION);
124 }
125 static RangeOp createRange(Token tok) {
126 if (Op.COUNT) Op.nofinstances ++;
127 return new RangeOp(Op.RANGE, tok);
128 }
129 static ChildOp createLook(int type, Op next, Op branch) {
130 if (Op.COUNT) Op.nofinstances ++;
131 ChildOp op = new ChildOp(type);
132 op.setChild(branch);
133 op.next = next;
134 return op;
135 }
136 static CharOp createBackReference(int refno) {
137 if (Op.COUNT) Op.nofinstances ++;
138 return new CharOp(Op.BACKREFERENCE, refno);
139 }
140 static StringOp createString(String literal) {
141 if (Op.COUNT) Op.nofinstances ++;
142 return new StringOp(Op.STRING, literal);
143 }
144 static ChildOp createIndependent(Op next, Op branch) {
145 if (Op.COUNT) Op.nofinstances ++;
146 ChildOp op = new ChildOp(Op.INDEPENDENT);
147 op.setChild(branch);
148 op.next = next;
149 return op;
150 }
151 static ModifierOp createModifier(Op next, Op branch, int add, int mask) {
152 if (Op.COUNT) Op.nofinstances ++;
153 ModifierOp op = new ModifierOp(Op.MODIFIER, add, mask);
154 op.setChild(branch);
155 op.next = next;
156 return op;
157 }
158 static ConditionOp createCondition(Op next, int ref, Op conditionflow, Op yesflow, Op noflow) {
159 if (Op.COUNT) Op.nofinstances ++;
160 ConditionOp op = new ConditionOp(Op.CONDITION, ref, conditionflow, yesflow, noflow);
161 op.next = next;
162 return op;
163 }
164
165 int type;
166 Op next = null;
167
168 protected Op(int type) {
169 this.type = type;
170 }
171
172 int size() { // for UNION
173 return 0;
174 }
175 Op elementAt(int index) { // for UNIoN
176 throw new RuntimeException("Internal Error: type="+this.type);
177 }
178 Op getChild() { // for CLOSURE, QUESTION
179 throw new RuntimeException("Internal Error: type="+this.type);
180 }
181 // ModifierOp
182 int getData() { // CharOp for CHAR, BACKREFERENCE, CAPTURE, ANCHOR,
183 throw new RuntimeException("Internal Error: type="+this.type);
184 }
185 int getData2() { // ModifierOp
186 throw new RuntimeException("Internal Error: type="+this.type);
187 }
188 RangeToken getToken() { // RANGE, NRANGE
189 throw new RuntimeException("Internal Error: type="+this.type);
190 }
191 String getString() { // STRING
192 throw new RuntimeException("Internal Error: type="+this.type);
193 }
194
195 // ================================================================
196 static class CharOp extends Op {
197 int charData;
198 CharOp(int type, int data) {
199 super(type);
200 this.charData = data;
201 }
202 int getData() {
203 return this.charData;
204 }
205 }
206
207 // ================================================================
208 static class UnionOp extends Op {
209 Vector branches;
210 UnionOp(int type, int size) {
211 super(type);
212 this.branches = new Vector(size);
213 }
214 void addElement(Op op) {
215 this.branches.addElement(op);
216 }
217 int size() {
218 return this.branches.size();
219 }
220 Op elementAt(int index) {
221 return (Op)this.branches.elementAt(index);
222 }
223 }
224
225 // ================================================================
226 static class ChildOp extends Op {
227 Op child;
228 ChildOp(int type) {
229 super(type);
230 }
231 void setChild(Op child) {
232 this.child = child;
233 }
234 Op getChild() {
235 return this.child;
236 }
237 }
238 // ================================================================
239 static class ModifierOp extends ChildOp {
240 int v1;
241 int v2;
242 ModifierOp(int type, int v1, int v2) {
243 super(type);
244 this.v1 = v1;
245 this.v2 = v2;
246 }
247 int getData() {
248 return this.v1;
249 }
250 int getData2() {
251 return this.v2;
252 }
253 }
254 // ================================================================
255 static class RangeOp extends Op {
256 Token tok;
257 RangeOp(int type, Token tok) {
258 super(type);
259 this.tok = tok;
260 }
261 RangeToken getToken() {
262 return (RangeToken)this.tok;
263 }
264 }
265 // ================================================================
266 static class StringOp extends Op {
267 String string;
268 StringOp(int type, String literal) {
269 super(type);
270 this.string = literal;
271 }
272 String getString() {
273 return this.string;
274 }
275 }
276 // ================================================================
277 static class ConditionOp extends Op {
278 int refNumber;
279 Op condition;
280 Op yes;
281 Op no;
282 ConditionOp(int type, int refno, Op conditionflow, Op yesflow, Op noflow) {
283 super(type);
284 this.refNumber = refno;
285 this.condition = conditionflow;
286 this.yes = yesflow;
287 this.no = noflow;
288 }
289 }
290 }