Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
126 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,41 @@ | |||
class SyntaxGenerator | |||
def generate | |||
require 'yaml' | |||
require 'erb' | |||
require 'cucumber' | |||
|
|||
scenario_keywords_array = [] | |||
step_keywords_array = [] | |||
|
|||
Cucumber::LANGUAGES.each do |lang, words| | |||
scenario_keywords_array << words.delete('scenario').split(/\|/) rescue nil | |||
scenario_keywords_array << words.delete('scenario_outline').split(/\|/) rescue nil | |||
scenario_keywords_array << words.delete('background').split(/\|/) rescue nil | |||
|
|||
step_keywords_array << keywords(lang, words) | |||
end | |||
|
|||
scenario_keywords = scenario_keywords_array.flatten.compact.sort.reverse.uniq.join('|') | |||
step_keywords = step_keywords_array.flatten.compact.sort.reverse.uniq.join('|') | |||
|
|||
template = ERB.new(IO.read(File.dirname(__FILE__) + '/lexer.erb.py')) | |||
syntax = template.result(binding) | |||
|
|||
syntax_file = File.dirname(__FILE__) + '/gherkin_lexer/__init__.py' | |||
File.open(syntax_file, "w") do |io| | |||
io.write(syntax) | |||
end | |||
end | |||
|
|||
def keywords(lang, words) | |||
space = words['space_after_keyword'] ? '\s+' : '' | |||
%w{given when then and but}.map do |key| | |||
words[key].split(/\|/).map{|w| "#{w}#{space}"} | |||
end | |||
end | |||
end | |||
|
|||
desc 'Generate Gherkin lexer for all languages supported by Cucumber' | |||
task :generate do | |||
SyntaxGenerator.new.generate | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,81 @@ | |||
#!/usr/bin/env python | |||
from pygments.lexer import RegexLexer, bygroups, include | |||
from pygments.token import \ | |||
Text, Comment, Literal, Operator, Keyword, Name, String | |||
|
|||
class GherkinLexer(RegexLexer): | |||
name = 'Gherkin' | |||
aliases = ['Cucumber', 'cucumber', 'Gherkin', 'gherkin'] | |||
filenames = ['*.feature', '*.story'] | |||
|
|||
step_keywords_regexp = r'(\s+)(<%= step_keywords %>)' | |||
scenario_sections_regexp = r'(\s+)(<%= scenario_keywords %>)(:)(.*)$' | |||
|
|||
tokens = { | |||
'comments': [ | |||
(r'#.*$', Comment), | |||
], | |||
'multiline_descriptions' : [ | |||
(step_keywords_regexp, Keyword, "#pop"), | |||
include('comments'), | |||
(r"(\s|.)", Name.Constant), | |||
], | |||
'multiline_descriptions_on_stack' : [ | |||
(step_keywords_regexp, Keyword, "#pop:2"), | |||
include('comments'), | |||
(r"(\s|.)", Name.Constant), | |||
], | |||
'scenario_table_description': [ | |||
(r"\s+\|", Text, 'scenario_table_header'), | |||
include('comments'), | |||
(r"(\s|.)", Name.Constant), | |||
], | |||
'scenario_table_header': [ | |||
(r"\s+\|\s*$", Text, "#pop:2"), | |||
(r"(\s+\|\s*)(#.*)$", bygroups(Text, Comment), "#pop:2"), | |||
include('comments'), | |||
(r"\s+\|", Text), | |||
(r"[^\|]", Literal.String.Symbol), | |||
], | |||
'scenario_sections_on_stack': [ | |||
(scenario_sections_regexp, bygroups(Text, Name.Class, Name.Class, Name.Constant), "multiline_descriptions_on_stack"), | |||
], | |||
'narrative': [ | |||
include('scenario_sections_on_stack'), | |||
(r"(\s|.)", Name.Builtin), | |||
], | |||
'table_vars': [ | |||
(r'(<)([^>]*)(>)', bygroups(Operator, Literal.String.Symbol, Operator)), | |||
], | |||
'string': [ | |||
include('table_vars'), | |||
(r'(\s|.)', String), | |||
], | |||
'py_string': [ | |||
(r'"""', String, "#pop"), | |||
include('string'), | |||
], | |||
'double_string': [ | |||
(r'"', String, "#pop"), | |||
include('string'), | |||
], | |||
'single_string': [ | |||
(r"'", String, "#pop"), | |||
include('string'), | |||
], | |||
'root': [ | |||
(r'\n', Text), | |||
include('comments'), | |||
(r'"""', String, "py_string"), | |||
(r'"', String, "double_string"), | |||
(r"'", String, "single_string"), | |||
include('table_vars'), | |||
(r'@[^@\s]+', Name.Namespace), | |||
(step_keywords_regexp, bygroups(Text, Keyword)), | |||
(r'^(Feature|Story)(:)(.*)$', bygroups(Name.Class, Name.Class, Name.Constant), 'narrative'), | |||
(scenario_sections_regexp, bygroups(Text, Name.Class, Name.Class, Name.Constant), "multiline_descriptions"), | |||
(r'(\s+)(Scenarios|Examples)(:)(.*)$', bygroups(Text, Name.Class, Name.Class, Name.Constant), "scenario_table_description"), | |||
(r'(\s|.)', Text), | |||
] | |||
|
|||
} |