Class OperatorTable<T>
java.lang.Object
com.google.common.labs.parse.OperatorTable<T>
Provides a fluent API for building an operator precedence grammar.
For example, to build a simple calculator parser, you can implement it as:
Parser<Integer> calculator = new OperatorTable<Integer>()
.leftAssociative("+", (l, r) -> l + r, 10)
.leftAssociative("-", (l, r) -> l - r, 10)
.leftAssociative("*", (l, r) -> l * r), 20)
.rightAssociative("^", (l, r) -> pow(l, r), 30)
.prefix("-", n -> -n, 40)
.build(consecutive(DIGIT, "number").map(Integer::parseInt));
calculator.parse("1+2*3^2") // 19
calculator.parseSkipping(whitespace(), "1 + 2") // 3
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionBuilds a parser with the configured operators applied to the given operand.leftAssociative
(Parser<? extends BinaryOperator<T>> operator, int precedence) Adds a left-associative infix operator with the given precedence to the table.leftAssociative
(String op, BinaryOperator<T> operator, int precedence) Adds a left-associative infix operator with the given precedence to the table.nonAssociative
(Parser<? extends BinaryOperator<T>> operator, int precedence) Adds a non-associative infix operator with the given precedence to the table.nonAssociative
(String op, BinaryOperator<T> operator, int precedence) Adds a non-associative infix operator with the given precedence to the table.postfix
(Parser<? extends UnaryOperator<T>> operator, int precedence) Adds a postfix operator with the given precedence to the table.postfix
(String op, UnaryOperator<T> operator, int precedence) Adds a postfix operator with the given precedence to the table.prefix
(Parser<? extends UnaryOperator<T>> operator, int precedence) Adds a prefix operator with the given precedence to the table.prefix
(String op, UnaryOperator<T> operator, int precedence) Adds a prefix operator with the given precedence to the table.rightAssociative
(Parser<? extends BinaryOperator<T>> operator, int precedence) Adds a right-associative infix operator with the given precedence to the table.rightAssociative
(String op, BinaryOperator<T> operator, int precedence) Adds a right-associative infix operator with the given precedence to the table.
-
Constructor Details
-
OperatorTable
public OperatorTable()
-
-
Method Details
-
prefix
@CanIgnoreReturnValue public OperatorTable<T> prefix(String op, UnaryOperator<T> operator, int precedence) Adds a prefix operator with the given precedence to the table. The higherprecedence
value the higher precedence it is. -
prefix
@CanIgnoreReturnValue public OperatorTable<T> prefix(Parser<? extends UnaryOperator<T>> operator, int precedence) Adds a prefix operator with the given precedence to the table. The higherprecedence
value the higher precedence it is. -
postfix
@CanIgnoreReturnValue public OperatorTable<T> postfix(String op, UnaryOperator<T> operator, int precedence) Adds a postfix operator with the given precedence to the table. The higherprecedence
value the higher precedence it is. -
postfix
@CanIgnoreReturnValue public OperatorTable<T> postfix(Parser<? extends UnaryOperator<T>> operator, int precedence) Adds a postfix operator with the given precedence to the table. The higherprecedence
value the higher precedence it is. -
leftAssociative
@CanIgnoreReturnValue public OperatorTable<T> leftAssociative(String op, BinaryOperator<T> operator, int precedence) Adds a left-associative infix operator with the given precedence to the table. The higherprecedence
value the higher precedence it is. -
leftAssociative
@CanIgnoreReturnValue public OperatorTable<T> leftAssociative(Parser<? extends BinaryOperator<T>> operator, int precedence) Adds a left-associative infix operator with the given precedence to the table. The higherprecedence
value the higher precedence it is. -
nonAssociative
@CanIgnoreReturnValue public OperatorTable<T> nonAssociative(String op, BinaryOperator<T> operator, int precedence) Adds a non-associative infix operator with the given precedence to the table. The higherprecedence
value the higher precedence it is. -
nonAssociative
@CanIgnoreReturnValue public OperatorTable<T> nonAssociative(Parser<? extends BinaryOperator<T>> operator, int precedence) Adds a non-associative infix operator with the given precedence to the table. The higherprecedence
value the higher precedence it is. -
rightAssociative
@CanIgnoreReturnValue public OperatorTable<T> rightAssociative(String op, BinaryOperator<T> operator, int precedence) Adds a right-associative infix operator with the given precedence to the table. The higherprecedence
value the higher precedence it is. -
rightAssociative
@CanIgnoreReturnValue public OperatorTable<T> rightAssociative(Parser<? extends BinaryOperator<T>> operator, int precedence) Adds a right-associative infix operator with the given precedence to the table. The higherprecedence
value the higher precedence it is. -
build
-