b089c599a4769ee26b334cb3e3959293cfe2952c
[platform/upstream/gcc.git] / libgo / go / template / parse.go
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package template
6
7 import (
8         "os"
9         "reflect"
10         "template/parse"
11 )
12
13 // Template is the representation of a parsed template.
14 type Template struct {
15         name string
16         *parse.Tree
17         // We use two maps, one for parsing and one for execution.
18         // This separation makes the API cleaner since it doesn't
19         // expose reflection to the client.
20         parseFuncs FuncMap
21         execFuncs  map[string]reflect.Value
22         set        *Set // can be nil.
23 }
24
25 // Name returns the name of the template.
26 func (t *Template) Name() string {
27         return t.name
28 }
29
30 // Parsing.
31
32 // New allocates a new template with the given name.
33 func New(name string) *Template {
34         return &Template{
35                 name:       name,
36                 parseFuncs: make(FuncMap),
37                 execFuncs:  make(map[string]reflect.Value),
38         }
39 }
40
41 // Funcs adds the elements of the argument map to the template's function
42 // map.  It panics if a value in the map is not a function with appropriate
43 // return type.
44 // The return value is the template, so calls can be chained.
45 func (t *Template) Funcs(funcMap FuncMap) *Template {
46         addValueFuncs(t.execFuncs, funcMap)
47         addFuncs(t.parseFuncs, funcMap)
48         return t
49 }
50
51 // Parse parses the template definition string to construct an internal
52 // representation of the template for execution.
53 func (t *Template) Parse(s string) (tmpl *Template, err os.Error) {
54         t.Tree, err = parse.New(t.name).Parse(s, t.parseFuncs, builtins)
55         if err != nil {
56                 return nil, err
57         }
58         return t, nil
59 }
60
61 // ParseInSet parses the template definition string to construct an internal
62 // representation of the template for execution. It also adds the template
63 // to the set.
64 // Function bindings are checked against those in the set.
65 func (t *Template) ParseInSet(s string, set *Set) (tmpl *Template, err os.Error) {
66         var setFuncs FuncMap
67         if set != nil {
68                 setFuncs = set.parseFuncs
69         }
70         t.Tree, err = parse.New(t.name).Parse(s, t.parseFuncs, setFuncs, builtins)
71         if err != nil {
72                 return nil, err
73         }
74         t.addToSet(set)
75         return t, nil
76 }
77
78 // addToSet adds the template to the set, verifying it's not being double-assigned.
79 func (t *Template) addToSet(set *Set) {
80         if set == nil || t.set == set {
81                 return
82         }
83         // If double-assigned, Add will panic and we will turn that into an error.
84         set.Add(t)
85 }