-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgherkin.y
More file actions
115 lines (92 loc) · 2.7 KB
/
gherkin.y
File metadata and controls
115 lines (92 loc) · 2.7 KB
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
# Compile with: racc gherkin.y -o parser.rb
class GherkinRuby::Parser
# Declare tokens produced by the lexer
token NEWLINE
token FEATURE BACKGROUND SCENARIO
token TAG
token GIVEN WHEN THEN AND BUT
token TEXT
rule
Root:
Feature { result = val[0]; }
|
Feature
Scenarios { result = val[0]; result.scenarios = val[1] }
| FeatureTags Feature { result = val[1]; result.tags = val[0] }
| FeatureTags Feature
Scenarios { result = val[1]; result.scenarios = val[2]; result.tags = val[0] }
;
Newline:
NEWLINE
| Newline NEWLINE
;
FeatureTags:
Tags { result = val[0] }
| Newline Tags { result = val[1] }
| FeatureTags Newline Tags { result += val[2] }
;
Feature:
FeatureHeader { result = val[0] }
| FeatureHeader
Background { result = val[0]; result.background = val[1] }
;
FeatureHeader:
FeatureName { result = val[0] }
| FeatureName Newline { result = val[0] }
| FeatureName Newline
Description { result = val[0]; result.description = val[2] }
;
FeatureName:
FEATURE TEXT { result = AST::Feature.new(val[1]); result.pos(filename, lineno) }
| Newline FEATURE TEXT { result = AST::Feature.new(val[2]); result.pos(filename, lineno) }
;
Description:
TEXT Newline { result = val[0] }
| Description TEXT Newline { result = val[0...-1].flatten }
;
Background:
BackgroundHeader
Steps { result = val[0]; result.steps = val[1] }
;
BackgroundHeader:
BACKGROUND Newline { result = AST::Background.new; result.pos(filename, lineno) }
;
Steps:
Step { result = [val[0]] }
| Step Newline { result = [val[0]] }
| Step Newline Steps { val[2].unshift(val[0]); result = val[2] }
;
Step:
Keyword TEXT { result = AST::Step.new(val[1], val[0]); result.pos(filename, lineno) }
;
Keyword:
GIVEN | WHEN | THEN | AND | BUT
;
Scenarios:
Scenario { result = [val[0]] }
| Scenarios Scenario { result = val[0] << val[1] }
;
Scenario:
SCENARIO TEXT Newline
Steps { result = AST::Scenario.new(val[1], val[3]); result.pos(filename, lineno - 1) }
| ScenarioTags
SCENARIO TEXT Newline
Steps { result = AST::Scenario.new(val[2], val[4], val[0]); result.pos(filename, lineno - 2) }
;
ScenarioTags:
Tags Newline { result = val[0] }
| Tags Newline ScenarioTags { result += val[2] }
;
Tags:
TAG { result = [AST::Tag.new(val[0])] }
| Tags TAG { result = val[0] << AST::Tag.new(val[1]) }
;
end
---- header
require_relative "lexer"
require_relative "../ast"
---- inner
def parse(input)
@yydebug = true if ENV['DEBUG_RACC']
scan_str(input)
end