-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.go
738 lines (680 loc) · 18.3 KB
/
compile.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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
package tpl
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"strings"
)
type fragment struct {
ftyp string
line, char int
text string
data fragments
linkextra fragments
ctx *step1_context
}
type fragments []*fragment
// Error returns a template error suitable for being returned or for panic
func (f *fragment) error(msg string, i ...interface{}) error {
return &Error{Message: fmt.Sprintf(msg, i...), Template: f.ctx.tpl, Line: f.line, Char: f.char}
}
func (f *fragment) newNode() *internalNode {
n := new(internalNode)
n.typ = internalInvalid
n.e = f.ctx.e
n.tpl, n.line, n.char = f.ctx.tpl, f.line, f.char
return n
}
type step1_context struct {
curLine, curChar int
startLine, startChar int
stack map[int]*fragment
level int
tmpStr bytes.Buffer
cLast, c, cNext byte
tpl string
e *Page
}
func (ctx *step1_context) newFragment(t string) *fragment {
f := new(fragment)
f.ftyp = t
f.ctx = ctx
f.data = make(fragments, 0)
f.line = ctx.startLine
f.char = ctx.startChar
ctx.startLine = ctx.curLine
ctx.startChar = ctx.curChar
if ctx.level >= 0 && t != "|" {
ctx.stack[ctx.level].data = append(ctx.stack[ctx.level].data, f)
} else if ctx.level >= 0 && t == "|" {
ctx.stack[ctx.level].linkextra = append(ctx.stack[ctx.level].linkextra, f)
}
ctx.level++
ctx.stack[ctx.level] = f
return f
}
func (ctx *step1_context) newTextFragment(txt string) *fragment {
f := new(fragment)
f.ftyp = "text"
f.ctx = ctx
f.text = txt
f.line = ctx.startLine
f.char = ctx.startChar
ctx.startLine = ctx.curLine
ctx.startChar = ctx.curChar
return f
}
func (e *Page) Compile(ctx context.Context) error {
// reset values
e.Version = 1
e.compiled = make(map[string]internalArray)
if !e.Raw.IsValid() {
return errors.New("template is not valid (is main template missing?)")
}
// for each raw template, compile
var err error
for tpl, data := range e.Raw.TemplateData {
err = e.compileTpl_step1(ctx, tpl, data)
if err != nil {
return err
}
}
return nil
}
func (ctx *step1_context) flush() {
if ctx.tmpStr.Len() == 0 {
return
}
ctx.stack[ctx.level].data = append(ctx.stack[ctx.level].data, ctx.newTextFragment(ctx.tmpStr.String()))
ctx.tmpStr.Reset()
}
func (e *Page) compileTpl_step1(rctx context.Context, tpl, data string) error {
// analyze string, detect {{ and }} and append in an array where it is cut
var ctx step1_context
// initialize context
ctx.tpl = tpl
ctx.e = e
ctx.level = -1
ctx.curLine, ctx.startLine = 1, 1
ctx.stack = make(map[int]*fragment)
// create root
root := ctx.newFragment("root")
for i := 0; i < len(data); i++ {
// initialize vars
ctx.curChar++
ctx.cLast = ctx.c
ctx.c = data[i]
if i+1 < len(data) {
ctx.cNext = data[i+1]
} else {
ctx.cNext = 0
}
// based on current var...
switch {
case ctx.c == '{' && ctx.cNext == '{': // opening expression
if ctx.cLast == '\\' {
// this is actually an escaped one, not to be an opening
ctx.tmpStr.Truncate(ctx.tmpStr.Len() - 1) // remove last char, which should be the \\
ctx.tmpStr.WriteString("{{")
i++
ctx.curChar++
break
}
// is this a literal fragment?
if strings.HasPrefix(data[i:], "{{literal}}") {
endPos := strings.Index(data[i:], "{{/literal}}")
if endPos > 0 {
ctx.tmpStr.WriteString(data[i+11 : i+endPos])
i += endPos + 11
break
}
}
ctx.flush()
ctx.newFragment("{{")
i++
ctx.curChar++
case ctx.c == '}' && ctx.cNext == '}' && ctx.level > 0 && ctx.stack[ctx.level].ftyp != `"`: // closing expression
ctx.flush()
if ctx.stack[ctx.level].ftyp == "|" {
ctx.level--
}
ctx.level--
i++
ctx.curChar++
case ctx.c == '\\' && ctx.cNext == '"' && ctx.stack[ctx.level].ftyp == `"`: // escaped quote
ctx.tmpStr.WriteByte('"')
i++
ctx.curChar++
case ctx.c == '"' && ctx.level > 0 && ctx.stack[ctx.level].ftyp != `"`: // opening quote
ctx.flush()
ctx.newFragment(`"`)
case ctx.c == '"' && ctx.stack[ctx.level].ftyp == `"`: // closing quote
ctx.flush()
ctx.level--
case ctx.c == '(' && ctx.level > 0 && ctx.stack[ctx.level].ftyp != `"`: // (sub)parenthesis in {{}}
ctx.flush()
ctx.newFragment("(")
case ctx.c == ')' && ctx.stack[ctx.level].ftyp == "(": // closing parenthesis
ctx.flush()
ctx.level--
case ctx.c == '|' && ctx.cNext != '|' && ctx.cLast != '|' && ctx.level > 0 && ctx.stack[ctx.level].ftyp != `"`: // argument separator (filters/etc)
ctx.flush()
if ctx.stack[ctx.level].ftyp == "|" {
ctx.level--
}
ctx.newFragment("|")
case ctx.c == '\n': // new line
ctx.curLine++
ctx.curChar = 0
ctx.tmpStr.WriteByte(ctx.c)
default:
ctx.tmpStr.WriteByte(ctx.c)
}
}
ctx.flush()
if ctx.level != 0 {
var tags []string
for i := 0; i <= ctx.level; i++ {
tags = append(tags, ctx.stack[i].ftyp)
}
return ctx.stack[ctx.level].error("Badly constructed page, not all tags are properly closed: %s", tags)
}
return e.compileTpl_step2(rctx, tpl, root)
}
func (e *Page) compileTpl_step2(ctx context.Context, tpl string, root *fragment) error {
newroot, err := e.compileTpl_step2_recurse(ctx, root.data, false)
if err != nil {
return err
}
e.compiled[tpl] = newroot
return nil
}
func (e *Page) compileTpl_step2_recurse(ctx context.Context, fl fragments, isExpression bool) (res internalArray, err error) {
res = internalArray{}
stack := make(map[int]*internalNode)
stackArray := make(map[int]*internalArray)
level := 0
cur := &res
stackArray[level] = &res
Loop:
for i := 0; i < len(fl); i++ {
cur = stackArray[level]
f := fl[i]
n := f.newNode()
switch f.ftyp {
case "text":
if isExpression {
txt := f.text
// character by character analysis
var val string
for j := 0; j < len(txt); j++ {
switch txt[j] {
case ' ', '\r', '\n', '\t':
break // NOOP
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.':
val = val + string(txt[j])
default:
// check if math operators
var l int
var ok bool
if j < len(txt)-1 {
_, ok = math_operators[txt[j:j+2]]
l = 2
}
if !ok {
_, ok = math_operators[txt[j:j+1]]
l = 1
}
if !ok {
return nil, &Error{Message: fmt.Sprintf("unhandled char %c", txt[j]), Template: n.tpl, Line: n.line, Char: n.char}
}
// cut text at position
if len(val) > 0 {
n.typ = internalText
n.str = val
res = append(res, n)
val = ""
n = f.newNode()
}
n.typ = internalOperator
n.str = txt[j : j+l]
res = append(res, n)
val = ""
n = f.newNode()
if l == 2 {
j++
}
}
}
if len(val) > 0 {
n.typ = internalText
n.str = val
} else {
n = nil
}
} else {
// check if text is not empty
if f.text == "" {
n = nil
} else {
n.typ = internalText
n.str = f.text
}
}
case `"`:
n.typ = internalQuote
n.sub = make([]internalArray, 1)
n.sub[0], err = e.compileTpl_step2_recurse(ctx, f.data, false)
case "(":
n.typ = internalSub
n.sub = make([]internalArray, 1)
n.sub[0], err = e.compileTpl_step2_recurse(ctx, f.data, true)
case "|":
if len(f.data) != 2 {
// should be 2 (string func name, then parenthesis args)
err = f.error("invalid filter call")
return
}
if f.data[0].ftyp != "text" {
err = f.error("invalid filter call")
return
}
n.typ = internalFilter
n.str = f.data[0].text
n.sub = make([]internalArray, 1)
n.sub[0], err = e.compileTpl_step2_recurse(ctx, f.data[1:2], true)
if err != nil {
return
}
if n.str[0] == '_' && n.str[len(n.str)-1] == '=' {
// not a filter but a var assign
n.typ = internalVar
n.str = strings.ToLower(n.str[:len(n.str)-1])
}
// filter filter? shouldn't happen but who knows...
n.filters, err = e.compileTpl_step2_recurse(ctx, f.linkextra, false)
case "{{":
// check for text
if f.data[0].ftyp != "text" {
// consider it a link
n.typ = internalLink
n.sub = make([]internalArray, 1)
n.sub[0], err = e.compileTpl_step2_recurse(ctx, f.data, false)
if err != nil {
return
}
n.filters, err = e.compileTpl_step2_recurse(ctx, f.linkextra, false)
break
}
// extract first word in text, analyze it (fallback to link if none)
txt := f.data[0].text
cmd := txt
if cmd[0] == '@' {
if len(f.data) != 2 {
// should be 2 (string func name, then parenthesis args)
err = f.error("invalid method call")
return
}
n.typ = internalFunc
n.str = cmd[1:]
n.sub = make([]internalArray, 1)
n.sub[0], err = e.compileTpl_step2_recurse(ctx, f.data[1:2], true)
if n.sub[0].isStatic() {
t := &interfaceValue{}
fnc, ok := tplFunctions[n.str]
if ok && fnc.CanCompile {
if params, err2 := n.sub[0].ToValues(ctx); err2 == nil {
err = fnc.Method(ctx, params, t)
if err != nil {
err = f.error("error running method %s: %s", n.str, err)
return
}
if err == nil {
// update things
n.typ = internalValue
n.str = ""
n.sub = nil
n.value = t
}
}
}
}
if err != nil {
return
}
n.filters, err = e.compileTpl_step2_recurse(ctx, f.linkextra, false)
break
}
pos := strings.IndexByte(txt, ' ')
if pos != -1 {
cmd = txt[:pos]
}
switch strings.ToLower(cmd) {
case "foreach":
n.typ = internalForeach
n.sub = make([]internalArray, 2) // will add one more if has else
if strings.TrimSpace(txt) != "foreach" {
err = f.error("foreach invalid syntax")
return
}
if len(f.data) != 3 {
// wat??? we expect "foreach " [var] "as (varname)"
err = f.error("foreach invalid syntax")
return
}
if f.data[2].ftyp != "text" {
// we expect text to contain "as _varname"
err = f.error("foreach invalid syntax")
return
}
// parse foreach variable
n.sub[0], err = e.compileTpl_step2_recurse(ctx, fragments{f.data[1]}, false) // condition for {{if}}
if err != nil {
return
}
n.sub[1] = internalArray{}
foreachVar := strings.TrimSpace(f.data[2].text)
if !strings.HasPrefix(foreachVar, "as ") {
err = f.error("foreach invalid syntax")
return
}
foreachVar = strings.TrimSpace(foreachVar[2:])
if len(foreachVar) < 2 || !strings.HasPrefix(foreachVar, "_") {
err = f.error("foreach invalid syntax")
return
}
n.str = strings.ToLower(foreachVar)
level++
stack[level] = n
stackArray[level] = &n.sub[1]
case "/foreach":
if level < 1 || stack[level].typ != internalForeach {
err = f.error("/foreach at invalid position")
return
}
level--
cur = stackArray[level]
n = nil
case "set":
n.typ = internalSet
n.sub = make([]internalArray, 1) // will add one more if has else
if len(txt) <= 4 {
if len(f.data) > 1 {
f.data = f.data[1:]
} else {
//hum? let's just avoid crashing
f.data = make(fragments, 0)
}
} else {
f.data[0].text = f.data[0].text[4:]
}
// let's just parse all we now have in f.data, should be one text node ( _key=) and a value (quoted, link, etc)
for len(f.data) > 1 {
if f.data[0].ftyp != "text" {
err = f.data[0].error("invalid set instruction")
return
}
tmp := strings.TrimSpace(f.data[0].text)
if !strings.HasSuffix(tmp, "=") {
err = f.data[0].error("invalid set instruction")
return
}
tmp = strings.ToLower(strings.TrimSpace(strings.TrimSuffix(tmp, "=")))
if tmp[0] != '_' {
err = f.data[0].error("invalid set instruction")
return
}
nVar := f.data[0].newNode()
nVar.typ = internalVar
nVar.str = tmp
nVar.sub = make([]internalArray, 1)
nVar.sub[0], err = e.compileTpl_step2_recurse(ctx, f.data[1:2], true)
if err != nil {
return
}
n.filters = append(n.filters, nVar)
f.data = f.data[2:]
}
n.sub[0] = internalArray{}
level++
stack[level] = n
stackArray[level] = &n.sub[0]
case "/set":
if level < 1 || stack[level].typ != internalSet {
err = f.error("/set at invalid position")
return
}
level--
cur = stackArray[level]
n = nil
case "if":
n.typ = internalIf
n.sub = make([]internalArray, 2) // will add one more if has else
if len(txt) <= 3 {
if len(f.data) > 1 {
f.data = f.data[1:]
} else {
//hum? let's just avoid crashing
f.data = make(fragments, 0)
}
} else {
f.data[0].text = f.data[0].text[3:]
}
n.sub[0], err = e.compileTpl_step2_recurse(ctx, f.data, true) // condition for {{if}}
if err != nil {
return
}
n.sub[1] = internalArray{}
level++
stack[level] = n
stackArray[level] = &n.sub[1]
case "/if":
if level < 1 || stack[level].typ != internalIf {
err = f.error("/if at invalid position")
return
}
level--
cur = stackArray[level]
n = nil
case "else":
if level < 1 || (stack[level].typ != internalIf && stack[level].typ != internalForeach) {
err = f.error("else at invalid position")
return
}
if len(stack[level].sub) > 2 {
err = f.error("else present more than once")
return
}
stack[level].sub = append(stack[level].sub, internalArray{})
stackArray[level] = &stack[level].sub[len(stack[level].sub)-1]
n = nil
case "elseif":
// create a new if in the current if's sub[2], and record it at the same level in the stack
if level < 1 || (stack[level].typ != internalIf) {
err = f.error("elseif at invalid position")
return
}
if len(stack[level].sub) > 2 {
err = f.error("elseif after else")
return
}
n.typ = internalIf
n.sub = make([]internalArray, 2) // will add one more if has else
if len(txt) <= 7 {
if len(f.data) > 1 {
f.data = f.data[1:]
} else {
//hum? let's just avoid crashing
f.data = make(fragments, 0)
}
} else {
f.data[0].text = f.data[0].text[7:]
}
n.sub[0], err = e.compileTpl_step2_recurse(ctx, f.data, true) // condition for {{if}}
if err != nil {
return
}
n.sub[1] = internalArray{}
// add this if as else of previous if
stack[level].sub = append(stack[level].sub, internalArray{n})
// then record it at the same level
stack[level] = n
stackArray[level] = &n.sub[1]
case "try":
n.typ = internalTry
n.sub = make([]internalArray, 1) // will add one more if has else
n.sub[0] = internalArray{}
level++
stack[level] = n
stackArray[level] = &n.sub[0]
case "catch":
// create a new if in the current if's sub[2], and record it at the same level in the stack
if level < 1 || (stack[level].typ != internalTry) {
err = f.error("catch at invalid position")
return
}
if len(stack[level].sub) > 1 {
err = f.error("catch after catch?")
return
}
if len(txt) <= 6 {
if len(f.data) > 1 {
err = f.error("invalid parameters in catch")
return
}
} else {
stack[level].str = strings.ToLower(strings.TrimSpace(txt[6:]))
}
stack[level].sub = append(stack[level].sub, internalArray{})
stackArray[level] = &stack[level].sub[len(stack[level].sub)-1]
n = nil
case "/try":
if level < 1 || stack[level].typ != internalTry {
err = f.error("/try at invalid position")
return
}
level--
cur = stackArray[level]
n = nil
}
if n == nil {
break
}
if n.typ == internalInvalid {
// consider it a link, but start by making text lowercase
pos := strings.IndexByte(txt, '/')
if pos > 0 {
f.data[0].text = strings.ToLower(txt[:pos]) + txt[pos:]
} else {
f.data[0].text = strings.ToLower(txt)
}
n.typ = internalLink
n.sub = make([]internalArray, 1)
n.sub[0], err = e.compileTpl_step2_recurse(ctx, f.data, false)
if err != nil {
return
}
n.filters, err = e.compileTpl_step2_recurse(ctx, f.linkextra, false)
}
}
if err != nil {
break Loop
}
if n != nil {
if n.typ == internalInvalid {
f.Dump(os.Stdout, 0)
}
*cur = append(*cur, n)
}
}
if level != 0 {
// something isn't closed on the stack
err = stack[level].error("element isn't closed")
return
}
// if not an expression, processing ends here
if !isExpression {
return
}
// range over res to check things that needs to be made recursive (operators, etc)
// we should always have an operator between values
var last_comma *internalNode
for {
has_op := false
op_pos := 0
op_weight := 999
// look for operator with lowest weight
for i, n := range res {
if n.typ == internalOperator && len(n.sub) == 0 {
has_op = true
op_w := math_operators[n.str]
if op_w == 0 {
// syntax error
return nil, n.error("invalid operator %s", n.str)
}
if op_w < op_weight {
op_weight = op_w
op_pos = i
}
}
}
if !has_op {
break
}
n := res[op_pos]
if op_pos == len(res)-1 {
return nil, n.error("invalid operator %s at end of expression", n.str)
}
next_n := res[op_pos+1]
if op_pos == 0 {
if n.str == "!" || n.str == "~" {
// special case!
n.sub = []internalArray{internalArray{next_n}}
res = res[1:]
res[0] = n
continue
}
return nil, n.error("invalid operator %s at start of expression", n.str)
}
if n.str == "!" || n.str == "~" {
// special case!
n.sub = []internalArray{internalArray{next_n}}
copy(res[op_pos:], res[op_pos+1:])
res[len(res)-1] = nil
res[op_pos] = n
res = res[:len(res)-1]
continue
}
prev_n := res[op_pos-1]
if n.str == "," {
if last_comma != nil {
// we already got a comma, group what we have here with it
if prev_n != last_comma {
return nil, n.error("invalid comma position")
}
// we need to remove cur and next, not prev
prev_n.sub = append(prev_n.sub, internalArray{next_n})
copy(res[op_pos:], res[op_pos+2:])
res[len(res)-1] = nil
res[len(res)-2] = nil
res = res[:len(res)-2]
continue
} else {
// special case
n.typ = internalList
n.str = ""
last_comma = n
}
}
// remove prev/next from res
copy(res[op_pos-1:], res[op_pos+1:])
res[len(res)-1] = nil
res[len(res)-2] = nil
res[op_pos-1] = n
res = res[:len(res)-2]
n.sub = []internalArray{internalArray{prev_n}, internalArray{next_n}}
}
return
}