| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
package net.sf.saxon.regex; |
| 32 |
|
| 33 |
import java.util.function.IntPredicate; |
| 34 |
|
| 35 |
import java.util.List; |
| 36 |
|
| 37 |
|
| 38 |
|
| 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; |
| 48 |
IntPredicate initialCharClass; |
| 49 |
List<RegexPrecondition> preconditions = new java.util.ArrayList<RegexPrecondition>(); |
| 50 |
int minimumLength = 0; |
| 51 |
int fixedLength = -1; |
| 52 |
int optimizationFlags; |
| 53 |
int maxParens = -1; |
| 54 |
int backtrackingLimit = -1; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private void setOperation(Operation operation) { |
| 81 |
|
| 82 |
this.operation = operation; |
| 83 |
|
| 84 |
|
| 85 |
this.optimizationFlags = 0; |
| 86 |
this.prefix = null; |
| 87 |
|
| 88 |
this.operation = operation.optimize(this, flags); |
| 89 |
|
| 90 |
|
| 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 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public boolean isNullable() { |
| 163 |
int m = operation.matchesEmptyString(); |
| 164 |
return (m & Operation.MATCHES_ZLS_ANYWHERE) != 0; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public UnicodeString getPrefix() { |
| 175 |
return prefix; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
} |