-
Notifications
You must be signed in to change notification settings - Fork 13
/
parser.go
385 lines (360 loc) · 8.86 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
375
376
377
378
379
380
381
382
383
384
385
package gohaml
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"text/scanner"
"unicode"
)
type hamlParser struct {
}
func (self *hamlParser) parse(input string) (output *tree, err error) {
output = newTree()
var currentNode inode
var node inode
lastSpaceChar := '\000'
line := 1
j := 0
for i, r := range input {
if r == '\n' {
line += 1
node, err, lastSpaceChar = parseLeadingSpace(input[j:i], lastSpaceChar, line)
if err != nil {
return
}
if node != nil && !node.nil() {
putNodeInPlace(currentNode, node, output)
currentNode = node
}
j = i + 1
}
}
node, err, lastSpaceChar = parseLeadingSpace(input[j:], lastSpaceChar, line)
if err != nil {
return
}
if node != nil && !node.nil() {
putNodeInPlace(currentNode, node, output)
}
return
}
func putNodeInPlace(cn inode, node inode, t *tree) {
if node == nil || node.nil() {
return
}
if cn == nil || cn.nil() {
//t.nodes.Push(node)
t.nodes = append(t.nodes, node)
} else if node.indentLevel() < cn.indentLevel() {
for cn = cn.parent(); cn != nil && node.indentLevel() < cn.indentLevel(); cn = cn.parent() {
}
putNodeInPlace(cn, node, t)
} else if node.indentLevel() == cn.indentLevel() && cn.parent() != nil {
cn.parent().addChild(node)
} else if node.indentLevel() == cn.indentLevel() {
//t.nodes.Push(node)
t.nodes = append(t.nodes, node)
} else if node.indentLevel() > cn.indentLevel() {
cn.addChild(node)
}
}
var parser hamlParser
func parseLeadingSpace(input string, lastSpaceChar rune, line int) (output inode, err error, spaceChar rune) {
node := new(node)
for i, r := range input {
switch {
case r == '!' && len(input) >= i+2 && input[i+1] == '!' && input[i+2] == '!':
if len(input) > i+2 {
output = parseDoctype(input[i+3:], node, line)
} else {
output = parseDoctype("", node, line)
}
case r == '-':
output = parseCode(input[i+1:], node, line)
case r == '%':
output, err = parseTag(input[i+1:], node, true, line)
case r == '#':
output, err = parseId(input[i+1:], node, line)
case r == '.':
output, err = parseClass(input[i+1:], node, line)
case r == '=':
output = parseKey(tl(input[i+1:]), node, line)
case r == '\\':
output = parseRemainder(input[i+1:], node, line)
case !unicode.IsSpace(r):
output = parseRemainder(input[i:], node, line)
case unicode.IsSpace(r):
if lastSpaceChar > 0 && r != lastSpaceChar {
from, to := "space", "tab"
if lastSpaceChar == 9 {
from = "tab"
to = "space"
}
msg := fmt.Sprintf("Syntax error on line %d: Inconsistent spacing in document changed from %s to %s characters.\n", line, from, to)
err = errors.New(msg)
} else {
lastSpaceChar = r
}
}
if nil != err {
break
}
if nil != output {
output.setIndentLevel(i)
break
}
}
spaceChar = lastSpaceChar
return
}
func parseDoctype(input string, n *node, line int) (output inode) {
output = n
n._name = "doctype"
if len(input) > 0 {
output = parseRemainder(input, n, line)
}
return
}
func parseKey(input string, n *node, line int) (output inode) {
if input[len(input)-1] == '<' {
n = parseNoNewline("", n, line)
n.setRemainder(input[0:len(input)-1], true)
} else {
n.setRemainder(input, true)
output = n
}
output = n
return
}
func parseTag(input string, node *node, newTag bool, line int) (output inode, err error) {
if 0 == len(input) && newTag {
err = errors.New(fmt.Sprintf("Syntax error on line %d: Invalid tag: %s.\n", line, input))
return
}
for i, r := range input {
switch {
case r == '.':
output, err = parseClass(input[i+1:], node, line)
case r == '#':
output, err = parseId(input[i+1:], node, line)
case r == '{':
output, err = parseAttributes(tl(input[i+1:]), node, line)
case r == '<':
output = parseNoNewline(input[i+1:], node, line)
case r == '=':
output = parseKey(tl(input[i+1:]), node, line)
case r == '/':
output = parseAutoclose("", node, line)
case unicode.IsSpace(r):
output = parseRemainder(input[i+1:], node, line)
}
if nil != err {
break
}
if nil != output {
node._name = input[0:i]
break
}
}
if nil == output {
node._name = input
output = node
}
return
}
func parseAutoclose(input string, node *node, line int) (output inode) {
node._autoclose = true
output = node
return
}
func parseAttributes(input string, node *node, line int) (output inode, err error) {
inKey := true
inRocket := false
keyEnd, attrStart := 0, 0
for i, r := range input {
if inKey && (r == '=' || unicode.IsSpace(r)) {
inKey = false
inRocket = true
keyEnd = i
} else if inRocket && r != '>' && r != '=' && r != '}' && !unicode.IsSpace(r) {
inRocket = false
attrStart = i
} else if r == ',' {
node.addAttr(t(input[0:keyEnd]), t(input[attrStart:i]))
output, err = parseAttributes(tl(input[i+1:]), node, line)
break
} else if r == '}' {
if attrStart == 0 {
msg := fmt.Sprintf("Syntax error on line %d: Attribute requires a value.\n", line)
//err = os.NewError(msg)
err = errors.New(msg)
return
}
if inKey {
msg := fmt.Sprintf("Syntax error on line %d: Attribute requires a rocket and value.\n", line)
err = errors.New(msg)
return
}
attrValue := t(input[attrStart:i])
node.addAttr(input[0:keyEnd], attrValue)
output, _ = parseTag(input[i+1:], node, false, line)
break
}
}
if nil == output {
msg := fmt.Sprintf("Syntax error on line %d: Attributes must have closing '}'.\n", line)
err = errors.New(msg)
}
return
}
func parseId(input string, node *node, line int) (output inode, err error) {
defer func() {
if nil == output {
msg := fmt.Sprintf("Syntax error on line %d: Illegal element: classes and ids must have values.\n", line)
err = errors.New(msg)
}
}()
if len(input) == 0 {
return
}
for i, r := range input {
if r == '.' || r == '=' || r == '{' || unicode.IsSpace(r) {
if i == 0 {
return
}
node.addAttrNoLookup("id", input[0:i])
}
switch {
case r == '.':
output, _ = parseClass(input[i+1:], node, line)
case r == '=':
output = parseKey(tl(input[i+1:]), node, line)
case r == '{':
output, err = parseAttributes(tl(input[i+1:]), node, line)
case unicode.IsSpace(r):
output = parseRemainder(input[i+1:], node, line)
}
if nil != output {
break
}
}
if nil == output {
output = node
node.addAttrNoLookup("id", input)
}
return
}
func parseClass(input string, node *node, line int) (output inode, err error) {
defer func() {
if nil == output {
msg := fmt.Sprintf("Syntax error on line %d: Illegal element: classes and ids must have values.\n", line)
err = errors.New(msg)
}
}()
if len(input) == 0 {
return
}
for i, r := range input {
if r == '{' || r == '.' || r == '=' || unicode.IsSpace(r) {
if i == 0 {
return
}
node.addAttrNoLookup("class", input[0:i])
}
switch {
case r == '{':
output, err = parseAttributes(tl(input[i+1:]), node, line)
case r == '.':
output, err = parseClass(input[i+1:], node, line)
case r == '=':
output = parseKey(tl(input[i+1:]), node, line)
case unicode.IsSpace(r):
output = parseRemainder(input[i+1:], node, line)
}
if nil != output {
break
}
}
if nil == output {
node.addAttrNoLookup("class", input)
output = node
}
return
}
func parseRemainder(input string, node *node, line int) (output inode) {
if input[len(input)-1] == '<' {
node = parseNoNewline("", node, line)
node._remainder.value = input[0 : len(input)-1]
node._remainder.needsResolution = false
} else {
node._remainder.value = input
node._remainder.needsResolution = false
}
output = node
return
}
func parseNoNewline(input string, node *node, line int) (output *node) {
node.setNoNewline(true)
output = node
return
}
func t(input string) (output string) {
output = strings.Trim(input, " ")
return
}
func tl(input string) (output string) {
output = strings.TrimLeft(input, " ")
return
}
func parseCode(input string, node inode, line int) (output inode) {
l.init(strings.NewReader(input))
success := yyParse(l)
if success != 0 {
fmt.Fprintf(os.Stderr, "Did not recognize %s", input)
}
output = Output
return
}
// var s scanner.Scanner
type Lexer struct {
s *scanner.Scanner
}
var l = &Lexer{new(scanner.Scanner)}
func (l *Lexer) init(reader *strings.Reader) {
l.s.Init(reader)
}
func (l *Lexer) Lex(v *yySymType) (output int) {
i := l.s.Scan()
switch i {
case scanner.Ident:
switch l.s.TokenText() {
case "for":
output = FOR
case "range":
output = RANGE
default:
output = IDENT
}
v.s = l.s.TokenText()
case scanner.String, scanner.RawString:
output = ATOM
text := l.s.TokenText()
v.i = text[1 : len(text)-1]
case scanner.Int:
output = ATOM
v.i, _ = strconv.Atoi(l.s.TokenText())
case scanner.Float:
output = ATOM
v.i, _ = strconv.ParseFloat(l.s.TokenText(), 64)
case scanner.EOF:
output = 0
default:
output = int(i)
}
return
}
func (l *Lexer) Error(e string) {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", e)
}