You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2017/04/14 17:32:59 UTC

groovy git commit: Add syntax check for the spread operator

Repository: groovy
Updated Branches:
  refs/heads/master a7f6c78ff -> e06a1ea99


Add syntax check for the spread operator


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/e06a1ea9
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/e06a1ea9
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/e06a1ea9

Branch: refs/heads/master
Commit: e06a1ea99ed5db05b7b376b5b52c9e23edf05434
Parents: a7f6c78
Author: sunlan <su...@apache.org>
Authored: Sat Apr 15 01:32:47 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Apr 15 01:32:47 2017 +0800

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/GroovyParser.g4 | 22 +++++++++++---------
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  5 +++++
 .../src/test/resources/fail/For_01.groovy       |  1 +
 .../src/test/resources/fail/For_02.groovy       |  1 +
 4 files changed, 19 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/e06a1ea9/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4 b/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
index ab1241d..daeb7b3 100644
--- a/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
+++ b/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
@@ -755,11 +755,11 @@ forControl
 
 forInit
     :   localVariableDeclaration
-    |   expressionList
+    |   expressionList[false]
     ;
 
 forUpdate
-    :   expressionList
+    :   expressionList[false]
     ;
 
 enhancedForControl
@@ -777,12 +777,14 @@ parExpression
     :   LPAREN (statementExpression | standardLambda) rparen
     ;
 
-expressionList
-    :   expressionListElement (COMMA expressionListElement)*
+expressionList[boolean canSpread]
+    :   expressionListElement[$canSpread] (COMMA expressionListElement[$canSpread])*
     ;
 
-expressionListElement
-    :   MUL? expression
+expressionListElement[boolean canSpread]
+    :   (   MUL { $canSpread }?<fail={"spread is not allowed here"}>
+        |
+        ) expression
     ;
 
 /**
@@ -1028,7 +1030,7 @@ dynamicMemberName
  *  The brackets may also be empty, as in T[].  This is how Groovy names array types.
  */
 indexPropertyArgs
-    :   QUESTION? LBRACK expressionList? RBRACK
+    :   QUESTION? LBRACK expressionList[true]? RBRACK
     ;
 
 namedPropertyArgs
@@ -1054,7 +1056,7 @@ list
 locals[boolean empty = true]
     :   LBRACK
         (
-            expressionList
+            expressionList[true]
             { $empty = false; }
         )?
         (
@@ -1144,12 +1146,12 @@ enhancedArgumentList
 
 argumentListElement
 options { baseContext = enhancedArgumentListElement; }
-    :   expressionListElement
+    :   expressionListElement[true]
     |   mapEntry
     ;
 
 enhancedArgumentListElement
-    :   expressionListElement
+    :   expressionListElement[true]
     |   standardLambda
     |   mapEntry
     ;

http://git-wip-us.apache.org/repos/asf/groovy/blob/e06a1ea9/subprojects/groovy-parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy b/subprojects/groovy-parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
index c6c0b62..5b7fec5 100644
--- a/subprojects/groovy-parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/subprojects/groovy-parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -129,6 +129,11 @@ class SyntaxErrorTest extends GroovyTestCase {
         TestUtils.doRunAndShouldFail('fail/DoWhile_01x.groovy');
     }
 
+    void "test groovy core - For"() {
+        TestUtils.shouldFail('fail/For_01.groovy');
+        TestUtils.shouldFail('fail/For_02.groovy');
+    }
+
     void "test groovy core - Modifier"() {
         TestUtils.doRunAndShouldFail('fail/Modifier_01x.groovy');
         TestUtils.doRunAndShouldFail('fail/Modifier_02x.groovy');

http://git-wip-us.apache.org/repos/asf/groovy/blob/e06a1ea9/subprojects/groovy-parser-antlr4/src/test/resources/fail/For_01.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-parser-antlr4/src/test/resources/fail/For_01.groovy b/subprojects/groovy-parser-antlr4/src/test/resources/fail/For_01.groovy
new file mode 100644
index 0000000..92ff34f
--- /dev/null
+++ b/subprojects/groovy-parser-antlr4/src/test/resources/fail/For_01.groovy
@@ -0,0 +1 @@
+for (*a; a.size() < 10;) {}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/groovy/blob/e06a1ea9/subprojects/groovy-parser-antlr4/src/test/resources/fail/For_02.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-parser-antlr4/src/test/resources/fail/For_02.groovy b/subprojects/groovy-parser-antlr4/src/test/resources/fail/For_02.groovy
new file mode 100644
index 0000000..ce63119
--- /dev/null
+++ b/subprojects/groovy-parser-antlr4/src/test/resources/fail/For_02.groovy
@@ -0,0 +1 @@
+for (; a.size() < 10; *a) {}
\ No newline at end of file