Skip to content

Commit

Permalink
GroovyEngine Inspector: manage for-each statement
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jul 11, 2020
1 parent ef81ae7 commit 722f2ef
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ private Set<String> retrieveClassesWithStaticMethods() {

private static class Inspector {
static final Pattern PATTERN_FOR = Pattern.compile("^for\\s*\\((.*?);.*");
static final Pattern PATTERN_FOR_EACH = Pattern.compile("^for\\s*\\((.*?):(.*?)\\).*");
static final Pattern LAMBDA_PATTERN = Pattern.compile(".*\\([\\(]*(.*?)[\\)]*->.*");
static final Pattern PATTERN_FUNCTION_BODY = Pattern.compile("^\\s*\\(([a-zA-Z0-9_ ,]*)\\)\\s*\\{(.*)?\\}(|\n)$"
, Pattern.DOTALL);
Expand Down Expand Up @@ -850,17 +851,30 @@ public void loadStatementVars(String line) {
for (String s : line.split("\\r?\\n")) {
String statement = s.trim();
try {
Matcher forEachMatcher = PATTERN_FOR_EACH.matcher(statement);
Matcher forMatcher = PATTERN_FOR.matcher(statement);
Matcher lambdaMatcher = LAMBDA_PATTERN.matcher(statement);
if (statement.matches("^(if|while)\\s*\\(.*") || statement.matches("(\\}\\s*|^)else(\\s*\\{|$)")
|| statement.matches("(\\}\\s*|^)else\\s+if\\s*\\(.*") || statement.matches("^break[;]{1,}")
|| statement.matches("^case\\s+.*:") || statement.matches("^default\\s+:")
|| statement.matches("(\\{|\\})") || statement.length() == 0) {
continue;
} else if (forEachMatcher.matches()) {
statement = forEachMatcher.group(1).trim();
if (statement.matches("\\w+\\s+\\w+.*")) {
int idx = statement.indexOf(' ');
statement = statement.substring(idx + 1);
}
statement += "=" + forEachMatcher.group(2) + "[0]";
} else if (forMatcher.matches()) {
statement = forMatcher.group(1).trim();
int idx = statement.indexOf(' ');
statement = statement.substring(idx + 1);
if (statement.matches("\\w+\\s+\\w+.*")) {
int idx = statement.indexOf(' ');
statement = statement.substring(idx + 1);
}
if (!statement.contains("=")) {
statement += " = null";
}
} else if (lambdaMatcher.matches()) {
String[] vars = lambdaMatcher.group(1).split(",");
statement = "";
Expand Down Expand Up @@ -1048,7 +1062,7 @@ private String trimMethodDescription(StringBuilder sb) {
private CmdDesc checkSyntax(CmdLine line) {
CmdDesc out = new CmdDesc();
int openingRound = Brackets.indexOfOpeningRound(line.getHead());
if (openingRound == -1 || line.getHead().substring(openingRound).matches("\\(\\s*\\w+\\s*[,\\s*\\w+\\s*]*\\)")) {
if (openingRound == -1) {
return out;
}
loadStatementVars(line.getHead());
Expand All @@ -1071,7 +1085,9 @@ private CmdDesc checkSyntax(CmdLine line) {
String objEquation = line.getHead().substring(eqsep + 1, end);
equationLines = objEquation.split("\\r?\\n");
cuttedSize = eqsep + 1;
if (objEquation != null) {
if (objEquation == null || objEquation.matches("\\(\\s*\\w+\\s*[,\\s*\\w+\\s*]*\\)")) {
// do nothing
} else {
try {
execute(objEquation);
} catch (groovy.lang.MissingPropertyException e) {
Expand Down

0 comments on commit 722f2ef

Please sign in to comment.