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