Debian Codegraph @

net/sf/saxon/regex/REProgram.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 /*
9 * Licensed to the Apache Software Foundation (ASF) under one or more
10 * contributor license agreements. See the NOTICE file distributed with
11 * this work for additional information regarding copyright ownership.
12 * The ASF licenses this file to You under the Apache License, Version 2.0
13 * (the "License"); you may not use this file except in compliance with
14 * the License. You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24
25 /*
26 * Originally part of Apache's Jakarta project (downloaded January 2012),
27 * this file has been extensively modified for integration into Saxon by
28 * Michael Kay, Saxonica.
29 */
30
31 package net.sf.saxon.regex;
32
33 import java.util.function.IntPredicate;
34
35 import java.util.List;
36
37 /**
38 * A class that holds compiled regular expressions.
39 */
40
41 public class REProgram {
42 static final int OPT_HASBACKREFS = 1;
43 static final int OPT_HASBOL = 2;
44
45 Operation operation;
46 REFlags flags;
47 UnicodeString prefix; // Prefix string optimization
48 IntPredicate initialCharClass;
49 List<RegexPrecondition> preconditions = new java.util.ArrayList<RegexPrecondition>();
50 int minimumLength = 0;
51 int fixedLength = -1;
52 int optimizationFlags; // Optimization flags (REProgram.OPT_*)
53 int maxParens = -1;
54 int backtrackingLimit = -1;
55
56 /**
57 * Constructs a program object from a character array
58 * @param operation Array with RE opcode instructions in it. The "next"
59 * @param parens Count of parens in the program
60 * pointers within the operations must already have been converted to absolute
61 * offsets.
62 * @param flags the regular expression flags
63 */
64 public REProgram(Operation operation, int parens, REFlags flags) {
65 this.flags = flags;
66 setOperation(operation);
67 this.maxParens = parens;
68 }
69
70 /**
71 * Sets a new regular expression program to run. It is this method which
72 * performs any special compile-time search optimizations. Currently only
73 * two optimizations are in place - one which checks for backreferences
74 * (so that they can be lazily allocated) and another which attempts to
75 * find an prefix anchor string so that substantial amounts of input can
76 * potentially be skipped without running the actual program.
77 *
78 * @param operation Program instruction buffer
79 */
80 private void setOperation(Operation operation) {
81 // Save reference to instruction array
82 this.operation = operation;
83
84 // Initialize other program-related variables
85 this.optimizationFlags = 0;
86 this.prefix = null;
87
88 this.operation = operation.optimize(this, flags);
89
90 // Try various compile-time optimizations
91
92 if (operation instanceof Operation.OpSequence) {
93 Operation first = ((Operation.OpSequence)operation).getOperations().get(0);
94 if (first instanceof Operation.OpBOL) {
95 optimizationFlags |= REProgram.OPT_HASBOL;
96 } else if (first instanceof Operation.OpAtom) {
97 prefix = ((Operation.OpAtom)first).getAtom();
98 } else if (first instanceof Operation.OpCharClass) {
99 initialCharClass = ((Operation.OpCharClass)first).getPredicate();
100 }
101 addPrecondition(operation, -1, 0);
102 }
103
104 minimumLength = operation.getMinimumMatchLength();
105 fixedLength = operation.getMatchLength();
106
107 }
108
109 public void setBacktrackingLimit(int limit) {
110 this.backtrackingLimit = limit;
111 }
112
113 public int getBacktrackingLimit() {
114 return backtrackingLimit;
115 }
116
117 private void addPrecondition(Operation op, int fixedPosition, int minPosition) {
118 if (op instanceof Operation.OpAtom || op instanceof Operation.OpCharClass) {
119 preconditions.add(new RegexPrecondition(op, fixedPosition, minPosition));
120 } else if (op instanceof Operation.OpRepeat && ((Operation.OpRepeat) op).min >= 1) {
121 Operation.OpRepeat parent = (Operation.OpRepeat) op;
122 Operation child = parent.op;
123 if (child instanceof Operation.OpAtom || child instanceof Operation.OpCharClass) {
124 if (parent.min == 1) {
125 preconditions.add(new RegexPrecondition(parent, fixedPosition, minPosition));
126 } else {
127 Operation.OpRepeat parent2 = new Operation.OpRepeat(child, parent.min, parent.min, true);
128 preconditions.add(new RegexPrecondition(parent2, fixedPosition, minPosition));
129 }
130 } else {
131 addPrecondition(child, fixedPosition, minPosition);
132 }
133 } else if (op instanceof Operation.OpCapture) {
134 addPrecondition(((Operation.OpCapture)op).childOp, fixedPosition, minPosition);
135 } else if (op instanceof Operation.OpSequence) {
136 int fp = fixedPosition;
137 int mp = minPosition;
138 for (Operation o : ((Operation.OpSequence)op).getOperations()) {
139 if (o instanceof Operation.OpBOL) {
140 fp = 0;
141 }
142 addPrecondition(o, fp, mp);
143 if (fp != -1 && o.getMatchLength() != -1) {
144 fp += o.getMatchLength();
145 } else {
146 fp = -1;
147 }
148 mp += o.getMinimumMatchLength();
149 }
150 }
151 }
152
153 /**
154 * Ask whether the regular expression is known statically to match a zero length string
155 *
156 * @return true if the regex is known statically to match a zero length string. If
157 * the result is true, then there is definitely a match; if the result is false, then
158 * it is not known statically whether there is a match (this arises when the expression
159 * contains back-references).
160 */
161
162 public boolean isNullable() {
163 int m = operation.matchesEmptyString();
164 return (m & Operation.MATCHES_ZLS_ANYWHERE) != 0;
165 }
166
167 /**
168 * Returns a copy of the prefix of current regular expression program
169 * in a character array. If there is no prefix, or there is no program
170 * compiled yet, <code>getPrefix</code> will return null.
171 *
172 * @return A copy of the prefix of current compiled RE program
173 */
174 public UnicodeString getPrefix() {
175 return prefix;
176 }
177
178
179 }