forked from gucumber/gucumber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
353 lines (310 loc) · 7.17 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
package gherkin
import (
"fmt"
"path/filepath"
"regexp"
"strings"
)
var reNL = regexp.MustCompile(`\r?\n`)
func ParseFilename(data, filename string) ([]Feature, error) {
lines := reNL.Split(data, -1)
p := parser{
lines: lines,
features: []Feature{},
translations: Translations[LANG_EN],
filename: filename,
}
if err := p.parse(); err != nil {
return nil, err
}
if len(p.features) == 0 {
return p.features, p.err("no features parsed")
}
return p.features, nil
}
func Parse(data string) ([]Feature, error) {
return ParseFilename(data, "")
}
type parser struct {
translations Translation
features []Feature
lines []string
lineNo int
lastLine int
started bool
filename string
}
func (p parser) err(msg string, args ...interface{}) error {
l := p.lineNo + 1
if l > len(p.lines) {
l = len(p.lines)
}
if p.filename == "" {
p.filename = "<unknown>.feature"
}
return fmt.Errorf("parse error (%s:%d): %s.",
filepath.Base(p.filename), l, fmt.Sprintf(msg, args...))
}
func (p *parser) line() string {
if p.lineNo < len(p.lines) {
return p.lines[p.lineNo]
}
return ""
}
func (p *parser) nextLine() bool {
p.lastLine = p.lineNo
if p.started {
p.lineNo++
}
p.started = true
for ; p.lineNo < len(p.lines); p.lineNo++ {
line, _ := p.lineStripped()
if line != "" && !strings.HasPrefix(line, "#") {
break
}
}
return p.stillReading()
}
func (p *parser) stillReading() bool {
return p.lineNo < len(p.lines)
}
func (p *parser) unread() {
if p.lineNo == 0 {
p.started = false
}
p.lineNo = p.lastLine
}
func (p *parser) parse() error {
for p.stillReading() {
if err := p.consumeFeature(); err != nil {
return err
}
}
return nil
}
func (p *parser) lineStripped() (string, int) {
line := p.line()
for i := 0; i < len(line); i++ {
c := line[i : i+1]
if c != " " && c != "\t" {
return line[i:], i
}
}
return line, 0
}
func (p *parser) consumeFeature() error {
desc := []string{}
tags, err := p.consumeTags()
if err != nil {
return err
}
if !p.nextLine() {
if len(tags) > 0 {
return p.err("tags not applied to feature")
}
return nil
}
line, startIndent := p.lineStripped()
parts := strings.SplitN(line, " ", 2)
prefix := p.translations.Feature + ":"
title := ""
if parts[0] != prefix {
return p.err("expected %q, found %q", prefix, line)
}
if len(parts) > 1 {
title = parts[1]
}
f := Feature{
Title: title, Tags: tags, Scenarios: []Scenario{},
Filename: p.filename, Line: p.lineNo,
}
var stags *[]string
seenScenario, seenBackground := false, false
for p.stillReading() { // consume until we get to Background or Scenario
// find indentation of next line
if p.nextLine() {
_, indent := p.lineStripped()
p.unread()
if indent <= startIndent { // de-dented
break // done parsing this feature
}
}
prevLine := p.lineNo
tags, err = p.consumeTags()
if err != nil {
return err
}
if p.lineNo != prevLine { // tags found
stags = &tags
}
if !p.nextLine() {
break
}
line, _ := p.lineStripped()
parts := strings.SplitN(line, ":", 2)
switch parts[0] {
case p.translations.Background:
if seenScenario {
return p.err("illegal background after scenario")
} else if seenBackground {
return p.err("multiple backgrounds not allowed")
}
seenBackground = true
b := Scenario{Filename: p.filename, Line: p.lineNo}
err = p.consumeScenario(&b)
if err != nil {
return err
}
if stags != nil {
b.Tags = *stags
} else {
b.Tags = []string{}
}
f.Background = b
stags = nil
case p.translations.Scenario, p.translations.Outline:
seenScenario = true
s := Scenario{Filename: p.filename, Line: p.lineNo}
err = p.consumeScenario(&s)
if err != nil {
return err
}
if stags != nil {
s.Tags = *stags
} else {
s.Tags = []string{}
}
if len(parts) > 1 {
s.Title = strings.TrimSpace(parts[1])
}
f.Scenarios = append(f.Scenarios, s)
stags = nil
default: // then this must be a description
if stags != nil {
return p.err("illegal description text after tags")
} else if seenScenario || seenBackground {
return p.err("illegal description text after scenario")
}
desc = append(desc, line)
}
}
f.Description = strings.Join(desc, "\n")
p.features = append(p.features, f)
return nil
}
func (p *parser) consumeScenario(scenario *Scenario) error {
scenario.Steps = []Step{}
_, startIndent := p.lineStripped()
for p.nextLine() { // consume all steps
_, indent := p.lineStripped()
if indent <= startIndent { // de-dented
p.unread()
break
}
if err := p.consumeStep(scenario); err != nil {
return err
}
}
return nil
}
func (p *parser) consumeStep(scenario *Scenario) error {
line, indent := p.lineStripped()
parts := strings.SplitN(line, " ", 2)
switch parts[0] {
case p.translations.Given, p.translations.Then,
p.translations.When, p.translations.And:
var arg StringData
if len(parts) < 2 {
return p.err("expected step text after %q", parts[0])
}
if p.nextLine() {
l, _ := p.lineStripped()
p.unread()
if len(l) > 0 && (l[0] == '|' || l == `"""`) {
// this is step argument data
arg = p.consumeIndentedData(indent)
}
}
var stype StepType
switch parts[0] {
case p.translations.Given:
stype = StepType("Given")
case p.translations.When:
stype = StepType("When")
case p.translations.Then:
stype = StepType("Then")
case p.translations.And:
stype = StepType("And")
}
s := Step{
Filename: p.filename, Line: p.lineNo,
Type: stype, Text: parts[1], Argument: arg,
}
scenario.Steps = append(scenario.Steps, s)
case p.translations.Examples + ":":
scenario.Examples = p.consumeIndentedData(indent)
default:
return p.err("illegal step prefix %q", parts[0])
}
return nil
}
func (p *parser) consumeIndentedData(scenarioIndent int) StringData {
stringData := []string{}
startIndent, quoted := -1, false
for p.nextLine() {
var line string
var indent int
if startIndent == -1 { // first line
line, indent = p.lineStripped()
startIndent = indent
if line == `"""` {
quoted = true // this is a docstring data block
continue // ignore this from data
}
} else {
line = p.line()
if len(line) <= startIndent {
// not enough text, not part of indented data
p.unread()
break
}
if line[0:startIndent] != strings.Repeat(" ", startIndent) {
// not enough indentation, not part of indented data
p.unread()
break
}
line = line[startIndent:]
if !quoted && line[0] != '|' {
// tabular data must start with | on each line
p.unread()
break
}
if quoted && line == `"""` { // end quote on docstring block
break
}
}
stringData = append(stringData, line)
}
return StringData(strings.Join(stringData, "\n"))
}
func (p *parser) consumeTags() ([]string, error) {
tags := []string{}
if !p.nextLine() {
return tags, nil
}
line, _ := p.lineStripped()
if len(p.lines) == 0 || !strings.HasPrefix(line, "@") {
p.unread()
return tags, nil
}
for _, t := range strings.Split(line, " ") {
if t == "" {
continue
}
if t[0:1] != "@" {
return nil, p.err("invalid tag %q", t)
}
tags = append(tags, t)
}
return tags, nil
}