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.apache.xerces.impl.xpath.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) Op.nofinstances ++; 54 return new Op(Op.DOT); 55 } 56 static CharOp createChar(int data) { 57 if (Op.COUNT) Op.nofinstances ++; 58 return new CharOp(Op.CHAR, data); 59 } 60 static CharOp createAnchor(int data) { 61 if (Op.COUNT) Op.nofinstances ++; 62 return new CharOp(Op.ANCHOR, data); 63 } 64 static CharOp createCapture(int number, Op next) { 65 if (Op.COUNT) Op.nofinstances ++; 66 CharOp op = new CharOp(Op.CAPTURE, number); 67 op.next = next; 68 return op; 69 } 70 static UnionOp createUnion(int size) { 71 if (Op.COUNT) Op.nofinstances ++; 72 //System.err.println("Creates UnionOp"); 73 return new UnionOp(Op.UNION, size); 74 } 75 static ChildOp createClosure(int id) { 76 if (Op.COUNT) Op.nofinstances ++; 77 return new ModifierOp(Op.CLOSURE, id, -1); 78 } 79 static ChildOp createNonGreedyClosure() { 80 if (Op.COUNT) Op.nofinstances ++; 81 return new ChildOp(Op.NONGREEDYCLOSURE); 82 } 83 static ChildOp createQuestion(boolean nongreedy) { 84 if (Op.COUNT) Op.nofinstances ++; 85 return new ChildOp(nongreedy ? Op.NONGREEDYQUESTION : Op.QUESTION); 86 } 87 static RangeOp createRange(Token tok) { 88 if (Op.COUNT) Op.nofinstances ++; 89 return new RangeOp(Op.RANGE, tok); 90 } 91 static ChildOp createLook(int type, Op next, Op branch) { 92 if (Op.COUNT) Op.nofinstances ++; 93 ChildOp op = new ChildOp(type); 94 op.setChild(branch); 95 op.next = next; 96 return op; 97 } 98 static CharOp createBackReference(int refno) { 99 if (Op.COUNT) Op.nofinstances ++; 100 return new CharOp(Op.BACKREFERENCE, refno); 101 } 102 static StringOp createString(String literal) { 103 if (Op.COUNT) Op.nofinstances ++; 104 return new StringOp(Op.STRING, literal); 105 } 106 static ChildOp createIndependent(Op next, Op branch) { 107 if (Op.COUNT) Op.nofinstances ++; 108 ChildOp op = new ChildOp(Op.INDEPENDENT); 109 op.setChild(branch); 110 op.next = next; 111 return op; 112 } 113 static ModifierOp createModifier(Op next, Op branch, int add, int mask) { 114 if (Op.COUNT) Op.nofinstances ++; 115 ModifierOp op = new ModifierOp(Op.MODIFIER, add, mask); 116 op.setChild(branch); 117 op.next = next; 118 return op; 119 } 120 static ConditionOp createCondition(Op next, int ref, Op conditionflow, Op yesflow, Op noflow) { 121 if (Op.COUNT) Op.nofinstances ++; 122 ConditionOp op = new ConditionOp(Op.CONDITION, ref, conditionflow, yesflow, noflow); 123 op.next = next; 124 return op; 125 } 126 127 final int type; 128 Op next = null; 129 130 protected Op(int type) { 131 this.type = type; 132 } 133 134 int size() { // for UNION 135 return 0; 136 } 137 Op elementAt(int index) { // for UNIoN 138 throw new RuntimeException("Internal Error: type="+this.type); 139 } 140 Op getChild() { // for CLOSURE, QUESTION 141 throw new RuntimeException("Internal Error: type="+this.type); 142 } 143 // ModifierOp 144 int getData() { // CharOp for CHAR, BACKREFERENCE, CAPTURE, ANCHOR, 145 throw new RuntimeException("Internal Error: type="+this.type); 146 } 147 int getData2() { // ModifierOp 148 throw new RuntimeException("Internal Error: type="+this.type); 149 } 150 RangeToken getToken() { // RANGE, NRANGE 151 throw new RuntimeException("Internal Error: type="+this.type); 152 } 153 String getString() { // STRING 154 throw new RuntimeException("Internal Error: type="+this.type); 155 } 156 157 // ================================================================ 158 static class CharOp extends Op { 159 final int charData; 160 CharOp(int type, int data) { 161 super(type); 162 this.charData = data; 163 } 164 int getData() { 165 return this.charData; 166 } 167 } 168 169 // ================================================================ 170 static class UnionOp extends Op { 171 final Vector branches; 172 UnionOp(int type, int size) { 173 super(type); 174 this.branches = new Vector(size); 175 } 176 void addElement(Op op) { 177 this.branches.addElement(op); 178 } 179 int size() { 180 return this.branches.size(); 181 } 182 Op elementAt(int index) { 183 return (Op)this.branches.elementAt(index); 184 } 185 } 186 187 // ================================================================ 188 static class ChildOp extends Op { 189 Op child; 190 ChildOp(int type) { 191 super(type); 192 } 193 void setChild(Op child) { 194 this.child = child; 195 } 196 Op getChild() { 197 return this.child; 198 } 199 } 200 // ================================================================ 201 static class ModifierOp extends ChildOp { 202 final int v1; 203 final int v2; 204 ModifierOp(int type, int v1, int v2) { 205 super(type); 206 this.v1 = v1; 207 this.v2 = v2; 208 } 209 int getData() { 210 return this.v1; 211 } 212 int getData2() { 213 return this.v2; 214 } 215 } 216 // ================================================================ 217 static class RangeOp extends Op { 218 final Token tok; 219 RangeOp(int type, Token tok) { 220 super(type); 221 this.tok = tok; 222 } 223 RangeToken getToken() { 224 return (RangeToken)this.tok; 225 } 226 } 227 // ================================================================ 228 static class StringOp extends Op { 229 final String string; 230 StringOp(int type, String literal) { 231 super(type); 232 this.string = literal; 233 } 234 String getString() { 235 return this.string; 236 } 237 } 238 // ================================================================ 239 static class ConditionOp extends Op { 240 final int refNumber; 241 final Op condition; 242 final Op yes; 243 final Op no; 244 ConditionOp(int type, int refno, Op conditionflow, Op yesflow, Op noflow) { 245 super(type); 246 this.refNumber = refno; 247 this.condition = conditionflow; 248 this.yes = yesflow; 249 this.no = noflow; 250 } 251 } 252 }