-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.go
374 lines (298 loc) · 7.98 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
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
package pixy
import (
"fmt"
"strings"
"unicode"
"github.com/aerogo/codetree"
"github.com/akyoto/color"
"github.com/akyoto/ignore"
)
// Compiles the children of a Pixy CodeTree.
func compileChildren(node *codetree.CodeTree) string {
output := ""
for _, child := range node.Children {
code := strings.TrimSpace(compileNode(child))
if len(code) > 0 {
if strings.HasPrefix(code, "else {") || strings.HasPrefix(code, "else if ") {
output = strings.TrimRight(output, "\n") + code + "\n"
} else {
output += code + "\n"
}
}
}
return output
}
// Writes expression to the output.
func write(expression string) string {
if strings.HasPrefix(expression, "'") {
color.Red("Strings must use \" instead of '")
return ""
}
return "_b.WriteString(" + expression + ")\n"
}
// Writes s interpreted as a string (not an expression) to the output.
func writeString(s string) string {
return write("\"" + s + "\"")
}
// isString
func isString(code string) bool {
// TODO: Fix this
return strings.HasPrefix(code, "\"") && strings.HasSuffix(code, "\"")
}
// tag returns the code for the tag and its attributes.
func tag(keyword string, attributes map[string]string) string {
code := acquireStringsBuilder()
if keyword == "html" {
code.WriteString(writeString("<!DOCTYPE html>"))
}
numAttributes := len(attributes)
if numAttributes == 0 {
code.WriteString(writeString("<" + keyword + ">"))
return code.String()
}
code.WriteString(writeString("<" + keyword + " "))
count := 1
// Attributes
for key, value := range attributes {
// Attributes without a value
if value == "" {
code.WriteString(writeString(key))
if count != numAttributes {
code.WriteString(writeString(" "))
}
count++
continue
}
code.WriteString(writeString(key + "='"))
if isString(value) {
// Attribute values are enclosed by apostrophes.
// Therefore we need to escape this character in the attribute value.
code.WriteString(write(strings.Replace(value, "'", "'", -1)))
} else {
code.WriteString(write("html.EscapeString(fmt.Sprint(" + value + "))"))
}
if count == numAttributes {
code.WriteString(writeString("'"))
} else {
code.WriteString(writeString("' "))
}
count++
}
code.WriteString(writeString(">"))
result := code.String()
pool.Put(code)
return result
}
// endTag returns the code for the end tag.
func endTag(keyword string) string {
if !selfClosingTags[keyword] {
return writeString("</" + keyword + ">")
}
return ""
}
// Compiles a single codetree.CodeTree.
func compileNode(node *codetree.CodeTree) string {
var keyword string
if node.Line[0] == '#' || node.Line[0] == '.' {
node.Line = "div" + node.Line
}
for i, letter := range node.Line {
// Function calls
if i == 0 && unicode.IsLetter(letter) && unicode.IsUpper(letter) {
if !strings.HasSuffix(node.Line, ")") {
node.Line += "()"
}
return "stream" + strings.Replace(node.Line, "(", "(_b, ", 1)
}
// Go external function call embeds
if i == 2 && node.Line[:3] == "go:" {
return write(node.Line[3:])
}
// Comments
if i == 1 && node.Line[0] == '/' && node.Line[1] == '/' {
return ""
}
// Find keyword
if len(keyword) == 0 && !unicode.IsLetter(letter) && !unicode.IsDigit(letter) && letter != '-' {
keyword = string([]rune(node.Line)[:i])
}
}
// Keyword takes full line
if len(keyword) == 0 {
keyword = node.Line
}
// Flow control
if keyword == "if" || keyword == "else" || keyword == "for" {
return node.Line + " {\n" + compileChildren(node) + "}"
}
// Each is just syntax sugar
if keyword == "each" {
// Each with reversed order
if strings.HasSuffix(node.Line, " reversed") {
line := strings.TrimSuffix(node.Line, " reversed")
inIndex := strings.Index(line, " in ")
sliceName := line[inIndex+len(" in "):]
iteratorName := line[len("each "):inIndex]
return fmt.Sprintf("{\n_s := %s\nfor _i := len(_s)-1; _i >= 0; _i-- {\n%s := _s[_i]\n%s}\n}", sliceName, iteratorName, compileChildren(node))
}
// TODO: This is a just quick prototype implementation and not correct at all
return strings.Replace(strings.Replace(node.Line, "each", "for _, ", 1), " in ", " := range ", 1) + " {\n" + compileChildren(node) + "}"
}
var contents string
attributes := make(map[string]string)
// No contents?
if node.Line == keyword {
code := tag(keyword, attributes)
code += compileChildren(node)
code += endTag(keyword)
return code
}
escapeInput := true
cursor := len(keyword)
expect := func(expected byte, callback func(int, string)) bool {
if cursor >= len(node.Line) {
return false
}
char := node.Line[cursor]
if char == expected {
cursor++
if callback != nil {
start := cursor
remaining := node.Line[cursor:]
callback(start, remaining)
}
return true
}
return false
}
// ID
expect('#', func(start int, remaining string) {
endFound := false
for index, letter := range remaining {
if !unicode.IsLetter(letter) && !unicode.IsDigit(letter) && letter != '-' {
cursor += index
id := node.Line[start:cursor]
attributes["id"] = "\"" + id + "\""
endFound = true
break
}
}
if !endFound {
cursor = len(node.Line)
id := node.Line[start:cursor]
attributes["id"] = "\"" + id + "\""
}
})
// Classes
var classes []string
for expect('.', func(start int, remaining string) {
endFound := false
for index, letter := range remaining {
if !unicode.IsLetter(letter) && !unicode.IsDigit(letter) && letter != '-' {
cursor += index
name := node.Line[start:cursor]
classes = append(classes, name)
endFound = true
break
}
}
if !endFound {
cursor = len(node.Line)
name := node.Line[start:cursor]
classes = append(classes, name)
}
}) {
// Empty loop
}
readOneAttribute := func() bool {
for node.Line[cursor] == ' ' {
cursor++
}
remaining := node.Line[cursor:]
start := cursor
var attributeName string
for index, letter := range remaining {
if !unicode.IsLetter(letter) && !unicode.IsDigit(letter) && letter != '-' {
cursor += index
attributeName = node.Line[start:cursor]
// fmt.Println("NAME", attributeName)
break
}
}
char := node.Line[cursor]
if char == '=' {
cursor++
start = cursor
remaining = node.Line[cursor:]
reader := ignore.Reader{}
for index, letter := range remaining {
if reader.CanIgnore(letter) {
continue
}
if letter == ',' || letter == ')' {
cursor += index
attributeValue := node.Line[start:cursor]
if strings.HasPrefix(attributeValue, "'") {
color.Yellow(attributeValue)
color.Red("Strings must use \" instead of '")
attributeValue = ""
}
attributes[attributeName] = attributeValue
cursor++
return letter == ','
}
}
} else if char == ',' || char == ')' {
// Attribute without a value
attributes[attributeName] = ""
cursor++
if char == ',' {
return true
}
}
return false
}
// Attributes
expect('(', func(int, string) {
for readOneAttribute() {
// ...
}
})
if len(classes) > 0 {
existingClassList := attributes["class"]
if existingClassList != "" {
attributes["class"] = "\"" + strings.Join(classes, " ") + " \" + " + existingClassList
} else {
attributes["class"] = "\"" + strings.Join(classes, " ") + "\""
}
}
if cursor < len(node.Line) {
// Bypass HTML escaping
if node.Line[cursor] == '!' {
escapeInput = false
cursor++
}
// Expressions
if node.Line[cursor] == '=' {
contents = strings.TrimLeft(node.Line[cursor+1:], " ")
code := tag(keyword, attributes)
if escapeInput {
code += write("html.EscapeString(fmt.Sprint(" + contents + "))")
} else {
code += write(contents)
}
code += compileChildren(node)
code += endTag(keyword)
return code
}
contents = node.Line[cursor+1:]
contents = strings.Replace(contents, "\"", "\\\"", -1)
} else {
contents = ""
}
code := tag(keyword, attributes)
code += writeString(contents)
code += compileChildren(node)
code += endTag(keyword)
return code
}