| 1 |
package gherkin |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"path/filepath" |
| 6 |
"regexp" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
var reNL = regexp.MustCompile(`\r?\n`) |
| 11 |
|
| 12 |
func ParseFilename(data, filename string) ([]Feature, error) { |
| 13 |
lines := reNL.Split(data, -1) |
| 14 |
p := parser{ |
| 15 |
lines: lines, |
| 16 |
features: []Feature{}, |
| 17 |
translations: Translations[LANG_EN], |
| 18 |
filename: filename, |
| 19 |
} |
| 20 |
if err := p.parse(); err != nil { |
| 21 |
return nil, err |
| 22 |
} |
| 23 |
if len(p.features) == 0 { |
| 24 |
return p.features, p.err("no features parsed") |
| 25 |
} |
| 26 |
return p.features, nil |
| 27 |
} |
| 28 |
|
| 29 |
func Parse(data string) ([]Feature, error) { |
| 30 |
return ParseFilename(data, "") |
| 31 |
} |
| 32 |
|
| 33 |
type parser struct { |
| 34 |
translations Translation |
| 35 |
features []Feature |
| 36 |
lines []string |
| 37 |
lineNo int |
| 38 |
lastLine int |
| 39 |
started bool |
| 40 |
filename string |
| 41 |
} |
| 42 |
|
| 43 |
func (p parser) err(msg string, args ...interface{}) error { |
| 44 |
l := p.lineNo + 1 |
| 45 |
if l > len(p.lines) { |
| 46 |
l = len(p.lines) |
| 47 |
} |
| 48 |
if p.filename == "" { |
| 49 |
p.filename = "<unknown>.feature" |
| 50 |
} |
| 51 |
return fmt.Errorf("parse error (%s:%d): %s.", |
| 52 |
filepath.Base(p.filename), l, fmt.Sprintf(msg, args...)) |
| 53 |
} |
| 54 |
|
| 55 |
func (p *parser) line() string { |
| 56 |
if p.lineNo < len(p.lines) { |
| 57 |
return p.lines[p.lineNo] |
| 58 |
} |
| 59 |
return "" |
| 60 |
} |
| 61 |
|
| 62 |
func (p *parser) nextLine() bool { |
| 63 |
p.lastLine = p.lineNo |
| 64 |
if p.started { |
| 65 |
p.lineNo++ |
| 66 |
} |
| 67 |
p.started = true |
| 68 |
|
| 69 |
for ; p.lineNo < len(p.lines); p.lineNo++ { |
| 70 |
line, _ := p.lineStripped() |
| 71 |
if line != "" && !strings.HasPrefix(line, "#") { |
| 72 |
break |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return p.stillReading() |
| 77 |
} |
| 78 |
|
| 79 |
func (p *parser) stillReading() bool { |
| 80 |
return p.lineNo < len(p.lines) |
| 81 |
} |
| 82 |
|
| 83 |
func (p *parser) unread() { |
| 84 |
if p.lineNo == 0 { |
| 85 |
p.started = false |
| 86 |
} |
| 87 |
p.lineNo = p.lastLine |
| 88 |
} |
| 89 |
|
| 90 |
func (p *parser) parse() error { |
| 91 |
for p.stillReading() { |
| 92 |
if err := p.consumeFeature(); err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
func (p *parser) lineStripped() (string, int) { |
| 101 |
line := p.line() |
| 102 |
for i := 0; i < len(line); i++ { |
| 103 |
c := line[i : i+1] |
| 104 |
if c != " " && c != "\t" { |
| 105 |
return line[i:], i |
| 106 |
} |
| 107 |
} |
| 108 |
return line, 0 |
| 109 |
} |
| 110 |
|
| 111 |
func (p *parser) consumeFeature() error { |
| 112 |
desc := []string{} |
| 113 |
tags, err := p.consumeTags() |
| 114 |
if err != nil { |
| 115 |
return err |
| 116 |
} |
| 117 |
|
| 118 |
if !p.nextLine() { |
| 119 |
if len(tags) > 0 { |
| 120 |
return p.err("tags not applied to feature") |
| 121 |
} |
| 122 |
return nil |
| 123 |
} |
| 124 |
|
| 125 |
line, startIndent := p.lineStripped() |
| 126 |
parts := strings.SplitN(line, " ", 2) |
| 127 |
prefix := p.translations.Feature + ":" |
| 128 |
title := "" |
| 129 |
|
| 130 |
if parts[0] != prefix { |
| 131 |
return p.err("expected %q, found %q", prefix, line) |
| 132 |
} |
| 133 |
|
| 134 |
if len(parts) > 1 { |
| 135 |
title = parts[1] |
| 136 |
} |
| 137 |
f := Feature{ |
| 138 |
Title: title, Tags: tags, Scenarios: []Scenario{}, |
| 139 |
Filename: p.filename, Line: p.lineNo, |
| 140 |
} |
| 141 |
var stags *[]string |
| 142 |
seenScenario, seenBackground := false, false |
| 143 |
for p.stillReading() { // consume until we get to Background or Scenario |
| 144 |
// find indentation of next line |
| 145 |
if p.nextLine() { |
| 146 |
_, indent := p.lineStripped() |
| 147 |
p.unread() |
| 148 |
if indent <= startIndent { // de-dented |
| 149 |
break // done parsing this feature |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
prevLine := p.lineNo |
| 154 |
tags, err = p.consumeTags() |
| 155 |
if err != nil { |
| 156 |
return err |
| 157 |
} |
| 158 |
if p.lineNo != prevLine { // tags found |
| 159 |
stags = &tags |
| 160 |
} |
| 161 |
|
| 162 |
if !p.nextLine() { |
| 163 |
break |
| 164 |
} |
| 165 |
|
| 166 |
line, _ := p.lineStripped() |
| 167 |
parts := strings.SplitN(line, ":", 2) |
| 168 |
|
| 169 |
switch parts[0] { |
| 170 |
case p.translations.Background: |
| 171 |
if seenScenario { |
| 172 |
return p.err("illegal background after scenario") |
| 173 |
} else if seenBackground { |
| 174 |
return p.err("multiple backgrounds not allowed") |
| 175 |
} |
| 176 |
seenBackground = true |
| 177 |
|
| 178 |
b := Scenario{Filename: p.filename, Line: p.lineNo} |
| 179 |
err = p.consumeScenario(&b) |
| 180 |
if err != nil { |
| 181 |
return err |
| 182 |
} |
| 183 |
if stags != nil { |
| 184 |
b.Tags = *stags |
| 185 |
} else { |
| 186 |
b.Tags = []string{} |
| 187 |
} |
| 188 |
f.Background = b |
| 189 |
stags = nil |
| 190 |
case p.translations.Scenario, p.translations.Outline: |
| 191 |
seenScenario = true |
| 192 |
|
| 193 |
s := Scenario{Filename: p.filename, Line: p.lineNo} |
| 194 |
err = p.consumeScenario(&s) |
| 195 |
if err != nil { |
| 196 |
return err |
| 197 |
} |
| 198 |
if stags != nil { |
| 199 |
s.Tags = *stags |
| 200 |
} else { |
| 201 |
s.Tags = []string{} |
| 202 |
} |
| 203 |
if len(parts) > 1 { |
| 204 |
s.Title = strings.TrimSpace(parts[1]) |
| 205 |
} |
| 206 |
f.Scenarios = append(f.Scenarios, s) |
| 207 |
stags = nil |
| 208 |
default: // then this must be a description |
| 209 |
if stags != nil { |
| 210 |
return p.err("illegal description text after tags") |
| 211 |
} else if seenScenario || seenBackground { |
| 212 |
return p.err("illegal description text after scenario") |
| 213 |
} |
| 214 |
desc = append(desc, line) |
| 215 |
} |
| 216 |
} |
| 217 |
f.Description = strings.Join(desc, "\n") |
| 218 |
p.features = append(p.features, f) |
| 219 |
|
| 220 |
return nil |
| 221 |
} |
| 222 |
|
| 223 |
func (p *parser) consumeScenario(scenario *Scenario) error { |
| 224 |
scenario.Steps = []Step{} |
| 225 |
_, startIndent := p.lineStripped() |
| 226 |
for p.nextLine() { // consume all steps |
| 227 |
_, indent := p.lineStripped() |
| 228 |
if indent <= startIndent { // de-dented |
| 229 |
p.unread() |
| 230 |
break |
| 231 |
} |
| 232 |
|
| 233 |
if err := p.consumeStep(scenario); err != nil { |
| 234 |
return err |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
return nil |
| 239 |
} |
| 240 |
|
| 241 |
func (p *parser) consumeStep(scenario *Scenario) error { |
| 242 |
line, indent := p.lineStripped() |
| 243 |
parts := strings.SplitN(line, " ", 2) |
| 244 |
|
| 245 |
switch parts[0] { |
| 246 |
case p.translations.Given, p.translations.Then, |
| 247 |
p.translations.When, p.translations.And: |
| 248 |
var arg StringData |
| 249 |
if len(parts) < 2 { |
| 250 |
return p.err("expected step text after %q", parts[0]) |
| 251 |
} |
| 252 |
if p.nextLine() { |
| 253 |
l, _ := p.lineStripped() |
| 254 |
p.unread() |
| 255 |
if len(l) > 0 && (l[0] == '|' || l == `"""`) { |
| 256 |
// this is step argument data |
| 257 |
arg = p.consumeIndentedData(indent) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
var stype StepType |
| 262 |
switch parts[0] { |
| 263 |
case p.translations.Given: |
| 264 |
stype = StepType("Given") |
| 265 |
case p.translations.When: |
| 266 |
stype = StepType("When") |
| 267 |
case p.translations.Then: |
| 268 |
stype = StepType("Then") |
| 269 |
case p.translations.And: |
| 270 |
stype = StepType("And") |
| 271 |
} |
| 272 |
s := Step{ |
| 273 |
Filename: p.filename, Line: p.lineNo, |
| 274 |
Type: stype, Text: parts[1], Argument: arg, |
| 275 |
} |
| 276 |
scenario.Steps = append(scenario.Steps, s) |
| 277 |
case p.translations.Examples + ":": |
| 278 |
scenario.Examples = p.consumeIndentedData(indent) |
| 279 |
default: |
| 280 |
return p.err("illegal step prefix %q", parts[0]) |
| 281 |
} |
| 282 |
return nil |
| 283 |
} |
| 284 |
|
| 285 |
func (p *parser) consumeIndentedData(scenarioIndent int) StringData { |
| 286 |
stringData := []string{} |
| 287 |
startIndent, quoted := -1, false |
| 288 |
for p.nextLine() { |
| 289 |
var line string |
| 290 |
var indent int |
| 291 |
if startIndent == -1 { // first line |
| 292 |
line, indent = p.lineStripped() |
| 293 |
startIndent = indent |
| 294 |
|
| 295 |
if line == `"""` { |
| 296 |
quoted = true // this is a docstring data block |
| 297 |
continue // ignore this from data |
| 298 |
} |
| 299 |
} else { |
| 300 |
line = p.line() |
| 301 |
if len(line) <= startIndent { |
| 302 |
// not enough text, not part of indented data |
| 303 |
p.unread() |
| 304 |
break |
| 305 |
} |
| 306 |
if line[0:startIndent] != strings.Repeat(" ", startIndent) { |
| 307 |
// not enough indentation, not part of indented data |
| 308 |
p.unread() |
| 309 |
break |
| 310 |
} |
| 311 |
|
| 312 |
line = line[startIndent:] |
| 313 |
if !quoted && line[0] != '|' { |
| 314 |
// tabular data must start with | on each line |
| 315 |
p.unread() |
| 316 |
break |
| 317 |
} |
| 318 |
if quoted && line == `"""` { // end quote on docstring block |
| 319 |
break |
| 320 |
} |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
stringData = append(stringData, line) |
| 325 |
} |
| 326 |
|
| 327 |
return StringData(strings.Join(stringData, "\n")) |
| 328 |
} |
| 329 |
|
| 330 |
func (p *parser) consumeTags() ([]string, error) { |
| 331 |
tags := []string{} |
| 332 |
if !p.nextLine() { |
| 333 |
return tags, nil |
| 334 |
} |
| 335 |
|
| 336 |
line, _ := p.lineStripped() |
| 337 |
if len(p.lines) == 0 || !strings.HasPrefix(line, "@") { |
| 338 |
p.unread() |
| 339 |
return tags, nil |
| 340 |
} |
| 341 |
|
| 342 |
for _, t := range strings.Split(line, " ") { |
| 343 |
if t == "" { |
| 344 |
continue |
| 345 |
} |
| 346 |
if t[0:1] != "@" { |
| 347 |
return nil, p.err("invalid tag %q", t) |
| 348 |
} |
| 349 |
tags = append(tags, t) |
| 350 |
} |
| 351 |
|
| 352 |
return tags, nil |
| 353 |
} |