Skip to content

Commit 781465f

Browse files
committed
Avoid using Parser.sequence() in Production, it allocates objects
1 parent 33a3f98 commit 781465f

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

dot-parse/src/main/java/com/google/common/labs/parse/Parser.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,8 +1043,13 @@ public final Parser<T> suchThat(Predicate<? super T> condition, String name) {
10431043
};
10441044
}
10451045

1046+
@Override public Parser<T> followedBy(Parser<?> suffix) {
1047+
requireNonNull(suffix);
1048+
return flatMap(v -> suffix.thenReturn(v));
1049+
}
1050+
10461051
@Override public final <S> Parser<T> followedBy(Parser<S>.OrEmpty suffix) {
1047-
return sequence(this, suffix, (value, unused) -> value);
1052+
return followedBy(suffix.asUnsafeZeroWidthParser());
10481053
}
10491054

10501055
/**
@@ -1512,6 +1517,10 @@ public <S> Parser<S> then(Parser<S> suffix) {
15121517
return sequence(this, suffix, (a, b) -> b);
15131518
}
15141519

1520+
@Override public Parser<T> followedBy(Parser<?> suffix) {
1521+
return asUnsafeZeroWidthParser().followedBy(suffix);
1522+
}
1523+
15151524
/** The current optional (or zero-or-more) parser may optionally be followed by {@code suffix}. */
15161525
@Override public <S> Parser<T>.OrEmpty followedBy(Parser<S>.OrEmpty suffix) {
15171526
return sequence(this, suffix, (a, b) -> a);
@@ -1905,7 +1914,7 @@ static int skipIfAny(Parser<?> skip, CharInput input, int start) {
19051914
};
19061915
}
19071916

1908-
static <T> Parser<T> allowZeroWidth(Production<T> production) {
1917+
private static <T> Parser<T> allowZeroWidth(Production<T> production) {
19091918
return switch (production) {
19101919
case Parser<T> parser -> parser;
19111920
case Parser<T>.OrEmpty orEmpty -> orEmpty.asUnsafeZeroWidthParser();

dot-parse/src/main/java/com/google/common/labs/parse/Production.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ default Parser<T> between(String prefix, String suffix) {
5151

5252
/** The current production must be enclosed between non-empty {@code prefix} and {@code suffix}. */
5353
default Parser<T> between(Parser<?> prefix, Parser<?> suffix) {
54-
return Parser.sequence(prefix, this, (p, t) -> t).followedBy(suffix);
54+
return prefix.then(this.followedBy(suffix));
5555
}
5656

5757
/**
@@ -70,7 +70,7 @@ default Parser<T> between(Parser<?> prefix, Parser<?> suffix) {
7070
* {@code zeroOrMore(isNot('"')).immediatelyBetween("\"", "\"")}.
7171
*/
7272
default Parser<T> immediatelyBetween(String prefix, String suffix) {
73-
return Parser.string(prefix).then(Parser.literally(followedBy(suffix)));
73+
return Parser.string(prefix).then(Parser.literally(this.followedBy(suffix)));
7474
}
7575

7676
/**
@@ -92,9 +92,7 @@ default Parser<T> followedBy(String suffix) {
9292
}
9393

9494
/** The current production must be followed by non-empty {@code suffix}. */
95-
default Parser<T> followedBy(Parser<?> suffix) {
96-
return Parser.sequence(Parser.allowZeroWidth(this), suffix, (a, b) -> a);
97-
}
95+
Parser<T> followedBy(Parser<?> suffix);
9896

9997
/**
10098
* The current production may optionally be followed by {@code suffix}.

0 commit comments

Comments
 (0)