Source code: org/apache/xmlbeans/impl/regex/Op.java
1 /* Copyright 2004 The Apache Software Foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 package org.apache.xmlbeans.impl.regex;
17
18 import java.util.Vector;
19
20 /**
21 */
22 class Op {
23 static final int DOT = 0;
24 static final int CHAR = 1; // Single character
25 static final int RANGE = 3; // [a-zA-Z]
26 static final int NRANGE = 4; // [^a-zA-Z]
27 static final int ANCHOR = 5; // ^ $ ...
28 static final int STRING = 6; // literal String
29 static final int CLOSURE = 7; // X*
30 static final int NONGREEDYCLOSURE = 8; // X*?
31 static final int QUESTION = 9; // X?
32 static final int NONGREEDYQUESTION = 10; // X??
33 static final int UNION = 11; // X|Y
34 static final int CAPTURE = 15; // ( and )
35 static final int BACKREFERENCE = 16; // \1 \2 ...
36 static final int LOOKAHEAD = 20; // (?=...)
37 static final int NEGATIVELOOKAHEAD = 21; // (?!...)
38 static final int LOOKBEHIND = 22; // (?<=...)
39 static final int NEGATIVELOOKBEHIND = 23; // (?<!...)
40 static final int INDEPENDENT = 24; // (?>...)
41 static final int MODIFIER = 25; // (?ims-ims:...)
42 static final int CONDITION = 26; // (?(..)yes|no)
43
44 static int nofinstances = 0;
45 static final boolean COUNT = false;
46
47 static Op createDot() {
48 if (Op.COUNT) Op.nofinstances ++;
49 return new Op(Op.DOT);
50 }
51 static CharOp createChar(int data) {
52 if (Op.COUNT) Op.nofinstances ++;
53 return new CharOp(Op.CHAR, data);
54 }
55 static CharOp createAnchor(int data) {
56 if (Op.COUNT) Op.nofinstances ++;
57 return new CharOp(Op.ANCHOR, data);
58 }
59 static CharOp createCapture(int number, Op next) {
60 if (Op.COUNT) Op.nofinstances ++;
61 CharOp op = new CharOp(Op.CAPTURE, number);
62 op.next = next;
63 return op;
64 }
65 static UnionOp createUnion(int size) {
66 if (Op.COUNT) Op.nofinstances ++;
67 //System.err.println("Creates UnionOp");
68 return new UnionOp(Op.UNION, size);
69 }
70 static ChildOp createClosure(int id) {
71 if (Op.COUNT) Op.nofinstances ++;
72 return new ModifierOp(Op.CLOSURE, id, -1);
73 }
74 static ChildOp createNonGreedyClosure() {
75 if (Op.COUNT) Op.nofinstances ++;
76 return new ChildOp(Op.NONGREEDYCLOSURE);
77 }
78 static ChildOp createQuestion(boolean nongreedy) {
79 if (Op.COUNT) Op.nofinstances ++;
80 return new ChildOp(nongreedy ? Op.NONGREEDYQUESTION : Op.QUESTION);
81 }
82 static RangeOp createRange(Token tok) {
83 if (Op.COUNT) Op.nofinstances ++;
84 return new RangeOp(Op.RANGE, tok);
85 }
86 static ChildOp createLook(int type, Op next, Op branch) {
87 if (Op.COUNT) Op.nofinstances ++;
88 ChildOp op = new ChildOp(type);
89 op.setChild(branch);
90 op.next = next;
91 return op;
92 }
93 static CharOp createBackReference(int refno) {
94 if (Op.COUNT) Op.nofinstances ++;
95 return new CharOp(Op.BACKREFERENCE, refno);
96 }
97 static StringOp createString(String literal) {
98 if (Op.COUNT) Op.nofinstances ++;
99 return new StringOp(Op.STRING, literal);
100 }
101 static ChildOp createIndependent(Op next, Op branch) {
102 if (Op.COUNT) Op.nofinstances ++;
103 ChildOp op = new ChildOp(Op.INDEPENDENT);
104 op.setChild(branch);
105 op.next = next;
106 return op;
107 }
108 static ModifierOp createModifier(Op next, Op branch, int add, int mask) {
109 if (Op.COUNT) Op.nofinstances ++;
110 ModifierOp op = new ModifierOp(Op.MODIFIER, add, mask);
111 op.setChild(branch);
112 op.next = next;
113 return op;
114 }
115 static ConditionOp createCondition(Op next, int ref, Op conditionflow, Op yesflow, Op noflow) {
116 if (Op.COUNT) Op.nofinstances ++;
117 ConditionOp op = new ConditionOp(Op.CONDITION, ref, conditionflow, yesflow, noflow);
118 op.next = next;
119 return op;
120 }
121
122 int type;
123 Op next = null;
124
125 protected Op(int type) {
126 this.type = type;
127 }
128
129 int size() { // for UNION
130 return 0;
131 }
132 Op elementAt(int index) { // for UNIoN
133 throw new RuntimeException("Internal Error: type="+this.type);
134 }
135 Op getChild() { // for CLOSURE, QUESTION
136 throw new RuntimeException("Internal Error: type="+this.type);
137 }
138 // ModifierOp
139 int getData() { // CharOp for CHAR, BACKREFERENCE, CAPTURE, ANCHOR,
140 throw new RuntimeException("Internal Error: type="+this.type);
141 }
142 int getData2() { // ModifierOp
143 throw new RuntimeException("Internal Error: type="+this.type);
144 }
145 RangeToken getToken() { // RANGE, NRANGE
146 throw new RuntimeException("Internal Error: type="+this.type);
147 }
148 String getString() { // STRING
149 throw new RuntimeException("Internal Error: type="+this.type);
150 }
151
152 // ================================================================
153 static class CharOp extends Op {
154 int charData;
155 CharOp(int type, int data) {
156 super(type);
157 this.charData = data;
158 }
159 int getData() {
160 return this.charData;
161 }
162 }
163
164 // ================================================================
165 static class UnionOp extends Op {
166 Vector branches;
167 UnionOp(int type, int size) {
168 super(type);
169 this.branches = new Vector(size);
170 }
171 void addElement(Op op) {
172 this.branches.addElement(op);
173 }
174 int size() {
175 return this.branches.size();
176 }
177 Op elementAt(int index) {
178 return (Op)this.branches.elementAt(index);
179 }
180 }
181
182 // ================================================================
183 static class ChildOp extends Op {
184 Op child;
185 ChildOp(int type) {
186 super(type);
187 }
188 void setChild(Op child) {
189 this.child = child;
190 }
191 Op getChild() {
192 return this.child;
193 }
194 }
195 // ================================================================
196 static class ModifierOp extends ChildOp {
197 int v1;
198 int v2;
199 ModifierOp(int type, int v1, int v2) {
200 super(type);
201 this.v1 = v1;
202 this.v2 = v2;
203 }
204 int getData() {
205 return this.v1;
206 }
207 int getData2() {
208 return this.v2;
209 }
210 }
211 // ================================================================
212 static class RangeOp extends Op {
213 Token tok;
214 RangeOp(int type, Token tok) {
215 super(type);
216 this.tok = tok;
217 }
218 RangeToken getToken() {
219 return (RangeToken)this.tok;
220 }
221 }
222 // ================================================================
223 static class StringOp extends Op {
224 String string;
225 StringOp(int type, String literal) {
226 super(type);
227 this.string = literal;
228 }
229 String getString() {
230 return this.string;
231 }
232 }
233 // ================================================================
234 static class ConditionOp extends Op {
235 int refNumber;
236 Op condition;
237 Op yes;
238 Op no;
239 ConditionOp(int type, int refno, Op conditionflow, Op yesflow, Op noflow) {
240 super(type);
241 this.refNumber = refno;
242 this.condition = conditionflow;
243 this.yes = yesflow;
244 this.no = noflow;
245 }
246 }
247 }