gherkin 2.2.5-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (132) hide show
  1. data/.gitattributes +2 -0
  2. data/.gitignore +11 -0
  3. data/.mailmap +2 -0
  4. data/.rspec +1 -0
  5. data/.rvmrc +1 -0
  6. data/Gemfile +5 -0
  7. data/History.txt +306 -0
  8. data/LICENSE +20 -0
  9. data/README.rdoc +59 -0
  10. data/Rakefile +16 -0
  11. data/VERSION +1 -0
  12. data/bin/gherkin +5 -0
  13. data/build_native_gems.sh +8 -0
  14. data/cucumber.yml +3 -0
  15. data/features/escaped_pipes.feature +8 -0
  16. data/features/feature_parser.feature +237 -0
  17. data/features/json_formatter.feature +278 -0
  18. data/features/json_parser.feature +318 -0
  19. data/features/native_lexer.feature +19 -0
  20. data/features/parser_with_native_lexer.feature +205 -0
  21. data/features/pretty_formatter.feature +15 -0
  22. data/features/step_definitions/eyeball_steps.rb +3 -0
  23. data/features/step_definitions/gherkin_steps.rb +29 -0
  24. data/features/step_definitions/json_formatter_steps.rb +28 -0
  25. data/features/step_definitions/json_parser_steps.rb +20 -0
  26. data/features/step_definitions/pretty_formatter_steps.rb +82 -0
  27. data/features/steps_parser.feature +46 -0
  28. data/features/support/env.rb +38 -0
  29. data/gherkin.gemspec +59 -0
  30. data/ikvm/.gitignore +3 -0
  31. data/java/.gitignore +2 -0
  32. data/java/src/main/java/gherkin/lexer/i18n/.gitignore +1 -0
  33. data/java/src/main/resources/gherkin/.gitignore +1 -0
  34. data/lib/.gitignore +4 -0
  35. data/lib/gherkin.rb +2 -0
  36. data/lib/gherkin/c_lexer.rb +17 -0
  37. data/lib/gherkin/cli/main.rb +33 -0
  38. data/lib/gherkin/formatter/argument.rb +28 -0
  39. data/lib/gherkin/formatter/colors.rb +119 -0
  40. data/lib/gherkin/formatter/escaping.rb +15 -0
  41. data/lib/gherkin/formatter/filter_formatter.rb +136 -0
  42. data/lib/gherkin/formatter/json_formatter.rb +72 -0
  43. data/lib/gherkin/formatter/line_filter.rb +26 -0
  44. data/lib/gherkin/formatter/model.rb +231 -0
  45. data/lib/gherkin/formatter/monochrome_format.rb +9 -0
  46. data/lib/gherkin/formatter/pretty_formatter.rb +174 -0
  47. data/lib/gherkin/formatter/regexp_filter.rb +21 -0
  48. data/lib/gherkin/formatter/tag_count_formatter.rb +47 -0
  49. data/lib/gherkin/formatter/tag_filter.rb +19 -0
  50. data/lib/gherkin/i18n.rb +180 -0
  51. data/lib/gherkin/i18n.yml +601 -0
  52. data/lib/gherkin/json_parser.rb +88 -0
  53. data/lib/gherkin/lexer/i18n_lexer.rb +47 -0
  54. data/lib/gherkin/listener/event.rb +45 -0
  55. data/lib/gherkin/listener/formatter_listener.rb +113 -0
  56. data/lib/gherkin/native.rb +7 -0
  57. data/lib/gherkin/native/ikvm.rb +55 -0
  58. data/lib/gherkin/native/java.rb +55 -0
  59. data/lib/gherkin/native/null.rb +9 -0
  60. data/lib/gherkin/parser/meta.txt +5 -0
  61. data/lib/gherkin/parser/parser.rb +164 -0
  62. data/lib/gherkin/parser/root.txt +11 -0
  63. data/lib/gherkin/parser/steps.txt +4 -0
  64. data/lib/gherkin/rb_lexer.rb +8 -0
  65. data/lib/gherkin/rb_lexer/.gitignore +1 -0
  66. data/lib/gherkin/rb_lexer/README.rdoc +8 -0
  67. data/lib/gherkin/rubify.rb +24 -0
  68. data/lib/gherkin/tag_expression.rb +62 -0
  69. data/lib/gherkin/tools.rb +8 -0
  70. data/lib/gherkin/tools/files.rb +34 -0
  71. data/lib/gherkin/tools/reformat.rb +20 -0
  72. data/lib/gherkin/tools/stats.rb +20 -0
  73. data/lib/gherkin/tools/stats_listener.rb +60 -0
  74. data/lib/gherkin/version.rb +3 -0
  75. data/ragel/i18n/.gitignore +1 -0
  76. data/ragel/lexer.c.rl.erb +459 -0
  77. data/ragel/lexer.java.rl.erb +224 -0
  78. data/ragel/lexer.rb.rl.erb +179 -0
  79. data/ragel/lexer_common.rl.erb +50 -0
  80. data/spec/gherkin/c_lexer_spec.rb +21 -0
  81. data/spec/gherkin/fixtures/1.feature +8 -0
  82. data/spec/gherkin/fixtures/comments_in_table.feature +9 -0
  83. data/spec/gherkin/fixtures/complex.feature +45 -0
  84. data/spec/gherkin/fixtures/complex.json +143 -0
  85. data/spec/gherkin/fixtures/complex_for_filtering.feature +60 -0
  86. data/spec/gherkin/fixtures/complex_with_tags.feature +61 -0
  87. data/spec/gherkin/fixtures/dos_line_endings.feature +45 -0
  88. data/spec/gherkin/fixtures/hantu_pisang.feature +35 -0
  89. data/spec/gherkin/fixtures/i18n_fr.feature +14 -0
  90. data/spec/gherkin/fixtures/i18n_no.feature +7 -0
  91. data/spec/gherkin/fixtures/i18n_zh-CN.feature +9 -0
  92. data/spec/gherkin/fixtures/scenario_outline_with_tags.feature +13 -0
  93. data/spec/gherkin/fixtures/scenario_without_steps.feature +5 -0
  94. data/spec/gherkin/fixtures/simple_with_comments.feature +7 -0
  95. data/spec/gherkin/fixtures/simple_with_tags.feature +11 -0
  96. data/spec/gherkin/fixtures/with_bom.feature +3 -0
  97. data/spec/gherkin/formatter/argument_spec.rb +28 -0
  98. data/spec/gherkin/formatter/colors_spec.rb +18 -0
  99. data/spec/gherkin/formatter/filter_formatter_spec.rb +165 -0
  100. data/spec/gherkin/formatter/model_spec.rb +15 -0
  101. data/spec/gherkin/formatter/pretty_formatter_spec.rb +140 -0
  102. data/spec/gherkin/formatter/spaces.feature +9 -0
  103. data/spec/gherkin/formatter/tabs.feature +9 -0
  104. data/spec/gherkin/formatter/tag_count_formatter_spec.rb +30 -0
  105. data/spec/gherkin/i18n_spec.rb +149 -0
  106. data/spec/gherkin/java_lexer_spec.rb +20 -0
  107. data/spec/gherkin/json.rb +5 -0
  108. data/spec/gherkin/json_parser_spec.rb +67 -0
  109. data/spec/gherkin/lexer/i18n_lexer_spec.rb +43 -0
  110. data/spec/gherkin/output_stream_string_io.rb +24 -0
  111. data/spec/gherkin/parser/parser_spec.rb +16 -0
  112. data/spec/gherkin/rb_lexer_spec.rb +19 -0
  113. data/spec/gherkin/sexp_recorder.rb +56 -0
  114. data/spec/gherkin/shared/lexer_group.rb +592 -0
  115. data/spec/gherkin/shared/py_string_group.rb +153 -0
  116. data/spec/gherkin/shared/row_group.rb +120 -0
  117. data/spec/gherkin/shared/tags_group.rb +54 -0
  118. data/spec/gherkin/tag_expression_spec.rb +137 -0
  119. data/spec/spec_helper.rb +68 -0
  120. data/tasks/bench.rake +184 -0
  121. data/tasks/bench/feature_builder.rb +49 -0
  122. data/tasks/bench/generated/.gitignore +1 -0
  123. data/tasks/bench/null_listener.rb +4 -0
  124. data/tasks/compile.rake +102 -0
  125. data/tasks/cucumber.rake +18 -0
  126. data/tasks/gems.rake +42 -0
  127. data/tasks/ikvm.rake +54 -0
  128. data/tasks/ragel_task.rb +70 -0
  129. data/tasks/rdoc.rake +9 -0
  130. data/tasks/release.rake +30 -0
  131. data/tasks/rspec.rake +8 -0
  132. metadata +447 -0
@@ -0,0 +1,11 @@
1
+ | | feature | background | scenario | scenario_outline | examples | step | row | py_string | eof | comment | tag |
2
+ | root | feature | E | E | E | E | E | E | E | eof | push(meta) | push(meta) |
3
+ | feature | E | background | scenario | scenario_outline | E | E | E | E | eof | push(meta) | push(meta) |
4
+ | step | E | E | scenario | scenario_outline | E | step | step | step | eof | push(meta) | push(meta) |
5
+ | outline_step | E | E | scenario | scenario_outline | examples | outline_step | outline_step | outline_step | eof | push(meta) | push(meta) |
6
+ | background | E | E | scenario | scenario_outline | E | step | E | E | eof | push(meta) | push(meta) |
7
+ | scenario | E | E | scenario | scenario_outline | E | step | E | E | eof | push(meta) | push(meta) |
8
+ | scenario_outline | E | E | E | E | E | outline_step | E | E | eof | push(meta) | push(meta) |
9
+ | examples | E | E | E | E | E | E | examples_table | E | eof | push(meta) | push(meta) |
10
+ | examples_table | E | E | scenario | scenario_outline | examples | E | examples_table | E | eof | push(meta) | push(meta) |
11
+ | eof | E | E | E | E | E | E | E | E | E | E | E |
@@ -0,0 +1,4 @@
1
+ | | feature | background | scenario | scenario_outline | examples | step | row | py_string | eof | comment | tag |
2
+ | steps | E | E | E | E | E | step | E | E | eof | E | E |
3
+ | step | E | E | E | E | E | step | step | steps | eof | E | E |
4
+ | eof | E | E | E | E | E | E | E | E | E | E | E |
@@ -0,0 +1,8 @@
1
+ module Gherkin
2
+ module RbLexer
3
+ def self.[](i18n_underscored_iso_code)
4
+ require "gherkin/rb_lexer/#{i18n_underscored_iso_code}"
5
+ const_get(i18n_underscored_iso_code.capitalize)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1 @@
1
+ *.rb
@@ -0,0 +1,8 @@
1
+ = Lexers
2
+
3
+ Gherkin support lexing of lots of natural languages, defined by gherkin/i18n.yml
4
+ The lexers are generated with the following command:
5
+
6
+ rake ragel:i18n
7
+
8
+ You have to run this command if you modify gherkin/i18n.yml
@@ -0,0 +1,24 @@
1
+ module Gherkin
2
+ module Rubify
3
+ if defined?(JRUBY_VERSION)
4
+ # Translate Java objects to Ruby.
5
+ # This is especially important to convert java.util.List coming
6
+ # from Java and back to a Ruby Array.
7
+ def rubify(o)
8
+ case(o)
9
+ when Java.java.util.Collection, Array
10
+ o.map{|e| rubify(e)}
11
+ when Java.gherkin.formatter.model.PyString
12
+ require 'gherkin/formatter/model'
13
+ Formatter::Model::PyString.new(o.value, o.line)
14
+ else
15
+ o
16
+ end
17
+ end
18
+ else
19
+ def rubify(o)
20
+ o
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,62 @@
1
+ require 'gherkin/native'
2
+
3
+ module Gherkin
4
+ class TagExpression
5
+ native_impl('gherkin')
6
+
7
+ attr_reader :limits
8
+
9
+ def initialize(tag_expressions)
10
+ @ands = []
11
+ @limits = {}
12
+ tag_expressions.each do |expr|
13
+ add(expr.strip.split(/\s*,\s*/))
14
+ end
15
+ end
16
+
17
+ def empty?
18
+ @ands.empty?
19
+ end
20
+
21
+ def eval(tags)
22
+ return true if @ands.flatten.empty?
23
+ vars = Hash[*tags.map{|tag| [tag, true]}.flatten]
24
+ !!Kernel.eval(ruby_expression)
25
+ end
26
+
27
+ private
28
+
29
+ def add(tags_with_negation_and_limits)
30
+ negatives, positives = tags_with_negation_and_limits.partition{|tag| tag =~ /^~/}
31
+ @ands << (store_and_extract_limits(negatives, true) + store_and_extract_limits(positives, false))
32
+ end
33
+
34
+ def store_and_extract_limits(tags_with_negation_and_limits, negated)
35
+ tags_with_negation = []
36
+ tags_with_negation_and_limits.each do |tag_with_negation_and_limit|
37
+ tag_with_negation, limit = tag_with_negation_and_limit.split(':')
38
+ tags_with_negation << tag_with_negation
39
+ if limit
40
+ tag_without_negation = negated ? tag_with_negation[1..-1] : tag_with_negation
41
+ if @limits[tag_without_negation] && @limits[tag_without_negation] != limit.to_i
42
+ raise "Inconsistent tag limits for #{tag_without_negation}: #{@limits[tag_without_negation]} and #{limit.to_i}"
43
+ end
44
+ @limits[tag_without_negation] = limit.to_i
45
+ end
46
+ end
47
+ tags_with_negation
48
+ end
49
+
50
+ def ruby_expression
51
+ "(" + @ands.map do |ors|
52
+ ors.map do |tag|
53
+ if tag =~ /^~(.*)/
54
+ "!vars['#{$1}']"
55
+ else
56
+ "vars['#{tag}']"
57
+ end
58
+ end.join("||")
59
+ end.join(")&&(") + ")"
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,8 @@
1
+ module Gherkin
2
+ module Tools
3
+ SUB_COMMANDS = %w(stats reformat)
4
+ SUB_COMMANDS.each do |cmd|
5
+ autoload cmd.capitalize.to_sym, "gherkin/tools/#{cmd}"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,34 @@
1
+ require 'gherkin'
2
+
3
+ module Gherkin
4
+ module Tools
5
+ # Base class for file based operations
6
+ class Files
7
+ include Enumerable
8
+
9
+ def initialize(paths)
10
+ raise "Please specify one or more paths" if paths.empty?
11
+ @paths = paths
12
+ end
13
+
14
+ def each(&proc)
15
+ globs = @paths.map do |path|
16
+ raise "#{path} does not exist" unless File.exist?(path)
17
+ File.directory?(path) ? File.join(path, '**', '*.feature') : path
18
+ end
19
+
20
+ Dir[*globs].uniq.sort.each(&proc)
21
+ end
22
+
23
+ def parse(file, formatter)
24
+ parser = Gherkin::Parser::Parser.new(formatter, true, "root")
25
+ begin
26
+ parser.parse(IO.read(file), file, 0)
27
+ rescue => e
28
+ e.message << " (#{file})"
29
+ raise e
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ require 'stringio'
2
+ require 'gherkin/tools/files'
3
+ require 'gherkin/formatter/pretty_formatter'
4
+ require 'gherkin/listener/formatter_listener'
5
+
6
+ module Gherkin
7
+ module Tools
8
+ class Reformat < Files
9
+ def run
10
+ each do |file|
11
+ io = defined?(JRUBY_VERSION) ? Java.java.io.StringWriter.new : StringIO.new
12
+ formatter = Formatter::PrettyFormatter.new(io, true)
13
+ parse(file, formatter)
14
+ string = defined?(JRUBY_VERSION) ? io.getBuffer.toString : io.string
15
+ File.open(file, 'w') {|io| io.write(string)}
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'gherkin'
2
+ require 'gherkin/tools/files'
3
+ require 'gherkin/tools/stats_listener'
4
+
5
+ module Gherkin
6
+ module Tools
7
+ class Stats < Files
8
+ def run
9
+ formatter = StatsFormatter.new
10
+ each do |f|
11
+ parser = Gherkin::Parser::Parser.new(formatter, true)
12
+ parser.parse(IO.read(f), f, 0)
13
+ end
14
+ puts "Features: #{listener.features}"
15
+ puts "Scenarios: #{listener.scenarios}"
16
+ puts "Steps: #{listener.steps}"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,60 @@
1
+ require 'gherkin/native'
2
+
3
+ # encoding: utf-8
4
+ module Gherkin
5
+ module Tools
6
+ class StatsListener
7
+ implements 'gherkin.Listener'
8
+
9
+ attr_reader :features, :scenarios, :steps
10
+
11
+ def initialize
12
+ @features = 0
13
+ @scenarios = 0
14
+ @steps = 0
15
+ end
16
+
17
+ def location(uri)
18
+ end
19
+
20
+ def tag(name, line)
21
+ end
22
+
23
+ def comment(content, line)
24
+ end
25
+
26
+ def feature(keyword, name, description, line)
27
+ @features += 1
28
+ end
29
+
30
+ def background(keyword, name, description, line)
31
+ end
32
+
33
+ def scenario(keyword, name, description, line)
34
+ @scenarios += 1
35
+ end
36
+
37
+ def scenario_outline(keyword, name, description, line)
38
+ end
39
+
40
+ def examples(keyword, name, description, line)
41
+ end
42
+
43
+ def step(keyword, name, line)
44
+ @steps += 1
45
+ end
46
+
47
+ def row(row, line)
48
+ end
49
+
50
+ def py_string(string, line)
51
+ end
52
+
53
+ def syntax_error(state, event, legal_events, uri, line)
54
+ end
55
+
56
+ def eof
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Gherkin
2
+ VERSION = IO.read(File.dirname(__FILE__) + '/../../VERSION').strip
3
+ end
@@ -0,0 +1 @@
1
+ *.rl
@@ -0,0 +1,459 @@
1
+ #include <assert.h>
2
+ #include <ruby.h>
3
+
4
+ #if defined(_WIN32)
5
+ #include <stddef.h>
6
+ #endif
7
+
8
+ #ifdef HAVE_RUBY_RE_H
9
+ #include <ruby/re.h>
10
+ #else
11
+ #include <re.h>
12
+ #endif
13
+
14
+ #ifdef HAVE_RUBY_ENCODING_H
15
+ #include <ruby/encoding.h>
16
+ #define ENCODED_STR_NEW(ptr, len) \
17
+ rb_enc_str_new(ptr, len, rb_utf8_encoding())
18
+ #else
19
+ #define ENCODED_STR_NEW(ptr, len) \
20
+ rb_str_new(ptr, len)
21
+ #endif
22
+
23
+ #define LF_FLAG 0
24
+ #define CRLF_FLAG 1
25
+ #define LF "\n"
26
+ #define CRLF "\r\n"
27
+
28
+ #ifndef RSTRING_PTR
29
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
30
+ #endif
31
+
32
+ #ifndef RSTRING_LEN
33
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
34
+ #endif
35
+
36
+ #define DATA_GET(FROM, TYPE, NAME) \
37
+ Data_Get_Struct(FROM, TYPE, NAME); \
38
+ if (NAME == NULL) { \
39
+ rb_raise(rb_eArgError, "NULL found for " # NAME " when it shouldn't be."); \
40
+ }
41
+
42
+ typedef struct lexer_state {
43
+ int content_len;
44
+ int line_number;
45
+ int current_line;
46
+ int start_col;
47
+ int eol;
48
+ size_t mark;
49
+ size_t keyword_start;
50
+ size_t keyword_end;
51
+ size_t next_keyword_start;
52
+ size_t content_start;
53
+ size_t content_end;
54
+ size_t query_start;
55
+ size_t last_newline;
56
+ size_t final_newline;
57
+ } lexer_state;
58
+
59
+ static VALUE mGherkin;
60
+ static VALUE mGherkinLexer;
61
+ static VALUE mCLexer;
62
+ static VALUE cI18nLexer;
63
+ static VALUE rb_eGherkinLexingError;
64
+
65
+ #define LEN(AT, P) (P - data - lexer->AT)
66
+ #define MARK(M, P) (lexer->M = (P) - data)
67
+ #define PTR_TO(P) (data + lexer->P)
68
+
69
+ #define STORE_KW_END_CON(EVENT) \
70
+ store_multiline_kw_con(listener, # EVENT, \
71
+ PTR_TO(keyword_start), LEN(keyword_start, PTR_TO(keyword_end - 1)), \
72
+ PTR_TO(content_start), LEN(content_start, PTR_TO(content_end)), \
73
+ lexer->current_line, lexer->eol); \
74
+ if (lexer->content_end != 0) { \
75
+ p = PTR_TO(content_end - 1); \
76
+ } \
77
+ lexer->content_end = 0
78
+
79
+ #define STORE_ATTR(ATTR) \
80
+ store_attr(listener, # ATTR, \
81
+ PTR_TO(content_start), LEN(content_start, p), \
82
+ lexer->line_number)
83
+
84
+ %%{
85
+ machine lexer;
86
+
87
+ action begin_content {
88
+ MARK(content_start, p);
89
+ lexer->current_line = lexer->line_number;
90
+ }
91
+
92
+ action begin_pystring_content {
93
+ MARK(content_start, p);
94
+ }
95
+
96
+ action start_pystring {
97
+ lexer->current_line = lexer->line_number;
98
+ lexer->start_col = p - data - lexer->last_newline;
99
+ }
100
+
101
+ action store_pystring_content {
102
+ int len = LEN(content_start, PTR_TO(final_newline));
103
+
104
+ if (len < 0) len = 0;
105
+
106
+ store_pystring_content(listener, lexer->start_col, PTR_TO(content_start), len, lexer->current_line);
107
+ }
108
+
109
+ action store_feature_content {
110
+ STORE_KW_END_CON(feature);
111
+ }
112
+
113
+ action store_background_content {
114
+ STORE_KW_END_CON(background);
115
+ }
116
+
117
+ action store_scenario_content {
118
+ STORE_KW_END_CON(scenario);
119
+ }
120
+
121
+ action store_scenario_outline_content {
122
+ STORE_KW_END_CON(scenario_outline);
123
+ }
124
+
125
+ action store_examples_content {
126
+ STORE_KW_END_CON(examples);
127
+ }
128
+
129
+ action store_step_content {
130
+ store_kw_con(listener, "step",
131
+ PTR_TO(keyword_start), LEN(keyword_start, PTR_TO(keyword_end)),
132
+ PTR_TO(content_start), LEN(content_start, p),
133
+ lexer->current_line);
134
+ }
135
+
136
+ action store_comment_content {
137
+ STORE_ATTR(comment);
138
+ lexer->mark = 0;
139
+ }
140
+
141
+ action store_tag_content {
142
+ STORE_ATTR(tag);
143
+ lexer->mark = 0;
144
+ }
145
+
146
+ action inc_line_number {
147
+ lexer->line_number += 1;
148
+ MARK(final_newline, p);
149
+ }
150
+
151
+ action last_newline {
152
+ MARK(last_newline, p + 1);
153
+ }
154
+
155
+ action start_keyword {
156
+ if (lexer->mark == 0) {
157
+ MARK(mark, p);
158
+ }
159
+ }
160
+
161
+ action end_keyword {
162
+ MARK(keyword_end, p);
163
+ MARK(keyword_start, PTR_TO(mark));
164
+ MARK(content_start, p + 1);
165
+ lexer->mark = 0;
166
+ }
167
+
168
+ action next_keyword_start {
169
+ MARK(content_end, p);
170
+ }
171
+
172
+ action start_row {
173
+ p = p - 1;
174
+ lexer->current_line = lexer->line_number;
175
+ current_row = rb_ary_new();
176
+ }
177
+
178
+ action begin_cell_content {
179
+ MARK(content_start, p);
180
+ }
181
+
182
+ action store_cell_content {
183
+ VALUE con = ENCODED_STR_NEW(PTR_TO(content_start), LEN(content_start, p));
184
+ rb_funcall(con, rb_intern("strip!"), 0);
185
+ VALUE re_pipe = rb_reg_regcomp(rb_str_new2("\\\\\\|"));
186
+ VALUE re_backslash = rb_reg_regcomp(rb_str_new2("\\\\\\\\"));
187
+ rb_funcall(con, rb_intern("gsub!"), 2, re_pipe, rb_str_new2("|"));
188
+ rb_funcall(con, rb_intern("gsub!"), 2, re_backslash, rb_str_new2("\\"));
189
+
190
+ rb_ary_push(current_row, con);
191
+ }
192
+
193
+ action store_row {
194
+ rb_funcall(listener, rb_intern("row"), 2, current_row, INT2FIX(lexer->current_line));
195
+ }
196
+
197
+ action end_feature {
198
+ if (cs < lexer_first_final) {
199
+ if (raise_lexer_error != NULL) {
200
+ size_t count = 0;
201
+ VALUE newstr_val;
202
+ char *newstr;
203
+ int newstr_count = 0;
204
+ size_t len;
205
+ const char *buff;
206
+ if (lexer->last_newline != 0) {
207
+ len = LEN(last_newline, eof);
208
+ buff = PTR_TO(last_newline);
209
+ } else {
210
+ len = strlen(data);
211
+ buff = data;
212
+ }
213
+
214
+ // Allocate as a ruby string so that it gets cleaned up by GC
215
+ newstr_val = rb_str_new(buff, len);
216
+ newstr = RSTRING_PTR(newstr_val);
217
+
218
+ for (count = 0; count < len; count++) {
219
+ if(buff[count] == 10) {
220
+ newstr[newstr_count] = '\0'; // terminate new string at first newline found
221
+ break;
222
+ } else {
223
+ if (buff[count] == '%') {
224
+ newstr[newstr_count++] = buff[count];
225
+ newstr[newstr_count] = buff[count];
226
+ } else {
227
+ newstr[newstr_count] = buff[count];
228
+ }
229
+ }
230
+ newstr_count++;
231
+ }
232
+
233
+ int line = lexer->line_number;
234
+ lexer_init(lexer); // Re-initialize so we can scan again with the same lexer
235
+ raise_lexer_error(newstr, line);
236
+ }
237
+ } else {
238
+ rb_funcall(listener, rb_intern("eof"), 0);
239
+ }
240
+ }
241
+
242
+ include lexer_common "lexer_common.<%= @i18n.underscored_iso_code %>.rl";
243
+
244
+ }%%
245
+
246
+ /** Data **/
247
+ %% write data;
248
+
249
+ static VALUE
250
+ strip_i(VALUE str, VALUE ary)
251
+ {
252
+ rb_funcall(str, rb_intern("strip!"), 0);
253
+ rb_ary_push(ary, str);
254
+
255
+ return Qnil;
256
+ }
257
+
258
+ static VALUE
259
+ multiline_strip(VALUE text)
260
+ {
261
+ VALUE map = rb_ary_new();
262
+ VALUE split = rb_str_split(text, "\n");
263
+
264
+ rb_iterate(rb_each, split, strip_i, map);
265
+
266
+ return split;
267
+ }
268
+
269
+ static void
270
+ store_kw_con(VALUE listener, const char * event_name,
271
+ const char * keyword_at, size_t keyword_length,
272
+ const char * at, size_t length,
273
+ int current_line)
274
+ {
275
+ VALUE con = Qnil, kw = Qnil;
276
+ kw = ENCODED_STR_NEW(keyword_at, keyword_length);
277
+ con = ENCODED_STR_NEW(at, length);
278
+ rb_funcall(con, rb_intern("strip!"), 0);
279
+ rb_funcall(listener, rb_intern(event_name), 3, kw, con, INT2FIX(current_line));
280
+ }
281
+
282
+ static void
283
+ store_multiline_kw_con(VALUE listener, const char * event_name,
284
+ const char * keyword_at, size_t keyword_length,
285
+ const char * at, size_t length,
286
+ int current_line, int eol)
287
+ {
288
+ VALUE con = Qnil, kw = Qnil, name = Qnil, desc = Qnil;
289
+
290
+ kw = ENCODED_STR_NEW(keyword_at, keyword_length);
291
+ con = ENCODED_STR_NEW(at, length);
292
+
293
+ VALUE split = multiline_strip(con);
294
+
295
+ name = rb_funcall(split, rb_intern("shift"), 0);
296
+ desc = rb_ary_join(split, rb_str_new2( \
297
+ eol == CRLF_FLAG ? CRLF : LF ));
298
+
299
+ if( name == Qnil )
300
+ {
301
+ name = rb_str_new2("");
302
+ }
303
+ if( rb_funcall(desc, rb_intern("size"), 0) == 0)
304
+ {
305
+ desc = rb_str_new2("");
306
+ }
307
+ rb_funcall(name, rb_intern("strip!"), 0);
308
+ rb_funcall(desc, rb_intern("strip!"), 0);
309
+ rb_funcall(listener, rb_intern(event_name), 4, kw, name, desc, INT2FIX(current_line));
310
+ }
311
+
312
+ static void
313
+ store_attr(VALUE listener, const char * attr_type,
314
+ const char * at, size_t length,
315
+ int line)
316
+ {
317
+ VALUE val = ENCODED_STR_NEW(at, length);
318
+ rb_funcall(listener, rb_intern(attr_type), 2, val, INT2FIX(line));
319
+ }
320
+
321
+ static void
322
+ store_pystring_content(VALUE listener,
323
+ int start_col,
324
+ const char *at, size_t length,
325
+ int current_line)
326
+ {
327
+ VALUE con = ENCODED_STR_NEW(at, length);
328
+ // Gherkin will crash gracefully if the string representation of start_col pushes the pattern past 32 characters
329
+ char pat[32];
330
+ snprintf(pat, 32, "^[\t ]{0,%d}", start_col);
331
+ VALUE re = rb_reg_regcomp(rb_str_new2(pat));
332
+ VALUE re2 = rb_reg_regcomp(rb_str_new2("\r\\Z"));
333
+ VALUE unescape_escaped_quotes = rb_reg_regcomp(rb_str_new2("\\\\\"\\\\\"\\\\\""));
334
+ rb_funcall(con, rb_intern("gsub!"), 2, re, rb_str_new2(""));
335
+ rb_funcall(con, rb_intern("sub!"), 2, re2, rb_str_new2(""));
336
+ rb_funcall(con, rb_intern("gsub!"), 2, unescape_escaped_quotes, rb_str_new2("\"\"\""));
337
+ rb_funcall(listener, rb_intern("py_string"), 2, con, INT2FIX(current_line));
338
+ }
339
+
340
+ static void
341
+ raise_lexer_error(const char * at, int line)
342
+ {
343
+ rb_raise(rb_eGherkinLexingError, "Lexing error on line %d: '%s'. See http://wiki.github.com/aslakhellesoy/gherkin/lexingerror for more information.", line, at);
344
+ }
345
+
346
+ static int
347
+ count_char(char char_to_count, char *str) {
348
+
349
+ int count = 0;
350
+ int i = 0;
351
+ while(str[i] != '\0') {
352
+ if(str[i] == char_to_count) {
353
+ count++;
354
+ }
355
+ i++;
356
+ }
357
+ return count;
358
+ }
359
+
360
+ static void lexer_init(lexer_state *lexer) {
361
+ lexer->content_start = 0;
362
+ lexer->content_end = 0;
363
+ lexer->content_len = 0;
364
+ lexer->mark = 0;
365
+ lexer->keyword_start = 0;
366
+ lexer->keyword_end = 0;
367
+ lexer->next_keyword_start = 0;
368
+ lexer->line_number = 1;
369
+ lexer->last_newline = 0;
370
+ lexer->final_newline = 0;
371
+ lexer->start_col = 0;
372
+ lexer->eol = LF_FLAG;
373
+ }
374
+
375
+ static VALUE CLexer_alloc(VALUE klass)
376
+ {
377
+ VALUE obj;
378
+ lexer_state *lxr = ALLOC(lexer_state);
379
+ lexer_init(lxr);
380
+
381
+ obj = Data_Wrap_Struct(klass, NULL, -1, lxr);
382
+
383
+ return obj;
384
+ }
385
+
386
+ static VALUE CLexer_init(VALUE self, VALUE listener)
387
+ {
388
+ rb_iv_set(self, "@listener", listener);
389
+
390
+ lexer_state *lxr = NULL;
391
+ DATA_GET(self, lexer_state, lxr);
392
+ lexer_init(lxr);
393
+
394
+ return self;
395
+ }
396
+
397
+ static VALUE CLexer_scan(VALUE self, VALUE input)
398
+ {
399
+ VALUE listener = rb_iv_get(self, "@listener");
400
+
401
+ lexer_state *lexer = NULL;
402
+ DATA_GET(self, lexer_state, lexer);
403
+
404
+ VALUE input_copy = rb_str_dup(input);
405
+
406
+ rb_str_append(input_copy, rb_str_new2("\n%_FEATURE_END_%"));
407
+ char *data = RSTRING_PTR(input_copy);
408
+ size_t len = RSTRING_LEN(input_copy);
409
+
410
+ if (count_char('\r', data) > (count_char('\n', data) / 2)) {
411
+ lexer->eol = CRLF_FLAG;
412
+ }
413
+
414
+ if (len == 0) {
415
+ rb_raise(rb_eGherkinLexingError, "No content to lex.");
416
+ } else {
417
+
418
+ const char *p, *pe, *eof;
419
+ int cs = 0;
420
+
421
+ VALUE current_row = Qnil;
422
+
423
+ p = data;
424
+ pe = data + len;
425
+ eof = pe;
426
+
427
+ assert(*pe == '\0' && "pointer does not end on NULL");
428
+
429
+ %% write init;
430
+ %% write exec;
431
+
432
+ assert(p <= pe && "data overflow after parsing execute");
433
+ assert(lexer->content_start <= len && "content starts after data end");
434
+ assert(lexer->mark < len && "mark is after data end");
435
+
436
+ // Reset lexer by re-initializing the whole thing
437
+ lexer_init(lexer);
438
+
439
+ if (cs == lexer_error) {
440
+ rb_raise(rb_eGherkinLexingError, "Invalid format, lexing fails.");
441
+ } else {
442
+ return Qtrue;
443
+ }
444
+ }
445
+ }
446
+
447
+ void Init_gherkin_lexer_<%= @i18n.underscored_iso_code %>()
448
+ {
449
+ mGherkin = rb_define_module("Gherkin");
450
+ mGherkinLexer = rb_define_module_under(mGherkin, "Lexer");
451
+ rb_eGherkinLexingError = rb_const_get(mGherkinLexer, rb_intern("LexingError"));
452
+
453
+ mCLexer = rb_define_module_under(mGherkin, "CLexer");
454
+ cI18nLexer = rb_define_class_under(mCLexer, "<%= @i18n.underscored_iso_code.capitalize %>", rb_cObject);
455
+ rb_define_alloc_func(cI18nLexer, CLexer_alloc);
456
+ rb_define_method(cI18nLexer, "initialize", CLexer_init, 1);
457
+ rb_define_method(cI18nLexer, "scan", CLexer_scan, 1);
458
+ }
459
+