| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.regex; |
| 9 |
|
| 10 |
|
| 11 |
import net.sf.saxon.expr.sort.EmptyIntIterator; |
| 12 |
import net.sf.saxon.regex.charclass.CharacterClass; |
| 13 |
import net.sf.saxon.regex.charclass.EmptyCharacterClass; |
| 14 |
import net.sf.saxon.regex.charclass.IntSetCharacterClass; |
| 15 |
import net.sf.saxon.regex.charclass.SingletonCharacterClass; |
| 16 |
import net.sf.saxon.trans.UncheckedXPathException; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 19 |
import net.sf.saxon.z.*; |
| 20 |
|
| 21 |
import java.util.Iterator; |
| 22 |
import java.util.List; |
| 23 |
import java.util.Stack; |
| 24 |
import java.util.function.IntPredicate; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public abstract class Operation { |
| 31 |
|
| 32 |
public static final int MATCHES_ZLS_AT_START = 1; |
| 33 |
public static final int MATCHES_ZLS_AT_END = 2; |
| 34 |
public static final int MATCHES_ZLS_ANYWHERE = 7; |
| 35 |
public static final int MATCHES_ZLS_NEVER = 1024; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public abstract IntIterator iterateMatches(REMatcher matcher, int position); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public int getMatchLength() { |
| 55 |
return -1; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public int getMinimumMatchLength() { |
| 64 |
int fixed = getMatchLength(); |
| 65 |
return fixed < 0 ? 0 : fixed; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public abstract int matchesEmptyString(); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public boolean containsCapturingExpressions() { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public CharacterClass getInitialCharacterClass(boolean caseBlind) { |
| 114 |
return EmptyCharacterClass.getComplement(); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public Operation optimize(REProgram program, REFlags flags) { |
| 125 |
return this; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public abstract String display(); |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public static class OpChoice extends Operation { |
| 141 |
|
| 142 |
List<Operation> branches; |
| 143 |
|
| 144 |
public OpChoice(List<Operation> branches) { |
| 145 |
this.branches = branches; |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public int getMatchLength() { |
| 150 |
int fixed = branches.get(0).getMatchLength(); |
| 151 |
for (int i = 1; i < branches.size(); i++) { |
| 152 |
if (branches.get(i).getMatchLength() != fixed) { |
| 153 |
return -1; |
| 154 |
} |
| 155 |
} |
| 156 |
return fixed; |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
public int getMinimumMatchLength() { |
| 161 |
int min = branches.get(0).getMinimumMatchLength(); |
| 162 |
for (int i = 1; i < branches.size(); i++) { |
| 163 |
int m = branches.get(i).getMinimumMatchLength(); |
| 164 |
if (m < min) { |
| 165 |
min = m; |
| 166 |
} |
| 167 |
} |
| 168 |
return min; |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public int matchesEmptyString() { |
| 173 |
int m = 0; |
| 174 |
for (Operation branch : branches) { |
| 175 |
int b = branch.matchesEmptyString(); |
| 176 |
if (b != MATCHES_ZLS_NEVER) { |
| 177 |
m |= b; |
| 178 |
} |
| 179 |
} |
| 180 |
return m; |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public boolean containsCapturingExpressions() { |
| 185 |
for (Operation o : branches) { |
| 186 |
if (o instanceof OpCapture || o.containsCapturingExpressions()) { |
| 187 |
return true; |
| 188 |
} |
| 189 |
} |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public CharacterClass getInitialCharacterClass(boolean caseBlind) { |
| 195 |
CharacterClass result = EmptyCharacterClass.getInstance(); |
| 196 |
for (Operation o : branches) { |
| 197 |
result = RECompiler.makeUnion(result, o.getInitialCharacterClass(caseBlind)); |
| 198 |
} |
| 199 |
return result; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
@Override |
| 204 |
public Operation optimize(REProgram program, REFlags flags) { |
| 205 |
for (int i=0; i<branches.size(); i++) { |
| 206 |
Operation o1 = branches.get(i); |
| 207 |
Operation o2 = o1.optimize(program, flags); |
| 208 |
if (o1 != o2) { |
| 209 |
branches.set(i, o2); |
| 210 |
} |
| 211 |
} |
| 212 |
return this; |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public IntIterator iterateMatches(final REMatcher matcher, final int position) { |
| 217 |
return new IntIterator() { |
| 218 |
Iterator<Operation> branchIter = branches.iterator(); |
| 219 |
IntIterator currentIter = null; |
| 220 |
Operation currentOp = null; |
| 221 |
|
| 222 |
public boolean hasNext() { |
| 223 |
while (true) { |
| 224 |
if (currentIter == null) { |
| 225 |
if (branchIter.hasNext()) { |
| 226 |
matcher.clearCapturedGroupsBeyond(position); |
| 227 |
currentOp = branchIter.next(); |
| 228 |
currentIter = currentOp.iterateMatches(matcher, position); |
| 229 |
} else { |
| 230 |
return false; |
| 231 |
} |
| 232 |
} |
| 233 |
if (currentIter.hasNext()) { |
| 234 |
return true; |
| 235 |
} else { |
| 236 |
currentIter = null; |
| 237 |
|
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
public int next() { |
| 243 |
return currentIter.next(); |
| 244 |
} |
| 245 |
|
| 246 |
}; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
@Override |
| 256 |
public String display() { |
| 257 |
FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64); |
| 258 |
fsb.append("(?:"); |
| 259 |
boolean first = true; |
| 260 |
for (Operation branch : branches) { |
| 261 |
if (first) { |
| 262 |
first = false; |
| 263 |
} else { |
| 264 |
fsb.append('|'); |
| 265 |
} |
| 266 |
fsb.append(branch.display()); |
| 267 |
} |
| 268 |
fsb.append(")"); |
| 269 |
return fsb.toString(); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public static class OpSequence extends Operation { |
| 278 |
private List<Operation> operations; |
| 279 |
|
| 280 |
public OpSequence(List<Operation> operations) { |
| 281 |
this.operations = operations; |
| 282 |
} |
| 283 |
|
| 284 |
public List<Operation> getOperations() { |
| 285 |
return operations; |
| 286 |
} |
| 287 |
|
| 288 |
@Override |
| 289 |
public int getMatchLength() { |
| 290 |
int len = 0; |
| 291 |
for (Operation o : operations) { |
| 292 |
int i = o.getMatchLength(); |
| 293 |
if (i == -1) { |
| 294 |
return -1; |
| 295 |
} |
| 296 |
len += i; |
| 297 |
} |
| 298 |
return len; |
| 299 |
} |
| 300 |
|
| 301 |
@Override |
| 302 |
public int getMinimumMatchLength() { |
| 303 |
int len = 0; |
| 304 |
for (Operation o : operations) { |
| 305 |
len += o.getMinimumMatchLength(); |
| 306 |
} |
| 307 |
return len; |
| 308 |
} |
| 309 |
|
| 310 |
@Override |
| 311 |
public int matchesEmptyString() { |
| 312 |
|
| 313 |
|
| 314 |
boolean matchesEmptyAnywhere = true; |
| 315 |
boolean matchesEmptyNowhere = false; |
| 316 |
for (Operation o : operations) { |
| 317 |
int m = o.matchesEmptyString(); |
| 318 |
if (m == MATCHES_ZLS_NEVER) { |
| 319 |
return MATCHES_ZLS_NEVER; |
| 320 |
} |
| 321 |
if (m != MATCHES_ZLS_ANYWHERE) { |
| 322 |
matchesEmptyAnywhere = false; |
| 323 |
break; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
if (matchesEmptyAnywhere) { |
| 328 |
return MATCHES_ZLS_ANYWHERE; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
boolean matchesBOL = true; |
| 334 |
for (Operation o : operations) { |
| 335 |
if ((o.matchesEmptyString() & MATCHES_ZLS_AT_START) == 0) { |
| 336 |
matchesBOL = false; |
| 337 |
break; |
| 338 |
} |
| 339 |
} |
| 340 |
if (matchesBOL) { |
| 341 |
return MATCHES_ZLS_AT_START; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
boolean matchesEOL = true; |
| 347 |
|
| 348 |
for (Operation o : operations) { |
| 349 |
if ((o.matchesEmptyString() & MATCHES_ZLS_AT_END) == 0) { |
| 350 |
matchesEOL = false; |
| 351 |
break; |
| 352 |
} |
| 353 |
} |
| 354 |
if (matchesEOL) { |
| 355 |
return MATCHES_ZLS_AT_END; |
| 356 |
} |
| 357 |
|
| 358 |
return 0; |
| 359 |
} |
| 360 |
|
| 361 |
@Override |
| 362 |
public boolean containsCapturingExpressions() { |
| 363 |
for (Operation o : operations) { |
| 364 |
if (o instanceof OpCapture || o.containsCapturingExpressions()) { |
| 365 |
return true; |
| 366 |
} |
| 367 |
} |
| 368 |
return false; |
| 369 |
} |
| 370 |
|
| 371 |
@Override |
| 372 |
public CharacterClass getInitialCharacterClass(boolean caseBlind) { |
| 373 |
CharacterClass result = EmptyCharacterClass.getInstance(); |
| 374 |
for (Operation o : operations) { |
| 375 |
result = RECompiler.makeUnion(result, o.getInitialCharacterClass(caseBlind)); |
| 376 |
if (o.matchesEmptyString() == MATCHES_ZLS_NEVER) { |
| 377 |
return result; |
| 378 |
} |
| 379 |
} |
| 380 |
return result; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
@Override |
| 390 |
public String display() { |
| 391 |
FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64); |
| 392 |
for (Operation op : operations) { |
| 393 |
fsb.append(op.display()); |
| 394 |
} |
| 395 |
return fsb.toString(); |
| 396 |
} |
| 397 |
|
| 398 |
@Override |
| 399 |
public Operation optimize(REProgram program, REFlags flags) { |
| 400 |
if (operations.size() == 0) { |
| 401 |
return new OpNothing(); |
| 402 |
} else if (operations.size() == 1) { |
| 403 |
return operations.get(0); |
| 404 |
} else { |
| 405 |
for (int i=0; i<operations.size()-1; i++) { |
| 406 |
Operation o1 = operations.get(i); |
| 407 |
Operation o2 = o1.optimize(program, flags); |
| 408 |
if (o1 != o2) { |
| 409 |
operations.set(i, o2); |
| 410 |
} |
| 411 |
if (o2 instanceof OpRepeat) { |
| 412 |
Operation o1r = ((OpRepeat)o1).getRepeatedOperation(); |
| 413 |
if (o1r instanceof OpAtom || o1r instanceof OpCharClass) { |
| 414 |
Operation o2r = operations.get(i+1); |
| 415 |
if (((OpRepeat)o1).min == ((OpRepeat)o1).max || |
| 416 |
RECompiler.noAmbiguity(o1r, o2r, flags.isCaseIndependent(), !((OpRepeat)o1).greedy)) { |
| 417 |
operations.set(i, new OpUnambiguousRepeat(o1r, ((OpRepeat)o1).min, ((OpRepeat)o1).max)); |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
return this; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
} |
| 427 |
|
| 428 |
@Override |
| 429 |
public IntIterator iterateMatches(final REMatcher matcher, final int position) { |
| 430 |
|
| 431 |
|
| 432 |
final Stack<IntIterator> iterators = new Stack<>(); |
| 433 |
final REMatcher.State savedState = |
| 434 |
containsCapturingExpressions() ? matcher.captureState() : null; |
| 435 |
final int backtrackingLimit = matcher.getProgram().getBacktrackingLimit(); |
| 436 |
|
| 437 |
return new IntIterator() { |
| 438 |
|
| 439 |
private boolean primed = false; |
| 440 |
private int nextPos; |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
private int advance() { |
| 453 |
int counter = 0; |
| 454 |
while (!iterators.isEmpty()) { |
| 455 |
IntIterator top = iterators.peek(); |
| 456 |
while (top.hasNext()) { |
| 457 |
int p = top.next(); |
| 458 |
matcher.clearCapturedGroupsBeyond(p); |
| 459 |
int i = iterators.size(); |
| 460 |
if (i >= operations.size()) { |
| 461 |
return p; |
| 462 |
} |
| 463 |
top = operations.get(i).iterateMatches(matcher, p); |
| 464 |
iterators.push(top); |
| 465 |
} |
| 466 |
iterators.pop(); |
| 467 |
if (backtrackingLimit >=0 && counter++ > backtrackingLimit) { |
| 468 |
throw new UncheckedXPathException(new XPathException( |
| 469 |
"Regex backtracking limit exceeded processing " + |
| 470 |
matcher.operation.display() + ". Simplify the regular expression, " |
| 471 |
+ "or set Feature.REGEX_BACKTRACKING_LIMIT to -1 to remove this limit." )); |
| 472 |
} |
| 473 |
} |
| 474 |
if (savedState != null) { |
| 475 |
matcher.resetState(savedState); |
| 476 |
} |
| 477 |
|
| 478 |
return -1; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
public boolean hasNext() { |
| 483 |
if (!primed) { |
| 484 |
iterators.push(operations.get(0).iterateMatches(matcher, position)); |
| 485 |
primed = true; |
| 486 |
} |
| 487 |
nextPos = advance(); |
| 488 |
return nextPos >= 0; |
| 489 |
} |
| 490 |
|
| 491 |
public int next() { |
| 492 |
return nextPos; |
| 493 |
} |
| 494 |
}; |
| 495 |
} |
| 496 |
|
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
public static class OpCharClass extends Operation { |
| 504 |
private IntPredicate predicate; |
| 505 |
|
| 506 |
public OpCharClass(IntPredicate predicate) { |
| 507 |
this.predicate = predicate; |
| 508 |
} |
| 509 |
|
| 510 |
public IntPredicate getPredicate() { |
| 511 |
return predicate; |
| 512 |
} |
| 513 |
|
| 514 |
@Override |
| 515 |
public int getMatchLength() { |
| 516 |
return 1; |
| 517 |
} |
| 518 |
|
| 519 |
@Override |
| 520 |
public int matchesEmptyString() { |
| 521 |
return MATCHES_ZLS_NEVER; |
| 522 |
} |
| 523 |
|
| 524 |
@Override |
| 525 |
public CharacterClass getInitialCharacterClass(boolean caseBlind) { |
| 526 |
if (predicate instanceof CharacterClass) { |
| 527 |
return (CharacterClass)predicate; |
| 528 |
} else { |
| 529 |
return super.getInitialCharacterClass(caseBlind); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
@Override |
| 534 |
public IntIterator iterateMatches(REMatcher matcher, int position) { |
| 535 |
|
| 536 |
UnicodeString in = matcher.search; |
| 537 |
if (position < in.uLength() && predicate.test(in.uCharAt(position))) { |
| 538 |
return new IntSingletonIterator(position + 1); |
| 539 |
} else { |
| 540 |
return EmptyIntIterator.getInstance(); |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
@Override |
| 551 |
public String display() { |
| 552 |
if (predicate instanceof IntSetPredicate) { |
| 553 |
IntSet s = ((IntSetPredicate)predicate).getIntSet(); |
| 554 |
if (s instanceof IntSingletonSet) { |
| 555 |
return "" + (char)((IntSingletonSet)s).getMember(); |
| 556 |
} else if (s instanceof IntRangeSet) { |
| 557 |
FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64); |
| 558 |
IntRangeSet irs = (IntRangeSet)s; |
| 559 |
fsb.append("["); |
| 560 |
for (int i=0; i<irs.getNumberOfRanges(); i++) { |
| 561 |
fsb.append((char)irs.getStartPoints()[1]); |
| 562 |
fsb.append("-"); |
| 563 |
fsb.append((char)irs.getEndPoints()[1]); |
| 564 |
} |
| 565 |
fsb.append("["); |
| 566 |
return fsb.toString(); |
| 567 |
} else { |
| 568 |
return "[....]"; |
| 569 |
} |
| 570 |
} else { |
| 571 |
return "[....]"; |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
public static class OpAtom extends Operation { |
| 581 |
private UnicodeString atom; |
| 582 |
private int len; |
| 583 |
|
| 584 |
public OpAtom(UnicodeString atom) { |
| 585 |
this.atom = atom; |
| 586 |
this.len = atom.uLength(); |
| 587 |
} |
| 588 |
|
| 589 |
public UnicodeString getAtom() { |
| 590 |
return atom; |
| 591 |
} |
| 592 |
|
| 593 |
@Override |
| 594 |
public int getMatchLength() { |
| 595 |
return len; |
| 596 |
} |
| 597 |
|
| 598 |
@Override |
| 599 |
public int matchesEmptyString() { |
| 600 |
return len == 0 ? MATCHES_ZLS_ANYWHERE : MATCHES_ZLS_NEVER; |
| 601 |
} |
| 602 |
|
| 603 |
@Override |
| 604 |
public CharacterClass getInitialCharacterClass(boolean caseBlind) { |
| 605 |
if (len == 0) { |
| 606 |
return EmptyCharacterClass.getInstance(); |
| 607 |
} else if (caseBlind) { |
| 608 |
IntSet set; |
| 609 |
int ch = atom.uCharAt(0); |
| 610 |
int[] variants = CaseVariants.getCaseVariants(ch); |
| 611 |
if (variants.length > 0) { |
| 612 |
set = new IntHashSet(variants.length); |
| 613 |
set.add(ch); |
| 614 |
for (int v : variants) { |
| 615 |
set.add(v); |
| 616 |
} |
| 617 |
return new IntSetCharacterClass(set); |
| 618 |
} |
| 619 |
} |
| 620 |
return new SingletonCharacterClass(atom.uCharAt(0)); |
| 621 |
} |
| 622 |
|
| 623 |
@Override |
| 624 |
public IntIterator iterateMatches(REMatcher matcher, int position) { |
| 625 |
UnicodeString in = matcher.search; |
| 626 |
if (position + len > in.uLength()) { |
| 627 |
return EmptyIntIterator.getInstance(); |
| 628 |
} |
| 629 |
if (matcher.program.flags.isCaseIndependent()) { |
| 630 |
for (int i = 0; i < len; i++) { |
| 631 |
if (!matcher.equalCaseBlind(in.uCharAt(position + i), atom.uCharAt(i))) { |
| 632 |
return EmptyIntIterator.getInstance(); |
| 633 |
} |
| 634 |
} |
| 635 |
} else { |
| 636 |
for (int i = 0; i < len; i++) { |
| 637 |
if (in.uCharAt(position + i) != atom.uCharAt(i)) { |
| 638 |
return EmptyIntIterator.getInstance(); |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
return new IntSingletonIterator(position + len); |
| 643 |
} |
| 644 |
|
| 645 |
public String display() { |
| 646 |
return atom.toString(); |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
public static class OpGreedyFixed extends OpRepeat { |
| 656 |
private int len; |
| 657 |
|
| 658 |
public OpGreedyFixed(Operation op, int min, int max, int len) { |
| 659 |
super(op, min, max, true); |
| 660 |
this.len = len; |
| 661 |
} |
| 662 |
|
| 663 |
@Override |
| 664 |
public int getMatchLength() { |
| 665 |
return min == max ? min * len : -1; |
| 666 |
} |
| 667 |
|
| 668 |
@Override |
| 669 |
public int matchesEmptyString() { |
| 670 |
if (min == 0) { |
| 671 |
return MATCHES_ZLS_ANYWHERE; |
| 672 |
} |
| 673 |
return op.matchesEmptyString(); |
| 674 |
} |
| 675 |
|
| 676 |
@Override |
| 677 |
public Operation optimize(REProgram program, REFlags flags) { |
| 678 |
if (max == 0) { |
| 679 |
return new OpNothing(); |
| 680 |
} |
| 681 |
if (op.getMatchLength() == 0) { |
| 682 |
return op; |
| 683 |
} |
| 684 |
op = op.optimize(program, flags); |
| 685 |
return this; |
| 686 |
} |
| 687 |
|
| 688 |
@Override |
| 689 |
public IntIterator iterateMatches(REMatcher matcher, int position) { |
| 690 |
int guard = matcher.search.uLength(); |
| 691 |
if (max < Integer.MAX_VALUE) { |
| 692 |
guard = java.lang.Math.min(guard, position + len * max); |
| 693 |
} |
| 694 |
if (position >= guard && min > 0) { |
| 695 |
return EmptyIntIterator.getInstance(); |
| 696 |
} |
| 697 |
|
| 698 |
int p = position; |
| 699 |
int matches = 0; |
| 700 |
while (p <= guard) { |
| 701 |
IntIterator it = op.iterateMatches(matcher, p); |
| 702 |
boolean matched = false; |
| 703 |
if (it.hasNext()) { |
| 704 |
matched = true; |
| 705 |
it.next(); |
| 706 |
} |
| 707 |
if (matched) { |
| 708 |
matches++; |
| 709 |
p += len; |
| 710 |
if (matches == max) { |
| 711 |
break; |
| 712 |
} |
| 713 |
} else { |
| 714 |
break; |
| 715 |
} |
| 716 |
} |
| 717 |
if (matches < min) { |
| 718 |
return EmptyIntIterator.getInstance(); |
| 719 |
} |
| 720 |
|
| 721 |
return new IntStepIterator(p, -len, position + len * min); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
public static class OpUnambiguousRepeat extends OpRepeat { |
| 739 |
|
| 740 |
public OpUnambiguousRepeat(Operation op, int min, int max) { |
| 741 |
super(op, min, max, true); |
| 742 |
} |
| 743 |
|
| 744 |
@Override |
| 745 |
public int matchesEmptyString() { |
| 746 |
if (min == 0) { |
| 747 |
return MATCHES_ZLS_ANYWHERE; |
| 748 |
} |
| 749 |
return op.matchesEmptyString(); |
| 750 |
} |
| 751 |
|
| 752 |
@Override |
| 753 |
public int getMatchLength() { |
| 754 |
if (op.getMatchLength() != -1 && min == max) { |
| 755 |
return op.getMatchLength()*min; |
| 756 |
} else { |
| 757 |
return -1; |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
@Override |
| 762 |
public Operation optimize(REProgram program, REFlags flags) { |
| 763 |
op = op.optimize(program, flags); |
| 764 |
return this; |
| 765 |
} |
| 766 |
|
| 767 |
@Override |
| 768 |
public IntIterator iterateMatches(REMatcher matcher, int position) { |
| 769 |
int guard = matcher.search.uLength(); |
| 770 |
|
| 771 |
int p = position; |
| 772 |
int matches = 0; |
| 773 |
while (matches < max && p <= guard) { |
| 774 |
IntIterator it = op.iterateMatches(matcher, p); |
| 775 |
if (it.hasNext()) { |
| 776 |
matches++; |
| 777 |
p = it.next(); |
| 778 |
} else { |
| 779 |
break; |
| 780 |
} |
| 781 |
} |
| 782 |
if (matches < min) { |
| 783 |
return EmptyIntIterator.getInstance(); |
| 784 |
} else { |
| 785 |
return new IntSingletonIterator(p); |
| 786 |
} |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
public static class OpRepeat extends Operation { |
| 797 |
protected Operation op; |
| 798 |
protected int min; |
| 799 |
protected int max; |
| 800 |
protected boolean greedy; |
| 801 |
|
| 802 |
public OpRepeat(Operation op, int min, int max, boolean greedy) { |
| 803 |
this.op = op; |
| 804 |
this.min = min; |
| 805 |
this.max = max; |
| 806 |
this.greedy = greedy; |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
public Operation getRepeatedOperation() { |
| 815 |
return op; |
| 816 |
} |
| 817 |
|
| 818 |
@Override |
| 819 |
public int matchesEmptyString() { |
| 820 |
if (min == 0) { |
| 821 |
return MATCHES_ZLS_ANYWHERE; |
| 822 |
} |
| 823 |
return op.matchesEmptyString(); |
| 824 |
} |
| 825 |
|
| 826 |
@Override |
| 827 |
public boolean containsCapturingExpressions() { |
| 828 |
return op instanceof OpCapture || op.containsCapturingExpressions(); |
| 829 |
} |
| 830 |
|
| 831 |
@Override |
| 832 |
public CharacterClass getInitialCharacterClass(boolean caseBlind) { |
| 833 |
return op.getInitialCharacterClass(caseBlind); |
| 834 |
} |
| 835 |
|
| 836 |
@Override |
| 837 |
public int getMatchLength() { |
| 838 |
return min == max && op.getMatchLength() >= 0 ? min * op.getMatchLength() : -1; |
| 839 |
} |
| 840 |
|
| 841 |
@Override |
| 842 |
public int getMinimumMatchLength() { |
| 843 |
return min * op.getMinimumMatchLength(); |
| 844 |
} |
| 845 |
|
| 846 |
@Override |
| 847 |
public Operation optimize(REProgram program, REFlags flags) { |
| 848 |
op = op.optimize(program, flags); |
| 849 |
if (min == 0 && op.matchesEmptyString() == MATCHES_ZLS_ANYWHERE) { |
| 850 |
|
| 851 |
min = 1; |
| 852 |
} |
| 853 |
return this; |
| 854 |
} |
| 855 |
|
| 856 |
@Override |
| 857 |
public IntIterator iterateMatches(final REMatcher matcher, int position) { |
| 858 |
final Stack<IntIterator> iterators = new Stack<>(); |
| 859 |
final Stack<Integer> positions = new Stack<>(); |
| 860 |
final int bound = Math.min(max, matcher.search.uLength() - position + 1); |
| 861 |
int p = position; |
| 862 |
if (greedy) { |
| 863 |
|
| 864 |
if (min == 0 && !matcher.history.isDuplicateZeroLengthMatch(this, position)) { |
| 865 |
|
| 866 |
iterators.push(new IntSingletonIterator(position)); |
| 867 |
positions.push(p); |
| 868 |
} |
| 869 |
for (int i=0; i<bound; i++) { |
| 870 |
IntIterator it = op.iterateMatches(matcher, p); |
| 871 |
if (it.hasNext()) { |
| 872 |
p = it.next(); |
| 873 |
iterators.push(it); |
| 874 |
positions.push(p); |
| 875 |
} else if (iterators.isEmpty()) { |
| 876 |
return EmptyIntIterator.getInstance(); |
| 877 |
} else { |
| 878 |
break; |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
IntIterator base = new IntIterator() { |
| 883 |
boolean primed = true; |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
private void advance() { |
| 891 |
IntIterator top = iterators.peek(); |
| 892 |
if (top.hasNext()) { |
| 893 |
int p = top.next(); |
| 894 |
positions.pop(); |
| 895 |
positions.push(p); |
| 896 |
while (iterators.size() < bound) { |
| 897 |
IntIterator it = op.iterateMatches(matcher, p); |
| 898 |
if (it.hasNext()) { |
| 899 |
p = it.next(); |
| 900 |
iterators.push(it); |
| 901 |
positions.push(p); |
| 902 |
} else { |
| 903 |
break; |
| 904 |
} |
| 905 |
} |
| 906 |
} else { |
| 907 |
iterators.pop(); |
| 908 |
positions.pop(); |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
public boolean hasNext() { |
| 913 |
if (primed && iterators.size() >= min) { |
| 914 |
return !iterators.isEmpty(); |
| 915 |
} else if (iterators.isEmpty()) { |
| 916 |
return false; |
| 917 |
} else { |
| 918 |
do { |
| 919 |
advance(); |
| 920 |
} while (iterators.size() < min && !iterators.isEmpty()); |
| 921 |
return !iterators.isEmpty(); |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
public int next() { |
| 926 |
primed = false; |
| 927 |
return positions.peek(); |
| 928 |
} |
| 929 |
}; |
| 930 |
return new ForceProgressIterator(base); |
| 931 |
} else { |
| 932 |
|
| 933 |
|
| 934 |
IntIterator iter = new IntIterator() { |
| 935 |
private int pos = position; |
| 936 |
private int counter = 0; |
| 937 |
|
| 938 |
private void advance() { |
| 939 |
IntIterator it = op.iterateMatches(matcher, pos); |
| 940 |
if (it.hasNext()) { |
| 941 |
pos = it.next(); |
| 942 |
if (++counter > max) { |
| 943 |
pos = -1; |
| 944 |
} |
| 945 |
} else if (min == 0 && counter == 0) { |
| 946 |
counter++; |
| 947 |
} else { |
| 948 |
pos = -1; |
| 949 |
} |
| 950 |
} |
| 951 |
public boolean hasNext() { |
| 952 |
do { |
| 953 |
advance(); |
| 954 |
} while (counter < min && pos >= 0); |
| 955 |
return pos >= 0; |
| 956 |
} |
| 957 |
|
| 958 |
public int next() { |
| 959 |
return pos; |
| 960 |
} |
| 961 |
}; |
| 962 |
return new ForceProgressIterator(iter); |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
@Override |
| 973 |
public String display() { |
| 974 |
String quantifier; |
| 975 |
if (min==0 && max==Integer.MAX_VALUE) { |
| 976 |
quantifier = "*"; |
| 977 |
} else if (min==1 && max==Integer.MAX_VALUE) { |
| 978 |
quantifier = "+"; |
| 979 |
} else if (min==0 && max==1) { |
| 980 |
quantifier = "?"; |
| 981 |
} else { |
| 982 |
quantifier = "{" + min + "," + max + "}"; |
| 983 |
} |
| 984 |
if (!greedy) { |
| 985 |
quantifier += "?"; |
| 986 |
} |
| 987 |
return op.display() + quantifier; |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
public static class OpReluctantFixed extends OpRepeat { |
| 997 |
private int len; |
| 998 |
|
| 999 |
public OpReluctantFixed(Operation op, int min, int max, int len) { |
| 1000 |
super(op, min, max, false); |
| 1001 |
this.len = len; |
| 1002 |
} |
| 1003 |
|
| 1004 |
@Override |
| 1005 |
public int getMatchLength() { |
| 1006 |
return min == max ? min * len : -1; |
| 1007 |
} |
| 1008 |
|
| 1009 |
@Override |
| 1010 |
public int matchesEmptyString() { |
| 1011 |
if (min == 0) { |
| 1012 |
return MATCHES_ZLS_ANYWHERE; |
| 1013 |
} |
| 1014 |
return op.matchesEmptyString(); |
| 1015 |
} |
| 1016 |
|
| 1017 |
@Override |
| 1018 |
public Operation optimize(REProgram program, REFlags flags) { |
| 1019 |
op = op.optimize(program, flags); |
| 1020 |
return this; |
| 1021 |
} |
| 1022 |
|
| 1023 |
@Override |
| 1024 |
public IntIterator iterateMatches(final REMatcher matcher, final int position) { |
| 1025 |
return new IntIterator() { |
| 1026 |
private int pos = position; |
| 1027 |
private int count = 0; |
| 1028 |
private boolean started = false; |
| 1029 |
|
| 1030 |
public boolean hasNext() { |
| 1031 |
if (!started) { |
| 1032 |
started = true; |
| 1033 |
while (count < min) { |
| 1034 |
IntIterator child = op.iterateMatches(matcher, pos); |
| 1035 |
if (child.hasNext()) { |
| 1036 |
pos = child.next(); |
| 1037 |
count++; |
| 1038 |
} else { |
| 1039 |
return false; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
return true; |
| 1043 |
} |
| 1044 |
if (count < max) { |
| 1045 |
matcher.clearCapturedGroupsBeyond(pos); |
| 1046 |
IntIterator child = op.iterateMatches(matcher, pos); |
| 1047 |
if (child.hasNext()) { |
| 1048 |
pos = child.next(); |
| 1049 |
count++; |
| 1050 |
return true; |
| 1051 |
} |
| 1052 |
} |
| 1053 |
return false; |
| 1054 |
} |
| 1055 |
|
| 1056 |
public int next() { |
| 1057 |
return pos; |
| 1058 |
} |
| 1059 |
}; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
public static class OpEndProgram extends Operation { |
| 1069 |
|
| 1070 |
@Override |
| 1071 |
public int getMatchLength() { |
| 1072 |
return 0; |
| 1073 |
} |
| 1074 |
|
| 1075 |
@Override |
| 1076 |
public int matchesEmptyString() { |
| 1077 |
return MATCHES_ZLS_ANYWHERE; |
| 1078 |
} |
| 1079 |
|
| 1080 |
@Override |
| 1081 |
public IntIterator iterateMatches(final REMatcher matcher, final int position) { |
| 1082 |
|
| 1083 |
|
| 1084 |
if (matcher.anchoredMatch) { |
| 1085 |
if (matcher.search.isEnd(position)) { |
| 1086 |
return new IntSingletonIterator(position); |
| 1087 |
} else { |
| 1088 |
return EmptyIntIterator.getInstance(); |
| 1089 |
} |
| 1090 |
} else { |
| 1091 |
matcher.setParenEnd(0, position); |
| 1092 |
return new IntSingletonIterator(position); |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
@Override |
| 1103 |
public String display() { |
| 1104 |
return "\\Z"; |
| 1105 |
} |
| 1106 |
} |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
public static class OpBOL extends Operation { |
| 1113 |
|
| 1114 |
@Override |
| 1115 |
public int getMatchLength() { |
| 1116 |
return 0; |
| 1117 |
} |
| 1118 |
|
| 1119 |
@Override |
| 1120 |
public int matchesEmptyString() { |
| 1121 |
return MATCHES_ZLS_AT_START; |
| 1122 |
} |
| 1123 |
|
| 1124 |
@Override |
| 1125 |
public IntIterator iterateMatches(final REMatcher matcher, final int position) { |
| 1126 |
|
| 1127 |
if (position != 0) { |
| 1128 |
|
| 1129 |
if (matcher.program.flags.isMultiLine()) { |
| 1130 |
|
| 1131 |
if (matcher.isNewline(position - 1) && !matcher.search.isEnd(position)) { |
| 1132 |
return new IntSingletonIterator(position); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
return EmptyIntIterator.getInstance(); |
| 1136 |
} |
| 1137 |
return new IntSingletonIterator(position); |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
@Override |
| 1147 |
public String display() { |
| 1148 |
return "^"; |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
public static class OpEOL extends Operation { |
| 1157 |
|
| 1158 |
public int getMatchLength() { |
| 1159 |
return 0; |
| 1160 |
} |
| 1161 |
|
| 1162 |
@Override |
| 1163 |
public int matchesEmptyString() { |
| 1164 |
return MATCHES_ZLS_AT_END; |
| 1165 |
} |
| 1166 |
|
| 1167 |
@Override |
| 1168 |
public IntIterator iterateMatches(final REMatcher matcher, final int position) { |
| 1169 |
|
| 1170 |
|
| 1171 |
UnicodeString search = matcher.search; |
| 1172 |
if (matcher.program.flags.isMultiLine()) { |
| 1173 |
if (search.isEnd(0) || search.isEnd(position) || matcher.isNewline(position)) { |
| 1174 |
return new IntSingletonIterator(position); |
| 1175 |
} else { |
| 1176 |
return EmptyIntIterator.getInstance(); |
| 1177 |
} |
| 1178 |
} else { |
| 1179 |
|
| 1180 |
if (search.isEnd(0) || search.isEnd(position)) { |
| 1181 |
return new IntSingletonIterator(position); |
| 1182 |
} else { |
| 1183 |
return EmptyIntIterator.getInstance(); |
| 1184 |
} |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
@Override |
| 1195 |
public String display() { |
| 1196 |
return "$"; |
| 1197 |
} |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
public static class OpCapture extends Operation { |
| 1205 |
|
| 1206 |
public int groupNr; |
| 1207 |
public Operation childOp; |
| 1208 |
|
| 1209 |
public OpCapture(Operation childOp, int group) { |
| 1210 |
this.childOp = childOp; |
| 1211 |
this.groupNr = group; |
| 1212 |
} |
| 1213 |
|
| 1214 |
@Override |
| 1215 |
public int getMatchLength() { |
| 1216 |
return childOp.getMatchLength(); |
| 1217 |
} |
| 1218 |
|
| 1219 |
@Override |
| 1220 |
public int matchesEmptyString() { |
| 1221 |
return childOp.matchesEmptyString(); |
| 1222 |
} |
| 1223 |
|
| 1224 |
@Override |
| 1225 |
public Operation optimize(REProgram program, REFlags flags) { |
| 1226 |
childOp = childOp.optimize(program, flags); |
| 1227 |
return this; |
| 1228 |
} |
| 1229 |
|
| 1230 |
@Override |
| 1231 |
public IntIterator iterateMatches(final REMatcher matcher, final int position) { |
| 1232 |
if ((matcher.program.optimizationFlags & REProgram.OPT_HASBACKREFS) != 0) { |
| 1233 |
matcher.startBackref[groupNr] = position; |
| 1234 |
} |
| 1235 |
final IntIterator base = childOp.iterateMatches(matcher, position); |
| 1236 |
return new IntIterator() { |
| 1237 |
public boolean hasNext() { |
| 1238 |
return base.hasNext(); |
| 1239 |
} |
| 1240 |
|
| 1241 |
public int next() { |
| 1242 |
int next = base.next(); |
| 1243 |
|
| 1244 |
if (groupNr >= matcher.captureState.parenCount) { |
| 1245 |
matcher.captureState.parenCount = groupNr + 1; |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
matcher.setParenStart(groupNr, position); |
| 1251 |
matcher.setParenEnd(groupNr, next); |
| 1252 |
|
| 1253 |
if ((matcher.program.optimizationFlags & REProgram.OPT_HASBACKREFS) != 0) { |
| 1254 |
matcher.startBackref[groupNr] = position; |
| 1255 |
matcher.endBackref[groupNr] = next; |
| 1256 |
} |
| 1257 |
return next; |
| 1258 |
} |
| 1259 |
}; |
| 1260 |
} |
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
@Override |
| 1269 |
public String display() { |
| 1270 |
return "(" + childOp.display() + ")"; |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
public static class OpBackReference extends Operation { |
| 1279 |
|
| 1280 |
public int groupNr; |
| 1281 |
|
| 1282 |
public OpBackReference(int groupNr) { |
| 1283 |
this.groupNr = groupNr; |
| 1284 |
} |
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
@Override |
| 1297 |
public int matchesEmptyString() { |
| 1298 |
return 0; |
| 1299 |
} |
| 1300 |
|
| 1301 |
@Override |
| 1302 |
public IntIterator iterateMatches(final REMatcher matcher, final int position) { |
| 1303 |
|
| 1304 |
int s = matcher.startBackref[groupNr]; |
| 1305 |
int e = matcher.endBackref[groupNr]; |
| 1306 |
|
| 1307 |
|
| 1308 |
if (s == -1 || e == -1) { |
| 1309 |
return EmptyIntIterator.getInstance(); |
| 1310 |
} |
| 1311 |
|
| 1312 |
|
| 1313 |
if (s == e) { |
| 1314 |
return new IntSingletonIterator(position); |
| 1315 |
} |
| 1316 |
|
| 1317 |
|
| 1318 |
int l = e - s; |
| 1319 |
|
| 1320 |
|
| 1321 |
UnicodeString search = matcher.search; |
| 1322 |
if (search.isEnd(position + l - 1)) { |
| 1323 |
return EmptyIntIterator.getInstance(); |
| 1324 |
} |
| 1325 |
|
| 1326 |
|
| 1327 |
if (matcher.program.flags.isCaseIndependent()) { |
| 1328 |
|
| 1329 |
for (int i = 0; i < l; i++) { |
| 1330 |
if (!matcher.equalCaseBlind(search.uCharAt(position + i), search.uCharAt(s + i))) { |
| 1331 |
return EmptyIntIterator.getInstance(); |
| 1332 |
} |
| 1333 |
} |
| 1334 |
} else { |
| 1335 |
|
| 1336 |
for (int i = 0; i < l; i++) { |
| 1337 |
if (search.uCharAt(position + i) != search.uCharAt(s + i)) { |
| 1338 |
return EmptyIntIterator.getInstance(); |
| 1339 |
} |
| 1340 |
} |
| 1341 |
} |
| 1342 |
return new IntSingletonIterator(position + l); |
| 1343 |
} |
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
@Override |
| 1352 |
public String display() { |
| 1353 |
return "\\" + groupNr; |
| 1354 |
} |
| 1355 |
} |
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
public static class OpNothing extends Operation { |
| 1363 |
|
| 1364 |
public IntIterator iterateMatches(final REMatcher matcher, final int position) { |
| 1365 |
return new IntSingletonIterator(position); |
| 1366 |
} |
| 1367 |
|
| 1368 |
@Override |
| 1369 |
public int matchesEmptyString() { |
| 1370 |
return MATCHES_ZLS_ANYWHERE; |
| 1371 |
} |
| 1372 |
|
| 1373 |
@Override |
| 1374 |
public int getMatchLength() { |
| 1375 |
return 0; |
| 1376 |
} |
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
@Override |
| 1385 |
public String display() { |
| 1386 |
return "()"; |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
public static class OpTrace extends Operation { |
| 1395 |
|
| 1396 |
private Operation base; |
| 1397 |
private static int counter = 0; |
| 1398 |
|
| 1399 |
public OpTrace(Operation base) { |
| 1400 |
this.base = base; |
| 1401 |
} |
| 1402 |
|
| 1403 |
@Override |
| 1404 |
public IntIterator iterateMatches(REMatcher matcher, int position) { |
| 1405 |
final IntIterator baseIter = base.iterateMatches(matcher, position); |
| 1406 |
final int iterNr = counter++; |
| 1407 |
String clName = baseIter.getClass().getName(); |
| 1408 |
int lastDot = clName.lastIndexOf("."); |
| 1409 |
String iterName = clName.substring(lastDot + 1); |
| 1410 |
System.err.println("Iterating over " + base.getClass().getSimpleName() + " " + |
| 1411 |
base.display() + " at position " + position + " returning " + |
| 1412 |
iterName + " " + iterNr); |
| 1413 |
return new IntIterator() { |
| 1414 |
public boolean hasNext() { |
| 1415 |
boolean b = baseIter.hasNext(); |
| 1416 |
System.err.println("IntIterator " + iterNr + " hasNext() = " + b); |
| 1417 |
return b; |
| 1418 |
} |
| 1419 |
|
| 1420 |
public int next() { |
| 1421 |
int n = baseIter.next(); |
| 1422 |
System.err.println("IntIterator " + iterNr + " next() = " + n); |
| 1423 |
return n; |
| 1424 |
} |
| 1425 |
}; |
| 1426 |
} |
| 1427 |
|
| 1428 |
@Override |
| 1429 |
public int getMatchLength() { |
| 1430 |
return base.getMatchLength(); |
| 1431 |
} |
| 1432 |
|
| 1433 |
@Override |
| 1434 |
public int matchesEmptyString() { |
| 1435 |
return base.matchesEmptyString(); |
| 1436 |
} |
| 1437 |
|
| 1438 |
@Override |
| 1439 |
public Operation optimize(REProgram program, REFlags flags) { |
| 1440 |
base = base.optimize(program, flags); |
| 1441 |
return this; |
| 1442 |
} |
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
@Override |
| 1451 |
public String display() { |
| 1452 |
return base.display(); |
| 1453 |
} |
| 1454 |
} |
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
private static class ForceProgressIterator implements IntIterator { |
| 1465 |
private IntIterator base; |
| 1466 |
int countZeroLength = 0; |
| 1467 |
int currentPos = -1; |
| 1468 |
|
| 1469 |
public ForceProgressIterator(IntIterator base) { |
| 1470 |
this.base = base; |
| 1471 |
} |
| 1472 |
|
| 1473 |
public boolean hasNext() { |
| 1474 |
return countZeroLength <= 3 && base.hasNext(); |
| 1475 |
} |
| 1476 |
|
| 1477 |
public int next() { |
| 1478 |
int p = base.next(); |
| 1479 |
if (p == currentPos) { |
| 1480 |
countZeroLength++; |
| 1481 |
} else { |
| 1482 |
countZeroLength = 0; |
| 1483 |
currentPos = p; |
| 1484 |
} |
| 1485 |
return p; |
| 1486 |
} |
| 1487 |
} |
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
} |
| 1492 |
|