diff options
| author | Rob Pike <r@golang.org> | 2012-09-14 15:25:37 -0700 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2012-09-14 15:25:37 -0700 |
| commit | 7b7a7a573789c3dd49fc4c1f6e76920a2fd9485e (patch) | |
| tree | 7220c4ab44410e0f7ede0995eae35d84ec5d2020 /src/pkg/text/template/parse/parse.go | |
| parent | 5c5c2c8112f774b118b9251eb15c2df529ad454c (diff) | |
| download | go-7b7a7a573789c3dd49fc4c1f6e76920a2fd9485e.tar.xz | |
text/template: towards better errors
Give the right name for errors, and add a test to check we're
getting the errors we expect.
Also fix an ordering bug (calling add after stopParse) that
caused a nil indirection rather than a helpful error.
Fixes #3280.
R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/6520043
Diffstat (limited to 'src/pkg/text/template/parse/parse.go')
| -rw-r--r-- | src/pkg/text/template/parse/parse.go | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/src/pkg/text/template/parse/parse.go b/src/pkg/text/template/parse/parse.go index 6177e32e73..c52e41d166 100644 --- a/src/pkg/text/template/parse/parse.go +++ b/src/pkg/text/template/parse/parse.go @@ -18,8 +18,9 @@ import ( // Tree is the representation of a single parsed template. type Tree struct { - Name string // name of the template represented by the tree. - Root *ListNode // top-level root of the tree. + Name string // name of the template represented by the tree. + ParseName string // name of the top-level template during parsing, for error messages. + Root *ListNode // top-level root of the tree. // Parsing only; cleared after parse. funcs []map[string]interface{} lex *lexer @@ -114,7 +115,7 @@ func New(name string, funcs ...map[string]interface{}) *Tree { // errorf formats the error and terminates processing. func (t *Tree) errorf(format string, args ...interface{}) { t.Root = nil - format = fmt.Sprintf("template: %s:%d: %s", t.Name, t.lex.lineNumber(), format) + format = fmt.Sprintf("template: %s:%d: %s", t.ParseName, t.lex.lineNumber(), format) panic(fmt.Errorf(format, args...)) } @@ -203,6 +204,7 @@ func (t *Tree) atEOF() bool { // the treeSet map. func (t *Tree) Parse(s, leftDelim, rightDelim string, treeSet map[string]*Tree, funcs ...map[string]interface{}) (tree *Tree, err error) { defer t.recover(&err) + t.ParseName = t.Name t.startParse(funcs, lex(t.Name, s, leftDelim, rightDelim)) t.parse(treeSet) t.add(treeSet) @@ -257,6 +259,7 @@ func (t *Tree) parse(treeSet map[string]*Tree) (next Node) { delim := t.next() if t.nextNonSpace().typ == itemDefine { newT := New("definition") // name will be updated once we know it. + newT.ParseName = t.ParseName newT.startParse(t.funcs, t.lex) newT.parseDefinition(treeSet) continue @@ -289,8 +292,8 @@ func (t *Tree) parseDefinition(treeSet map[string]*Tree) { if end.Type() != nodeEnd { t.errorf("unexpected %s in %s", end, context) } - t.stopParse() t.add(treeSet) + t.stopParse() } // itemList: |
