Skip to content

Commit

Permalink
Upgrade JavaParser to 3.23.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauernfeind committed Aug 27, 2021
1 parent a791fd3 commit 2373cb7
Show file tree
Hide file tree
Showing 11 changed files with 9,412 additions and 11,117 deletions.
38 changes: 38 additions & 0 deletions DB/src/main/java/com/github/javaparser/ExpressionParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.javaparser;

import com.github.javaparser.ast.expr.Expression;

import java.io.IOException;
import java.io.StringReader;

/**
* Helpful class which parses expressions
* and performs extra "this is exactly one expression" validation
*/
public class ExpressionParser {
public static Expression parseExpression(String expression) throws ParseException, IOException {
// nothing static here, so no synchronization needed (javaparser used to create a static-only parser instance)
StringReader sr = new StringReader(expression);
ParseResult<Expression> result = new JavaParser().parse(ParseStart.EXPRESSION, Providers.provider(sr));
if (!result.isSuccessful()) {
throw new IllegalArgumentException("Invalid expression " + expression + ": " + result.getProblems().toString());
}
// a successful parse does not always mean there is a result.
Expression expr = null;
if (result.getResult().isPresent()) {
expr = result.getResult().get();
}

// unlikely for there to be tokens left w/out parser.token.next already being non-null (we would already have thrown...)
int test;
try {
while ((test = sr.read()) != -1) {
if (!Character.isWhitespace(test)) {
throw new IllegalArgumentException("Invalid expression " + expression + " was already terminated after " + expr);
}
}
sr.close();
} catch (IOException ignored){}
return expr;
}
}

Large diffs are not rendered by default.

Loading

0 comments on commit 2373cb7

Please sign in to comment.