-
Notifications
You must be signed in to change notification settings - Fork 0
/
astbuilder.go
443 lines (393 loc) · 12.1 KB
/
astbuilder.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package gherkin
import (
"strings"
"github.com/acst/gherkin/messages"
)
type AstBuilder interface {
Builder
GetGherkinDocument() *messages.GherkinDocument
}
type astBuilder struct {
stack []*astNode
comments []*messages.GherkinDocument_Comment
}
func (t *astBuilder) Reset() {
t.comments = []*messages.GherkinDocument_Comment{}
t.stack = []*astNode{}
t.push(newAstNode(RuleTypeNone))
}
func (t *astBuilder) GetGherkinDocument() *messages.GherkinDocument {
res := t.currentNode().getSingle(RuleTypeGherkinDocument)
if val, ok := res.(*messages.GherkinDocument); ok {
return val
}
return nil
}
type astNode struct {
ruleType RuleType
subNodes map[RuleType][]interface{}
}
func (a *astNode) add(rt RuleType, obj interface{}) {
a.subNodes[rt] = append(a.subNodes[rt], obj)
}
func (a *astNode) getSingle(rt RuleType) interface{} {
if val, ok := a.subNodes[rt]; ok {
for i := range val {
return val[i]
}
}
return nil
}
func (a *astNode) getItems(rt RuleType) []interface{} {
var res []interface{}
if val, ok := a.subNodes[rt]; ok {
for i := range val {
res = append(res, val[i])
}
}
return res
}
func (a *astNode) getToken(tt TokenType) *Token {
if val, ok := a.getSingle(tt.RuleType()).(*Token); ok {
return val
}
return nil
}
func (a *astNode) getTokens(tt TokenType) []*Token {
var items = a.getItems(tt.RuleType())
var tokens []*Token
for i := range items {
if val, ok := items[i].(*Token); ok {
tokens = append(tokens, val)
}
}
return tokens
}
func (t *astBuilder) currentNode() *astNode {
if len(t.stack) > 0 {
return t.stack[len(t.stack)-1]
}
return nil
}
func newAstNode(rt RuleType) *astNode {
return &astNode{
ruleType: rt,
subNodes: make(map[RuleType][]interface{}),
}
}
func NewAstBuilder() AstBuilder {
builder := new(astBuilder)
builder.comments = []*messages.GherkinDocument_Comment{}
builder.push(newAstNode(RuleTypeNone))
return builder
}
func (t *astBuilder) push(n *astNode) {
t.stack = append(t.stack, n)
}
func (t *astBuilder) pop() *astNode {
x := t.stack[len(t.stack)-1]
t.stack = t.stack[:len(t.stack)-1]
return x
}
func (t *astBuilder) Build(tok *Token) (bool, error) {
if tok.Type == TokenTypeComment {
comment := &messages.GherkinDocument_Comment{
Location: astLocation(tok),
Text: tok.Text,
}
t.comments = append(t.comments, comment)
} else {
t.currentNode().add(tok.Type.RuleType(), tok)
}
return true, nil
}
func (t *astBuilder) StartRule(r RuleType) (bool, error) {
t.push(newAstNode(r))
return true, nil
}
func (t *astBuilder) EndRule(r RuleType) (bool, error) {
node := t.pop()
transformedNode, err := t.transformNode(node)
t.currentNode().add(node.ruleType, transformedNode)
return true, err
}
func (t *astBuilder) transformNode(node *astNode) (interface{}, error) {
switch node.ruleType {
case RuleTypeStep:
stepLine := node.getToken(TokenTypeStepLine)
step := &messages.GherkinDocument_Feature_Step{
Location: astLocation(stepLine),
Keyword: stepLine.Keyword,
Text: stepLine.Text,
}
dataTable := node.getSingle(RuleTypeDataTable)
if dataTable != nil {
step.Argument = &messages.GherkinDocument_Feature_Step_DataTable_{
DataTable: dataTable.(*messages.GherkinDocument_Feature_Step_DataTable),
}
} else {
docString := node.getSingle(RuleTypeDocString)
if docString != nil {
step.Argument = &messages.GherkinDocument_Feature_Step_DocString_{DocString: docString.(*messages.GherkinDocument_Feature_Step_DocString)}
}
}
return step, nil
case RuleTypeDocString:
separatorToken := node.getToken(TokenTypeDocStringSeparator)
lineTokens := node.getTokens(TokenTypeOther)
var text string
for i := range lineTokens {
if i > 0 {
text += "\n"
}
text += lineTokens[i].Text
}
ds := &messages.GherkinDocument_Feature_Step_DocString{
Location: astLocation(separatorToken),
ContentType: separatorToken.Text,
Content: text,
Delimiter: separatorToken.Keyword,
}
return ds, nil
case RuleTypeDataTable:
rows, err := astTableRows(node)
dt := &messages.GherkinDocument_Feature_Step_DataTable{
Location: rows[0].Location,
Rows: rows,
}
return dt, err
case RuleTypeBackground:
backgroundLine := node.getToken(TokenTypeBackgroundLine)
description, _ := node.getSingle(RuleTypeDescription).(string)
bg := &messages.GherkinDocument_Feature_Background{
Location: astLocation(backgroundLine),
Keyword: backgroundLine.Keyword,
Name: backgroundLine.Text,
Description: description,
Steps: astSteps(node),
}
return bg, nil
case RuleTypeScenarioDefinition:
tags := astTags(node)
scenarioNode, _ := node.getSingle(RuleTypeScenario).(*astNode)
scenarioLine := scenarioNode.getToken(TokenTypeScenarioLine)
description, _ := scenarioNode.getSingle(RuleTypeDescription).(string)
sc := &messages.GherkinDocument_Feature_Scenario{
Tags: tags,
Location: astLocation(scenarioLine),
Keyword: scenarioLine.Keyword,
Name: scenarioLine.Text,
Description: description,
Steps: astSteps(scenarioNode),
Examples: astExamples(scenarioNode),
}
return sc, nil
case RuleTypeExamplesDefinition:
tags := astTags(node)
examplesNode, _ := node.getSingle(RuleTypeExamples).(*astNode)
examplesLine := examplesNode.getToken(TokenTypeExamplesLine)
description, _ := examplesNode.getSingle(RuleTypeDescription).(string)
examplesTable := examplesNode.getSingle(RuleTypeExamplesTable)
// TODO: Is this mutation style ok?
ex := &messages.GherkinDocument_Feature_Scenario_Examples{}
ex.Tags = tags
ex.Location = astLocation(examplesLine)
ex.Keyword = examplesLine.Keyword
ex.Name = examplesLine.Text
ex.Description = description
ex.TableHeader = nil
ex.TableBody = nil
if examplesTable != nil {
allRows, _ := examplesTable.([]*messages.GherkinDocument_Feature_TableRow)
ex.TableHeader = allRows[0]
ex.TableBody = allRows[1:]
}
return ex, nil
case RuleTypeExamplesTable:
allRows, err := astTableRows(node)
return allRows, err
case RuleTypeDescription:
lineTokens := node.getTokens(TokenTypeOther)
// Trim trailing empty lines
end := len(lineTokens)
for end > 0 && strings.TrimSpace(lineTokens[end-1].Text) == "" {
end--
}
var desc []string
for i := range lineTokens[0:end] {
desc = append(desc, lineTokens[i].Text)
}
return strings.Join(desc, "\n"), nil
case RuleTypeFeature:
header, ok := node.getSingle(RuleTypeFeatureHeader).(*astNode)
if !ok {
return nil, nil
}
tags := astTags(header)
featureLine := header.getToken(TokenTypeFeatureLine)
if featureLine == nil {
return nil, nil
}
var children []*messages.GherkinDocument_Feature_FeatureChild
background, _ := node.getSingle(RuleTypeBackground).(*messages.GherkinDocument_Feature_Background)
if background != nil {
children = append(children, &messages.GherkinDocument_Feature_FeatureChild{
Value: &messages.GherkinDocument_Feature_FeatureChild_Background{Background: background},
})
}
scenarios := node.getItems(RuleTypeScenarioDefinition)
for i := range scenarios {
scenario := scenarios[i].(*messages.GherkinDocument_Feature_Scenario)
children = append(children, &messages.GherkinDocument_Feature_FeatureChild{
Value: &messages.GherkinDocument_Feature_FeatureChild_Scenario{Scenario: scenario},
})
}
rules := node.getItems(RuleTypeRule)
for i := range rules {
rule := rules[i].(*messages.GherkinDocument_Feature_FeatureChild_Rule)
children = append(children, &messages.GherkinDocument_Feature_FeatureChild{
Value: &messages.GherkinDocument_Feature_FeatureChild_Rule_{
Rule: rule,
},
})
}
description, _ := header.getSingle(RuleTypeDescription).(string)
feat := &messages.GherkinDocument_Feature{}
feat.Tags = tags
feat.Location = astLocation(featureLine)
feat.Language = featureLine.GherkinDialect
feat.Keyword = featureLine.Keyword
feat.Name = featureLine.Text
feat.Description = description
feat.Children = children
return feat, nil
case RuleTypeRule:
header, ok := node.getSingle(RuleTypeRuleHeader).(*astNode)
if !ok {
return nil, nil
}
ruleLine := header.getToken(TokenTypeRuleLine)
if ruleLine == nil {
return nil, nil
}
var children []*messages.GherkinDocument_Feature_FeatureChild_RuleChild
background, _ := node.getSingle(RuleTypeBackground).(*messages.GherkinDocument_Feature_Background)
if background != nil {
children = append(children, &messages.GherkinDocument_Feature_FeatureChild_RuleChild{
Value: &messages.GherkinDocument_Feature_FeatureChild_RuleChild_Background{Background: background},
})
}
scenarios := node.getItems(RuleTypeScenarioDefinition)
for i := range scenarios {
scenario := scenarios[i].(*messages.GherkinDocument_Feature_Scenario)
children = append(children, &messages.GherkinDocument_Feature_FeatureChild_RuleChild{
Value: &messages.GherkinDocument_Feature_FeatureChild_RuleChild_Scenario{Scenario: scenario},
})
}
description, _ := header.getSingle(RuleTypeDescription).(string)
rule := &messages.GherkinDocument_Feature_FeatureChild_Rule{}
rule.Location = astLocation(ruleLine)
rule.Keyword = ruleLine.Keyword
rule.Name = ruleLine.Text
rule.Description = description
rule.Children = children
return rule, nil
case RuleTypeGherkinDocument:
feature, _ := node.getSingle(RuleTypeFeature).(*messages.GherkinDocument_Feature)
doc := &messages.GherkinDocument{}
if feature != nil {
doc.Feature = feature
}
doc.Comments = t.comments
return doc, nil
}
return node, nil
}
func astLocation(t *Token) *messages.Location {
return &messages.Location{
Line: uint32(t.Location.Line),
Column: uint32(t.Location.Column),
}
}
func astTableRows(t *astNode) (rows []*messages.GherkinDocument_Feature_TableRow, err error) {
rows = []*messages.GherkinDocument_Feature_TableRow{}
tokens := t.getTokens(TokenTypeTableRow)
for i := range tokens {
row := &messages.GherkinDocument_Feature_TableRow{
Location: astLocation(tokens[i]),
Cells: astTableCells(tokens[i]),
}
rows = append(rows, row)
}
err = ensureCellCount(rows)
return
}
func ensureCellCount(rows []*messages.GherkinDocument_Feature_TableRow) error {
if len(rows) <= 1 {
return nil
}
cellCount := len(rows[0].Cells)
for i := range rows {
if cellCount != len(rows[i].Cells) {
return &parseError{"inconsistent cell count within the table", &Location{
Line: int(rows[i].Location.Line),
Column: int(rows[i].Location.Column),
}}
}
}
return nil
}
func astTableCells(t *Token) (cells []*messages.GherkinDocument_Feature_TableRow_TableCell) {
cells = []*messages.GherkinDocument_Feature_TableRow_TableCell{}
for i := range t.Items {
item := t.Items[i]
cell := &messages.GherkinDocument_Feature_TableRow_TableCell{}
cell.Location = &messages.Location{
Line: uint32(t.Location.Line),
Column: uint32(item.Column),
}
cell.Value = item.Text
cells = append(cells, cell)
}
return
}
func astSteps(t *astNode) (steps []*messages.GherkinDocument_Feature_Step) {
steps = []*messages.GherkinDocument_Feature_Step{}
tokens := t.getItems(RuleTypeStep)
for i := range tokens {
step, _ := tokens[i].(*messages.GherkinDocument_Feature_Step)
steps = append(steps, step)
}
return
}
func astExamples(t *astNode) (examples []*messages.GherkinDocument_Feature_Scenario_Examples) {
examples = []*messages.GherkinDocument_Feature_Scenario_Examples{}
tokens := t.getItems(RuleTypeExamplesDefinition)
for i := range tokens {
example, _ := tokens[i].(*messages.GherkinDocument_Feature_Scenario_Examples)
examples = append(examples, example)
}
return
}
func astTags(node *astNode) (tags []*messages.GherkinDocument_Feature_Tag) {
tags = []*messages.GherkinDocument_Feature_Tag{}
tagsNode, ok := node.getSingle(RuleTypeTags).(*astNode)
if !ok {
return
}
tokens := tagsNode.getTokens(TokenTypeTagLine)
for i := range tokens {
token := tokens[i]
for k := range token.Items {
item := token.Items[k]
tag := &messages.GherkinDocument_Feature_Tag{}
tag.Location = &messages.Location{
Line: uint32(token.Location.Line),
Column: uint32(item.Column),
}
tag.Name = item.Text
tags = append(tags, tag)
}
}
return
}