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