forked from eknkc/amber
/
compiler.go
630 lines (524 loc) · 14 KB
/
compiler.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
package amber
import (
"bytes"
"container/list"
"errors"
"fmt"
"github.com/pendo-io/amber/parser"
"go/ast"
gp "go/parser"
gt "go/token"
"html/template"
"io"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
)
var builtinFunctions = [...]string{
"len",
"print",
"printf",
"println",
"urlquery",
"js",
"json",
"index",
"html",
"unescaped",
}
// Compiler is the main interface of Amber Template Engine.
// In order to use an Amber template, it is required to create a Compiler and
// compile an Amber source to native Go template.
// compiler := amber.New()
// // Parse the input file
// err := compiler.ParseFile("./input.amber")
// if err == nil {
// // Compile input file to Go template
// tpl, err := compiler.Compile()
// if err == nil {
// // Check built in html/template documentation for further details
// tpl.Execute(os.Stdout, somedata)
// }
// }
type Compiler struct {
// Compiler options
Options
filename string
node parser.Node
indentLevel int
newline bool
buffer *bytes.Buffer
tempvarIndex int
}
// Create and initialize a new Compiler
func New() *Compiler {
compiler := new(Compiler)
compiler.filename = ""
compiler.tempvarIndex = 0
compiler.PrettyPrint = true
compiler.Options = DefaultOptions
return compiler
}
type Options struct {
// Setting if pretty printing is enabled.
// Pretty printing ensures that the output html is properly indented and in human readable form.
// If disabled, produced HTML is compact. This might be more suitable in production environments.
// Default: true
PrettyPrint bool
// Setting if line number emitting is enabled
// In this form, Amber emits line number comments in the output template. It is usable in debugging environments.
// Default: false
LineNumbers bool
}
var DefaultOptions = Options{true, false}
// Parses and compiles the supplied amber template string. Returns corresponding Go Template (html/templates) instance.
// Necessary runtime functions will be injected and the template will be ready to be executed.
func Compile(input string, options Options) (*template.Template, error) {
comp := New()
comp.Options = options
err := comp.Parse(input)
if err != nil {
return nil, err
}
return comp.Compile()
}
// Parses and compiles the contents of supplied filename. Returns corresponding Go Template (html/templates) instance.
// Necessary runtime functions will be injected and the template will be ready to be executed.
func CompileFile(filename string, options Options) (*template.Template, error) {
comp := New()
comp.Options = options
err := comp.ParseFile(filename)
if err != nil {
return nil, err
}
return comp.Compile()
}
// Parse given raw amber template string.
func (c *Compiler) Parse(input string) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New(r.(string))
}
}()
parser, err := parser.StringParser(input)
if err != nil {
return
}
c.node = parser.Parse()
return
}
// Parse the amber template file in given path
func (c *Compiler) ParseFile(filename string) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New(r.(string))
}
}()
parser, err := parser.FileParser(filename)
if err != nil {
return
}
c.node = parser.Parse()
c.filename = filename
return
}
// Compile amber and create a Go Template (html/templates) instance.
// Necessary runtime functions will be injected and the template will be ready to be executed.
func (c *Compiler) Compile() (*template.Template, error) {
return c.CompileWithName(filepath.Base(c.filename))
}
// Same as Compile but allows to specify a name for the template
func (c *Compiler) CompileWithName(name string) (*template.Template, error) {
t := template.New(name)
t.Delims("{<{", "}>}")
return c.CompileWithTemplate(t)
}
// Same as Compile but allows to specify a template
func (c *Compiler) CompileWithTemplate(t *template.Template) (*template.Template, error) {
data, err := c.CompileString()
if err != nil {
return nil, err
}
tpl, err := t.Funcs(funcMap).Parse(data)
if err != nil {
return nil, err
}
return tpl, nil
}
// Compile amber and write the Go Template source into given io.Writer instance
// You would not be using this unless debugging / checking the output. Please use Compile
// method to obtain a template instance directly.
func (c *Compiler) CompileWriter(out io.Writer) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New(r.(string))
}
}()
c.buffer = new(bytes.Buffer)
c.visit(c.node)
if c.buffer.Len() > 0 {
c.write("\n")
}
_, err = c.buffer.WriteTo(out)
return
}
// Compile template and return the Go Template source
// You would not be using this unless debugging / checking the output. Please use Compile
// method to obtain a template instance directly.
func (c *Compiler) CompileString() (string, error) {
var buf bytes.Buffer
if err := c.CompileWriter(&buf); err != nil {
return "", err
}
result := buf.String()
return result, nil
}
func (c *Compiler) visit(node parser.Node) {
defer func() {
if r := recover(); r != nil {
if rs, ok := r.(string); ok && rs[:len("Amber Error")] == "Amber Error" {
panic(r)
}
pos := node.Pos()
if len(pos.Filename) > 0 {
panic(fmt.Sprintf("Amber Error in <%s>: %v - Line: %d, Column: %d, Length: %d", pos.Filename, r, pos.LineNum, pos.ColNum, pos.TokenLength))
} else {
panic(fmt.Sprintf("Amber Error: %v - Line: %d, Column: %d, Length: %d", r, pos.LineNum, pos.ColNum, pos.TokenLength))
}
}
}()
switch node.(type) {
case *parser.Block:
c.visitBlock(node.(*parser.Block))
case *parser.Doctype:
c.visitDoctype(node.(*parser.Doctype))
case *parser.Comment:
c.visitComment(node.(*parser.Comment))
case *parser.Tag:
c.visitTag(node.(*parser.Tag))
case *parser.Text:
c.visitText(node.(*parser.Text))
case *parser.Condition:
c.visitCondition(node.(*parser.Condition))
case *parser.Each:
c.visitEach(node.(*parser.Each))
case *parser.Assignment:
c.visitAssignment(node.(*parser.Assignment))
}
}
func (c *Compiler) write(value string) {
c.buffer.WriteString(value)
}
func (c *Compiler) indent(offset int, newline bool) {
if !c.PrettyPrint {
return
}
if newline && c.buffer.Len() > 0 {
c.write("\n")
}
for i := 0; i < c.indentLevel+offset; i++ {
c.write("\t")
}
}
func (c *Compiler) tempvar() string {
c.tempvarIndex++
return "$__amber_" + strconv.Itoa(c.tempvarIndex)
}
func (c *Compiler) escape(input string) string {
return strings.Replace(strings.Replace(input, `\`, `\\`, -1), `"`, `\"`, -1)
}
func (c *Compiler) visitBlock(block *parser.Block) {
for _, node := range block.Children {
if _, ok := node.(*parser.Text); !block.CanInline() && ok {
c.indent(0, true)
}
c.visit(node)
}
}
func (c *Compiler) visitDoctype(doctype *parser.Doctype) {
c.write(doctype.String())
}
func (c *Compiler) visitComment(comment *parser.Comment) {
if comment.Silent {
return
}
c.indent(0, false)
if comment.Block == nil {
c.write(`{<{unescaped "<!-- ` + c.escape(comment.Value) + ` -->"}>}`)
} else {
c.write(`<!-- ` + comment.Value)
c.visitBlock(comment.Block)
c.write(` -->`)
}
}
func (c *Compiler) visitCondition(condition *parser.Condition) {
c.write(`{<{if ` + c.visitRawInterpolation(condition.Expression) + `}>}`)
c.visitBlock(condition.Positive)
if condition.Negative != nil {
c.write(`{<{else}>}`)
c.visitBlock(condition.Negative)
}
c.write(`{<{end}>}`)
}
func (c *Compiler) visitEach(each *parser.Each) {
if each.Block == nil {
return
}
if len(each.Y) == 0 {
c.write(`{<{range ` + each.X + ` := ` + c.visitRawInterpolation(each.Expression) + `}>}`)
} else {
c.write(`{<{range ` + each.X + `, ` + each.Y + ` := ` + c.visitRawInterpolation(each.Expression) + `}>}`)
}
c.visitBlock(each.Block)
c.write(`{<{end}>}`)
}
func (c *Compiler) visitAssignment(assgn *parser.Assignment) {
c.write(`{<{` + assgn.X + ` := ` + c.visitRawInterpolation(assgn.Expression) + `}>}`)
}
var quoteEscape = regexp.MustCompile(`^([^"]|\\")*$`)
func (c *Compiler) visitTag(tag *parser.Tag) {
type attrib struct {
name string
value string
condition string
}
attribs := make(map[string]*attrib)
for _, item := range tag.Attributes {
attr := new(attrib)
attr.name = item.Name
if !item.IsRaw {
attr.value = c.visitInterpolation(item.Value)
} else if item.Value == "" {
attr.value = ""
} else if len(item.Value) > 5 && strings.Index(item.Value, "{{") < strings.Index(item.Value, "}}") {
attr.value = item.Value
} else if item.IsRaw {
attr.value = item.Value
} else {
attr.value = `{<{"` + item.Value + `"}>}`
}
if len(item.Condition) != 0 {
attr.condition = c.visitRawInterpolation(item.Condition)
}
if attr.name == "class" && attribs["class"] != nil {
prevclass := attribs["class"]
attr.value = ` ` + attr.value
if len(attr.condition) > 0 {
attr.value = `{<{if ` + attr.condition + `}>}` + attr.value + `{<{end}>}`
attr.condition = ""
}
if len(prevclass.condition) > 0 {
prevclass.value = `{<{if ` + prevclass.condition + `}>}` + prevclass.value + `{<{end}>}`
prevclass.condition = ""
}
prevclass.value = prevclass.value + attr.value
} else {
attribs[item.Name] = attr
}
}
c.indent(0, true)
c.write("<" + tag.Name)
for name, value := range attribs {
if len(value.condition) > 0 {
c.write(`{<{if ` + value.condition + `}>}`)
}
if value.value == "" {
c.write(` ` + name)
} else {
c.write(` ` + name + `="` + value.value + `"`)
}
if len(value.condition) > 0 {
c.write(`{<{end}>}`)
}
}
if tag.IsSelfClosing() {
c.write(` />`)
} else {
c.write(`>`)
if tag.Block != nil {
if !tag.Block.CanInline() {
c.indentLevel++
}
c.visitBlock(tag.Block)
if !tag.Block.CanInline() {
c.indentLevel--
c.indent(0, true)
}
}
c.write(`</` + tag.Name + `>`)
}
}
var textInterpolateRegexp = regexp.MustCompile(`#\{(.*?)\}`)
var textEscapeRegexp = regexp.MustCompile(`\{<\{(.*?)\}>\}`)
func (c *Compiler) visitText(txt *parser.Text) {
value := textEscapeRegexp.ReplaceAllStringFunc(txt.Value, func(value string) string {
return `{<{"{<{"}>}` + value[2:len(value)-2] + `{<{"}>}"}>}`
})
value = textInterpolateRegexp.ReplaceAllStringFunc(value, func(value string) string {
return c.visitInterpolation(value[2 : len(value)-1])
})
lines := strings.Split(value, "\n")
for i := 0; i < len(lines); i++ {
c.write(lines[i])
if i < len(lines)-1 {
c.write("\n")
c.indent(0, false)
}
}
}
func (c *Compiler) visitInterpolation(value string) string {
return `{<{` + c.visitRawInterpolation(value) + `}>}`
}
func (c *Compiler) visitRawInterpolation(value string) string {
value = strings.Replace(value, "$", "__DOLLAR__", -1)
expr, err := gp.ParseExpr(value)
if err != nil {
panic("Unable to parse expression.")
}
value = strings.Replace(c.visitExpression(expr), "__DOLLAR__", "$", -1)
return value
}
func (c *Compiler) visitExpression(outerexpr ast.Expr) string {
stack := list.New()
pop := func() string {
if stack.Front() == nil {
return ""
}
val := stack.Front().Value.(string)
stack.Remove(stack.Front())
return val
}
var exec func(ast.Expr)
exec = func(expr ast.Expr) {
switch expr.(type) {
case *ast.BinaryExpr:
{
be := expr.(*ast.BinaryExpr)
exec(be.Y)
exec(be.X)
negate := false
name := c.tempvar()
c.write(`{<{` + name + ` := `)
switch be.Op {
case gt.ADD:
c.write("__amber_add ")
case gt.SUB:
c.write("__amber_sub ")
case gt.MUL:
c.write("__amber_mul ")
case gt.QUO:
c.write("__amber_quo ")
case gt.REM:
c.write("__amber_rem ")
case gt.LAND:
c.write("and ")
case gt.LOR:
c.write("or ")
case gt.EQL:
c.write("__amber_eql ")
case gt.NEQ:
c.write("__amber_eql ")
negate = true
case gt.LSS:
c.write("__amber_lss ")
case gt.GTR:
c.write("__amber_gtr ")
case gt.LEQ:
c.write("__amber_gtr ")
negate = true
case gt.GEQ:
c.write("__amber_lss ")
negate = true
default:
panic("Unexpected operator!")
}
c.write(pop() + ` ` + pop() + `}>}`)
if !negate {
stack.PushFront(name)
} else {
negname := c.tempvar()
c.write(`{<{` + negname + ` := not ` + name + `}>}`)
stack.PushFront(negname)
}
}
case *ast.UnaryExpr:
{
ue := expr.(*ast.UnaryExpr)
exec(ue.X)
name := c.tempvar()
c.write(`{<{` + name + ` := `)
switch ue.Op {
case gt.SUB:
c.write("__amber_minus ")
case gt.ADD:
c.write("__amber_plus ")
case gt.NOT:
c.write("not ")
default:
panic("Unexpected operator!")
}
c.write(pop() + `}>}`)
stack.PushFront(name)
}
case *ast.ParenExpr:
exec(expr.(*ast.ParenExpr).X)
case *ast.BasicLit:
stack.PushFront(expr.(*ast.BasicLit).Value)
case *ast.Ident:
name := expr.(*ast.Ident).Name
if len(name) >= len("__DOLLAR__") && name[:len("__DOLLAR__")] == "__DOLLAR__" {
if name == "__DOLLAR__" {
stack.PushFront(`.`)
} else {
stack.PushFront(`$` + expr.(*ast.Ident).Name[len("__DOLLAR__"):])
}
} else {
stack.PushFront(`.` + expr.(*ast.Ident).Name)
}
case *ast.SelectorExpr:
se := expr.(*ast.SelectorExpr)
exec(se.X)
x := pop()
if x == "." {
x = ""
}
name := c.tempvar()
c.write(`{<{` + name + ` := ` + x + `.` + se.Sel.Name + `}>}`)
stack.PushFront(name)
case *ast.CallExpr:
ce := expr.(*ast.CallExpr)
for i := len(ce.Args) - 1; i >= 0; i-- {
exec(ce.Args[i])
}
name := c.tempvar()
builtin := false
if ident, ok := ce.Fun.(*ast.Ident); ok {
for _, fname := range builtinFunctions {
if fname == ident.Name {
builtin = true
break
}
}
}
if builtin {
stack.PushFront(ce.Fun.(*ast.Ident).Name)
c.write(`{<{` + name + ` := ` + pop())
} else {
exec(ce.Fun)
c.write(`{<{` + name + ` := call ` + pop())
}
for i := 0; i < len(ce.Args); i++ {
c.write(` `)
c.write(pop())
}
c.write(`}>}`)
stack.PushFront(name)
default:
panic("Unable to parse expression. Unsupported: " + reflect.TypeOf(expr).String())
}
}
exec(outerexpr)
return pop()
}