Skip to content

Commit

Permalink
Groovy REPL for statement: improved looping variable type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Sep 5, 2020
1 parent 80d4d3f commit 9c7acb9
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -793,8 +793,8 @@ 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)$"
static final Pattern LAMBDA_PATTERN = Pattern.compile(".*\\([(]*(.*?)[)]*->.*");
static final Pattern PATTERN_FUNCTION_BODY = Pattern.compile("^\\s*\\(([a-zA-Z0-9_ ,]*)\\)\\s*\\{(.*)?}(|\n)$"
, Pattern.DOTALL);
static final String DEFAULT_NANORC_SYNTAX = "classpath:/org/jline/groovy/java.nanorc";
static final String DEFAULT_GROOVY_COLORS = "ti=1;34:me=31";
Expand Down Expand Up @@ -889,18 +889,19 @@ public void loadStatementVars(String line) {
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,}")
if (statement.matches("^(if|while)\\s*\\(.*") || statement.matches("(}\\s*|^)else(\\s*\\{|$)")
|| statement.matches("(}\\s*|^)else\\s+if\\s*\\(.*") || statement.matches("^break[;]+")
|| statement.matches("^case\\s+.*:") || statement.matches("^default\\s+:")
|| statement.matches("(\\{|\\})") || statement.length() == 0) {
|| statement.matches("(\\{|})") || statement.length() == 0) {
continue;
} else if (forEachMatcher.matches()) {
statement = forEachMatcher.group(1).trim();
String cc = forEachMatcher.group(2);
if (statement.matches("\\w+\\s+\\w+.*")) {
int idx = statement.indexOf(' ');
statement = statement.substring(idx + 1);
}
statement += "=" + forEachMatcher.group(2) + "[0]";
statement += "=" + cc + " instanceof Map ? " + cc + ".entrySet()[0] : " + cc + "[0]";
} else if (forMatcher.matches()) {
statement = forMatcher.group(1).trim();
if (statement.matches("\\w+\\s+\\w+.*")) {
Expand All @@ -921,8 +922,11 @@ public void loadStatementVars(String line) {
statement = statement.substring(idx + 1);
}
Brackets br = new Brackets(statement);
if (statement.contains("=") && !br.openRound() && !br.openCurly()) {
execute(statement);
if (statement.contains("=") && !br.openRound() && !br.openCurly() && !br.openSquare()) {
String st = statement.substring(statement.indexOf('=') + 1).trim();
if (!st.isEmpty() && !st.equals("new")) {
execute(statement);
}
}
} catch (Exception e) {
if (Log.isDebugEnabled()) {
Expand Down Expand Up @@ -1301,6 +1305,7 @@ private static class Brackets {
int quoteId = -1;
int round = 0;
int curly = 0;
int square = 0;
int rounds = 0;
int curlies = 0;

Expand Down Expand Up @@ -1342,6 +1347,10 @@ public Brackets(String line) {
curly--;
curlyOpen.removeLast();
lastCurlyClose = pos;
} else if (ch == '[') {
square++;
} else if (ch == ']') {
square--;
} else if (ch == ',' && !roundOpen.isEmpty()) {
lastComma.put(roundOpen.getLast(), pos);
} else if (ch == ';' || ch == '\n' || (ch == '>' && prevChar == '-')) {
Expand All @@ -1352,7 +1361,7 @@ public Brackets(String line) {
lastDelim = pos;
}
prevChar = ch;
if (round < 0 || curly < 0) {
if (round < 0 || curly < 0 || square < 0) {
throw new IllegalArgumentException();
}
}
Expand Down Expand Up @@ -1410,6 +1419,10 @@ public boolean openCurly() {
return curly > 0;
}

public boolean openSquare() {
return square > 0;
}

public int numberOfRounds() {
return rounds;
}
Expand Down

0 comments on commit 9c7acb9

Please sign in to comment.