Parser 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);
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static enum | OperatorTable.
Describes operator associativity, in order of precedence. |
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| private final List |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public Parser | Returns: the expression parser.parser for the operands. operand)Builds a |
| private static <T> Parser | |
| pack-priv static <T> Parser | Returns: the expression parser.parser for the terminals. term, final OperatorTable.the operators. opsBuilds a |
| public OperatorTable | Returns: this.the parser for the operator. parser, int the precedence number. precedence)Adds an infix left-associative binary operator. |
| public OperatorTable | Returns: this.the parser for the operator. parser, int the precedence number. precedence)Adds an infix non-associative binary operator. |
| public OperatorTable | Returns: this.the parser for the operator. parser, int the precedence number. precedence)Adds an infix right-associative binary operator. |
| pack-priv OperatorTable. | |
| public OperatorTable | |
| public OperatorTable | |
| private static Parser |
| ops | back to summary |
|---|---|
| private final List<OperatorTable. | |
| OperatorTable | back to summary |
|---|---|
| public OperatorTable() | |
| build | back to summary |
|---|---|
| public Parser Builds a | |
| build | back to summary |
|---|---|
private static <T> Parser
| |
| buildExpressionParser | back to summary |
|---|---|
| pack-priv static <T> Parser Builds a
| |
| infixl | back to summary |
|---|---|
| public OperatorTable Adds an infix left-associative binary operator.
| |
| infixn | back to summary |
|---|---|
| public OperatorTable Adds an infix non-associative binary operator.
| |
| infixr | back to summary |
|---|---|
| public OperatorTable Adds an infix right-associative binary operator.
| |
| operators | back to summary |
|---|---|
pack-priv OperatorTable.
| |
| postfix | back to summary |
|---|---|
| public OperatorTable Adds a postfix unary operator.
| |
| prefix | back to summary |
|---|---|
| public OperatorTable Adds a prefix unary operator.
| |
| slice | back to summary |
|---|---|
| private static Parser | |
| Modifier and Type | Field and Description |
|---|---|
| public static final OperatorTable. | |
| public static final OperatorTable. | |
| public static final OperatorTable. | |
| public static final OperatorTable. | |
| public static final OperatorTable. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static OperatorTable. | |
| public static OperatorTable. |
| LASSOC | back to summary |
|---|---|
| public static final OperatorTable. | |
| NASSOC | back to summary |
|---|---|
| public static final OperatorTable. | |
| POSTFIX | back to summary |
|---|---|
| public static final OperatorTable. | |
| PREFIX | back to summary |
|---|---|
| public static final OperatorTable. | |
| RASSOC | back to summary |
|---|---|
| public static final OperatorTable. | |
| Associativity | back to summary |
|---|---|
| private Associativity() | |
| valueOf | back to summary |
|---|---|
| public static OperatorTable. | |
| values | back to summary |
|---|---|
| public static OperatorTable. | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final OperatorTable. | |
| pack-priv final Parser | |
| pack-priv final int |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public int | compareTo(OperatorTable.
the object to be compared. thatImplements java. Higher precedence first. |
| associativity | back to summary |
|---|---|
| pack-priv final OperatorTable. | |
| op | back to summary |
|---|---|
| pack-priv final Parser<?> op | |
| precedence | back to summary |
|---|---|
| pack-priv final int precedence | |
| Operator | back to summary |
|---|---|
| pack-priv Operator(Parser<?> op, int precedence, OperatorTable. | |
| compareTo | back to summary |
|---|---|
| public int compareTo(OperatorTable. Implements java. Higher precedence first. For tie, compares associativity.
| |