forked from karlseguin/gerb
/
code.go
91 lines (78 loc) · 1.87 KB
/
code.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
package gerb
import (
"github.com/karlseguin/gerb/core"
)
var CodeFactories = map[string]CodeFactory{
"if": IfFactory,
"content": ContentFactory,
"for": ForFactory,
"break": BreakFactory,
"continue": ContinueFactory,
}
var (
endScope = new(EndScope)
elseBytes = []byte("else")
closeBytes = []byte("%>")
trimCloseBytes = []byte("%%>")
)
type CodeFactory func(*core.Parser) (core.Code, error)
func createCodeTag(p *core.Parser) (*core.Codes, error) {
codes := make([]core.Code, 0, 1)
for {
token, err := p.ReadToken()
if err != nil {
return nil, err
}
length := len(token)
if length == 0 {
if err := p.ReadCloseTag(); err != nil {
return nil, err
}
return nil, nil
}
var code core.Code
if token == "}" {
if p.SkipSpaces() == 'e' && p.ConsumeIf(elseBytes) {
code, err = ElseFactory(p)
} else {
code = endScope
}
} else if factory, ok := CodeFactories[token]; ok {
code, err = factory(p)
} else {
p.Backwards(length)
code, err = p.ReadAssignment()
}
if err != nil {
return nil, err
}
codes = append(codes, code)
if p.SkipSpaces() == '%' {
if p.ConsumeIf(trimCloseBytes) {
return &core.Codes{true, codes}, nil
}
if p.ConsumeIf(closeBytes) {
return &core.Codes{false, codes}, nil
}
}
}
}
type EndScope struct{}
func (c *EndScope) Execute(context *core.Context) core.ExecutionState {
panic("Execute called on EndScope tag")
}
func (c *EndScope) IsCodeContainer() bool {
panic("IsCodeContainer called on EndScope tag")
}
func (c *EndScope) IsContentContainer() bool {
panic("IsContentContainer called on EndScope tag")
}
func (c *EndScope) IsSibling() bool {
return false
}
func (c *EndScope) AddExecutable(e core.Executable) {
panic("AddExecutable called on EndScope tag")
}
func (c *EndScope) AddCode(core.Code) error {
panic("AddCode called on EndScope tag")
}