@@ -29,17 +29,15 @@ public class PatternAcceptor {
2929 private static final String extendedSyntaxCharacters = "^$.*+?()[|" ;
3030 private static final String [] controlCharacters = new String []{"a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , "y" , "z" , "A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" , "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" , "U" , "V" , "W" , "X" , "Y" , "Z" };
3131
32- private static HashMap <String , Integer > constructControlEscapeCharacterValues () {
33- HashMap <String , Integer > map = new HashMap <>();
34- map .put ("f" , (int ) '\f' );
35- map .put ("n" , (int ) '\n' );
36- map .put ("r" , (int ) '\r' );
37- map .put ("t" , (int ) '\t' );
38- map .put ("v" , 0x11 ); // \v in javascript
39- return map ;
40- }
32+ private static final HashMap <String , Integer > controlEscapeCharacterValues = new HashMap <>();
4133
42- private static final HashMap <String , Integer > controlEscapeCharacterValues = constructControlEscapeCharacterValues ();
34+ static {
35+ controlEscapeCharacterValues .put ("f" , (int ) '\f' );
36+ controlEscapeCharacterValues .put ("n" , (int ) '\n' );
37+ controlEscapeCharacterValues .put ("r" , (int ) '\r' );
38+ controlEscapeCharacterValues .put ("t" , (int ) '\t' );
39+ controlEscapeCharacterValues .put ("v" , 0x11 ); // \v in javascript
40+ }
4341
4442 private static final String [] controlEscapeCharacters = controlEscapeCharacterValues .keySet ().toArray (new String [0 ]);
4543
@@ -86,21 +84,21 @@ public boolean verifyBackreferences() {
8684 return true ;
8785 }
8886
89- public Context goDeeper () {
87+ public Context backtrackOnFailure () {
9088 return new Context (this );
9189 }
9290
93- public boolean goDeeper (F <Context , Boolean > predicate ) {
94- Context context = this .goDeeper ();
91+ public boolean backtrackOnFailure (F <Context , Boolean > predicate ) {
92+ Context context = this .backtrackOnFailure ();
9593 boolean accepted = predicate .apply (context );
9694 if (accepted ) {
9795 this .absorb (context );
9896 }
9997 return accepted ;
10098 }
10199
102- public <B > Maybe <B > goDeeperMaybe (F <Context , Maybe <B >> predicate ) {
103- Context context = this .goDeeper ();
100+ public <B > Maybe <B > backtrackOnFailureMaybe (F <Context , Maybe <B >> predicate ) {
101+ Context context = this .backtrackOnFailure ();
104102 Maybe <B > accepted = predicate .apply (context );
105103 if (accepted .isJust ()) {
106104 this .absorb (context );
@@ -270,7 +268,7 @@ private boolean acceptTerm(Context context) {
270268 }
271269
272270 private F <Context , Boolean > acceptLabeledGroup (F <Context , Boolean > predicate ) {
273- return currentContext -> currentContext .goDeeper (context -> {
271+ return currentContext -> currentContext .backtrackOnFailure (context -> {
274272 if (!context .eat ("(" )) {
275273 return false ;
276274 }
@@ -300,12 +298,12 @@ private boolean acceptDecimal(Context context) {
300298 }
301299
302300 private F <Context , Boolean > acceptQuantified (F <Context , Boolean > acceptor ) {
303- return superContext -> superContext .goDeeper (context ->{
301+ return superContext -> superContext .backtrackOnFailure (context ->{
304302 if (!acceptor .apply (context )) {
305303 return false ;
306304 }
307305 if (context .match ("{" )) {
308- return context .goDeeper (subContext -> {
306+ return context .backtrackOnFailure (subContext -> {
309307 if (!subContext .eat ("{" )) {
310308 return false ;
311309 }
@@ -351,7 +349,7 @@ private boolean acceptExtendedPatternCharacter(Context context) {
351349 }
352350
353351 private boolean acceptInvalidBracedQuantifier (Context context ) {
354- return context .goDeeper (subContext -> {
352+ return context .backtrackOnFailure (subContext -> {
355353 if (!subContext .eat ("{" )) {
356354 return false ;
357355 }
@@ -369,7 +367,7 @@ private boolean acceptAtom(Context context) {
369367 if (this .unicode ) {
370368 return acceptPatternCharacter (context ) ||
371369 context .eat ("." ) ||
372- context .goDeeper (subContext -> {
370+ context .backtrackOnFailure (subContext -> {
373371 if (!subContext .eat ("\\ " )) {
374372 return false ;
375373 }
@@ -380,7 +378,7 @@ private boolean acceptAtom(Context context) {
380378 acceptGrouping (context );
381379 }
382380 boolean matched = context .eat ("." ) ||
383- context .goDeeper (subContext -> {
381+ context .backtrackOnFailure (subContext -> {
384382 if (!subContext .eat ("\\ " )) {
385383 return false ;
386384 }
@@ -396,7 +394,7 @@ private boolean acceptAtom(Context context) {
396394 }
397395
398396 private boolean acceptGrouping (Context superContext ) {
399- return superContext .goDeeper (context -> {
397+ return superContext .backtrackOnFailure (context -> {
400398 if (!context .eat ("(" )) {
401399 return false ;
402400 }
@@ -415,7 +413,7 @@ private boolean acceptAtomEscape(Context context) {
415413 }
416414
417415 private boolean acceptDecimalEscapeBackreference (Context superContext ) {
418- return superContext .goDeeper (context -> {
416+ return superContext .backtrackOnFailure (context -> {
419417 StringBuilder digits = new StringBuilder ();
420418 Maybe <String > firstDecimal = context .eatAny (decimalDigits );
421419 if (firstDecimal .isNothing ()) {
@@ -436,7 +434,7 @@ private boolean acceptDecimalEscapeBackreference(Context superContext) {
436434
437435 @ Nonnull
438436 private Maybe <Integer > acceptDecimalEscape (Context superContext ) {
439- return superContext .goDeeperMaybe (context -> {
437+ return superContext .backtrackOnFailureMaybe (context -> {
440438 StringBuilder digits = new StringBuilder ();
441439 Maybe <String > firstDigit = context .eatAny (decimalDigits );
442440 if (firstDigit .isNothing ()) {
@@ -471,7 +469,7 @@ private boolean acceptCharacterClassEscape(Context context) {
471469
472470 @ Nonnull
473471 private Maybe <Integer > acceptUnicodeEscape (Context superContext ) {
474- return superContext .goDeeperMaybe (context -> {
472+ return superContext .backtrackOnFailureMaybe (context -> {
475473 if (!context .eat ("u" )) {
476474 return Maybe .empty ();
477475 }
@@ -490,7 +488,7 @@ private Maybe<Integer> acceptUnicodeEscape(Context superContext) {
490488 int value = Integer .parseInt (hex , 16 );
491489
492490 if (value >= 0xD800 && value <= 0xDBFF ) {
493- Maybe <Integer > surrogatePairValue = context .goDeeperMaybe (subContext -> {
491+ Maybe <Integer > surrogatePairValue = context .backtrackOnFailureMaybe (subContext -> {
494492 if (!subContext .eat ("\\ u" )) {
495493 return Maybe .empty ();
496494 }
@@ -521,7 +519,7 @@ private Maybe<Integer> acceptCharacterEscape(Context superContext) {
521519 }
522520 return Maybe .of (controlEscapeCharacterValues .get (escaped .fromJust ()));
523521 },
524- context -> context .goDeeperMaybe (subContext -> {
522+ context -> context .backtrackOnFailureMaybe (subContext -> {
525523 if (!subContext .eat ("c" )) {
526524 return Maybe .empty ();
527525 }
@@ -531,7 +529,7 @@ private Maybe<Integer> acceptCharacterEscape(Context superContext) {
531529 }
532530 return Maybe .of (character .fromJust ().charAt (0 ) % 32 );
533531 }),
534- context -> context .goDeeperMaybe (subContext -> {
532+ context -> context .backtrackOnFailureMaybe (subContext -> {
535533 if (!subContext .eat ("0" )) {
536534 return Maybe .empty ();
537535 }
@@ -540,7 +538,7 @@ private Maybe<Integer> acceptCharacterEscape(Context superContext) {
540538 }
541539 return Maybe .of (0 );
542540 }),
543- context -> context .goDeeperMaybe (subContext -> {
541+ context -> context .backtrackOnFailureMaybe (subContext -> {
544542 if (!subContext .eat ("x" )) {
545543 return Maybe .empty ();
546544 }
@@ -551,11 +549,11 @@ private Maybe<Integer> acceptCharacterEscape(Context superContext) {
551549 return Maybe .of (Integer .parseInt (hex , 16 ));
552550 }),
553551 this ::acceptUnicodeEscape ,
554- context -> context .goDeeperMaybe (subContext -> {
552+ context -> context .backtrackOnFailureMaybe (subContext -> {
555553 if (this .unicode ) {
556554 return Maybe .empty ();
557555 }
558- F <Context , Maybe <Integer >> acceptOctalDigit = subContext2 -> subContext2 .goDeeperMaybe (subContext3 -> {
556+ F <Context , Maybe <Integer >> acceptOctalDigit = subContext2 -> subContext2 .backtrackOnFailureMaybe (subContext3 -> {
559557 Maybe <String > octal2 = subContext3 .eatAny (octalDigits );
560558 if (octal2 .isNothing ()) {
561559 return Maybe .empty ();
@@ -579,7 +577,7 @@ private Maybe<Integer> acceptCharacterEscape(Context superContext) {
579577 return Maybe .of (octal1 .fromJust () << 3 | octal2 .fromJust ());
580578 }
581579 }),
582- context -> context .goDeeperMaybe (subContext -> {
580+ context -> context .backtrackOnFailureMaybe (subContext -> {
583581 if (!this .unicode ) {
584582 return Maybe .empty ();
585583 }
@@ -592,7 +590,7 @@ private Maybe<Integer> acceptCharacterEscape(Context superContext) {
592590 }
593591 return Maybe .empty ();
594592 },
595- context -> context .goDeeperMaybe (subContext -> {
593+ context -> context .backtrackOnFailureMaybe (subContext -> {
596594 if (this .unicode ) {
597595 return Maybe .empty ();
598596 }
@@ -608,7 +606,7 @@ private Maybe<Integer> acceptCharacterEscape(Context superContext) {
608606
609607 private Maybe <Maybe <Integer >> acceptClassEscape (Context superContext ) {
610608 return this .<Maybe <Integer >>maybeLogicalOr (
611- context -> context .goDeeperMaybe (subContext -> {
609+ context -> context .backtrackOnFailureMaybe (subContext -> {
612610 if (!subContext .eat ("b" )) {
613611 return Maybe .empty ();
614612 }
@@ -621,7 +619,7 @@ private Maybe<Maybe<Integer>> acceptClassEscape(Context superContext) {
621619 }
622620 return Maybe .empty ();
623621 },
624- context -> context .goDeeperMaybe (subContext -> {
622+ context -> context .backtrackOnFailureMaybe (subContext -> {
625623 if (this .unicode || !subContext .eat ("c" )) {
626624 return Maybe .empty ();
627625 }
@@ -636,7 +634,7 @@ private Maybe<Maybe<Integer>> acceptClassAtomNoDash(Context context) {
636634 if (context .eat ("\\ " )) {
637635 return this .maybeLogicalOr (
638636 this ::acceptClassEscape ,
639- subContext -> subContext .goDeeperMaybe (subContext2 -> {
637+ subContext -> subContext .backtrackOnFailureMaybe (subContext2 -> {
640638 if (!this .unicode && subContext2 .match ("c" )) {
641639 return Maybe .of (0x005C ); // reverse solidus
642640 }
@@ -705,7 +703,7 @@ private Maybe<Maybe<Integer>> acceptNonEmptyClassRangesNoDash(Context context) {
705703
706704 // all `Maybe<Maybe<Integer>>` types represent matched, not terminating, and contain in the character value for ranges
707705 private boolean acceptCharacterClass (Context superContext ) {
708- return superContext .goDeeper (context -> {
706+ return superContext .backtrackOnFailure (context -> {
709707 if (!context .eat ("[" )) {
710708 return false ;
711709 }
0 commit comments