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