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 2020/03/28 09:38:56 UTC

[groovy] 01/01: GROOVY-9310: Groovy does not ignore second bash directive

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch pull/1203
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit c1d9129363343bff7a2e9e465d8769a3782cf7f5
Author: John Mercier <mo...@gmail.com>
AuthorDate: Sat Mar 28 17:38:30 2020 +0800

    GROOVY-9310: Groovy does not ignore second bash directive
---
 src/antlr/GroovyLexer.g4                           |  7 ++++++-
 .../groovy/parser/antlr4/GroovyParserTest.groovy   |  4 ++++
 .../src/test/resources/core/Comments_03x.groovy    | 23 ++++++++++++++++++++++
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/src/antlr/GroovyLexer.g4 b/src/antlr/GroovyLexer.g4
index 9348ef4..4c3b76d 100644
--- a/src/antlr/GroovyLexer.g4
+++ b/src/antlr/GroovyLexer.g4
@@ -918,6 +918,11 @@ JavaLetterOrDigitInGString
     :   JavaLetterOrDigit  { _input.LA(-1) != '$' }?
     ;
 
+fragment
+ShCommand
+    :   ~[\r\n\uFFFF]*
+    ;
+
 //
 // Additional symbols not defined in the lexical specification
 //
@@ -948,7 +953,7 @@ SL_COMMENT
 // Script-header comments.
 // The very first characters of the file may be "#!".  If so, ignore the first line.
 SH_COMMENT
-    :   '#!' { require(0 == this.tokenIndex, "Shebang comment should appear at the first line", -2, true); } ~[\r\n\uFFFF]* -> skip
+    :   '#!' { require(0 == this.tokenIndex, "Shebang comment should appear at the first line", -2, true); } ShCommand (LineTerminator '#!' ShCommand)* -> skip
     ;
 
 // Unexpected characters will be handled by groovy parser later.
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 a32b079..aefac72 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
@@ -44,6 +44,10 @@ final class GroovyParserTest extends GroovyTestCase {
         doTestAttachedComments()
     }
 
+    void "test groovy core - Shebang Comments"() {
+        doRunAndTestAntlr4('core/Comments_03x.groovy')
+    }
+
     // java.lang.ClassFormatError: Illegal method name "test IO stream/reader closed by the parser properly" when using Java9
     void "test IO reader closed by the parser properly"() {
         def f = File.createTempFile("Script${System.nanoTime()}", ".groovy")
diff --git a/subprojects/parser-antlr4/src/test/resources/core/Comments_03x.groovy b/subprojects/parser-antlr4/src/test/resources/core/Comments_03x.groovy
new file mode 100644
index 0000000..18d2b03
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/core/Comments_03x.groovy
@@ -0,0 +1,23 @@
+#! /usr/bin/env nix-shell
+#! nix-shell -i groovy -p groovy
+#! other directives
+/*
+ *  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.
+ */
+
+assert true