Class OperatorTable<T>
- java.lang.Object
-
- org.jparsec.OperatorTable<T>
-
public final class OperatorTable<T> extends java.lang.Object
BuildsParser
to parse expressions with operator-precedence grammar. The operators and precedences are declared in this table.Operators have precedences. The higher the precedence number, the higher the precedence. For the same precedence, prefix > postfix > left-associative > non-associative > right-associative.
For example:
Unary<Integer> negate = new Unary<Integer>() {... return -n; }; Binary<Integer> plus = new Binary<Integer>() {... return a + b; }; Binary<Integer> minus = new Binary<Integer>() {... return a - b; }; ... Terminals terms = Terminals.operators("+", "-", "*", "/"); Parser<Integer> calculator = new OperatorTable() .prefix(terms.token("-").retn(negate), 100) .infixl(terms.token("+").retn(plus), 10) .infixl(terms.token("-").retn(minus), 10) .infixl(terms.token("*").retn(multiply), 20) .infixl(terms.token("/").retn(divide), 20) .build(Terminals.IntegerLiteral.PARSER.map(stringToInteger)); Parser<Integer> parser = calculator.from( terms.tokenizer().or(Terminals.IntegerLiteral.TOKENIZER), Scanners.WHITESPACES.optional()); return parser.parse(text);
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
OperatorTable.Associativity
Describes operator associativity, in order of precedence.(package private) static class
OperatorTable.Operator
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<OperatorTable.Operator>
ops
-
Constructor Summary
Constructors Constructor Description OperatorTable()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Parser<T>
build(Parser<? extends T> operand)
Builds aParser
based on information in thisOperatorTable
.private static <T> Parser<T>
build(Parser op, OperatorTable.Associativity associativity, Parser<T> operand)
(package private) static <T> Parser<T>
buildExpressionParser(Parser<? extends T> term, OperatorTable.Operator... ops)
Builds aParser
based on information described byOperatorTable
.OperatorTable<T>
infixl(Parser<? extends java.util.function.BiFunction<? super T,? super T,? extends T>> parser, int precedence)
Adds an infix left-associative binary operator.OperatorTable<T>
infixn(Parser<? extends java.util.function.BiFunction<? super T,? super T,? extends T>> parser, int precedence)
Adds an infix non-associative binary operator.OperatorTable<T>
infixr(Parser<? extends java.util.function.BiFunction<? super T,? super T,? extends T>> parser, int precedence)
Adds an infix right-associative binary operator.(package private) OperatorTable.Operator[]
operators()
OperatorTable<T>
postfix(Parser<? extends java.util.function.Function<? super T,? extends T>> parser, int precedence)
Adds a postfix unary operator.OperatorTable<T>
prefix(Parser<? extends java.util.function.Function<? super T,? extends T>> parser, int precedence)
Adds a prefix unary operator.private static Parser<?>
slice(OperatorTable.Operator[] ops, int begin, int end)
-
-
-
Field Detail
-
ops
private final java.util.List<OperatorTable.Operator> ops
-
-
Method Detail
-
prefix
public OperatorTable<T> prefix(Parser<? extends java.util.function.Function<? super T,? extends T>> parser, int precedence)
Adds a prefix unary operator.- Parameters:
parser
- the parser for the operator.precedence
- the precedence number.- Returns:
- this.
-
postfix
public OperatorTable<T> postfix(Parser<? extends java.util.function.Function<? super T,? extends T>> parser, int precedence)
Adds a postfix unary operator.- Parameters:
parser
- the parser for the operator.precedence
- the precedence number.- Returns:
- this.
-
infixl
public OperatorTable<T> infixl(Parser<? extends java.util.function.BiFunction<? super T,? super T,? extends T>> parser, int precedence)
Adds an infix left-associative binary operator.- Parameters:
parser
- the parser for the operator.precedence
- the precedence number.- Returns:
- this.
-
infixr
public OperatorTable<T> infixr(Parser<? extends java.util.function.BiFunction<? super T,? super T,? extends T>> parser, int precedence)
Adds an infix right-associative binary operator.- Parameters:
parser
- the parser for the operator.precedence
- the precedence number.- Returns:
- this.
-
infixn
public OperatorTable<T> infixn(Parser<? extends java.util.function.BiFunction<? super T,? super T,? extends T>> parser, int precedence)
Adds an infix non-associative binary operator.- Parameters:
parser
- the parser for the operator.precedence
- the precedence number.- Returns:
- this.
-
build
public Parser<T> build(Parser<? extends T> operand)
Builds aParser
based on information in thisOperatorTable
.- Parameters:
operand
- parser for the operands.- Returns:
- the expression parser.
-
operators
OperatorTable.Operator[] operators()
-
buildExpressionParser
static <T> Parser<T> buildExpressionParser(Parser<? extends T> term, OperatorTable.Operator... ops)
Builds aParser
based on information described byOperatorTable
.- Parameters:
term
- parser for the terminals.ops
- the operators.- Returns:
- the expression parser.
-
slice
private static Parser<?> slice(OperatorTable.Operator[] ops, int begin, int end)
-
build
private static <T> Parser<T> build(Parser op, OperatorTable.Associativity associativity, Parser<T> operand)
-
-