Package org.scijava.parsington
Class ExpressionParser
java.lang.Object
org.scijava.parsington.ExpressionParser
A parser for mathematical expressions, using Dijkstra's famous shunting-yard
algorithm.
It is important to note that this parser does not attempt to evaluate the expression in any way; rather, it only provides the parsed tree according to the desired operators.
- Author:
- Curtis Rueden
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an expression parser with the standard set of operators and default separator symbols (,
for group elements,;
for statements).ExpressionParser
(String elementSeparator, String statementSeparator) Creates an expression parser with custom separator symbols.ExpressionParser
(Collection<? extends Operator> operators) Creates an expression parser with custom operators and default separator symbols (,
for group elements,;
for statements).ExpressionParser
(Collection<? extends Operator> operators, String elementSeparator, String statementSeparator) Creates an expression parser with custom operators and separator symbols.ExpressionParser
(Collection<? extends Operator> operators, String elementSeparator, String statementSeparator, BiFunction<ExpressionParser, String, ParseOperation> parseOperationFactory) Creates an expression parser maximally customized to your requirements!ExpressionParser
(BiFunction<ExpressionParser, String, ParseOperation> parseOperationFactory) Creates an expression parser with customParseOperation
behavior. -
Method Summary
Modifier and TypeMethodDescriptionSeparator symbol between group elements.Gets the list of operators available to expressions.parsePostfix
(String expression) Parses the given mathematical expression into a queue in Reverse Polish notation (i.e., postfix notation).Parses the given mathematical expression into a syntax tree.Separator symbol between statements.
-
Constructor Details
-
ExpressionParser
public ExpressionParser()Creates an expression parser with the standard set of operators and default separator symbols (,
for group elements,;
for statements).- See Also:
-
ExpressionParser
Creates an expression parser with custom operators and default separator symbols (,
for group elements,;
for statements).- Parameters:
operators
- The collection of operators available to expressions.
-
ExpressionParser
Creates an expression parser with custom separator symbols.- Parameters:
elementSeparator
- The symbol to use for separating group elements.statementSeparator
- The symbol to use for separating statements.
-
ExpressionParser
Creates an expression parser with customParseOperation
behavior. Customizing this behavior allows you to control lower level parsing characteristics, including what character sequences constitute whitespace, literals, variables, operators, group terminators, element separators, and statement separators.- Parameters:
parseOperationFactory
- A function producingParseOperation
objects with behavior customized to your requirements. The typical use case is to subclassParseOperation
to override one or more of itsparseSomething
methods, and then passMyCustomParseOperation::new
for this argument.
-
ExpressionParser
public ExpressionParser(Collection<? extends Operator> operators, String elementSeparator, String statementSeparator) Creates an expression parser with custom operators and separator symbols.- Parameters:
operators
- The collection of operators available to expressions.elementSeparator
- The symbol to use for separating group elements.statementSeparator
- The symbol to use for separating statements.
-
ExpressionParser
public ExpressionParser(Collection<? extends Operator> operators, String elementSeparator, String statementSeparator, BiFunction<ExpressionParser, String, ParseOperation> parseOperationFactory) Creates an expression parser maximally customized to your requirements!- Parameters:
operators
- The collection of operators available to expressions.elementSeparator
- The symbol to use for separating group elements.statementSeparator
- The symbol to use for separating statements.parseOperationFactory
- A function producingParseOperation
objects with behavior customized to your requirements. The typical use case is to subclassParseOperation
to override one or more of itsparseSomething
methods, and then passMyCustomParseOperation::new
for this argument.
-
-
Method Details
-
parseTree
Parses the given mathematical expression into a syntax tree.- Parameters:
expression
- The mathematical expression to parse.- Returns:
- Parsed hierarchy of tokens.
- Throws:
IllegalArgumentException
- if the syntax of the expression is incorrect.
-
parsePostfix
Parses the given mathematical expression into a queue in Reverse Polish notation (i.e., postfix notation).- Parameters:
expression
- The mathematical expression to parse.- Returns:
- Parsed queue of tokens in postfix notation.
- Throws:
IllegalArgumentException
- if the syntax of the expression is incorrect.
-
operators
Gets the list of operators available to expressions.- Returns:
- A read-only view of the operators list.
-
elementSeparator
Separator symbol between group elements. The default symbol is comma (,
). Example:f(1, 2)
- See Also:
-
statementSeparator
Separator symbol between statements. The default symbol is semicolon (;
). Example:x = 1; y = 2; z = x + y
- See Also:
-