]> Cypherpunks repositories - gostls13.git/commitdiff
text/template/parse: nicer error when comment ends before final delimiter
authorRob Pike <r@golang.org>
Fri, 9 Aug 2013 02:57:21 +0000 (12:57 +1000)
committerRob Pike <r@golang.org>
Fri, 9 Aug 2013 02:57:21 +0000 (12:57 +1000)
By separating finding the end of the comment from the end of the action,
we can diagnose malformed comments better.
Also tweak the documentation to make the comment syntax clearer.

Fixes #6022.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/12570044

src/pkg/text/template/doc.go
src/pkg/text/template/parse/lex.go
src/pkg/text/template/parse/lex_test.go

index 2da339ce8351d9b5a8be237b0b699bbf8a250d5c..c9121f74d3577728111ebd390913fa1fcb4dd356 100644 (file)
@@ -44,7 +44,8 @@ data, defined in detail below.
 */
 //     {{/* a comment */}}
 //             A comment; discarded. May contain newlines.
-//             Comments do not nest.
+//             Comments do not nest and must start and end at the
+//             delimiters, as shown here.
 /*
 
        {{pipeline}}
index 23c0cf0793c7d367bcc3f4758aa39b334633fecf..1674aaf9cd4bb63a42c0bee5aa833875bc5ec81a 100644 (file)
@@ -243,11 +243,16 @@ func lexLeftDelim(l *lexer) stateFn {
 // lexComment scans a comment. The left comment marker is known to be present.
 func lexComment(l *lexer) stateFn {
        l.pos += Pos(len(leftComment))
-       i := strings.Index(l.input[l.pos:], rightComment+l.rightDelim)
+       i := strings.Index(l.input[l.pos:], rightComment)
        if i < 0 {
                return l.errorf("unclosed comment")
        }
-       l.pos += Pos(i + len(rightComment) + len(l.rightDelim))
+       l.pos += Pos(i + len(rightComment))
+       if !strings.HasPrefix(l.input[l.pos:], l.rightDelim) {
+               return l.errorf("comment ends before closing delimiter")
+
+       }
+       l.pos += Pos(len(l.rightDelim))
        l.ignore()
        return lexText
 }
index d2264c991c989088e9a5afdb1f7fbbb150eb4d4f..ae90ae407b1e99fd5612cdb7b7f46b2f05fddfda 100644 (file)
@@ -336,6 +336,10 @@ var lexTests = []lexTest{
                {itemText, 0, "hello-"},
                {itemError, 0, `unclosed comment`},
        }},
+       {"text with comment close separted from delim", "hello-{{/* */ }}-world", []item{
+               {itemText, 0, "hello-"},
+               {itemError, 0, `comment ends before closing delimiter`},
+       }},
 }
 
 // collect gathers the emitted items into a slice.