Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.northconcepts.datapipeline.examples.cookbook;

import com.northconcepts.datapipeline.internal.expression.Expression;
import com.northconcepts.datapipeline.internal.expression.InvalidSyntax;
import com.northconcepts.datapipeline.internal.parser.Parser;

public class ParseInvalidExpressions {

public static void main(String[] args) {
parseAnInvalidExpression();
parseInvalidExpressions();
}

private static void parseAnInvalidExpression() {
System.out.println("=========================== Parse An Invalid Expression =======================================");
// Parse an invalid expression which is missing closing parenthesis

Expression expression = Parser.parseExpression("toInt('123'", true);
executeExpression(expression);
}

private static void executeExpression(Expression expression) {
InvalidSyntax invalidSyntax = (InvalidSyntax) expression;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's extract this code as a separate method (executeExpression(Expression expression)) for use here and with the 2 expressions below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created this common method.

System.out.println("Source Expression: " + invalidSyntax.getSourceString());
System.out.println("Expression Parse Exception: " + invalidSyntax.getParseException().getMessage());

try {
System.out.println(expression.evaluate(null));
} catch (Throwable e) {
System.err.println("Exception while evaluating invalid expression: " + e.getMessage());
}
}

private static void parseInvalidExpressions() {
System.out.println("=========================== Parse Multiple Invalid Expression =================================");

Parser.doWithoutParseException(() -> {
Expression expression1 = Parser.parseExpression("toInt('123'");
executeExpression(expression1);

System.out.println("=============================================================================");

Expression expression2 = Parser.parseExpression("toInt('345'");
executeExpression(expression2);
});
}
}