public
Last active

Cucumber treetop grammar

  • Download Gist
feature_parser.erb
HTML+ERB
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
module Cucumber
# :stopdoc:
module TreetopParser
grammar Feature
rule root
space? header scenario_sequence space? {
def compile
feature_elements = scenario_sequence.compile
feature = Cucumber::Ast::Feature.new(nil, nil, header.text_value.strip, feature_elements)
feature
end
}
end
rule header
(!(scenario_keyword / scenario_outline_keyword / comment_to_eol) .)+
end
rule scenario_sequence
head:scenario_outline_or_scenario? tail:(space scenario_or_scenario_outline)* {
def compile
sequence = []
([head] + tail).each do |scenario_or_scenario_outline|
sequence << scenario_or_scenario_outline.compile if scenario_or_scenario_outline.respond_to?(:compile)
end
sequence
end
def tail
super.elements.map { |elt| elt.scenario_or_scenario_outline }
end
}
end
 
rule scenario_outline_or_scenario
scenario_outline / scenario
end
 
rule scenario
scenario_keyword space? name:line_to_eol steps:(space step_sequence)? table:(space examples)? {
def compile
step_names_and_inline_args = []
step_names_and_inline_args = steps.step_sequence.compile if steps.respond_to?(:step_sequence)
#TODO: compile table
Ast::Scenario.new(@step_mother, nil, nil, name.text_value.strip, step_names_and_inline_args)
end
}
end
 
rule scenario_outline
scenario_outline_keyword space? name:line_to_eol steps:(space step_sequence)? table:(space examples) {
def compile
step_names = []
matrix = []
step_names = steps.step_sequence.compile if steps.respond_to?(:step_sequence)
matrix = table.examples.compile if table.respond_to?(:examples)
Ast::ScenarioOutline.new(@step_mother, nil, nil, name.text_value.strip, step_names, matrix)
end
}
end
rule scenario_or_scenario_outline
scenario_outline / scenario
end
rule examples
(more_examples_keyword / examples_keyword) table {
def compile
table.compile
end
}
end
 
rule table
space head:table_line body:(blank* eol space? table_line)* {
def compile
([head] + body).map do |table_line|
table_line.cell_values
end
end
 
def line
input.line_of(interval.first)
end
def body
super.elements.map { |elt| elt.table_line }
end
 
}
end
 
rule table_line
separator cells:(blank* cell_value blank* separator)+ {
def cell_values
cells.elements.map { |elt| elt.cell_value.text_value.strip }
end
def line
input.line_of(interval.first)
end
}
end
 
rule cell_value
(!(separator / eol) .)*
end
 
rule step_sequence
head:step tail:(space step)* {
def compile
([head] + tail).map do |step|
step.compile
end
end
def tail
super.elements.map { |elt| elt.step }
end
}
end
 
rule step
given_scenario / plain_step
end
rule given_scenario
given_scenario_keyword space? name:line_to_eol {
def compile
end
}
end
 
rule plain_step
step_keyword space? name:line_to_eol multi:multiline_arg? {
def compile
step_data = [step_keyword.text_value.strip, name.text_value.strip]
step_data += [multi.compile] if multi.respond_to?(:compile)
step_data
end
}
end
rule multiline_arg
table / multiline_string
end
rule multiline_string
eol indent:space triple_quote string:(!triple_quote .)* triple_quote {
def compile
indent_length = indent.text_value.length
significant_lines = string.text_value.split("\n")[1..-2]
significant_lines.map do |l|
l[indent_length..-1]
end.join("\n")
end
}
end
 
rule triple_quote
'"""'
end
 
rule separator
'|'
end
 
rule space
(white / comment_to_eol)+
end
 
rule line_to_eol
(!eol .)*
end
 
rule comment_to_eol
'#' line_to_eol
end
 
rule white
blank / eol
end
rule blank
[ \t]
end
rule eol
("\r" "\n"?) / "\n"
end
rule step_keyword
"<%= words['given'] %>" / "<%= words['when'] %>" / "<%= words['then'] %>" / "<%= words['and'] %>" / "<%= words['but'] %>"
end
rule scenario_keyword
"<%= words['scenario'] %>" ":"?
end
rule scenario_outline_keyword
"<%= words['scenario_outline'] %>" ":"?
end
rule more_examples_keyword
"<%= words['more_examples'] %>" ":"?
end
rule examples_keyword
"<%= words['examples'] %>" ":"?
end
rule given_scenario_keyword
"<%= words['given_scenario'] %>" ":"?
end
 
end
 
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.