Skip to content

Commit 9e301c6

Browse files
committed
remove all -1
1 parent 04f7e12 commit 9e301c6

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

src/main/java/com/shapesecurity/shift/es2017/parser/PatternAcceptor.java

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,14 @@ public Maybe<String> eatAny(@Nonnull String[]... stringArrays) {
159159
}
160160

161161
public String collect(@Nonnull String[]... stringArrays) {
162-
return collect(-1, stringArrays);
162+
return collect(Maybe.empty(), stringArrays);
163163
}
164164

165-
public String collect(int limit, @Nonnull String[]... stringArrays) {
165+
public String collect(Maybe<Integer> limit, @Nonnull String[]... stringArrays) {
166166
StringBuilder stringBuilder = new StringBuilder();
167167

168168
outer:
169-
for (int i = 0; limit < 0 || i < limit; ++i) {
169+
for (int i = 0; limit.isNothing() || limit.fromJust() > i; ++i) {
170170
for (String[] strings : stringArrays) {
171171
for (String string : strings) {
172172
if (this.eat(string)) {
@@ -483,7 +483,7 @@ private Maybe<Integer> acceptUnicodeEscape(Context superContext) {
483483
int value = Integer.parseInt(hex, 16);
484484
return value > 0x10FFFF ? Maybe.empty() : Maybe.of(value);
485485
}
486-
String hex = context.collect(4, hexDigits);
486+
String hex = context.collect(Maybe.of(4), hexDigits);
487487
if (hex.length() != 4) {
488488
return Maybe.empty();
489489
}
@@ -494,7 +494,7 @@ private Maybe<Integer> acceptUnicodeEscape(Context superContext) {
494494
if (!subContext.eat("\\u")) {
495495
return Maybe.empty();
496496
}
497-
String hex2 = subContext.collect(4, hexDigits);
497+
String hex2 = subContext.collect(Maybe.of(4), hexDigits);
498498
if (hex2.length() != 4) {
499499
return Maybe.empty();
500500
}
@@ -544,7 +544,7 @@ private Maybe<Integer> acceptCharacterEscape(Context superContext) {
544544
if (!subContext.eat("x")) {
545545
return Maybe.empty();
546546
}
547-
String hex = subContext.collect(2, hexDigits);
547+
String hex = subContext.collect(Maybe.of(2), hexDigits);
548548
if (hex.length() != 2) {
549549
return Maybe.empty();
550550
}
@@ -606,18 +606,18 @@ private Maybe<Integer> acceptCharacterEscape(Context superContext) {
606606
).apply(superContext);
607607
}
608608

609-
private Maybe<Integer> acceptClassEscape(Context superContext) {
610-
return this.maybeLogicalOr(
609+
private Maybe<Maybe<Integer>> acceptClassEscape(Context superContext) {
610+
return this.<Maybe<Integer>>maybeLogicalOr(
611611
context -> context.goDeeperMaybe(subContext -> {
612612
if (!subContext.eat("b")) {
613613
return Maybe.empty();
614614
}
615615
return Maybe.of(0x0008); // backspace
616-
}),
617-
PatternAcceptor.this::acceptDecimalEscape,
616+
}).map(Maybe::of),
617+
context -> acceptDecimalEscape(context).map(Maybe::of),
618618
context -> {
619619
if (this.unicode && context.eat("-")) {
620-
return Maybe.of((int)"-".charAt(0));
620+
return Maybe.of(Maybe.of((int)"-".charAt(0)));
621621
}
622622
return Maybe.empty();
623623
},
@@ -626,13 +626,13 @@ private Maybe<Integer> acceptClassEscape(Context superContext) {
626626
return Maybe.empty();
627627
}
628628
return subContext.eatAny(decimalDigits, new String[]{"_"}).map(str -> str.codePointAt(0) % 32);
629-
}),
630-
context -> acceptCharacterClassEscape(context) ? Maybe.of(-1) : Maybe.empty(),
631-
this::acceptCharacterEscape
629+
}).map(Maybe::of),
630+
context -> acceptCharacterClassEscape(context) ? Maybe.of(Maybe.empty()) : Maybe.empty(),
631+
context -> acceptCharacterEscape(context).map(Maybe::of)
632632
).apply(superContext);
633633
}
634634

635-
private Maybe<Integer> acceptClassAtomNoDash(Context context) {
635+
private Maybe<Maybe<Integer>> acceptClassAtomNoDash(Context context) {
636636
if (context.eat("\\")) {
637637
return this.maybeLogicalOr(
638638
this::acceptClassEscape,
@@ -641,67 +641,69 @@ private Maybe<Integer> acceptClassAtomNoDash(Context context) {
641641
return Maybe.of(0x005C); // reverse solidus
642642
}
643643
return Maybe.empty();
644-
})
644+
}).map(Maybe::of)
645645
).apply(context);
646646
}
647647
Maybe<String> nextCodePoint = context.nextCodePoint();
648648
if (nextCodePoint.isNothing() || nextCodePoint.fromJust().equals("]") || nextCodePoint.fromJust().equals("-")) {
649649
return Maybe.empty();
650650
}
651651
context.skipCodePoint();
652-
return Maybe.of(nextCodePoint.fromJust().codePointAt(0));
652+
return Maybe.of(Maybe.of(nextCodePoint.fromJust().codePointAt(0)));
653653
}
654654

655-
private Maybe<Integer> acceptClassAtom(Context context) {
655+
private Maybe<Maybe<Integer>> acceptClassAtom(Context context) {
656656
if (context.eat("-")) {
657-
return Maybe.of((int)"-".charAt(0));
657+
return Maybe.of(Maybe.of((int)"-".charAt(0)));
658658
}
659659
return acceptClassAtomNoDash(context);
660660
}
661661

662-
private Maybe<Integer> finishClassRange(Context context, int atom) {
662+
private Maybe<Maybe<Integer>> finishClassRange(Context context, Maybe<Integer> atom) {
663663
if (context.eat("-")) {
664664
if (context.match("]")) {
665-
return Maybe.of(-1); // termination sentinel
665+
return Maybe.of(Maybe.empty()); // terminate gracefully
666666
}
667-
Maybe<Integer> otherAtom = acceptClassAtom(context);
667+
Maybe<Maybe<Integer>> otherAtom = acceptClassAtom(context);
668668
if (otherAtom.isNothing()) {
669669
return Maybe.empty();
670670
}
671-
if (this.unicode && (atom == -1 || otherAtom.fromJust() == -1)) {
671+
Maybe<Integer> justOtherAtom = otherAtom.fromJust();
672+
if (this.unicode && (atom.isNothing() || justOtherAtom.isNothing())) {
672673
return Maybe.empty();
673-
} else if (!(!this.unicode && (atom == -1 || otherAtom.fromJust() == -1)) && atom > otherAtom.fromJust()) {
674+
} else if (!(!this.unicode && (atom.isNothing() || justOtherAtom.isNothing())) && atom.fromJust() > justOtherAtom.fromJust()) {
674675
return Maybe.empty();
675676
} else if (context.match("]")) {
676-
return Maybe.of(-1);
677+
return Maybe.of(Maybe.empty());
677678
}
678679
return acceptNonEmptyClassRanges(context);
679680
}
680681
if (context.match("]")) {
681-
return Maybe.of(-1);
682+
return Maybe.of(Maybe.empty());
682683
}
683684
return acceptNonEmptyClassRangesNoDash(context);
684685
}
685686

686-
private Maybe<Integer> acceptNonEmptyClassRanges(Context context) {
687-
Maybe<Integer> atom = acceptClassAtom(context);
687+
private Maybe<Maybe<Integer>> acceptNonEmptyClassRanges(Context context) {
688+
Maybe<Maybe<Integer>> atom = acceptClassAtom(context);
688689
if (atom.isNothing()) {
689690
return Maybe.empty();
690691
}
691692
return finishClassRange(context, atom.fromJust());
692693
}
693694

694-
private Maybe<Integer> acceptNonEmptyClassRangesNoDash(Context context) {
695+
private Maybe<Maybe<Integer>> acceptNonEmptyClassRangesNoDash(Context context) {
695696
if (context.eat("-") && !context.match("]")) {
696697
return Maybe.empty();
697698
}
698-
Maybe<Integer> atom = acceptClassAtomNoDash(context);
699+
Maybe<Maybe<Integer>> atom = acceptClassAtomNoDash(context);
699700
if (atom.isNothing()) {
700701
return Maybe.empty();
701702
}
702703
return finishClassRange(context, atom.fromJust());
703704
}
704705

706+
// all `Maybe<Maybe<Integer>>` types represent matched, not terminating, and contain in the character value for ranges
705707
private boolean acceptCharacterClass(Context superContext) {
706708
return superContext.goDeeper(context -> {
707709
if (!context.eat("[")) {
@@ -712,10 +714,7 @@ private boolean acceptCharacterClass(Context superContext) {
712714
return true;
713715
}
714716
if (acceptNonEmptyClassRanges(context).isJust()) {
715-
if (!context.eat("]")) {
716-
return false;
717-
}
718-
return true;
717+
return context.eat("]");
719718
}
720719
return false;
721720
});

0 commit comments

Comments
 (0)