-
Notifications
You must be signed in to change notification settings - Fork 6
/
dom_parser.go
210 lines (175 loc) · 4.42 KB
/
dom_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
package gherkin
import (
. "github.com/muhqu/go-gherkin/events"
. "github.com/muhqu/go-gherkin/nodes"
)
type GherkinDOM interface {
Feature() FeatureNode
}
type GherkinDOMParser interface {
GherkinParser
GherkinDOM
ParseDOM() (GherkinDOM, error)
ParseFeature() (FeatureNode, error)
}
type gherkinDOMParser struct {
gp GherkinParser
processed bool
parseErr error
feature MutableFeatureNode
scenario MutableScenarioNode
background MutableBackgroundNode
outline MutableOutlineNode
examples MutableOutlineExamplesNode
step MutableStepNode
pyStringIndent int
pyString MutablePyStringNode
table MutableTableNode
comment CommentNode
}
func NewGherkinDOMParser(content string) GherkinDOMParser {
g := &gherkinDOMParser{gp: NewGherkinParser(content)}
g.gp.WithEventProcessor(g)
return g
}
func (g *gherkinDOMParser) WithEventProcessor(ep EventProcessor) {
g.gp.WithEventProcessor(ep)
}
func (g *gherkinDOMParser) WithLogFn(logFn LogFn) {
g.gp.WithLogFn(logFn)
}
func (g *gherkinDOMParser) Init() {
g.gp.Init()
}
func (g *gherkinDOMParser) Parse() error {
return g.gp.Parse()
}
func (g *gherkinDOMParser) Execute() {
g.gp.Parse()
}
func ParseGherkinFeature(content string) (FeatureNode, error) {
return NewGherkinDOMParser(content).ParseFeature()
}
func (g *gherkinDOMParser) Feature() FeatureNode {
if !g.processed {
_, err := g.ParseFeature()
if err != nil {
return nil
}
}
return g.feature
}
func (g *gherkinDOMParser) ParseDOM() (GherkinDOM, error) {
g.processed = true
g.gp.Init()
if err := g.gp.Parse(); err != nil {
g.parseErr = err
return nil, err
}
g.gp.Execute()
return g, nil
}
func (g *gherkinDOMParser) ParseFeature() (FeatureNode, error) {
dom, err := g.ParseDOM()
if err != nil {
return nil, err
}
return dom.Feature(), nil
}
func (g *gherkinDOMParser) ProcessEvent(event GherkinEvent) {
switch e := event.(type) {
// default:
// fmt.Printf("Unexpected Event %T\n", e)
case *FeatureEvent:
g.feature = NewMutableFeatureNode(e.Title, e.Description, e.Tags)
g.feature.SetComment(g.comment)
g.comment = nil
// case *FeatureEndEvent:
// // do nothing
case *BackgroundEvent:
node := NewMutableBackgroundNode(e.Title, e.Tags)
node.SetDescription(e.Description)
g.scenario = node
g.feature.SetBackground(node)
node.SetComment(g.comment)
g.comment = nil
case *ScenarioEvent:
node := NewMutableScenarioNode(e.Title, e.Tags)
node.SetDescription(e.Description)
g.scenario = node
g.feature.AddScenario(node)
node.SetComment(g.comment)
g.comment = nil
case *OutlineEvent:
node := NewMutableOutlineNode(e.Title, e.Tags)
node.SetDescription(e.Description)
g.scenario = node
g.outline = node
g.feature.AddScenario(node)
node.SetComment(g.comment)
g.comment = nil
case *OutlineExamplesEvent:
g.table = nil
node := NewMutableOutlineExamplesNode(e.Title)
g.examples = node
node.SetComment(g.comment)
g.comment = nil
case *OutlineExamplesEndEvent:
g.examples.SetTable(g.table)
g.outline.AddExamples(g.examples)
g.examples = nil
g.table = nil
g.comment = nil
case *BackgroundEndEvent, *ScenarioEndEvent, *OutlineEndEvent:
g.scenario = nil
g.outline = nil
g.table = nil
g.pyString = nil
g.comment = nil
case *StepEvent:
g.step = NewMutableStepNode(e.StepType, e.Text)
g.scenario.AddStep(g.step)
g.step.SetComment(g.comment)
g.comment = nil
case *StepEndEvent:
if g.pyString != nil {
g.step.WithPyString(g.pyString)
} else if g.table != nil {
g.step.WithTable(g.table)
}
g.pyString = nil
g.table = nil
g.step = nil
g.comment = nil
case *TableEvent:
g.table = NewMutableTableNode()
case *TableRowEvent:
g.table.NewRow()
case *TableRowEndEvent:
g.table.SetRowComment(g.comment)
g.comment = nil
case *TableCellEvent:
g.table.AddCell(e.Content)
// case *TableEndEvent:
// // do nothing
case *PyStringEvent:
g.pyStringIndent = len(e.Intent)
g.pyString = NewMutablePyStringNode()
case *PyStringLineEvent:
indent := g.pyStringIndent
prefix, suffix := e.Line[:indent], e.Line[indent:]
line := trimLeadingWS(prefix) + suffix
g.pyString.AddLine(line)
// case *PyStringEndEvent:
// // do nothing
case *BlankLineEvent:
node := NewBlankLineNode()
node.SetComment(g.comment)
if g.scenario != nil {
g.scenario.AddBlankLine(node)
}
g.comment = nil
case *CommentEvent:
g.comment = NewCommentNode(e.Comment)
}
}