]> Cypherpunks repositories - gostls13.git/commitdiff
template: fix comments with different delimiters.
authorRob Pike <r@golang.org>
Thu, 6 Oct 2011 22:21:56 +0000 (15:21 -0700)
committerRob Pike <r@golang.org>
Thu, 6 Oct 2011 22:21:56 +0000 (15:21 -0700)
R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/5208042

src/pkg/template/exec_test.go
src/pkg/template/parse/lex.go
src/pkg/template/parse/lex_test.go

index 57c63257c4317297249ae3be7ebd3b0c9f74e5b9..46b89fd294e9d3c4918fc7f9e990ec9c750b930f 100644 (file)
@@ -507,14 +507,21 @@ func TestDelims(t *testing.T) {
        for i := 0; i < len(delimPairs); i += 2 {
                text := ".Str"
                left := delimPairs[i+0]
+               trueLeft := left
                right := delimPairs[i+1]
+               trueRight := right
                if left == "" { // default case
-                       text = "{{" + text
+                       trueLeft = "{{"
                }
                if right == "" { // default case
-                       text = text + "}}"
+                       trueRight = "}}"
                }
-               text = left + text + right
+               text = trueLeft + text + trueRight
+               // Now add a comment
+               text += trueLeft + "/*comment*/" + trueRight
+               // Now add  an action containing a string.
+               text += trueLeft + `"` + trueLeft + `"` + trueRight
+               // At this point text looks like `{{.Str}}{{/*comment*/}}{{"{{"}}`.
                tmpl, err := New("delims").Delims(left, right).Parse(text)
                if err != nil {
                        t.Fatalf("delim %q text %q parse err %s", left, text, err)
@@ -524,8 +531,8 @@ func TestDelims(t *testing.T) {
                if err != nil {
                        t.Fatalf("delim %q exec err %s", left, err)
                }
-               if b.String() != hello {
-                       t.Error("expected %q got %q", hello, b.String())
+               if b.String() != hello+trueLeft {
+                       t.Error("expected %q got %q", hello+trueLeft, b.String())
                }
        }
 }
index 07740d79117403e8caf85d72515daf15ca6fbdf0..16ff590d3b4da4f4efd7a7f683260a162326b964 100644 (file)
@@ -230,8 +230,8 @@ func lex(name, input, left, right string) *lexer {
 const (
        leftDelim    = "{{"
        rightDelim   = "}}"
-       leftComment  = "{{/*"
-       rightComment = "*/}}"
+       leftComment  = "/*"
+       rightComment = "*/"
 )
 
 // lexText scans until an opening action delimiter, "{{".
@@ -257,7 +257,7 @@ func lexText(l *lexer) stateFn {
 
 // lexLeftDelim scans the left delimiter, which is known to be present.
 func lexLeftDelim(l *lexer) stateFn {
-       if strings.HasPrefix(l.input[l.pos:], leftComment) {
+       if strings.HasPrefix(l.input[l.pos:], l.leftDelim+leftComment) {
                return lexComment
        }
        l.pos += len(l.leftDelim)
@@ -267,11 +267,11 @@ func lexLeftDelim(l *lexer) stateFn {
 
 // lexComment scans a comment. The left comment marker is known to be present.
 func lexComment(l *lexer) stateFn {
-       i := strings.Index(l.input[l.pos:], rightComment)
+       i := strings.Index(l.input[l.pos:], rightComment+l.rightDelim)
        if i < 0 {
                return l.errorf("unclosed comment")
        }
-       l.pos += i + len(rightComment)
+       l.pos += i + len(rightComment) + len(l.rightDelim)
        l.ignore()
        return lexText
 }
index f2569b1576478ce54eb9109bd85bafa95a2bc06d..6ee1b47010284925ac11b7405392d0fc82e3cbf9 100644 (file)
@@ -222,7 +222,7 @@ func TestLex(t *testing.T) {
        }
 }
 
-// Some easy cases from above, but with delimiters are $$ and @@
+// Some easy cases from above, but with delimiters $$ and @@
 var lexDelimTests = []lexTest{
        {"punctuation", "$$,@%{{}}@@", []item{
                tLeftDelim,