1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.impl.xpath.regex;
  58. import java.util.Vector;
  59. /**
  60. * @version $Id: Op.java,v 1.3 2002/08/09 15:18:17 neilg Exp $
  61. */
  62. class Op {
  63. static final int DOT = 0;
  64. static final int CHAR = 1; // Single character
  65. static final int RANGE = 3; // [a-zA-Z]
  66. static final int NRANGE = 4; // [^a-zA-Z]
  67. static final int ANCHOR = 5; // ^ $ ...
  68. static final int STRING = 6; // literal String
  69. static final int CLOSURE = 7; // X*
  70. static final int NONGREEDYCLOSURE = 8; // X*?
  71. static final int QUESTION = 9; // X?
  72. static final int NONGREEDYQUESTION = 10; // X??
  73. static final int UNION = 11; // X|Y
  74. static final int CAPTURE = 15; // ( and )
  75. static final int BACKREFERENCE = 16; // \1 \2 ...
  76. static final int LOOKAHEAD = 20; // (?=...)
  77. static final int NEGATIVELOOKAHEAD = 21; // (?!...)
  78. static final int LOOKBEHIND = 22; // (?<=...)
  79. static final int NEGATIVELOOKBEHIND = 23; // (?<!...)
  80. static final int INDEPENDENT = 24; // (?>...)
  81. static final int MODIFIER = 25; // (?ims-ims:...)
  82. static final int CONDITION = 26; // (?(..)yes|no)
  83. static int nofinstances = 0;
  84. static final boolean COUNT = false;
  85. static Op createDot() {
  86. if (Op.COUNT) Op.nofinstances ++;
  87. return new Op(Op.DOT);
  88. }
  89. static CharOp createChar(int data) {
  90. if (Op.COUNT) Op.nofinstances ++;
  91. return new CharOp(Op.CHAR, data);
  92. }
  93. static CharOp createAnchor(int data) {
  94. if (Op.COUNT) Op.nofinstances ++;
  95. return new CharOp(Op.ANCHOR, data);
  96. }
  97. static CharOp createCapture(int number, Op next) {
  98. if (Op.COUNT) Op.nofinstances ++;
  99. CharOp op = new CharOp(Op.CAPTURE, number);
  100. op.next = next;
  101. return op;
  102. }
  103. static UnionOp createUnion(int size) {
  104. if (Op.COUNT) Op.nofinstances ++;
  105. //System.err.println("Creates UnionOp");
  106. return new UnionOp(Op.UNION, size);
  107. }
  108. static ChildOp createClosure(int id) {
  109. if (Op.COUNT) Op.nofinstances ++;
  110. return new ModifierOp(Op.CLOSURE, id, -1);
  111. }
  112. static ChildOp createNonGreedyClosure() {
  113. if (Op.COUNT) Op.nofinstances ++;
  114. return new ChildOp(Op.NONGREEDYCLOSURE);
  115. }
  116. static ChildOp createQuestion(boolean nongreedy) {
  117. if (Op.COUNT) Op.nofinstances ++;
  118. return new ChildOp(nongreedy ? Op.NONGREEDYQUESTION : Op.QUESTION);
  119. }
  120. static RangeOp createRange(Token tok) {
  121. if (Op.COUNT) Op.nofinstances ++;
  122. return new RangeOp(Op.RANGE, tok);
  123. }
  124. static ChildOp createLook(int type, Op next, Op branch) {
  125. if (Op.COUNT) Op.nofinstances ++;
  126. ChildOp op = new ChildOp(type);
  127. op.setChild(branch);
  128. op.next = next;
  129. return op;
  130. }
  131. static CharOp createBackReference(int refno) {
  132. if (Op.COUNT) Op.nofinstances ++;
  133. return new CharOp(Op.BACKREFERENCE, refno);
  134. }
  135. static StringOp createString(String literal) {
  136. if (Op.COUNT) Op.nofinstances ++;
  137. return new StringOp(Op.STRING, literal);
  138. }
  139. static ChildOp createIndependent(Op next, Op branch) {
  140. if (Op.COUNT) Op.nofinstances ++;
  141. ChildOp op = new ChildOp(Op.INDEPENDENT);
  142. op.setChild(branch);
  143. op.next = next;
  144. return op;
  145. }
  146. static ModifierOp createModifier(Op next, Op branch, int add, int mask) {
  147. if (Op.COUNT) Op.nofinstances ++;
  148. ModifierOp op = new ModifierOp(Op.MODIFIER, add, mask);
  149. op.setChild(branch);
  150. op.next = next;
  151. return op;
  152. }
  153. static ConditionOp createCondition(Op next, int ref, Op conditionflow, Op yesflow, Op noflow) {
  154. if (Op.COUNT) Op.nofinstances ++;
  155. ConditionOp op = new ConditionOp(Op.CONDITION, ref, conditionflow, yesflow, noflow);
  156. op.next = next;
  157. return op;
  158. }
  159. int type;
  160. Op next = null;
  161. protected Op(int type) {
  162. this.type = type;
  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. static class CharOp extends Op {
  188. int charData;
  189. CharOp(int type, int data) {
  190. super(type);
  191. this.charData = data;
  192. }
  193. int getData() {
  194. return this.charData;
  195. }
  196. }
  197. // ================================================================
  198. static class UnionOp extends Op {
  199. Vector branches;
  200. UnionOp(int type, int size) {
  201. super(type);
  202. this.branches = new Vector(size);
  203. }
  204. void addElement(Op op) {
  205. this.branches.addElement(op);
  206. }
  207. int size() {
  208. return this.branches.size();
  209. }
  210. Op elementAt(int index) {
  211. return (Op)this.branches.elementAt(index);
  212. }
  213. }
  214. // ================================================================
  215. static class ChildOp extends Op {
  216. Op child;
  217. ChildOp(int type) {
  218. super(type);
  219. }
  220. void setChild(Op child) {
  221. this.child = child;
  222. }
  223. Op getChild() {
  224. return this.child;
  225. }
  226. }
  227. // ================================================================
  228. static class ModifierOp extends ChildOp {
  229. int v1;
  230. int v2;
  231. ModifierOp(int type, int v1, int v2) {
  232. super(type);
  233. this.v1 = v1;
  234. this.v2 = v2;
  235. }
  236. int getData() {
  237. return this.v1;
  238. }
  239. int getData2() {
  240. return this.v2;
  241. }
  242. }
  243. // ================================================================
  244. static class RangeOp extends Op {
  245. Token tok;
  246. RangeOp(int type, Token tok) {
  247. super(type);
  248. this.tok = tok;
  249. }
  250. RangeToken getToken() {
  251. return (RangeToken)this.tok;
  252. }
  253. }
  254. // ================================================================
  255. static class StringOp extends Op {
  256. String string;
  257. StringOp(int type, String literal) {
  258. super(type);
  259. this.string = literal;
  260. }
  261. String getString() {
  262. return this.string;
  263. }
  264. }
  265. // ================================================================
  266. static class ConditionOp extends Op {
  267. int refNumber;
  268. Op condition;
  269. Op yes;
  270. Op no;
  271. ConditionOp(int type, int refno, Op conditionflow, Op yesflow, Op noflow) {
  272. super(type);
  273. this.refNumber = refno;
  274. this.condition = conditionflow;
  275. this.yes = yesflow;
  276. this.no = noflow;
  277. }
  278. }
  279. }