-
Notifications
You must be signed in to change notification settings - Fork 5
/
parse.go
299 lines (279 loc) · 7.46 KB
/
parse.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright (c) 2014 Alex Kalyvitis
package mustache
import (
"fmt"
"io"
)
type parser struct {
lexer *lexer
buf []token
ast []node
}
// read returns the next token from the lexer and advances the cursor. This
// token will not be available by the parser after it has been read.
func (p *parser) read() token {
if len(p.buf) > 0 {
r := p.buf[0]
p.buf = p.buf[1:]
return r
}
return p.lexer.token()
}
// readn returns the next n tokens from the lexer and advances the cursor. If it
// coundn't read all n tokens, for example if a tokenEOF was returned by the
// lexer, an error is returned and the returned slice will have all tokens read
// until that point, including tokenEOF.
func (p *parser) readn(n int) ([]token, error) {
tokens := make([]token, 0, n) // make a slice capable of storing up to n tokens
for i := 0; i < n; i++ {
tokens = append(tokens, p.read())
if tokens[i].typ == tokenEOF {
return tokens, io.EOF
}
}
return tokens, nil
}
// readt returns the tokens starting from the current position until the first
// match of t. Similar to readn it will return an error if a tokenEOF was
// returned by the lexer before a match was made.
func (p *parser) readt(t tokenType) ([]token, error) {
var tokens []token
for {
token := p.read()
tokens = append(tokens, token)
switch token.typ {
case tokenEOF:
return tokens, fmt.Errorf("token %q not found", t)
case t:
return tokens, nil
}
}
}
// readv returns the tokens starting from the current position until the first
// match of t. A match is made only of t.typ and t.val are equal to the examined
// token.
func (p *parser) readv(t token) ([]token, error) {
var tokens []token
for {
read, err := p.readt(t.typ)
tokens = append(tokens, read...)
if err != nil {
return tokens, err
}
if len(read) > 0 && read[len(read)-1].val == t.val {
break
}
}
return tokens, nil
}
// peek returns the next token without advancing the cursor. Consecutive calls
// of peek would result in the same token being retuned. To advance the cursor,
// a read must be made.
func (p *parser) peek() token {
if len(p.buf) > 0 {
return p.buf[0]
}
t := p.lexer.token()
p.buf = append(p.buf, t)
return t
}
// peekn returns the next n tokens without advancing the cursor.
func (p *parser) peekn(n int) ([]token, error) {
if len(p.buf) > n {
return p.buf[:n], nil
}
for i := len(p.buf) - 1; i < n; i++ {
t := p.lexer.token()
p.buf = append(p.buf, t)
if t.typ == tokenEOF {
return p.buf, io.EOF
}
}
return p.buf, nil
}
// peekt returns the tokens from the current postition until the first match of
// t. it will not advance the cursor.
func (p *parser) peekt(t tokenType) ([]token, error) {
for i := 0; i < len(p.buf); i++ {
switch p.buf[i].typ {
case t:
return p.buf[:i], nil
case tokenEOF:
return p.buf[:i], io.EOF
}
}
for {
token := p.lexer.token()
p.buf = append(p.buf, token)
switch token.typ {
case t:
return p.buf, nil
case tokenEOF:
return p.buf, io.EOF
}
}
}
func (p *parser) errorf(t token, format string, v ...interface{}) error {
return fmt.Errorf("%d:%d syntax error: %s", t.line, t.col, fmt.Sprintf(format, v...))
}
// parse begins parsing based on tokens read from the lexer.
func (p *parser) parse() ([]node, error) {
var nodes []node
loop:
for {
token := p.read()
switch token.typ {
case tokenEOF:
break loop
case tokenError:
return nil, p.errorf(token, "%s", token.val)
case tokenText:
nodes = append(nodes, textNode(token.val))
case tokenLeftDelim:
node, err := p.parseTag()
if err != nil {
return nodes, err
}
nodes = append(nodes, node)
case tokenRawStart:
node, err := p.parseRawTag()
if err != nil {
return nodes, err
}
nodes = append(nodes, node)
case tokenSetDelim:
nodes = append(nodes, new(delimNode))
}
}
return nodes, nil
}
// parseTag parses a beggining of a mustache tag. It is assumed that a leftDelim
// was already read by the parser.
func (p *parser) parseTag() (node, error) {
token := p.read()
switch token.typ {
case tokenIdentifier:
return p.parseVar(token, true)
case tokenRawStart:
return p.parseRawTag()
case tokenRawAlt:
return p.parseVar(p.read(), false)
case tokenComment:
return p.parseComment()
case tokenSectionInverse:
return p.parseSection(true)
case tokenSectionStart:
return p.parseSection(false)
case tokenPartial:
return p.parsePartial()
}
return nil, p.errorf(token, "unreachable code %s", token)
}
// parseRawTag parses a simple variable tag. It is assumed that the read from
// the parser should return an identifier.
func (p *parser) parseRawTag() (node, error) {
t := p.read()
if t.typ != tokenIdentifier {
return nil, p.errorf(t, "unexpected token %s", t)
}
if next := p.read(); next.typ != tokenRawEnd {
return nil, p.errorf(t, "unexpected token %s", t)
}
if next := p.read(); next.typ != tokenRightDelim {
return nil, p.errorf(t, "unexpected token %s", t)
}
return &varNode{name: t.val, escape: false}, nil
}
// parseVar parses a simple variable tag. It is assumed that the read from the
// parser should return an identifier.
func (p *parser) parseVar(ident token, escape bool) (node, error) {
if t := p.read(); t.typ != tokenRightDelim {
return nil, p.errorf(t, "unexpected token %s", t)
}
return &varNode{name: ident.val, escape: escape}, nil
}
// parseComment parses a comment block. It is assumed that the next read should
// return a t_comment token.
func (p *parser) parseComment() (node, error) {
var comment string
for {
t := p.read()
switch t.typ {
case tokenEOF:
return nil, p.errorf(t, "unexpected token %s", t)
case tokenError:
return nil, p.errorf(t, t.val)
case tokenRightDelim:
return commentNode(comment), nil
default:
comment += t.val
}
}
}
// parseSection parses a section block. It is assumed that the next read should
// return a t_section token.
func (p *parser) parseSection(inverse bool) (node, error) {
t := p.read()
if t.typ != tokenIdentifier {
return nil, p.errorf(t, "unexpected token %s", t)
}
if next := p.read(); next.typ != tokenRightDelim {
return nil, p.errorf(t, "unexpected token %s", t)
}
var (
tokens []token
stack = 1
)
for {
read, err := p.readv(t)
if err != nil {
return nil, err
}
tokens = append(tokens, read...)
if len(read) > 1 {
// Check the token that preceeded the matching identifier. For
// section start and inverse tokens we increase the stack, otherwise
// decrease.
tt := read[len(read)-2]
switch {
case tt.typ == tokenSectionStart || tt.typ == tokenSectionInverse:
stack++
case tt.typ == tokenSectionEnd:
stack--
}
}
if stack == 0 {
break
}
}
nodes, err := subParser(tokens[:len(tokens)-3]).parse()
if err != nil {
return nil, err
}
section := §ionNode{
name: t.val,
inverted: inverse,
elems: nodes,
}
return section, nil
}
// parsePartial parses a partial block. It is assumed that the next read should
// return a t_ident token.
func (p *parser) parsePartial() (node, error) {
t := p.read()
if t.typ != tokenIdentifier {
return nil, p.errorf(t, "unexpected token %s", t)
}
if next := p.read(); next.typ != tokenRightDelim {
return nil, p.errorf(t, "unexpected token %s", t)
}
return &partialNode{t.val}, nil
}
// newParser creates a new parser using the suppliad lexer.
func newParser(l *lexer) *parser {
return &parser{lexer: l}
}
// subParser creates a new parser with a pre-defined token buffer.
func subParser(b []token) *parser {
return &parser{buf: append(b, token{typ: tokenEOF})}
}