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/12/31 05:42:09 UTC

groovy git commit: GROOVY-8426: Fix line and column numbers assigned to BlockStatements generated out of method definitions(closes #649, closes #650, closes #651)

Repository: groovy
Updated Branches:
  refs/heads/master 7050ae43f -> 0c34922e0


GROOVY-8426: Fix line and column numbers assigned to BlockStatements generated out of method definitions(closes #649, closes #650, closes #651)


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

Branch: refs/heads/master
Commit: 0c34922e074e8780e771159a589b146b83024163
Parents: 7050ae4
Author: Marcin Erdmann <er...@gmail.com>
Authored: Thu Dec 28 11:19:56 2017 +0000
Committer: sunlan <su...@apache.org>
Committed: Sun Dec 31 13:41:37 2017 +0800

----------------------------------------------------------------------
 .../groovy/antlr/AntlrParserPlugin.java         | 13 ++++--
 .../groovy/antlr/AntlrParserPluginTest.groovy   | 20 +++++++++
 .../parser/antlr4/GroovyParserTest.groovy       |  3 ++
 .../groovy/parser/antlr4/TestUtils.groovy       | 12 +++++
 .../test/resources/bugs/BUG-GROOVY-8426.groovy  | 46 ++++++++++++++++++++
 5 files changed, 91 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/0c34922e/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java b/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
index 37a105f..2af9882 100644
--- a/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
+++ b/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
@@ -1414,18 +1414,25 @@ public class AntlrParserPlugin extends ASTHelper implements ParserPlugin, Groovy
     }
 
     protected Statement statementList(AST code) {
-        return statementListNoChild(code.getFirstChild(), code);
+        BlockStatement block = siblingsToBlockStatement(code.getFirstChild());
+        configureAST(block, code);
+        return block;
     }
 
     protected Statement statementListNoChild(AST node, AST alternativeConfigureNode) {
-        BlockStatement block = new BlockStatement();
+        BlockStatement block = siblingsToBlockStatement(node);
         // alternativeConfigureNode is used only to set the source position
         if (node != null) {
             configureAST(block, node);
         } else {
             configureAST(block, alternativeConfigureNode);
         }
-        for (; node != null; node = node.getNextSibling()) {
+        return block;
+    }
+
+    private BlockStatement siblingsToBlockStatement(AST firstSiblingNode) {
+        BlockStatement block = new BlockStatement();
+        for (AST node = firstSiblingNode; node != null; node = node.getNextSibling()) {
             block.addStatement(statement(node));
         }
         return block;

http://git-wip-us.apache.org/repos/asf/groovy/blob/0c34922e/src/test/org/codehaus/groovy/antlr/AntlrParserPluginTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/antlr/AntlrParserPluginTest.groovy b/src/test/org/codehaus/groovy/antlr/AntlrParserPluginTest.groovy
index 2ecc9a7..98bd59d 100644
--- a/src/test/org/codehaus/groovy/antlr/AntlrParserPluginTest.groovy
+++ b/src/test/org/codehaus/groovy/antlr/AntlrParserPluginTest.groovy
@@ -19,6 +19,7 @@
 package org.codehaus.groovy.antlr
 
 import org.codehaus.groovy.ast.builder.AstBuilder
+import org.codehaus.groovy.ast.stmt.BlockStatement
 import org.codehaus.groovy.ast.stmt.Statement
 import org.codehaus.groovy.ast.*
 import static org.codehaus.groovy.control.CompilePhase.CONVERSION
@@ -76,4 +77,23 @@ class AntlrParserPluginTest extends GroovyTestCase {
         assert statement.lastColumnNumber == 34
         assert statement.statementLabel == 'label'
     }
+
+    void testMethodBlockStatement() {
+        def result = new AstBuilder().buildFromString CONVERSION, false, '''
+            def method() {
+                'return value'
+                
+            }
+        '''
+
+        ClassNode classNode = result[1]
+        MethodNode method = classNode.getMethods('method')[0]
+        BlockStatement statement = method.code
+
+        assert statement.lineNumber == 2
+        assert statement.lastLineNumber == 5
+        assert statement.columnNumber == 26
+        assert statement.lastColumnNumber == 14
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/0c34922e/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
index 053ab1a..08723b1 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
@@ -29,6 +29,7 @@ import org.codehaus.groovy.syntax.Token
 
 import static org.apache.groovy.parser.antlr4.TestUtils.doTest
 import static org.apache.groovy.parser.antlr4.TestUtils.doRunAndTest
+import static org.apache.groovy.parser.antlr4.TestUtils.doRunAndTestWithAntlr2
 
 /**
  * Some basic test cases for the new parser
@@ -381,5 +382,7 @@ class GroovyParserTest extends GroovyTestCase {
         doRunAndTest('bugs/GROOVY-3898.groovy')
         doRunAndTest('bugs/BUG-GROOVY-8311.groovy')
         doRunAndTest('bugs/GROOVY-8228.groovy')
+
+        doRunAndTestWithAntlr2('bugs/BUG-GROOVY-8426.groovy')
     }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/0c34922e/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/TestUtils.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/TestUtils.groovy b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/TestUtils.groovy
index 02db2ad..19b0fd6 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/TestUtils.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/TestUtils.groovy
@@ -24,6 +24,7 @@ import org.apache.groovy.parser.Antlr2Parser
 import org.apache.groovy.parser.Antlr4Parser
 import org.apache.groovy.parser.antlr4.util.ASTComparatorCategory
 import org.apache.groovy.parser.antlr4.util.AstDumper
+import org.codehaus.groovy.antlr.AntlrParserPluginFactory
 import org.codehaus.groovy.ast.*
 import org.codehaus.groovy.ast.stmt.*
 import org.codehaus.groovy.control.CompilerConfiguration
@@ -226,6 +227,10 @@ class TestUtils {
         assert executeScript(path);
     }
 
+    static doRunAndTestWithAntlr2(String path) {
+        assert executeScript(createAntlr2Shell(), "$RESOURCES_PATH/$path")
+    }
+
     static executeScript(String path) {
         executeScript(createAntlr4Shell(), "$RESOURCES_PATH/$path")
     }
@@ -251,5 +256,12 @@ class TestUtils {
         return new GroovyShell(configuration);
     }
 
+    static createAntlr2Shell() {
+        CompilerConfiguration configuration = new CompilerConfiguration(CompilerConfiguration.DEFAULT)
+        configuration.pluginFactory = new AntlrParserPluginFactory()
+
+        return new GroovyShell(configuration);
+    }
+
     public static final List COMMON_IGNORE_CLASS_LIST = Collections.unmodifiableList([AssertStatement, BreakStatement, ConstructorNode, ContinueStatement, ExpressionStatement, FieldNode, ForStatement, GenericsType, IfStatement, MethodNode, PackageNode, Parameter, PropertyNode, ReturnStatement, ThrowStatement, Token, WhileStatement]);
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/0c34922e/subprojects/parser-antlr4/src/test/resources/bugs/BUG-GROOVY-8426.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/bugs/BUG-GROOVY-8426.groovy b/subprojects/parser-antlr4/src/test/resources/bugs/BUG-GROOVY-8426.groovy
new file mode 100644
index 0000000..59404a4
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/bugs/BUG-GROOVY-8426.groovy
@@ -0,0 +1,46 @@
+import org.codehaus.groovy.ast.ClassNode
+import org.codehaus.groovy.ast.MethodNode
+import org.codehaus.groovy.ast.builder.AstBuilder
+import org.codehaus.groovy.ast.stmt.BlockStatement
+import static org.codehaus.groovy.control.CompilePhase.CONVERSION
+
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+ class Groovy8426Test {
+    void testMethodBlockStatement() {
+        def result = new AstBuilder().buildFromString CONVERSION, false, '''
+            def method() {
+                'return value'
+                
+            }
+        '''
+
+        ClassNode classNode = result[1]
+        MethodNode method = classNode.getMethods('method')[0]
+        BlockStatement statement = method.code
+
+        assert statement.lineNumber == 2
+        assert statement.lastLineNumber == 5
+        assert statement.columnNumber == 26
+        assert statement.lastColumnNumber == 14
+    }
+}
+
+new Groovy8426Test().testMethodBlockStatement()