Top Description Inners Fields Constructors Methods
org.jparsec

public final Class OperatorTable<T>

extends Object
Class Inheritance
Imports
java.util.Collections, .List, java.util.function.BiFunction, .Function, org.jparsec.internal.annotations.Private, org.jparsec.internal.util.Lists

Builds 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);

Author
Ben Yu

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static enum
OperatorTable.Associativity

Describes operator associativity, in order of precedence.

pack-priv static class

Field Summary

Modifier and TypeField and Description
private final List<OperatorTable.Operator>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Parser<T>

Returns:

the expression parser.
build
(Parser<? extends T>
parser for the operands.
operand
)

Builds a Parser based on information in this OperatorTable.

private static <T> Parser<T>
build(Parser<T> op, OperatorTable.Associativity associativity, Parser<T> operand)

pack-priv static <T> Parser<T>

Returns:

the expression parser.
buildExpressionParser
(final Parser<? extends T>
parser for the terminals.
term
,
final OperatorTable.Operator...
the operators.
ops
)

Builds a Parser based on information described by OperatorTable.

public OperatorTable<T>

Returns:

this.
infixl
(Parser<? extends BiFunction<? super T, ? super T, ? extends T>>
the parser for the operator.
parser
,
int
the precedence number.
precedence
)

Adds an infix left-associative binary operator.

public OperatorTable<T>

Returns:

this.
infixn
(Parser<? extends BiFunction<? super T, ? super T, ? extends T>>
the parser for the operator.
parser
,
int
the precedence number.
precedence
)

Adds an infix non-associative binary operator.

public OperatorTable<T>

Returns:

this.
infixr
(Parser<? extends BiFunction<? super T, ? super T, ? extends T>>
the parser for the operator.
parser
,
int
the precedence number.
precedence
)

Adds an infix right-associative binary operator.

pack-priv OperatorTable.Operator[]
public OperatorTable<T>

Returns:

this.
postfix
(Parser<? extends Function<? super T, ? extends T>>
the parser for the operator.
parser
,
int
the precedence number.
precedence
)

Adds a postfix unary operator.

public OperatorTable<T>

Returns:

this.
prefix
(Parser<? extends Function<? super T, ? extends T>>
the parser for the operator.
parser
,
int
the precedence number.
precedence
)

Adds a prefix unary operator.

private static Parser<?>
slice(OperatorTable.Operator[] ops, int begin, int end)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

opsback to summary
private final List<OperatorTable.Operator> ops

Constructor Detail

OperatorTableback to summary
public OperatorTable()

Method Detail

buildback to summary
public Parser<T> build(Parser<? extends T> operand)

Builds a Parser based on information in this OperatorTable.

Parameters
operand:Parser<? extends T>

parser for the operands.

Returns:Parser<T>

the expression parser.

buildback to summary
private static <T> Parser<T> build(Parser<T> op, OperatorTable.Associativity associativity, Parser<T> operand)
Annotations
@SuppressWarnings:rawtypes, unchecked
buildExpressionParserback to summary
pack-priv static <T> Parser<T> buildExpressionParser(final Parser<? extends T> term, final OperatorTable.Operator... ops)

Builds a Parser based on information described by OperatorTable.

Parameters
term:Parser<? extends T>

parser for the terminals.

ops:OperatorTable.Operator[]

the operators.

Returns:Parser<T>

the expression parser.

infixlback to summary
public OperatorTable<T> infixl(Parser<? extends BiFunction<? super T, ? super T, ? extends T>> parser, int precedence)

Adds an infix left-associative binary operator.

Parameters
parser:Parser<? extends BiFunction<? super T, ? super T, ? extends T>>

the parser for the operator.

precedence:int

the precedence number.

Returns:OperatorTable<T>

this.

infixnback to summary
public OperatorTable<T> infixn(Parser<? extends BiFunction<? super T, ? super T, ? extends T>> parser, int precedence)

Adds an infix non-associative binary operator.

Parameters
parser:Parser<? extends BiFunction<? super T, ? super T, ? extends T>>

the parser for the operator.

precedence:int

the precedence number.

Returns:OperatorTable<T>

this.

infixrback to summary
public OperatorTable<T> infixr(Parser<? extends BiFunction<? super T, ? super T, ? extends T>> parser, int precedence)

Adds an infix right-associative binary operator.

Parameters
parser:Parser<? extends BiFunction<? super T, ? super T, ? extends T>>

the parser for the operator.

precedence:int

the precedence number.

Returns:OperatorTable<T>

this.

operatorsback to summary
pack-priv OperatorTable.Operator[] operators()
Annotations
@Private
postfixback to summary
public OperatorTable<T> postfix(Parser<? extends Function<? super T, ? extends T>> parser, int precedence)

Adds a postfix unary operator.

Parameters
parser:Parser<? extends Function<? super T, ? extends T>>

the parser for the operator.

precedence:int

the precedence number.

Returns:OperatorTable<T>

this.

prefixback to summary
public OperatorTable<T> prefix(Parser<? extends Function<? super T, ? extends T>> parser, int precedence)

Adds a prefix unary operator.

Parameters
parser:Parser<? extends Function<? super T, ? extends T>>

the parser for the operator.

precedence:int

the precedence number.

Returns:OperatorTable<T>

this.

sliceback to summary
private static Parser<?> slice(OperatorTable.Operator[] ops, int begin, int end)
org.jparsec back to summary

pack-priv final Enum OperatorTable.Associativity

extends Enum<OperatorTable.Associativity>
Class Inheritance

Describes operator associativity, in order of precedence.

Field Summary

Modifier and TypeField and Description
public static final OperatorTable.Associativity
public static final OperatorTable.Associativity
public static final OperatorTable.Associativity
public static final OperatorTable.Associativity
public static final OperatorTable.Associativity

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static OperatorTable.Associativity
public static OperatorTable.Associativity[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

LASSOCback to summary
public static final OperatorTable.Associativity LASSOC
NASSOCback to summary
public static final OperatorTable.Associativity NASSOC
POSTFIXback to summary
public static final OperatorTable.Associativity POSTFIX
PREFIXback to summary
public static final OperatorTable.Associativity PREFIX
RASSOCback to summary
public static final OperatorTable.Associativity RASSOC

Constructor Detail

Associativityback to summary
private Associativity()

Method Detail

valueOfback to summary
public static OperatorTable.Associativity valueOf(String name)
valuesback to summary
public static OperatorTable.Associativity[] values()
org.jparsec back to summary

pack-priv final Class OperatorTable.Operator

extends Object
implements Comparable<OperatorTable.Operator>
Class Inheritance
All Implemented Interfaces
java.lang.Comparable

Field Summary

Modifier and TypeField and Description
pack-priv final OperatorTable.Associativity
pack-priv final Parser<?>
pack-priv final int

Constructor Summary

AccessConstructor and Description
pack-priv
Operator(Parser<?> op, int precedence, OperatorTable.Associativity associativity)

Method Summary

Modifier and TypeMethod and Description
public int
compareTo(OperatorTable.Operator
the object to be compared.
that
)

Implements java.lang.Comparable.compareTo.

Higher precedence first.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

associativityback to summary
pack-priv final OperatorTable.Associativity associativity
opback to summary
pack-priv final Parser<?> op
precedenceback to summary
pack-priv final int precedence

Constructor Detail

Operatorback to summary
pack-priv Operator(Parser<?> op, int precedence, OperatorTable.Associativity associativity)

Method Detail

compareToback to summary
public int compareTo(OperatorTable.Operator that)

Implements java.lang.Comparable.compareTo.

Higher precedence first. For tie, compares associativity.

Parameters
that:OperatorTable.Operator

Doc from java.lang.Comparable.compareTo.

the object to be compared.

Returns:int

Doc from java.lang.Comparable.compareTo.

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Annotations
@Override