Commit 8b230662 authored by Rob Pike's avatar Rob Pike

text/template: catch (A).X as a parse error

This shouldn't be an error (see issue 3999), but until it's handled
correctly, treat it as one to avoid confusion. Without this CL,
(A).X parses as two arguments.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6473059
parent cc842c73
...@@ -354,6 +354,12 @@ func lexInsideAction(l *lexer) stateFn { ...@@ -354,6 +354,12 @@ func lexInsideAction(l *lexer) stateFn {
if l.parenDepth < 0 { if l.parenDepth < 0 {
return l.errorf("unexpected right paren %#U", r) return l.errorf("unexpected right paren %#U", r)
} }
// Catch the mistake of (a).X, which will parse as two args.
// See issue 3999. TODO: Remove once arg parsing is
// better defined.
if l.peek() == '.' {
return l.errorf("cannot evaluate field of parenthesized expression")
}
return lexInsideAction return lexInsideAction
case r <= unicode.MaxASCII && unicode.IsPrint(r): case r <= unicode.MaxASCII && unicode.IsPrint(r):
l.emit(itemChar) l.emit(itemChar)
......
...@@ -232,6 +232,9 @@ var parseTests = []parseTest{ ...@@ -232,6 +232,9 @@ var parseTests = []parseTest{
{"invalid punctuation", "{{printf 3, 4}}", hasError, ""}, {"invalid punctuation", "{{printf 3, 4}}", hasError, ""},
{"multidecl outside range", "{{with $v, $u := 3}}{{end}}", hasError, ""}, {"multidecl outside range", "{{with $v, $u := 3}}{{end}}", hasError, ""},
{"too many decls in range", "{{range $u, $v, $w := 3}}{{end}}", hasError, ""}, {"too many decls in range", "{{range $u, $v, $w := 3}}{{end}}", hasError, ""},
// This one should work but doesn't. Caught as a parse error to avoid confusion.
// TODO: Update after issue 3999 is resolved.
{"dot applied to parentheses", "{{printf (printf .).}}", hasError, ""},
// Equals (and other chars) do not assignments make (yet). // Equals (and other chars) do not assignments make (yet).
{"bug0a", "{{$x := 0}}{{$x}}", noError, "{{$x := 0}}{{$x}}"}, {"bug0a", "{{$x := 0}}{{$x}}", noError, "{{$x := 0}}{{$x}}"},
{"bug0b", "{{$x = 1}}{{$x}}", hasError, ""}, {"bug0b", "{{$x = 1}}{{$x}}", hasError, ""},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment