Russian-language documentation
This project solves the following problem: in the case when there is a need to build, syntactically bypass the abstract tree of mathematical expressions, with easy customization of tokens, priorities of operations without the need to edit the grammar and re-engineer the parser code.
This solution has one non-obvious drawback. The parser works according to the sorting station algorithm, because of this, there is a need for a separate operation for the unary minus - "~".
- Detailed description of the logic of the components
- Example of constructing a syntactically abstract tree
- Get Started
- References
Statement: (field_1 > 5 AND field_1 < 10) AND NOT (field_2 = "value1" OR field_2 = "value2")
For the default case, you can use the MathExpressionParserFacade object:
MathExpressionParserFacade parserFacade = new MathExpressionParserFacade();
parserFacade.parse(statement);Underneath the facade lies the following logic, it is recommended to use the following method to work with the parser.
Lexer lexer = new Lexer();
Parser parser = new MathExpressionParserFactory().create();
TokenStream stream = lexer.getTokenStream(statement);
ExpressionNode node = parser.parse(stream);The parsing result can be bypassed using - DefaultExpressionNodeVisitor:
ExpressionNodeVisitor visitor = new DefaultExpressionNodeVisitor();
node.accept(visitor);You can also add DefaultExpressionNodeVisitor to variables, which will be taken into account when traversing the tree:
Map<String, VisitorData> data = new HashMap<>();
data.put("key", new VisitorData("value"));
ExpressionNodeVisitor visitor = new DefaultExpressionNodeVisitor(data);
node.accept(visitor);