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 2021/12/23 14:57:32 UTC

[groovy] 01/01: GROOVY-10406: Dollar slashy string is too greedy

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

sunlan pushed a commit to branch GROOVY-10406
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 8f5cdb203f06cbcae87f6576f06b36847f6658c7
Author: Daniel Sun <su...@apache.org>
AuthorDate: Thu Dec 23 22:56:17 2021 +0800

    GROOVY-10406: Dollar slashy string is too greedy
---
 src/antlr/GroovyLexer.g4                           | 12 +++++++++++-
 src/test-resources/bugs/BUG-GROOVY-10406.groovy    | 22 ++++++++++++++++++++++
 .../groovy/parser/antlr4/GroovyParserTest.groovy   |  4 ++++
 3 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/src/antlr/GroovyLexer.g4 b/src/antlr/GroovyLexer.g4
index b66afc8..5e1965f 100644
--- a/src/antlr/GroovyLexer.g4
+++ b/src/antlr/GroovyLexer.g4
@@ -382,7 +382,9 @@ fragment SlashyStringCharacter
 
 // character in the dollar slashy string. e.g. $/a/$
 fragment DollarSlashyStringCharacter
-    :   DollarSlashEscape | DollarDollarEscape
+    :   DollarDollarEscape
+    |   DollarSlashDollarEscape { _input.LA(-4) != '$' }?
+    |   DollarSlashEscape { _input.LA(1) != '$' }?
     |   Slash { _input.LA(1) != '$' }?
     |   Dollar { !isFollowedByJavaLetterInGString(_input) }?
     |   ~[/$\u0000]
@@ -810,16 +812,24 @@ DollarSlashyGStringQuotationMarkEnd
     :   '/$'
     ;
 
+// escaped forward slash
 fragment
 DollarSlashEscape
     :   '$/'
     ;
 
+// escaped dollar sign
 fragment
 DollarDollarEscape
     :   '$$'
     ;
 
+// escaped dollar slashy string delimiter
+fragment
+DollarSlashDollarEscape
+    :   '$/$'
+    ;
+
 // ยง3.10.7 The Null Literal
 NullLiteral
     :   'null'
diff --git a/src/test-resources/bugs/BUG-GROOVY-10406.groovy b/src/test-resources/bugs/BUG-GROOVY-10406.groovy
new file mode 100644
index 0000000..71c2671
--- /dev/null
+++ b/src/test-resources/bugs/BUG-GROOVY-10406.groovy
@@ -0,0 +1,22 @@
+/*
+ *  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 ($/$//$ + 'x' + /$$/) == '/x$$'
+assert ($/$$ /$ + 'x' + /$$/) == '$ x$$'
+assert ($/$$/$ + 'x' + /$$/) == '$x$$'
diff --git a/src/test/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy b/src/test/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
index 82e0c42..0919c4f 100644
--- a/src/test/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
+++ b/src/test/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
@@ -525,4 +525,8 @@ final class GroovyParserTest extends GroovyTestCase {
     void "test groovy core - GROOVY-10181"() {
         doTest('bugs/BUG-GROOVY-10181.groovy');
     }
+
+    void "test groovy core - GROOVY-10406"() {
+        doTest('bugs/BUG-GROOVY-10406.groovy');
+    }
 }