/
processor.go
176 lines (147 loc) · 3.63 KB
/
processor.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
package processors
import (
"go/ast"
"go/parser"
"go/token"
"io"
"strings"
"github.com/linuxerwang/goats-html/expl"
"github.com/linuxerwang/goats-html/pkgmgr"
"github.com/linuxerwang/goats-html/symbolmgr"
"github.com/linuxerwang/goats-html/util"
)
type FilterInstance struct {
Name string
Start token.Pos
End token.Pos
Pivot token.Pos // Unused
Params string
}
type SelectorInstance struct {
Name string
NewName string
Start token.Pos
End token.Pos
}
type TagContext struct {
nextId int
ExprParser *expl.ExprParser
pkgMgr *pkgmgr.PkgManager
pkgRefs pkgmgr.AliasReferer
symMgr *symbolmgr.SymbolMgr
fitlers map[string]*RegisteredFilter
AutoEscape bool
OutputFormat string
}
func (ctx *TagContext) GetFilters() map[string]*RegisteredFilter {
return ctx.fitlers
}
func (ctx *TagContext) NextId() int {
ctx.nextId++
return ctx.nextId
}
func (ctx *TagContext) MaybeAddImports(expression string) {
expression = util.TrimWhiteSpaces(expression)
if expression == "" {
return
}
f, err := parser.ParseExpr(util.ToGoString(expression))
if err != nil {
panic(err)
}
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.SelectorExpr:
selector := expression[x.Pos()-1 : x.End()-1]
ctx.pkgRefs.RefByAlias(strings.Split(selector, ".")[0], false)
return false
}
return true
})
}
func (ctx *TagContext) RewriteExpression(originalExpr string) (string, error) {
ctx.MaybeAddImports(originalExpr)
er := expl.NewExprRewriter(ctx.symMgr, ctx.pkgRefs, ctx.OutputFormat)
return er.RewriteExpression(originalExpr)
}
func NewTagContext(pkgMgr *pkgmgr.PkgManager, pkgRefs pkgmgr.AliasReferer, outputFormat string) *TagContext {
ctx := &TagContext{
nextId: 0,
pkgMgr: pkgMgr,
pkgRefs: pkgRefs,
symMgr: symbolmgr.New(),
fitlers: map[string]*RegisteredFilter{},
AutoEscape: true,
OutputFormat: outputFormat,
}
sm := make(map[string]*symbolmgr.Symbol)
if ctx.symMgr.Size() == 0 {
for alias, pkgImpt := range pkgMgr.GetPkgsForAlias() {
sm[alias] = &symbolmgr.Symbol{
Name: alias,
Type: symbolmgr.TypeImport,
IsPb: pkgImpt.PbPkg() != "",
PkgImpt: pkgImpt,
}
}
}
ctx.symMgr.Push(sm)
ctx.ExprParser = expl.NewExprParser(ctx)
return ctx
}
type Processor interface {
HasNext() bool
GetNext() Processor
SetNext(next Processor)
Process(writer io.Writer, context *TagContext)
}
type BaseProcessor struct {
next Processor
}
func (b *BaseProcessor) HasNext() bool {
return b.next != nil
}
func (b *BaseProcessor) GetNext() Processor {
return b.next
}
func (b *BaseProcessor) SetNext(next Processor) {
b.next = next
}
type GoHeadProcessor struct {
BaseProcessor
}
func (gheadp *GoHeadProcessor) Process(writer io.Writer, ctx *TagContext) {
sm := make(map[string]*symbolmgr.Symbol)
ctx.symMgr.Push(sm)
if gheadp.next != nil {
gheadp.next.Process(writer, ctx)
}
ctx.symMgr.Pop()
}
func NewHeadProcessor() *GoHeadProcessor {
return &GoHeadProcessor{}
}
type ProcessorCallbackFunc func()
/*
* Callback processor gives a chance to execute a special processing when
* the processing chain reaches its end but before the call stack starts to
* go back.
*/
type GoCallbackProcessor struct {
BaseProcessor
callback ProcessorCallbackFunc
}
func (c *GoCallbackProcessor) Process(writer io.Writer, context *TagContext) {
// Do nothing, only call the callback function
c.callback()
}
func NewCallbackProcessor(callback ProcessorCallbackFunc) *GoCallbackProcessor {
return &GoCallbackProcessor{
callback: callback,
}
}
type RegisteredFilter struct {
PkgName string
Type string
VarName string
}