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/04/17 14:54:10 UTC

[groovy] 01/01: GROOVY-9507: JSP style loop in StreamingTemplateEngine template results in TemplateParseException

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

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

commit 776b7407bdd18425ec2acadae2a9363816d49243
Author: Daniel Sun <su...@apache.org>
AuthorDate: Fri Apr 17 22:53:42 2020 +0800

    GROOVY-9507: JSP style loop in StreamingTemplateEngine template results in TemplateParseException
---
 src/antlr/GroovyParser.g4                          |  2 +-
 .../src/spec/test/TemplateEnginesTest.groovy       | 26 +++++++++++++++++++++-
 .../groovy/parser/antlr4/GroovyParserTest.groovy   |  4 ++++
 .../src/test/resources/bugs/BUG-GROOVY-9507.groovy | 20 +++++++++++++++++
 4 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index 39c4186..29f347c 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -485,7 +485,7 @@ lambdaBody
 
 // CLOSURE
 closure
-    :   LBRACE nls (formalParameterList? nls ARROW nls)? blockStatementsOpt RBRACE
+    :   LBRACE (nls (formalParameterList nls)? ARROW)? sep? blockStatementsOpt RBRACE
     ;
 
 // GROOVY-8991: Difference in behaviour with closure and lambda
diff --git a/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy b/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy
index 63fb256..6761a8b 100644
--- a/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy
+++ b/subprojects/groovy-templates/src/spec/test/TemplateEnginesTest.groovy
@@ -17,6 +17,9 @@
  *  under the License.
  */
 import gls.CompilableTestSupport
+import groovy.text.StreamingTemplateEngine
+import groovy.text.Template
+import groovy.text.TemplateEngine
 
 class TemplateEnginesTest extends CompilableTestSupport {
 
@@ -192,4 +195,25 @@ The conference committee.'''
 </document>
 '''
     }
-}
\ No newline at end of file
+
+    void testStreamingTemplateEngine_GROOVY9507() {
+        TemplateEngine engine = new StreamingTemplateEngine()
+        Template template = engine.createTemplate('''
+<ul>
+    <% items.each { %>
+    <li>${it}</li>
+    <% } %>
+</ul>
+''')
+
+        def result = template.make([items : [1,2,3,4]]).toString()
+        assert result == '''
+<ul>
+    <li>1</li>
+    <li>2</li>
+    <li>3</li>
+    <li>4</li>
+</ul>
+'''
+    }
+}
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 e81633c..cf841e5 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
@@ -458,4 +458,8 @@ final class GroovyParserTest extends GroovyTestCase {
     void "test groovy core - GROOVY-9511"() {
         doTest('bugs/BUG-GROOVY-9511.groovy', [MethodNode]);
     }
+
+    void "test groovy core - GROOVY-9507"() {
+        doTest('bugs/BUG-GROOVY-9507.groovy');
+    }
 }
diff --git a/subprojects/parser-antlr4/src/test/resources/bugs/BUG-GROOVY-9507.groovy b/subprojects/parser-antlr4/src/test/resources/bugs/BUG-GROOVY-9507.groovy
new file mode 100644
index 0000000..5255d4e
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/bugs/BUG-GROOVY-9507.groovy
@@ -0,0 +1,20 @@
+/*
+ *  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.
+ */
+
+package groovy.tmp.templates;def getTemplate() { return { _p, _s, _b, out -> int _i = 0;try {delegate = new Binding(_b);out<<_s[_i=0];           items.each { ;out<<_s[_i=1];out<<"""${it}""";out<<_s[_i=2];           } ;out<<_s[_i=3];} catch (Throwable e) { _p.error(_i, _s, e);}}.asWritable()}
\ No newline at end of file