-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
262 lines (247 loc) · 5.42 KB
/
parser.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
package main
import (
"io"
"os"
"path"
"strings"
Goh "github.com/OblivionOcean/Goh/utils"
)
var Cache = map[string]*Block{}
const (
Escape = iota
Val
HTML
Code
rawCode
Extend
)
const (
Bool = iota
Int
Uint
Float
String
Bytes
Any
)
type Block struct {
Type int
Content string
VarType int
Before Blocks
After Blocks
AfterCount int
}
type Parser struct {
text string
cursor int
endCursor int
root Blocks
defindFunc *Block
rawCode string
fpath string
extends map[string]*Block
extendList []*Block
unClosedExtend int
}
type Blocks []*Block
func (b *Blocks) addChild(child *Block) {
if len(*b) > 0 && child.Type == HTML && (*b)[len(*b)-1].Type == HTML {
if len((*b)[len(*b)-1].Content) > 0 {
(*b)[len(*b)-1].Content += child.Content
} else {
(*b)[len(*b)-1] = child
}
} else {
*b = append(*b, child)
}
}
// Parse Template File
func (p *Parser) Parse(fpath string) (Blocks, string, *Block) {
p.text = readFile(fpath)
p.root = Blocks{}
p.fpath = fpath
p.extends = map[string]*Block{}
for {
p.cursor = strings.Index(p.text, "<%")
if p.cursor == -1 {
p.root.addChild(&Block{
Content: p.text,
Type: HTML,
})
break
}
p.root.addChild(&Block{
Content: p.text[:p.cursor],
Type: HTML,
})
for {
p.text = p.text[p.cursor+2:]
p.endCursor = strings.Index(p.text, "%>")
if p.cursor == -1 {
panic("Syntax error")
}
if Goh.CountByte(p.text[:p.endCursor], '`')%2 != 0 || Goh.CountByte(p.text[:p.endCursor], '"')%2 != 0 {
p.cursor = p.endCursor
continue
} else {
switch p.text[0] {
case '=', '-':
p.pHTML()
case '#':
case '+':
p.pInclude()
case ':':
p.defindFunc = &Block{
Content: strings.Trim(p.text[1:p.endCursor], "\n\t\r "),
}
case '!':
p.rawCode += p.text[1:p.endCursor] + "\n"
case '~':
p.addParent()
case '@':
p.useBlock()
default:
p.root.addChild(&Block{
Type: Code,
Content: p.text[:p.endCursor],
})
}
p.text = p.text[p.endCursor+2:]
break
}
}
if 2 >= len(p.text) {
p.root.addChild(&Block{
Content: p.text,
Type: HTML,
})
break
}
}
if p.unClosedExtend > 0 {
panic("Unclosed extend, please check the syntax!\n\033[0;33mWarning:\033[0m This syntax is different from the Hero, please check Goh's documentation: https://github.com/OblivionOcean/Goh#syntax")
}
for _, block := range p.extends {
if block.AfterCount > 0 {
block.After = append(block.After, p.root[block.AfterCount:]...)
}
block.AfterCount = 0
}
return p.root, p.rawCode, p.defindFunc
}
func readFile(fpath string) string {
file, err := os.OpenFile(fpath, os.O_RDONLY, 0644)
if err != nil {
panic(err.Error())
}
tmp, err := io.ReadAll(file)
if err != nil {
panic(err.Error())
}
return Goh.Byte2String(tmp)
}
func (p *Parser) pHTML() {
block := &Block{
Type: Escape,
}
if p.text[0] == '-' {
block.Type = Val
p.text = p.text[1:]
p.endCursor -= 1
} else if p.text[1] == '=' {
block.Type = Val
p.text = p.text[2:]
p.endCursor -= 2
} else {
p.text = p.text[1:]
p.endCursor -= 1
}
if p.text[1] == ' ' || p.text[2] == ' ' {
switch p.text[0] { // 使用switch,底层是cmp比较,不是树遍历,速度更快
case 'b':
if p.text[1] == 's' {
block.VarType = Bytes
} else {
block.VarType = Bool
}
case 'u':
block.VarType = Uint
case 'i':
block.VarType = Int
case 's':
block.VarType = String
case 'f':
block.VarType = Float
case 'v', 'a':
block.VarType = Any
default:
block.VarType = String
}
if p.text[1] == 's' {
p.text = p.text[2:]
p.endCursor -= 2
} else {
p.text = p.text[1:]
p.endCursor -= 1
}
} else {
block.VarType = String
}
block.Content = p.text[:p.endCursor]
p.root.addChild(block)
}
func (p *Parser) pInclude() {
fpath := path.Join(path.Dir(p.fpath), strings.Trim(p.text[1:p.endCursor], " \"\t\n\r"))
np := Parser{}
tmp, rawCode, _ := np.Parse(fpath)
p.rawCode += rawCode
for i := 0; i < len(tmp); i++ {
p.root.addChild(tmp[i])
}
}
func (p *Parser) addParent() {
fpath := path.Join(path.Dir(p.fpath), strings.Trim(p.text[1:p.endCursor], " \"\t\n\r"))
np := &Parser{}
np.Parse(fpath)
for name, block := range np.extends {
if _, ok := p.extends[name]; !ok {
p.extends[name] = block
}
}
}
func (p *Parser) useBlock() {
name := strings.Trim(p.text[1:p.endCursor], " \"\t\n\r{")
if name == "}" {
if len(p.extendList) == 0 {
panic("Unclosed extend, please check the syntax!\n\033[0;33mWarning:\033[0m This syntax is different from the Hero, please check Goh's documentation: https://github.com/OblivionOcean/Goh#syntax")
}
block := p.extendList[len(p.extendList)-1]
if _, ok := p.extends[block.Content]; ok {
for i := 0; i < len(block.After); i++ {
p.root.addChild(block.After[i])
}
} else {
p.root.addChild(block)
block.AfterCount = len(p.root)
p.extends[block.Content] = block
}
p.unClosedExtend--
p.extendList = p.extendList[:len(p.extendList)-1]
} else if block, ok := p.extends[name]; ok {
p.unClosedExtend++
for i := 0; i < len(block.Before); i++ {
p.root.addChild(block.Before[i])
}
p.extendList = append(p.extendList, block)
} else {
p.unClosedExtend++
block := &Block{
Type: Extend,
Content: name,
Before: p.root[:len(p.root)],
}
p.root.addChild(block)
p.extendList = append(p.extendList, block)
}
}