Skip to content

Commit 49f49c0

Browse files
committed
rename Grammar to Production
1 parent 93184b8 commit 49f49c0

3 files changed

Lines changed: 48 additions & 43 deletions

File tree

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
* can cause StackOverflowError.
9090
*/
9191
@ThreadSafe
92-
public abstract non-sealed class Parser<T> implements Grammar<T> {
92+
public abstract non-sealed class Parser<T> implements Production<T> {
9393
private static final Set<String> EMPTY_PREFIX = Set.of("");
9494

9595
/**
@@ -501,7 +501,7 @@ public static Parser<Integer> bmpCodeUnit() {
501501
* @since 10.0
502502
*/
503503
public static <A, B, C> Parser<C> sequence(
504-
Parser<A> left, Grammar<B> right, BiFunction<? super A, ? super B, ? extends C> combiner) {
504+
Parser<A> left, Production<B> right, BiFunction<? super A, ? super B, ? extends C> combiner) {
505505
return sequence(left, maybeZeroWidth(right), combiner);
506506
}
507507

@@ -550,7 +550,7 @@ public static <A, B, C> Parser<C> sequence(
550550
* @since 9.5
551551
*/
552552
public static <A, B, C, T> Parser<T> sequence(
553-
Parser<A> a, Grammar<B> b, Grammar<C> c,
553+
Parser<A> a, Production<B> b, Production<C> c,
554554
TriFunction<? super A, ? super B, ? super C, ? extends T> combiner) {
555555
requireNonNull(combiner);
556556
return sequence(
@@ -565,7 +565,7 @@ a, sequence(maybeZeroWidth(b), c, AbstractMap.SimpleImmutableEntry<B, C>::new),
565565
* @since 9.5
566566
*/
567567
public static <A, B, C, D, T> Parser<T> sequence(
568-
Parser<A> a, Grammar<B> b, Grammar<C> c, Grammar<D> d,
568+
Parser<A> a, Production<B> b, Production<C> c, Production<D> d,
569569
Function4<? super A, ? super B, ? super C, ? super D, ? extends T> combiner) {
570570
requireNonNull(combiner);
571571
return sequence(
@@ -877,7 +877,7 @@ public final <A, R> Parser<R>.OrEmpty zeroOrMoreDelimitedBy(
877877
*/
878878
public static <A, B, R> Parser<R>.OrEmpty zeroOrMoreDelimited(
879879
Parser<A> first,
880-
Grammar<B> second,
880+
Production<B> second,
881881
String delimiter,
882882
BiCollector<? super A, ? super B, R> collector) {
883883
return sequence(first, second, Both::of)
@@ -1038,7 +1038,7 @@ public final Parser<T> suchThat(Predicate<? super T> condition, String name) {
10381038
}
10391039

10401040
/** If this parser matches, continue to match the optional {@code suffix}. */
1041-
public final <S> Parser<T> followedBy(Parser<S>.OrEmpty suffix) {
1041+
@Override public final <S> Parser<T> followedBy(Parser<S>.OrEmpty suffix) {
10421042
return sequence(this, suffix, (value, unused) -> value);
10431043
}
10441044

@@ -1229,7 +1229,7 @@ public final Lexical skipping(CharPredicate charsToSkip) {
12291229
*
12301230
* <p>Equivalent to {@code skipping(skip).parse(input)}.
12311231
*/
1232-
public final T parseSkipping(Parser<?> skip, String input) {
1232+
@Override public final T parseSkipping(Parser<?> skip, String input) {
12331233
return skipping(skip).parse(input);
12341234
}
12351235

@@ -1238,7 +1238,7 @@ public final T parseSkipping(Parser<?> skip, String input) {
12381238
*
12391239
* <p>Equivalent to {@code skipping(charsToSkip).parse(input)}.
12401240
*/
1241-
public final T parseSkipping(CharPredicate charsToSkip, String input) {
1241+
@Override public final T parseSkipping(CharPredicate charsToSkip, String input) {
12421242
return skipping(charsToSkip).parse(input);
12431243
}
12441244

@@ -1430,7 +1430,7 @@ MatchResult.Success<T> nextOrNull() {
14301430
* <p>Besides {@link #between between()} and {@link #followedBy(String) followedBy()}, the {@link
14311431
* Parser#sequence(Parser, Parser.OrEmpty, BiFunction) sequence()} and {@link
14321432
* Parser#followedBy(Parser.OrEmpty)} methods can be used to specify that a {@code Parser.OrEmpty}
1433-
* grammar rule follows a regular consuming {@code Parser}.
1433+
* production rule follows a regular consuming {@code Parser}.
14341434
*
14351435
* <p>The following is a simplified example of parsing a CSV line: a comma-separated list of
14361436
* fields with an optional trailing newline. The field values can be empty; empty line results in
@@ -1453,7 +1453,7 @@ MatchResult.Success<T> nextOrNull() {
14531453
* input in this one stop shop without having to remember to check for emptiness, because this
14541454
* class already knows the default value to use when the input is empty.
14551455
*/
1456-
public final class OrEmpty implements Grammar<T> {
1456+
public final class OrEmpty implements Production<T> {
14571457
private final Supplier<? extends T> defaultSupplier;
14581458

14591459
private OrEmpty(Supplier<? extends T> defaultSupplier) {
@@ -1781,7 +1781,7 @@ public static <T> Parser<T> define(
17811781
}
17821782

17831783
/**
1784-
* A forward-declared grammar rule, to be used for recursive grammars.
1784+
* A forward-declared production rule, to be used for recursive grammars.
17851785
*
17861786
* <p>For example, to create a parser for a simple calculator that supports single-digit numbers,
17871787
* addition, and parentheses, you can write:
@@ -1896,8 +1896,8 @@ static int skipIfAny(Parser<?> skip, CharInput input, int start) {
18961896
};
18971897
}
18981898

1899-
static <T> Parser<T> maybeZeroWidth(Grammar<T> grammar) {
1900-
return switch (grammar) {
1899+
static <T> Parser<T> maybeZeroWidth(Production<T> production) {
1900+
return switch (production) {
19011901
case Parser<T> parser -> parser;
19021902
case Parser<T>.OrEmpty orEmpty -> orEmpty.asUnsafeZeroWidthParser();
19031903
};

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

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
import java.util.function.BiFunction;
44
import java.util.function.Function;
55

6+
import com.google.common.labs.parse.Parser.OrEmpty;
67
import com.google.common.labs.parse.Parser.ParseException;
78
import com.google.mu.util.CharPredicate;
89

910
/**
10-
* A sealed interface that can either be an always-consuming {@link Parser}
11-
* or an optional {@link Parser.OrEmpty}. Useful as a parameter to {@code Parser.sequence()}
12-
* methods, so that we can avoid exponential number of overloads.
11+
* A sealed interface representing an abstract production rule that can either be an
12+
* always-consuming {@link Parser} or an optional {@link Parser.OrEmpty}.
13+
*
14+
* <p>Useful as a parameter to {@code Parser.sequence()} methods, so as to avoid exponential
15+
* number of overloads.
1316
*
1417
* @since 10.0
1518
*/
16-
public sealed interface Grammar<T> permits Parser, Parser.OrEmpty {
19+
public sealed interface Production<T> permits Parser, OrEmpty {
1720

1821
/**
1922
* Parses the entire input string and returns the result. Upon successful return, the {@code
@@ -39,15 +42,15 @@ public sealed interface Grammar<T> permits Parser, Parser.OrEmpty {
3942
*/
4043
T parseSkipping(CharPredicate charsToSkip, String input);
4144

42-
/** Returns true if this grammar matches the entirety of {@code input} string. */
45+
/** Returns true if this production matches the entirety of {@code input} string. */
4346
boolean matches(String input);
4447

45-
/** The current grammar must be enclosed between non-empty {@code prefix} and {@code suffix}. */
48+
/** The current production must be enclosed between non-empty {@code prefix} and {@code suffix}. */
4649
default Parser<T> between(String prefix, String suffix) {
4750
return between(Parser.string(prefix), Parser.string(suffix));
4851
}
4952

50-
/** The current grammar must be enclosed between non-empty {@code prefix} and {@code suffix}. */
53+
/** The current production must be enclosed between non-empty {@code prefix} and {@code suffix}. */
5154
default Parser<T> between(Parser<?> prefix, Parser<?> suffix) {
5255
return Parser.sequence(prefix, this, (p, t) -> t).followedBy(suffix);
5356
}
@@ -56,13 +59,13 @@ default Parser<T> between(Parser<?> prefix, Parser<?> suffix) {
5659
* Returns a parser that matches {@code this} pattern enclosed between {@code prefix} and {@code suffix},
5760
* both allowed to be empty.
5861
*
59-
* <p>Note that the {@link Parser} and {@link Parser.OrEmpty} implementations are declared to return the more specific
60-
* {@code Parser<T>} or {@code Parser<T>.OrEmpty} subtypes respectively.
62+
* <p>Note that the {@link Parser} and {@link Parser.OrEmpty} implementations are re-declared to
63+
* return the more specific {@code Parser<T>} or {@code Parser<T>.OrEmpty} subtypes respectively.
6164
*/
62-
Grammar<T> between(Parser<?>.OrEmpty prefix, Parser<?>.OrEmpty suffix);
65+
Production<T> between(Parser<?>.OrEmpty prefix, Parser<?>.OrEmpty suffix);
6366

6467
/**
65-
* The current grammar must be <em>immediately</em> enclosed between
68+
* The current production must be <em>immediately</em> enclosed between
6669
* non-empty {@code prefix} and {@code suffix} (no skippable characters as specified by {@link
6770
* #parseSkipping parseSkipping()} in between). Useful for matching a literal string, such as
6871
* {@code zeroOrMore(isNot('"')).immediatelyBetween("\"", "\"")}.
@@ -72,7 +75,7 @@ default Parser<T> immediatelyBetween(String prefix, String suffix) {
7275
}
7376

7477
/**
75-
* After matching the current grammar, proceed to match {@code suffix}.
78+
* After matching the current production, proceed to match {@code suffix}.
7679
*/
7780
default <S> Parser<S> then(Parser<S> suffix) {
7881
return Parser.sequence(Parser.maybeZeroWidth(this), suffix, (a, b) -> b);
@@ -81,53 +84,53 @@ default <S> Parser<S> then(Parser<S> suffix) {
8184
/**
8285
* After matching the current optional (or zero-or-more) parser, proceed to match {@code suffix}.
8386
*
84-
* <p>Note that the {@link Parser} and {@link Parser.OrEmpty} implementations are declared to
87+
* <p>Note that the {@link Parser} and {@link Parser.OrEmpty} implementations are re-declared to
8588
* return the more specific {@code Parser<S>} or {@code Parser<S>.OrEmpty} subtypes respectively.
8689
*/
87-
<S> Grammar<S> then(Parser<S>.OrEmpty suffix);
90+
<S> Production<S> then(Parser<S>.OrEmpty suffix);
8891

89-
/** The current grammar must be followed by non-empty {@code suffix}. */
92+
/** The current production must be followed by non-empty {@code suffix}. */
9093
default Parser<T> followedBy(String suffix) {
9194
return followedBy(Parser.string(suffix));
9295
}
9396

94-
/** The current grammar must be followed by non-empty {@code suffix}. */
97+
/** The current production must be followed by non-empty {@code suffix}. */
9598
default Parser<T> followedBy(Parser<?> suffix) {
9699
return Parser.sequence(Parser.maybeZeroWidth(this), suffix, (a, b) -> a);
97100
}
98101

99102
/**
100-
* The current grammar may optionally be followed by {@code suffix}.
103+
* The current production may optionally be followed by {@code suffix}.
101104
*
102-
* <p>Note that the {@link Parser} and {@link Parser.OrEmpty} implementations are declared to
105+
* <p>Note that the {@link Parser} and {@link Parser.OrEmpty} implementations are re-declared to
103106
* return the more specific {@code Parser<T>} or {@code Parser<T>.OrEmpty} subtypes respectively.
104107
*/
105-
<S> Grammar<T> followedBy(Parser<S>.OrEmpty suffix);
108+
<S> Production<T> followedBy(Parser<S>.OrEmpty suffix);
106109

107110
/**
108-
* Returns an equivalent grammar except it allows {@code suffix} if present.
111+
* Returns an equivalent production except it allows {@code suffix} if present.
109112
*
110-
* <p>Note that the {@link Parser} and {@link Parser.OrEmpty} implementations are declared to
113+
* <p>Note that the {@link Parser} and {@link Parser.OrEmpty} implementations are re-declared to
111114
* return the more specific {@code Parser<T>} or {@code Parser<T>.OrEmpty} subtypes respectively.
112115
*/
113-
Grammar<T> optionallyFollowedBy(String suffix);
116+
Production<T> optionallyFollowedBy(String suffix);
114117

115118
/**
116-
* If this parser matches, optionally applies the {@code op} function if this grammar is followed
119+
* If this parser matches, optionally applies the {@code op} function if this production is followed
117120
* by {@code suffix}.
118121
*
119-
* <p>Note that the {@link Parser} and {@link Parser.OrEmpty} implementations are declared to
122+
* <p>Note that the {@link Parser} and {@link Parser.OrEmpty} implementations are re-declared to
120123
* return the more specific {@code Parser<T>} or {@code Parser<T>.OrEmpty} subtypes respectively.
121124
*/
122-
Grammar<T> optionallyFollowedBy(String suffix, Function<? super T, ? extends T> op);
125+
Production<T> optionallyFollowedBy(String suffix, Function<? super T, ? extends T> op);
123126

124127
/**
125-
* If this grammar matches, optionally matches {@code suffix} with the {@code op} BiFunction
126-
* to transform the current grammar's result.
128+
* If this production matches, optionally matches {@code suffix} with the {@code op} BiFunction
129+
* to transform the current production's result.
127130
*
128-
* <p>Note that the {@link Parser} and {@link Parser.OrEmpty} implementations are declared to
131+
* <p>Note that the {@link Parser} and {@link Parser.OrEmpty} implementations are re-declared to
129132
* return the more specific {@code Parser<T>} or {@code Parser<T>.OrEmpty} subtypes respectively.
130133
*/
131-
<S> Grammar<T> optionallyFollowedBy(
134+
<S> Production<T> optionallyFollowedBy(
132135
Parser<S> suffix, BiFunction<? super T, ? super S, ? extends T> op);
133136
}

dot-parse/src/test/java/com/google/common/labs/parse/ParserTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ public void bmpCodeUnit_emoji() {
536536
}
537537

538538
@Test
539-
@Ignore("Grammar is a sealed interface, unsupported by NPT")
539+
@Ignore("Production is a sealed interface, unsupported by NPT")
540540
public void testNulls() throws Exception {
541541
NullPointerTester tester =
542542
new NullPointerTester()
@@ -3836,6 +3836,7 @@ public void orEmpty_optionallyFollowedBy_string_function_success() {
38363836
.between("[", "]");
38373837
assertThat(parser.parse("[aa++]")).isEqualTo("aab");
38383838
assertThat(parser.parse("[aa]")).isEqualTo("aa");
3839+
assertThat(parser.parse("[++]")).isEqualTo("b");
38393840
assertThat(parser.parse("[]")).isEqualTo("");
38403841
}
38413842

@@ -3848,6 +3849,7 @@ public void orEmpty_optionallyFollowedBy_parser_biFunction_success() {
38483849
.between("[", "]");
38493850
assertThat(parser.parse("[aa+123]")).isEqualTo("aa3");
38503851
assertThat(parser.parse("[aa]")).isEqualTo("aa");
3852+
assertThat(parser.parse("[+123]")).isEqualTo("3");
38513853
assertThat(parser.parse("[]")).isEqualTo("");
38523854
}
38533855

0 commit comments

Comments
 (0)