Skip to content

Commit 2fe8dfe

Browse files
committed
rename Parser prefix(), postfix() to withPrefixes() and withPostfixes()
1 parent e634c8c commit 2fe8dfe

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,19 @@ private interface OperatorGroup<T> {
218218

219219
private static final class Prefix<T> extends Unary<T> {
220220
@Override public Parser<T> makeExpressionParser(Parser<T> operand) {
221-
return operand.prefix(opParser());
221+
return operand.withPrefixes(opParser());
222222
}
223223
}
224224

225225
private static final class Postfix<T> extends Unary<T> {
226226
@Override public Parser<T> makeExpressionParser(Parser<T> operand) {
227-
return operand.postfix(opParser());
227+
return operand.withPostfixes(opParser());
228228
}
229229
}
230230

231231
private static final class Infixl<T> extends Binary<T> {
232232
@Override public Parser<T> makeExpressionParser(Parser<T> operand) {
233-
return operand.postfix(opWithRightHandSide(operand));
233+
return operand.withPostfixes(opWithRightHandSide(operand));
234234
}
235235
}
236236

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*****************************************************************************
2+
* Copyright (C) google.com *
23
* ------------------------------------------------------------------------- *
34
* Licensed under the Apache License, Version 2.0 (the "License"); *
45
* you may not use this file except in compliance with the License. *
@@ -821,8 +822,10 @@ public static <A, B, R> Parser<R>.OrEmpty zeroOrMoreDelimited(
821822
* this} and applies the result unary operator functions iteratively.
822823
*
823824
* <p>For infix operator support, consider using {@link OperatorTable}.
825+
*
826+
* @since 9.9.3
824827
*/
825-
public final Parser<T> prefix(Parser<? extends UnaryOperator<T>> operator) {
828+
public final Parser<T> withPrefixes(Parser<? extends UnaryOperator<T>> operator) {
826829
return sequence(
827830
operator.zeroOrMore(), this, (ops, operand) -> applyOperators(ops.reversed(), operand));
828831
}
@@ -835,8 +838,10 @@ public final Parser<T> prefix(Parser<? extends UnaryOperator<T>> operator) {
835838
* postfix.
836839
*
837840
* <p>For infix operator support, consider using {@link OperatorTable}.
841+
*
842+
* @since 9.9.3
838843
*/
839-
public final Parser<T> postfix(Parser<? extends UnaryOperator<T>> operator) {
844+
public final Parser<T> withPostfixes(Parser<? extends UnaryOperator<T>> operator) {
840845
return sequence(this, operator.zeroOrMore(), (operand, ops) -> applyOperators(ops, operand));
841846
}
842847

@@ -847,16 +852,35 @@ public final Parser<T> postfix(Parser<? extends UnaryOperator<T>> operator) {
847852
* <pre>{@code
848853
* Parser<Expr> parser = word()
849854
* .map(Expr::variable)
850-
* .postfix(string(".").then(word()), (expr, field) -> Expr.fieldAccess(expr, field)));
855+
* .withPostfixes(string(".").then(word()), (expr, field) -> Expr.fieldAccess(expr, field)));
851856
* }</pre>
852857
*
853858
* <p>For infix operator support, consider using {@link OperatorTable}.
854859
*
855-
* @since 9.5
860+
* @since 9.9.3
856861
*/
862+
public final <S> Parser<T> withPostfixes(
863+
Parser<S> operator, BiFunction<? super T, ? super S, ? extends T> postfixFunction) {
864+
return withPostfixes(asPostfixOperator(operator, postfixFunction));
865+
}
866+
867+
/** @deprecated Use {@link #withPrefixes} instead. */
868+
@Deprecated
869+
public final Parser<T> prefix(Parser<? extends UnaryOperator<T>> operator) {
870+
return withPrefixes(operator);
871+
}
872+
873+
/** @deprecated Use {@link #withPostfixes(Parser)} instead. */
874+
@Deprecated
875+
public final Parser<T> postfix(Parser<? extends UnaryOperator<T>> operator) {
876+
return withPostfixes(operator);
877+
}
878+
879+
880+
/** @deprecated Use {@link #withPostfixes(Parser, BiFunction)} instead. */
857881
public final <S> Parser<T> postfix(
858882
Parser<S> operator, BiFunction<? super T, ? super S, ? extends T> postfixFunction) {
859-
return postfix(asPostfixOperator(operator, postfixFunction));
883+
return withPostfixes(operator, postfixFunction);
860884
}
861885

862886
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static Parser<RegexPattern> pattern() {
6868
consecutive(is('#').or(Character::isWhitespace), "whitespace or #").map(Literal::new),
6969
ESCAPED_CHAR.map(c -> new Literal(Character.toString(c))));
7070
Parser<RegexPattern> sequence =
71-
atomic.postfix(quantifier()).atLeastOnce(RegexPattern.inSequence());
71+
atomic.withPostfixes(quantifier()).atLeastOnce(RegexPattern.inSequence());
7272
return lazy.definedAs(sequence.atLeastOnceDelimitedBy("|", RegexPattern.asAlternation()));
7373
}
7474

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,7 +3245,7 @@ public void chars_skipping() {
32453245
public void prefix_zeroOperator_success() {
32463246
Parser<Integer> number = digits().map(Integer::parseInt);
32473247
Parser<UnaryOperator<Integer>> neg = string("-").thenReturn(i -> -i);
3248-
Parser<Integer> parser = number.prefix(neg);
3248+
Parser<Integer> parser = number.withPrefixes(neg);
32493249
assertThat(parser.parse("10")).isEqualTo(10);
32503250
assertThat(parser.parseToStream("10")).containsExactly(10);
32513251
assertThat(parser.parseToStream("")).isEmpty();
@@ -3255,7 +3255,7 @@ public void prefix_zeroOperator_success() {
32553255
public void prefix_zeroOperator_success_source() {
32563256
Parser<Integer> number = digits().map(Integer::parseInt);
32573257
Parser<UnaryOperator<Integer>> neg = string("-").thenReturn(i -> -i);
3258-
Parser<Integer> parser = number.prefix(neg);
3258+
Parser<Integer> parser = number.withPrefixes(neg);
32593259
assertThat(parser.source().parse("10")).isEqualTo("10");
32603260
assertThat(parser.source().parseToStream("10")).containsExactly("10");
32613261
assertThat(parser.source().parseToStream("")).isEmpty();
@@ -3265,7 +3265,7 @@ public void prefix_zeroOperator_success_source() {
32653265
public void prefix_oneOperator_success() {
32663266
Parser<Integer> number = digits().map(Integer::parseInt);
32673267
Parser<UnaryOperator<Integer>> neg = string("-").thenReturn(i -> -i);
3268-
Parser<Integer> parser = number.prefix(neg);
3268+
Parser<Integer> parser = number.withPrefixes(neg);
32693269
assertThat(parser.parse("-10")).isEqualTo(-10);
32703270
assertThat(parser.parseToStream("-10")).containsExactly(-10);
32713271
}
@@ -3274,7 +3274,7 @@ public void prefix_oneOperator_success() {
32743274
public void prefix_oneOperator_success_source() {
32753275
Parser<Integer> number = digits().map(Integer::parseInt);
32763276
Parser<UnaryOperator<Integer>> neg = string("-").thenReturn(i -> -i);
3277-
Parser<Integer> parser = number.prefix(neg);
3277+
Parser<Integer> parser = number.withPrefixes(neg);
32783278
assertThat(parser.source().parse("-10")).isEqualTo("-10");
32793279
assertThat(parser.source().parseToStream("-10")).containsExactly("-10");
32803280
}
@@ -3286,7 +3286,7 @@ public void prefix_multipleOperators_success() {
32863286
Parser<UnaryOperator<Integer>> plus = string("+").thenReturn(i -> i);
32873287
Parser<UnaryOperator<Integer>> flip = string("~").thenReturn(i -> ~i);
32883288
Parser<UnaryOperator<Integer>> op = anyOf(neg, plus, flip);
3289-
Parser<Integer> parser = number.prefix(op);
3289+
Parser<Integer> parser = number.withPrefixes(op);
32903290
assertThat(parser.parse("--10")).isEqualTo(10);
32913291
assertThat(parser.parse("-~10")).isEqualTo(-(~10));
32923292
assertThat(parser.parse("~-10")).isEqualTo(~(-10));
@@ -3304,7 +3304,7 @@ public void prefix_multipleOperators_success_source() {
33043304
Parser<UnaryOperator<Integer>> plus = string("+").thenReturn(i -> i);
33053305
Parser<UnaryOperator<Integer>> flip = string("~").thenReturn(i -> ~i);
33063306
Parser<UnaryOperator<Integer>> op = anyOf(neg, plus, flip);
3307-
Parser<Integer> parser = number.prefix(op);
3307+
Parser<Integer> parser = number.withPrefixes(op);
33083308
assertThat(parser.source().parse("--10")).isEqualTo("--10");
33093309
assertThat(parser.source().parse("-~10")).isEqualTo("-~10");
33103310
assertThat(parser.source().parse("~-10")).isEqualTo("~-10");
@@ -3319,7 +3319,7 @@ public void prefix_multipleOperators_success_source() {
33193319
public void prefix_operandParseFails() {
33203320
Parser<Integer> number = digits().map(Integer::parseInt);
33213321
Parser<UnaryOperator<Integer>> neg = string("-").thenReturn(i -> -i);
3322-
Parser<Integer> parser = number.prefix(neg);
3322+
Parser<Integer> parser = number.withPrefixes(neg);
33233323
assertThrows(ParseException.class, () -> parser.parse("a"));
33243324
assertThrows(ParseException.class, () -> parser.parseToStream("a").toList());
33253325
assertThrows(ParseException.class, () -> parser.parse("-a"));
@@ -3330,7 +3330,7 @@ public void prefix_operandParseFails() {
33303330
public void prefix_failure_withLeftover() {
33313331
Parser<Integer> number = digits().map(Integer::parseInt);
33323332
Parser<UnaryOperator<Integer>> neg = string("-").thenReturn(i -> -i);
3333-
Parser<Integer> parser = number.prefix(neg);
3333+
Parser<Integer> parser = number.withPrefixes(neg);
33343334
assertThrows(ParseException.class, () -> parser.parse("10a"));
33353335
assertThrows(ParseException.class, () -> parser.parseToStream("10a").toList());
33363336
assertThrows(ParseException.class, () -> parser.parse("-10a"));
@@ -3343,7 +3343,7 @@ public void postfix_success() {
33433343
Parser<UnaryOperator<Integer>> inc = string("++").thenReturn(i -> i + 1);
33443344
Parser<UnaryOperator<Integer>> dec = string("--").thenReturn(i -> i - 1);
33453345
Parser<UnaryOperator<Integer>> op = anyOf(inc, dec);
3346-
Parser<Integer> parser = number.postfix(op);
3346+
Parser<Integer> parser = number.withPostfixes(op);
33473347
assertThat(parser.parse("10")).isEqualTo(10);
33483348
assertThat(parser.parseToStream("10")).containsExactly(10);
33493349
assertThat(parser.parse("10++")).isEqualTo(11);
@@ -3361,7 +3361,7 @@ public void postfix_success_source() {
33613361
Parser<UnaryOperator<Integer>> inc = string("++").thenReturn(i -> i + 1);
33623362
Parser<UnaryOperator<Integer>> dec = string("--").thenReturn(i -> i - 1);
33633363
Parser<UnaryOperator<Integer>> op = anyOf(inc, dec);
3364-
Parser<Integer> parser = number.postfix(op);
3364+
Parser<Integer> parser = number.withPostfixes(op);
33653365
assertThat(parser.source().parse("10")).isEqualTo("10");
33663366
assertThat(parser.source().parseToStream("10")).containsExactly("10");
33673367
assertThat(parser.source().parse("10++")).isEqualTo("10++");
@@ -3379,7 +3379,7 @@ public void postfix_failure() {
33793379
Parser<UnaryOperator<Integer>> inc = string("++").thenReturn(i -> i + 1);
33803380
Parser<UnaryOperator<Integer>> dec = string("--").thenReturn(i -> i - 1);
33813381
Parser<UnaryOperator<Integer>> op = anyOf(inc, dec);
3382-
Parser<Integer> parser = number.postfix(op);
3382+
Parser<Integer> parser = number.withPostfixes(op);
33833383
assertThrows(ParseException.class, () -> parser.parse("a++"));
33843384
assertThrows(ParseException.class, () -> parser.parseToStream("a++").toList());
33853385
assertThrows(ParseException.class, () -> parser.parse("10+"));
@@ -3392,7 +3392,7 @@ public void postfix_failure_withLeftover() {
33923392
Parser<UnaryOperator<Integer>> inc = string("++").thenReturn(i -> i + 1);
33933393
Parser<UnaryOperator<Integer>> dec = string("--").thenReturn(i -> i - 1);
33943394
Parser<UnaryOperator<Integer>> op = anyOf(inc, dec);
3395-
Parser<Integer> parser = number.postfix(op);
3395+
Parser<Integer> parser = number.withPostfixes(op);
33963396
assertThrows(ParseException.class, () -> parser.parse("10++a"));
33973397
assertThrows(ParseException.class, () -> parser.parseToStream("10++a").toList());
33983398
assertThrows(ParseException.class, () -> parser.parse("10 a"));
@@ -3402,7 +3402,7 @@ public void postfix_failure_withLeftover() {
34023402
@Test
34033403
public void postfix_withBiFunction_success() {
34043404
Parser<Integer> parser =
3405-
digits().map(Integer::parseInt).postfix(string("++").map(s -> 1), (a, b) -> a + b);
3405+
digits().map(Integer::parseInt).withPostfixes(string("++").map(s -> 1), (a, b) -> a + b);
34063406
assertThat(parser.parse("10")).isEqualTo(10);
34073407
assertThat(parser.parse("10++")).isEqualTo(11);
34083408
assertThat(parser.parse("10++++")).isEqualTo(12);
@@ -3411,7 +3411,7 @@ public void postfix_withBiFunction_success() {
34113411
@Test
34123412
public void postfix_withBiFunction_failure() {
34133413
Parser<Integer> parser =
3414-
digits().map(Integer::parseInt).postfix(string("!").map(s -> 1), (a, b) -> a + b);
3414+
digits().map(Integer::parseInt).withPostfixes(string("!").map(s -> 1), (a, b) -> a + b);
34153415
assertThrows(ParseException.class, () -> parser.parse("10!a"));
34163416
}
34173417

mug/src/main/java/com/google/mu/function/CheckedFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*****************************************************************************
2-
* Copyright (C) google.com *
2+
* Copyright (C) google.com *
33
* ------------------------------------------------------------------------- *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *

0 commit comments

Comments
 (0)