Repository: incubator-groovy Updated Branches: refs/heads/GROOVY_2_4_X 5a616354c -> 574ea48a8
GROOVY-2178 - Shell can not handle multi-line list defs (closes #181) Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/574ea48a Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/574ea48a Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/574ea48a Branch: refs/heads/GROOVY_2_4_X Commit: 574ea48a8efe30a524c4e407db805e2054c5743d Parents: 5a61635 Author: John Wagenleitner <john.wagenleit...@gmail.com> Authored: Sat Nov 7 07:28:48 2015 -0800 Committer: pascalschumacher <pascalschumac...@gmx.net> Committed: Sat Nov 7 18:10:11 2015 +0100 ---------------------------------------------------------------------- .../codehaus/groovy/tools/shell/Parser.groovy | 22 +++++++++++++- .../tools/shell/GroovyshParsersTest.groovy | 11 +++++++ .../groovy/tools/shell/GroovyshTest.groovy | 30 ++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/574ea48a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Parser.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Parser.groovy b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Parser.groovy index ea546e7..ecb8477 100644 --- a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Parser.groovy +++ b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Parser.groovy @@ -181,7 +181,8 @@ final class RigidParser implements Parsing // HACK: Super insane hack... we detect a syntax error, but might still ignore // it depending on the line ending if (ignoreSyntaxErrorForLineEnding(buffer[-1].trim()) || - isAnnotationExpression(e, buffer[-1].trim())) { + isAnnotationExpression(e, buffer[-1].trim()) || + hasUnmatchedOpenBracketOrParen(source)) { log.debug("Ignoring parse failure; might be valid: $e") } else { error = e @@ -211,6 +212,25 @@ final class RigidParser implements Parsing return false } + static boolean hasUnmatchedOpenBracketOrParen(String source) { + if (!source) { + return false + } + int parens = 0 + int brackets = 0 + for (ch in source) { + switch(ch) { + case '[': ++brackets; break; + case ']': --brackets; break; + case '(': ++parens; break; + case ')': --parens; break; + default: + break + } + } + return (brackets > 0 || parens > 0) + } + static boolean isAnnotationExpression(CompilationFailedException e, String line) { return e.getMessage().contains('unexpected token: @') && ANNOTATION_PATTERN.matcher(line).find() } http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/574ea48a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshParsersTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshParsersTest.groovy b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshParsersTest.groovy index e20eb0f..5c66da0 100644 --- a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshParsersTest.groovy +++ b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshParsersTest.groovy @@ -36,4 +36,15 @@ class GroovyshParsersTest extends GroovyTestCase { assert RigidParser.isAnnotationExpression(mcee, '@Override') } } + + void testHasUnmatchedOpenBracketOrParen() { + assert RigidParser.hasUnmatchedOpenBracketOrParen('a = [') + assert !RigidParser.hasUnmatchedOpenBracketOrParen('a = [1,2,3]') + assert !RigidParser.hasUnmatchedOpenBracketOrParen('a = 1,2,3]') + + assert RigidParser.hasUnmatchedOpenBracketOrParen('myfunc(3') + assert !RigidParser.hasUnmatchedOpenBracketOrParen('myfunc(1,2,3)') + assert !RigidParser.hasUnmatchedOpenBracketOrParen('a = 1,2,3)') + } + } http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/574ea48a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy index 515e43c..4cfe851 100644 --- a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy +++ b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy @@ -92,6 +92,36 @@ class GroovyshTest extends GroovyTestCase { } } + void testIncompleteBracketMultilineExpr() { + Groovysh groovysh = createGroovysh() + groovysh.execute('a = [') + groovysh.execute('1,') + groovysh.execute('2,') + groovysh.execute('3') + groovysh.execute(']') + groovysh.execute('a.size() == 3') + assert mockOut.toString().contains('true') + } + + void testIncompleteParenMultilineExpr() { + Groovysh groovysh = createGroovysh() + groovysh.execute('mc = { num1, num2 -> num1 + num2 }') + groovysh.execute('mc(3') + groovysh.execute(',') + groovysh.execute('7') + groovysh.execute(') == 10') + assert mockOut.toString().contains('true') + } + + void testIncompleteBraceMultilineExpr() { + Groovysh groovysh = createGroovysh() + groovysh.execute('mc = {') + groovysh.execute('3') + groovysh.execute('}') + groovysh.execute('mc() == 3') + assert mockOut.toString().contains('true') + } + void testMissingPropertyExpr() { Groovysh groovysh = createGroovysh() // this is a special case, e.g. happens for Gradle DefaultExtraPropertiesExtension