/
parse.go
696 lines (630 loc) · 17.5 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
package jigo
import (
"fmt"
"runtime"
"strings"
)
// Important to jigo, as to most languages, is the idea of an expression.
// Here is the ebnf grammar for jigo expressions, which are actually largely
// based off of the ebnf grammar for Go. The way that these manifest
// themselves in a template is quite different, since they must be evaluated
// in potentially different contexts at runtime.
/*
NOTE: Jigo's only knowledge of custom types is whether or not the semantic
operators provide can operate on them. These decisions obviously must be
made at runtime. Its internally defined types are any type which for which
you can define a literal:
Type Go Type
string string
int int64
float float64
list []interface{}
map map[interface{}]interface{}
Obviously, wherever there is interface{}, you can use expressions which
evaluate to Go types.
NOTE: Operands form the basis for all combinatorial expressions. They can
be parenthetical expressions themselves. Lower-cased things like int_lit
and identifier are actually defined at the lexer.
Operand = Literal | identifier | MethodExpr | "(" Expression ")" .
Literal = BasicLit | MapLiteral | ListLiteral .
BasicLit = int_lit | float_lit string_lit .
MapLiteral = "{" [ MapElementList [ "," ] ] "}"
ListLiteral = "[" [ ElementList [ "," ] ] "]"
MapElementList = MapElement { "," MapElement }
MapElement = Key ":" Element
ElementList = Element { "," Element }
Element = Expression | Operand
Key = Literal | Operand
PrimaryExpr =
Operand |
PrimaryExpr Selector |
PrimaryExpr Index |
PrimaryExpr Slice |
PrimaryExpr TypeAssertion |
PrimaryExpr Call .
// NOTE: Variadic functions, type assertions, and 3-part slices are removed from the
// Go grammar to reduce initial complexity. We are not quite implementing a statically
// typed templating language, just one that has "sane" literals and sensible evaluation
// of expected programming language expressions.
Selector = "." identifier .
Index = "[" Expression "]" .
Slice = "[" ( [ Expression ] ":" [ Expression ] ) "]" .
Call = "(" [ ArgumentList [ "," ] ] ")" .
ArgumentList = ExpressionList
ExpressionList = Expression { "," Expression }
Expression = UnaryExpr | Expression binary_op UnaryExpr .
UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
binary_op = "||" | "&&" | rel_op | add_op | mul_op .
rel_op = "==" | "!=" | "<" | "<=" | ">" | ">=" .
add_op = "+" | "-" | "^" .
mul_op = "*" | "/" | "%" | "//" .
unary_op = "+" | "-" | "!" | "*" .
NOTE: Because bitwise or operator | is required as the filter operator,
the other bitwise &, &^, ^, << and >> and unary ^ operators have been
removed as their usefulness in templates seems dubious.
In addition, divmod/floordiv (//) has been added, and the channel op `<-`
has been removed.
Precedence Operator
5 * / // %
4 + -
3 == != < <= > >=
2 &&
1 ||
+ sum integers, floats, strings
- difference integers, floats
* product integers, floats
/ quotient integers, floats
// divmod integers, floats
% remainder integers
*/
type Template struct {
Name string
base *Tree
env *Environment
}
// Render this template with the given context.
func (t *Template) Render(context interface{}) (string, error) {
c := NewContextStack(context)
r := newRenderer(t)
return r.render(c)
}
// Tree is the representation of a single parsed template.
type Tree struct {
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.
text string // text parsed to create the template (or its parent)
// Parsing only; cleared after parse.
// funcs []map[string]interface{}
lex *lexer
// FIXME: the peek max is based on the way that the grammar works,
// but I don't know enough about the expression grammar i've loosely
// described to know if 3 is sufficient.
token [3]item // three-token lookahead for parser.
peekCount int
stack nodeStack
// vars []string // variables defined at the moment.
}
// Copy returns a copy of the Tree. Any parsing state is discarded.
func (t *Tree) Copy() *Tree {
if t == nil {
return nil
}
return &Tree{
Name: t.Name,
ParseName: t.ParseName,
Root: t.Root.CopyList(),
text: t.text,
}
}
// next returns the next token.
func (t *Tree) next() item {
if t.peekCount > 0 {
t.peekCount--
} else {
t.token[0] = t.lex.nextItem()
}
return t.token[t.peekCount]
}
// backup backs the input stream up one token.
func (t *Tree) backup() {
t.peekCount++
}
// backup2 backs the input stream up two tokens.
// The zeroth token is already there.
func (t *Tree) backup2(t1 item) {
t.token[1] = t1
t.peekCount = 2
}
// backup3 backs the input stream up three tokens
// The zeroth token is already there.
func (t *Tree) backup3(t2, t1 item) { // Reverse order: we're pushing back.
t.token[1] = t1
t.token[2] = t2
t.peekCount = 3
}
// peek returns but does not consume the next token.
func (t *Tree) peek() item {
if t.peekCount > 0 {
return t.token[t.peekCount-1]
}
t.peekCount = 1
t.token[0] = t.lex.nextItem()
return t.token[0]
}
// nextNonSpace returns the next non-space token.
func (t *Tree) nextNonSpace() (token item) {
for {
token = t.next()
if token.typ != tokenWhitespace {
break
}
}
return token
}
// peekNonSpace returns but does not consume the next non-space token.
func (t *Tree) peekNonSpace() (token item) {
for {
token = t.next()
if token.typ != tokenWhitespace {
break
}
}
t.backup()
return token
}
// expect peeks at the next non-space token, and if it is not itemType
// fails with an error. If it is, that item is returned and consumed.
func (t *Tree) expect(i itemType) (token item) {
token = t.peekNonSpace()
if token.typ != i {
t.unexpected(token, fmt.Sprint(i))
}
return t.nextNonSpace()
}
// Collapse N nodes into one
func (t *Tree) collapse(stack *nodeStack) Node {
if stack.len() < 2 {
return stack.pop()
}
rhs := stack.pop()
lhs := stack.pop()
switch lhs.(type) {
case *AddExpr:
lhs := lhs.(*AddExpr)
lhs.rhs = rhs
return lhs
case *MulExpr:
lhs := lhs.(*MulExpr)
lhs.rhs = rhs
return lhs
default:
stack.push(lhs)
stack.push(rhs)
return nil
}
}
// Parsing.
// New allocates a new parse tree with the given name.
func newTree(name string) *Tree {
return &Tree{
Name: name,
}
}
// ErrorContext returns a textual representation of the location of the node in the input text.
func (t *Tree) ErrorContext(n Node) (location, context string) {
pos := int(n.Position())
text := t.text[:pos]
byteNum := strings.LastIndex(text, "\n")
if byteNum == -1 {
byteNum = pos // On first line.
} else {
byteNum++ // After the newline.
byteNum = pos - byteNum
}
lineNum := 1 + strings.Count(text, "\n")
context = n.String()
if len(context) > 20 {
context = fmt.Sprintf("%.20s...", context)
}
return fmt.Sprintf("%s:%d:%d", t.ParseName, lineNum, byteNum), context
}
// 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.ParseName, t.lex.lineNumber(), format)
panic(fmt.Errorf(format, args...))
}
// recover is the handler that turns panics into returns from the top level of Parse.
func (t *Tree) recover(errp *error) {
e := recover()
if e != nil {
if _, ok := e.(runtime.Error); ok {
panic(e)
}
if t != nil {
t.stopParse()
}
*errp = e.(error)
}
return
}
// unexpected complains about the token and terminates processing.
func (t *Tree) unexpected(token item, context string) {
t.errorf("unexpected %s in %s", token, context)
}
// startParse initializes the parser, using the lexer.
func (t *Tree) startParse(lex *lexer) {
t.Root = nil
t.lex = lex
}
// stopParse terminates parsing.
func (t *Tree) stopParse() {
t.lex = nil
}
// Parse parses the template given the lexer.
func (t *Tree) Parse(lex *lexer) (tree *Tree, err error) {
defer t.recover(&err)
t.ParseName = t.Name
t.startParse(lex)
t.text = lex.input
t.parse()
t.stopParse()
return t, nil
}
// -- parsing --
// Here is where the code will depart quite a bit from text/template.
// Starting at parse(), we must take care of all sorts of different
// block types and tags, and we also have no concept of embedding
// multiple named templates within one file/[]byte/string.
// parse is the top-level parser for a template, essentially the same
// as itemList except it also parses {{define}} actions.
// It runs to EOF.
func (t *Tree) parse() {
t.Root = newList(t.peek().pos)
for n := t.parseNextNode(); n != nil; n = t.parseNextNode() {
t.Root.append(n)
}
}
// parseNextNode parses the next outer node and returns it. If EOF is encountered,
// parseNextNode returns nil. Comments are discarded.
func (t *Tree) parseNextNode() Node {
for t.peek().typ != tokenEOF {
switch t.peek().typ {
case tokenCommentBegin:
t.skipComment()
continue
case tokenBlockBegin:
return t.parseBlock()
case tokenVariableBegin:
return t.parseVar()
case tokenText:
return t.parseText()
}
}
return nil
}
func (t *Tree) nextBlockName() string {
if t.peekNonSpace().typ != tokenBlockBegin {
return ""
}
eat := t.nextNonSpace()
name := t.peekNonSpace()
t.backup2(eat)
return name.val
}
func (t *Tree) parseText() Node {
switch token := t.next(); token.typ {
case tokenText:
return newText(token.pos, token.val)
default:
t.unexpected(token, "input")
}
return nil
}
// Skips over a comment; comments are not represented in the final AST.
func (t *Tree) skipComment() {
t.expect(tokenCommentBegin)
for {
token := t.nextNonSpace()
switch token.typ {
case tokenText:
continue
case tokenCommentEnd:
default:
t.unexpected(token, "end comment")
}
break
}
}
// Parse a variable print expression, from tokenVariableBegin to tokenVariableEnd
// Contains a single expression.
func (t *Tree) parseVar() Node {
token := t.expect(tokenVariableBegin)
expr := newVar(token.pos)
expr.Node = t.parseExpr(nil, tokenVariableEnd)
t.expect(tokenVariableEnd)
return expr
}
func (t *Tree) parseBlock() Node {
start := t.expect(tokenBlockBegin)
blockType := t.peekNonSpace()
switch blockType.val {
case "for":
case "if":
t.backup2(start)
return t.parseIf()
case "block":
case "extends":
case "print":
case "macro":
case "include":
case "from":
case "import":
case "call":
case "set":
t.backup2(start)
return t.parseSet()
default:
t.unexpected(blockType, "invalid block type")
}
return nil
}
func (t *Tree) parseSet() Node {
start := t.expect(tokenBlockBegin)
set := t.nextNonSpace()
if set.val != "set" {
t.unexpected(set, "set")
}
name := t.lookupExpr()
t.expect(tokenEq)
val := t.parseSingleExpr(nil, tokenBlockEnd)
t.expect(tokenBlockEnd)
return newSet(start.pos, name, val)
}
func (t *Tree) parseIf() Node {
begin := t.expect(tokenBlockBegin)
iftok := t.nextNonSpace()
if iftok.val != "if" {
t.unexpected(iftok, "if")
}
node := newIf(begin.pos)
cond := newIfCond(begin.pos)
cond.Guard = t.parseSingleExpr(nil, tokenBlockEnd)
t.expect(tokenBlockEnd)
body := newList(t.peek().pos)
// we need some kind of parseBody here
inElse := false
for {
block := t.nextBlockName()
switch block {
case "elif":
if inElse {
panic("Elif encountered after previous else")
}
// set the body for the previous conditional and append it
cond.Body = body
node.Conditionals = append(node.Conditionals, cond)
// create a new elif conditional
cond := newElifCond(t.next().pos)
t.nextNonSpace()
cond.Guard = t.parseSingleExpr(nil, tokenBlockEnd)
t.expect(tokenBlockEnd)
body = newList(t.peek().pos)
case "else":
if inElse {
panic("Else encountered after previous else")
}
cond.Body = body
node.Conditionals = append(node.Conditionals, cond)
t.expect(tokenBlockBegin)
t.nextNonSpace()
t.expect(tokenBlockEnd)
body = newList(t.peek().pos)
inElse = true
case "endif":
// eat the endif and return successfully
t.expect(tokenBlockBegin)
t.nextNonSpace()
t.expect(tokenBlockEnd)
if inElse {
node.Else = body
} else {
node.Conditionals = append(node.Conditionals, cond)
}
return node
default:
n := t.parseNextNode()
if n == nil {
panic("EOF inside an If")
// EOF inside an If
return nil
}
body.append(n)
}
}
fmt.Println(t.peekNonSpace())
return nil
}
// parse a single expression simple expression. This is a lookup, literal, or
// index expression.
func (t *Tree) parseSingleExpr(stack *nodeStack, terminator itemType) Node {
token := t.peekNonSpace()
switch token.typ {
case terminator:
t.unexpected(token, "expected expression")
case tokenName:
return t.lookupExpr()
case tokenLparen:
t.expect(tokenLparen)
return t.parseExpr(nil, tokenRparen)
case tokenLbrace:
return t.mapExpr()
case tokenLbracket:
return t.listExpr()
case tokenFloat, tokenInteger, tokenString, tokenBool:
return t.literalExpr()
case tokenAdd, tokenSub:
unary := t.nextNonSpace()
value := t.parseSingleExpr(nil, terminator)
switch value.Type() {
case NodeUnary:
t.unexpected(unary, "expression")
case NodeFloat:
// FIXME: apply unary oper to value
return value
case NodeInteger:
// FIXME: apply unary oper to value
return value
default:
return newUnaryNode(value, unary)
}
default:
t.unexpected(token, "expression")
}
panic("unexpected")
}
// Parses an expression until it hits a terminator. An expression one of
// a few types of expressions, some of which can contain Expressions
// themselves. A stack is passed with each callframe.
func (t *Tree) parseExpr(stack *nodeStack, terminator itemType) Node {
token := t.peekNonSpace()
if stack == nil {
stack = newStack(token.pos)
}
stack.push(t.parseSingleExpr(stack, terminator))
for {
token = t.peekNonSpace()
switch token.typ {
case terminator:
if stack.len() == 0 {
fmt.Printf("Stack: %#v\n", stack)
t.unexpected(token, "zero length expression")
}
return stack.pop()
case tokenAdd, tokenSub:
// consume the plus token
t.nextNonSpace()
// if the stack isn't empty, the previous expression is the lhs for the
// upcoming expression:
if stack.len() > 0 {
rhs := t.parseExpr(stack, terminator)
// if the token after the rhs expression is an operator with greater precedence,
// push rhs to the stack and continue going from there
if tok := t.peekNonSpace(); tok.precedence() > token.precedence() {
stack.push(rhs)
return t.parseExpr(stack, terminator)
} else {
// otherwise, take lhs off the stack and create a new AddExpr
lhs := stack.pop()
stack.push(newAddExpr(lhs, rhs, token))
}
}
case tokenMul, tokenMod, tokenDiv, tokenFloordiv:
t.nextNonSpace()
if stack.len() > 0 {
lhs := stack.pop()
rhs := t.parseSingleExpr(stack, terminator)
// we know this strongly binds ltr so no need to peek
stack.push(newMulExpr(lhs, rhs, token))
} else {
t.unexpected(token, "binary op")
}
case tokenComma:
// if we are terminating a map, param list, or list, return the expression
if terminator == tokenRbracket || terminator == tokenRparen || terminator == tokenRbrace {
if stack.len() != 1 {
t.unexpected(token, "expression")
}
return stack.pop()
}
default:
t.unexpected(token, "expression")
}
}
panic("required expr but got none")
}
// in this sense, a literal is a simple lexer-level literal
func (t *Tree) literalExpr() Node {
token := t.nextNonSpace()
switch token.typ {
case tokenFloat, tokenInteger, tokenString, tokenBool:
return newLiteral(token.pos, token.typ, token.val)
default:
t.unexpected(token, "literal")
}
return nil
}
func (t *Tree) lookupExpr() Node {
name := t.nextNonSpace()
return t.maybeIndexExpr(newLookup(name.pos, name.val))
}
// determine if there is one or more index expressions on the end
// of the expression passed in. If there is, return a lookup expr,
// otherwise, return the original node
func (t *Tree) maybeIndexExpr(n Node) Node {
for {
tok := t.peekNonSpace()
if tok.typ == tokenLbrace {
t.nextNonSpace()
index := t.parseExpr(nil, tokenRbrace)
n = newIndexExpr(n, index)
} else {
return n
}
}
}
func (t *Tree) parenExpr() Node {
t.next()
return nil
}
func (t *Tree) mapExpr() Node {
tok := t.expect(tokenLbrace)
map_ := newMapExpr(tok.pos)
for {
token := t.peekNonSpace()
switch token.typ {
case tokenComma:
if map_.len() == 0 {
t.unexpected(token, "map expression")
}
t.expect(tokenComma)
case tokenRbrace:
t.next()
return t.maybeIndexExpr(map_)
default:
elem := t.mapElem()
map_.append(elem.(*MapElem))
}
}
}
// parse a single map element; assume that the next token is not '}'
func (t *Tree) mapElem() Node {
key := t.parseExpr(nil, tokenColon)
colon := t.nextNonSpace()
if colon.typ != tokenColon {
t.unexpected(colon, "map key expr")
}
val := t.parseExpr(nil, tokenRbrace)
return newMapElem(key, val)
}
func (t *Tree) listExpr() Node {
tok := t.expect(tokenLbracket)
list := newList(tok.pos)
for {
token := t.peekNonSpace()
switch token.typ {
case tokenComma:
if list.len() == 0 {
t.unexpected(token, "list expression")
}
t.expect(tokenComma)
case tokenRbracket:
t.next()
return t.maybeIndexExpr(list)
default:
elem := t.parseExpr(nil, tokenRbrace)
list.append(elem)
}
}
}