-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
320 lines (290 loc) · 6.5 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
package main
import (
"fmt"
"strings"
)
type Parser struct {
debug bool
Scanner Scanner
output string
// node heirarchy stack
stack []*Node
// edge in stack
node *Node
// states as the Parser object figures out how to output the tokens
// new line has started
isNewLine bool
isIndent bool
isDedent bool
// flag indicates we are still checking for attribute assignments
isAttr bool
isStringFlag bool
// attrName and attrValue are buffers to determine if TokWord for a tag are
// potentially for an attribute, or if they are just normal text
attrName string
attrValue string
attrAssigned bool
}
func (p *Parser) Output() string {
var tokens []rune
var text string
p.node = p.newNode()
p.node.Type = NodeRoot
p.stack = make([]*Node, 0)
p.stack = append(p.stack, p.node)
p.output = ""
p.isNewLine = true
p.isIndent = false
p.isDedent = false
p.isAttr = false
p.attrName = ""
p.attrValue = ""
p.attrAssigned = false
for {
tokens = p.Scanner.Scan()
for _, token := range tokens {
text = p.Scanner.TokenText()
if p.debug {
fmt.Printf("[debug] token: [%s]\n", TokenString(token))
fmt.Printf("[debug] text: [%s]\n", text)
}
p.processToken(token, text)
if p.debug {
fmt.Printf("[debug] node: [%s]\n\n", p.node.Debug())
}
}
if tokens[0] == TokEOF {
break
}
}
return p.output
}
func (p *Parser) trimOutput() {
p.output = strings.TrimSpace(p.output)
}
func (p *Parser) newNode() *Node {
n := new(Node)
n.attrs = make(map[string]string)
return n
}
func (p *Parser) lastNode() *Node {
if len(p.stack) > 0 {
return p.stack[len(p.stack) - 1]
} else {
return nil
}
}
func (p *Parser) popNode() *Node {
n := p.lastNode()
if n != nil {
p.stack = p.stack[0:len(p.stack)-1]
}
return n
}
func (p *Parser) pushNode(n *Node) {
ln := p.lastNode()
n.parent = ln
p.stack = append(p.stack, n)
}
func (p *Parser) dedentFromStack() {
n := p.lastNode()
if n != nil && n.Type != NodeRoot {
p.output += n.OpenString()
p.trimOutput()
// newlines in NodeText have spaces. Adding one here for consistency
if n.Type == NodeText {
p.output += " "
}
p.output += n.CloseString()
p.popNode()
}
}
func (p *Parser) processToken(tok rune, text string) {
// indent/dedent/nodent is the first place to look to see if new nodes need
// to be made. checks are for parent node being certain types like NodeText
// or NodeComment
switch tok {
case TokIndent:
p.isIndent = true
p.isDedent = false
if p.node.Type != NodeText {
p.output += p.node.OpenString()
p.node = p.newNode()
p.pushNode(p.node)
}
case TokDedent:
p.isIndent = false
p.isDedent = true
p.dedentFromStack()
if p.node.Type != NodeText {
p.dedentFromStack()
}
p.node = p.newNode()
p.pushNode(p.node)
case TokNodent:
p.isIndent = false
p.isDedent = false
switch p.node.Type {
case NodeRoot:
p.node = p.newNode()
p.pushNode(p.node)
case NodeText:
default:
// replace top of stack with new node (pop then push)
n := p.lastNode()
p.output += n.OpenString()
p.trimOutput()
p.output += n.CloseString()
p.popNode()
p.node = p.newNode()
p.pushNode(p.node)
}
}
if p.node.Type == NodeText && tok != TokDedent {
if tok != TokIndent {
if tok == TokNewLine {
p.node.text += " "
} else if tok != TokWhitespace || p.node.text != "" {
p.node.text += text
}
}
return
}
switch tok {
case TokWord, TokComma:
switch p.node.Type {
case NodeNil:
p.node.Type = NodeTag
p.node.tag = text
// found the tag, now check for attributes
p.isAttr = true
p.attrAssigned = false
case NodeTag:
if p.isAttr {
if p.attrAssigned {
if _, found := p.node.attrs[p.attrName]; found {
p.node.attrs[p.attrName] += " "
p.node.attrs[p.attrName] += text
} else {
p.node.attrs[p.attrName] = text
}
// reset to look for a new attribute assignment
p.attrName = ""
p.attrValue = ""
p.attrAssigned = false
p.node.attrString = ""
} else if p.attrName == "" {
p.node.attrString += text
p.attrName = text
} else {
p.node.text += p.node.attrString
p.node.text += text
p.node.attrString = ""
p.isAttr = false
p.attrAssigned = false
p.attrName = ""
p.attrValue = ""
}
} else {
p.node.text += text
}
}
case TokString:
switch p.node.Type {
case NodeTag:
if p.isAttr {
if p.attrAssigned {
if _, found := p.node.attrs[p.attrName]; found {
p.node.attrs[p.attrName] += " "
p.node.attrs[p.attrName] += text[1:len(text)-1]
} else {
p.node.attrs[p.attrName] = text[1:len(text)-1]
}
// reset to look for a new attribute assignment
p.attrName = ""
p.attrValue = ""
p.attrAssigned = false
p.node.attrString = ""
} else if p.attrName != "" {
// TODO using string in attrName shouldn't be allowed
p.node.text += p.node.attrString
p.node.text += text[1:len(text)-1]
p.node.attrString = ""
p.isAttr = false
p.attrName = ""
p.attrValue = ""
p.attrAssigned = false
}
} else {
p.node.text += text
}
case NodeText:
p.node.text += text
}
case TokAssign:
switch p.node.Type {
case NodeTag:
if p.isAttr {
if p.attrName != "" && p.attrValue == "" {
p.attrAssigned = true
p.node.attrString += text
}
if p.attrName == "" || p.attrValue != "" {
p.attrAssigned = false
p.isAttr = false
p.node.text += p.node.attrString
p.node.attrString = ""
}
} else {
p.node.text += text
}
case NodeText:
p.node.text += text
}
case TokWhitespace:
switch p.node.Type {
case NodeTag:
if p.isAttr && p.node.attrString != "" {
p.node.attrString += text
// skip initial whitespace for text
} else if p.node.text != "" {
p.node.text += text
}
}
case TokStringFlag:
switch p.node.Type {
case NodeNil:
// new node, but indent indicates continue with text of previous node
p.node.Type = NodeText
case NodeTag:
if p.isAttr {
p.node.text += p.node.attrString
p.node.attrString = ""
} else {
p.node.text += text
}
p.isAttr = false
}
case TokNewLine:
// reset
p.isIndent = false
p.isDedent = false
p.isAttr = false
p.attrName = ""
p.attrValue = ""
p.attrAssigned = false
case TokEOF:
// close the remaining nodes in the stack
for len(p.stack) > 0 {
lastNode := p.popNode()
p.trimOutput()
p.output += lastNode.CloseString()
}
// nop
case TokIndent, TokDedent, TokNodent:
default:
if !p.isAttr {
p.node.text += text
}
}
}