-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
151 lines (131 loc) · 3.11 KB
/
tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2015 Seth Bunce. All rights reserved. Use of this source code is
// governed by a BSD-style license that can be found in the LICENSE file.
package stem
import (
"fmt"
)
// depthLimit is the max recurse depth used to stop pathological cases.
const depthLimit = 32
// node in the parse tree.
type node interface {
// Prints node type.
String() string
}
// nodeArray is a repeated section.
type nodeArray struct {
name string
nodes []node
}
// nodeIfdef renders if the name is defined.
type nodeIfdef struct {
name string
nodes []node
}
// nodeIfndef renders if the name is not defined.
type nodeIfndef struct {
name string
nodes []node
}
// nodeInclude includes another template by name.
type nodeInclude struct {
name string
}
// nodeObject enters a JSON object.
type nodeObject struct {
name string
nodes []node
}
// nodePrint prints a symbol.
type nodePrint struct {
name string
}
// nodeString is a string literal.
type nodeString struct {
val string
}
func (n *nodeArray) String() string {
return "array"
}
func (n *nodeIfdef) String() string {
return "ifdef"
}
func (n *nodeIfndef) String() string {
return "ifndef"
}
func (n *nodeInclude) String() string {
return "include"
}
func (n *nodeObject) String() string {
return "object"
}
func (n *nodePrint) String() string {
return "print"
}
func (n *nodeString) String() string {
return "string"
}
// parse creates a parse tree.
func parse(name, src string) ([]node, error) {
return parseRecurse(make([]node, 0), newLexer(name, src), nil, 0)
}
// parseRecurse recursively builds a parse tree.
func parseRecurse(tree []node, l *lexer, end *token, depth int) ([]node, error) {
depth++
if depth > depthLimit {
return nil, fmt.Errorf("depth limit %v", depthLimit)
}
for {
t, err := l.Next()
if err != nil {
return nil, err
}
if t == nil {
if end != nil {
return nil, fmt.Errorf("unclosed scope %v", end)
}
return tree, nil
}
switch t.tt {
case ttArray:
nodes, err := parseRecurse(make([]node, 0), l, t, depth)
if err != nil {
return nil, err
}
tree = append(tree, &nodeArray{name: t.val, nodes: nodes})
case ttEnd:
if end == nil {
return nil, fmt.Errorf("unopened scope %v", t)
}
if t.val != end.val {
return nil, fmt.Errorf("unmatched tag %v", t)
}
return tree, nil
case ttIfdef:
nodes, err := parseRecurse(make([]node, 0), l, t, depth)
if err != nil {
return nil, err
}
tree = append(tree, &nodeIfdef{name: t.val, nodes: nodes})
case ttIfndef:
nodes, err := parseRecurse(make([]node, 0), l, t, depth)
if err != nil {
return nil, err
}
tree = append(tree, &nodeIfndef{name: t.val, nodes: nodes})
case ttInclude:
tree = append(tree, &nodeInclude{name: t.val})
case ttObject:
nodes, err := parseRecurse(make([]node, 0), l, t, depth)
if err != nil {
return nil, err
}
tree = append(tree, &nodeObject{name: t.val, nodes: nodes})
case ttPrint:
tree = append(tree, &nodePrint{name: t.val})
case ttString:
tree = append(tree, &nodeString{val: t.val})
default:
panic(fmt.Sprintf("unknown type %q, programmer error", t.tt))
}
}
}