Commit 10e012c8 authored by Rob Pike's avatar Rob Pike

template/parse: rename Set to Parse

Preamble to the simplification of the template API.
Although the signature of Parse (nee Set) changes,
it's really an internal function, used only by
text/template.

R=golang-dev, rsc, gri, r
CC=golang-dev
https://golang.org/cl/5415052
parent 5cad8611
...@@ -9,6 +9,5 @@ GOFILES=\ ...@@ -9,6 +9,5 @@ GOFILES=\
lex.go\ lex.go\
node.go\ node.go\
parse.go\ parse.go\
set.go\
include ../../../../Make.pkg include ../../../../Make.pkg
...@@ -13,10 +13,10 @@ import ( ...@@ -13,10 +13,10 @@ import (
"unicode" "unicode"
) )
// Tree is the representation of a parsed template. // Tree is the representation of a single parsed template.
type Tree struct { type Tree struct {
Name string // Name is the name of the template. Name string // name of the template represented by the tree.
Root *ListNode // Root is the top-level root of the parse tree. Root *ListNode // top-level root of the tree.
// Parsing only; cleared after parse. // Parsing only; cleared after parse.
funcs []map[string]interface{} funcs []map[string]interface{}
lex *lexer lex *lexer
...@@ -25,6 +25,16 @@ type Tree struct { ...@@ -25,6 +25,16 @@ type Tree struct {
vars []string // variables defined at the moment. vars []string // variables defined at the moment.
} }
// Parse returns a map from template name to parse.Tree, created by parsing the
// templates described in the argument string. The top-level template will be
// given the specified name. If an error is encountered, parsing stops and an
// empty map is returned with the error.
func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]interface{}) (treeSet map[string]*Tree, err error) {
treeSet = make(map[string]*Tree)
_, err = New(name).Parse(text, leftDelim, rightDelim, treeSet, funcs...)
return
}
// next returns the next token. // next returns the next token.
func (t *Tree) next() item { func (t *Tree) next() item {
if t.peekCount > 0 { if t.peekCount > 0 {
...@@ -58,7 +68,7 @@ func (t *Tree) peek() item { ...@@ -58,7 +68,7 @@ func (t *Tree) peek() item {
// Parsing. // Parsing.
// New allocates a new template with the given name. // New allocates a new parse tree with the given name.
func New(name string, funcs ...map[string]interface{}) *Tree { func New(name string, funcs ...map[string]interface{}) *Tree {
return &Tree{ return &Tree{
Name: name, Name: name,
...@@ -107,7 +117,7 @@ func (t *Tree) recover(errp *error) { ...@@ -107,7 +117,7 @@ func (t *Tree) recover(errp *error) {
return return
} }
// startParse starts the template parsing from the lexer. // startParse initializes the parser, using the lexer.
func (t *Tree) startParse(funcs []map[string]interface{}, lex *lexer) { func (t *Tree) startParse(funcs []map[string]interface{}, lex *lexer) {
t.Root = nil t.Root = nil
t.lex = lex t.lex = lex
...@@ -143,9 +153,10 @@ func (t *Tree) atEOF() bool { ...@@ -143,9 +153,10 @@ func (t *Tree) atEOF() bool {
return false return false
} }
// Parse parses the template definition string to construct an internal // Parse parses the template definition string to construct a representation of
// representation of the template for execution. If either action delimiter // the template for execution. If either action delimiter string is empty, the
// string is empty, the default ("{{" or "}}") is used. // default ("{{" or "}}") is used. Embedded template definitions are added to
// the treeSet map.
func (t *Tree) Parse(s, leftDelim, rightDelim string, treeSet map[string]*Tree, funcs ...map[string]interface{}) (tree *Tree, err error) { func (t *Tree) Parse(s, leftDelim, rightDelim string, treeSet map[string]*Tree, funcs ...map[string]interface{}) (tree *Tree, err error) {
defer t.recover(&err) defer t.recover(&err)
t.startParse(funcs, lex(t.Name, s, leftDelim, rightDelim)) t.startParse(funcs, lex(t.Name, s, leftDelim, rightDelim))
......
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package parse
// Set returns a slice of Trees created by parsing the template set
// definition in the argument string. If an error is encountered,
// parsing stops and an empty slice is returned with the error.
func Set(text, leftDelim, rightDelim string, funcs ...map[string]interface{}) (tree map[string]*Tree, err error) {
tree = make(map[string]*Tree)
// Top-level template name is needed but unused. TODO: clean this up.
_, err = New("ROOT").Parse(text, leftDelim, rightDelim, tree, funcs...)
return
}
...@@ -104,7 +104,8 @@ func (s *Set) Execute(wr io.Writer, name string, data interface{}) error { ...@@ -104,7 +104,8 @@ func (s *Set) Execute(wr io.Writer, name string, data interface{}) error {
// multiple times for a given set, adding the templates defined in the string // multiple times for a given set, adding the templates defined in the string
// to the set. It is an error if a template has a name already defined in the set. // to the set. It is an error if a template has a name already defined in the set.
func (s *Set) Parse(text string) (*Set, error) { func (s *Set) Parse(text string) (*Set, error) {
trees, err := parse.Set(text, s.leftDelim, s.rightDelim, s.parseFuncs, builtins) // TODO: "ROOT" is just a placeholder while we rejig the API.
trees, err := parse.Parse("ROOT", text, s.leftDelim, s.rightDelim, s.parseFuncs, builtins)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
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