public class Parser extends Object
Takes a string input as String or Reader which will be translated into an Expression
. If one or more errors
occur, a ParseException
will be thrown. The parser tries to continue as long a possible to provide good
insight into the errors within the expression.
This is a recursive descending parser which has a method per non-terminal.
Using this parser is as easy as:
Scope scope = Scope.create();
Variable a = scope.getVariable("a");
Expression expr = Parser.parse("3 + a * 4");
a.setValue(4);
System.out.println(expr.evaluate());
a.setValue(5);
System.out.println(expr.evaluate());
Modifier | Constructor and Description |
---|---|
|
Parser() |
protected |
Parser(Reader input,
Scope scope,
Map<String,Function> functionTable) |
Modifier and Type | Method and Description |
---|---|
protected Expression |
atom()
Parser rule for parsing an atom.
|
protected void |
expect(Token.TokenType type,
String trigger)
Signals that the given token is expected.
|
protected Expression |
expression()
Parser rule for parsing an expression.
|
protected Expression |
functionCall()
Parses a function call.
|
Scope |
getScope() |
Expression |
parse()
Parses the expression in input
|
Expression |
parse(Reader input)
Parses the given input into an expression.
|
Expression |
parse(Reader input,
Scope scope)
Parses the given input into an expression.
|
Expression |
parse(String input)
Parses the given input into an expression.
|
Expression |
parse(String input,
Scope scope)
Parses the given input into an expression.
|
protected Expression |
power()
Parser rule for parsing a power.
|
protected Expression |
product()
Parser rule for parsing a product.
|
void |
registerFunction(String name,
Function function)
Registers a new function which can be referenced from within an expression.
|
protected Expression |
relationalExpression()
Parser rule for parsing a relational expression.
|
protected Expression |
reOrder(Expression left,
Expression right,
BinaryOperation.Op op) |
protected void |
replaceLeft(BinaryOperation target,
Expression newLeft,
BinaryOperation.Op op) |
protected Expression |
term()
Parser rule for parsing a term.
|
public Scope getScope()
public void registerFunction(String name, Function function)
A function must be registered before an expression is parsed in order to be visible.
name
- the name of the function. If a function with the same name is already available, it will be
overriddenfunction
- the function which is invoked as an expression is evaluatedpublic Expression parse(String input) throws ParseException
input
- the expression to be parsedParseException
- if the expression contains one or more errorspublic Expression parse(Reader input) throws ParseException
input
- the expression to be parsedParseException
- if the expression contains one or more errorspublic Expression parse(String input, Scope scope) throws ParseException
Referenced variables will be resolved using the given Scope
input
- the expression to be parsedscope
- the scope used to resolve variablesParseException
- if the expression contains one or more errorspublic Expression parse(Reader input, Scope scope) throws ParseException
Referenced variables will be resolved using the given Scope
input
- the expression to be parsedscope
- the scope used to resolve variablesParseException
- if the expression contains one or more errorspublic Expression parse() throws ParseException
ParseException
- if the expression contains one or more errorsprotected Expression expression()
This is the root rule. An expression is a relationalExpression which might be followed by a logical operator (&& or ||) and another expression.
protected Expression relationalExpression()
A relational expression is a term which might be followed by a relational operator (<,<=,...,>) and another relationalExpression.
protected Expression term()
A term is a product which might be followed by + or - as operator and another term.
protected Expression product()
A product is a power which might be followed by *, / or % as operator and another product.
protected Expression reOrder(Expression left, Expression right, BinaryOperation.Op op)
protected void replaceLeft(BinaryOperation target, Expression newLeft, BinaryOperation.Op op)
protected Expression power()
A power is an atom which might be followed by ^ or ** as operator and another power.
protected Expression atom()
An atom is either a numeric constant, an expression in brackets, an expression surrounded by | to signal the absolute function, an identifier to signal a variable reference or an identifier followed by a bracket to signal a function call.
protected Expression functionCall()
protected void expect(Token.TokenType type, String trigger)
If the current input is pointing at the specified token, it will be consumed. If not, an error will be added to the error list and the input remains unchanged.
type
- the type of the expected tokentrigger
- the trigger of the expected tokenCopyright © 2020. All rights reserved.