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)
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())
}
}
}
const (
leftDelim = "{{"
rightDelim = "}}"
- leftComment = "{{/*"
- rightComment = "*/}}"
+ leftComment = "/*"
+ rightComment = "*/"
)
// lexText scans until an opening action delimiter, "{{".
// 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)
// 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
}
}
}
-// 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,