gherkin 0.0.3-universal-java-1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. data/.gitignore +8 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +47 -0
  4. data/Rakefile +48 -0
  5. data/VERSION.yml +4 -0
  6. data/bin/gherkin +10 -0
  7. data/cucumber.yml +3 -0
  8. data/ext/gherkin_lexer/.gitignore +6 -0
  9. data/ext/gherkin_lexer/extconf.rb +6 -0
  10. data/features/feature_parser.feature +206 -0
  11. data/features/native_lexer.feature +19 -0
  12. data/features/parser_with_native_lexer.feature +205 -0
  13. data/features/pretty_printer.feature +11 -0
  14. data/features/step_definitions/gherkin_steps.rb +34 -0
  15. data/features/step_definitions/pretty_printer_steps.rb +51 -0
  16. data/features/steps_parser.feature +46 -0
  17. data/features/support/env.rb +33 -0
  18. data/gherkin.gemspec +177 -0
  19. data/java/.gitignore +2 -0
  20. data/java/Gherkin.iml +24 -0
  21. data/java/build.xml +13 -0
  22. data/java/src/gherkin/FixJava.java +34 -0
  23. data/java/src/gherkin/Lexer.java +5 -0
  24. data/java/src/gherkin/LexingError.java +7 -0
  25. data/java/src/gherkin/Listener.java +27 -0
  26. data/java/src/gherkin/ParseError.java +22 -0
  27. data/java/src/gherkin/Parser.java +185 -0
  28. data/java/src/gherkin/lexer/.gitignore +1 -0
  29. data/java/src/gherkin/parser/StateMachineReader.java +62 -0
  30. data/lib/.gitignore +2 -0
  31. data/lib/gherkin.rb +2 -0
  32. data/lib/gherkin/c_lexer.rb +10 -0
  33. data/lib/gherkin/i18n.yml +535 -0
  34. data/lib/gherkin/i18n_lexer.rb +29 -0
  35. data/lib/gherkin/java_lexer.rb +10 -0
  36. data/lib/gherkin/lexer.rb +42 -0
  37. data/lib/gherkin/parser.rb +19 -0
  38. data/lib/gherkin/parser/meta.txt +4 -0
  39. data/lib/gherkin/parser/root.txt +9 -0
  40. data/lib/gherkin/parser/steps.txt +3 -0
  41. data/lib/gherkin/rb_lexer.rb +9 -0
  42. data/lib/gherkin/rb_lexer/.gitignore +1 -0
  43. data/lib/gherkin/rb_lexer/README.rdoc +8 -0
  44. data/lib/gherkin/rb_parser.rb +117 -0
  45. data/lib/gherkin/tools/pretty_printer.rb +77 -0
  46. data/ragel/i18n/.gitignore +1 -0
  47. data/ragel/lexer.c.rl.erb +385 -0
  48. data/ragel/lexer.java.rl.erb +198 -0
  49. data/ragel/lexer.rb.rl.erb +172 -0
  50. data/ragel/lexer_common.rl.erb +46 -0
  51. data/spec/gherkin/c_lexer_spec.rb +21 -0
  52. data/spec/gherkin/fixtures/complex.feature +43 -0
  53. data/spec/gherkin/fixtures/i18n_fr.feature +13 -0
  54. data/spec/gherkin/fixtures/i18n_no.feature +6 -0
  55. data/spec/gherkin/fixtures/i18n_zh-CN.feature +8 -0
  56. data/spec/gherkin/fixtures/simple.feature +3 -0
  57. data/spec/gherkin/fixtures/simple_with_comments.feature +7 -0
  58. data/spec/gherkin/fixtures/simple_with_tags.feature +11 -0
  59. data/spec/gherkin/i18n_spec.rb +57 -0
  60. data/spec/gherkin/java_lexer_spec.rb +20 -0
  61. data/spec/gherkin/parser_spec.rb +28 -0
  62. data/spec/gherkin/rb_lexer_spec.rb +18 -0
  63. data/spec/gherkin/sexp_recorder.rb +29 -0
  64. data/spec/gherkin/shared/lexer_spec.rb +420 -0
  65. data/spec/gherkin/shared/py_string_spec.rb +112 -0
  66. data/spec/gherkin/shared/table_spec.rb +97 -0
  67. data/spec/gherkin/shared/tags_spec.rb +50 -0
  68. data/spec/spec_helper.rb +53 -0
  69. data/tasks/bench.rake +176 -0
  70. data/tasks/bench/feature_builder.rb +49 -0
  71. data/tasks/bench/generated/.gitignore +1 -0
  72. data/tasks/bench/null_listener.rb +4 -0
  73. data/tasks/cucumber.rake +20 -0
  74. data/tasks/ext.rake +49 -0
  75. data/tasks/ragel.rake +94 -0
  76. data/tasks/rdoc.rake +12 -0
  77. data/tasks/rspec.rake +15 -0
  78. metadata +204 -0
@@ -0,0 +1,198 @@
1
+ package gherkin.lexer;
2
+
3
+ import java.util.List;
4
+ import java.util.ArrayList;
5
+ import java.util.regex.Pattern;
6
+ import gherkin.Lexer;
7
+ import gherkin.Listener;
8
+ import gherkin.LexingError;
9
+
10
+ public class <%= i18n_lexer_class_name %> implements Lexer {
11
+ %%{
12
+ machine lexer;
13
+ alphtype byte;
14
+
15
+ action begin_content {
16
+ contentStart = p;
17
+ currentLine = lineNumber;
18
+ }
19
+
20
+ action start_pystring {
21
+ currentLine = lineNumber;
22
+ startCol = p - lastNewline;
23
+ }
24
+
25
+ action begin_pystring_content {
26
+ contentStart = p;
27
+ }
28
+
29
+ action store_pystring_content {
30
+ String con = unindent(startCol, substring(data, contentStart, nextKeywordStart-1).replaceFirst("(\\r?\\n)?( )*\\Z", ""));
31
+ listener.py_string(con, currentLine);
32
+ }
33
+
34
+ action store_feature_content {
35
+ String con = multilineStrip(keywordContent(data, p, eof, nextKeywordStart, contentStart).trim());
36
+ listener.feature(keyword, con, currentLine);
37
+ if(nextKeywordStart != -1) p = nextKeywordStart - 1;
38
+ nextKeywordStart = -1;
39
+ }
40
+
41
+ action store_background_content {
42
+ String con = multilineStrip(keywordContent(data, p, eof, nextKeywordStart, contentStart));
43
+ listener.background(keyword, con, currentLine);
44
+ if(nextKeywordStart != -1) p = nextKeywordStart - 1;
45
+ nextKeywordStart = -1;
46
+ }
47
+
48
+ action store_scenario_content {
49
+ String con = multilineStrip(keywordContent(data, p, eof, nextKeywordStart, contentStart));
50
+ listener.scenario(keyword, con, currentLine);
51
+ if(nextKeywordStart != -1) p = nextKeywordStart - 1;
52
+ nextKeywordStart = -1;
53
+ }
54
+
55
+ action store_scenario_outline_content {
56
+ String con = multilineStrip(keywordContent(data, p, eof, nextKeywordStart, contentStart));
57
+ listener.scenario_outline(keyword, con, currentLine);
58
+ if(nextKeywordStart != -1) p = nextKeywordStart - 1;
59
+ nextKeywordStart = -1;
60
+ }
61
+
62
+ action store_examples_content {
63
+ String con = multilineStrip(keywordContent(data, p, eof, nextKeywordStart, contentStart));
64
+ listener.examples(keyword, con, currentLine);
65
+ if(nextKeywordStart != -1) p = nextKeywordStart - 1;
66
+ nextKeywordStart = -1;
67
+ }
68
+
69
+ action store_step_content {
70
+ listener.step(keyword, substring(data, contentStart, p).trim(), currentLine);
71
+ }
72
+
73
+ action store_comment_content {
74
+ listener.comment(substring(data, contentStart, p).trim(), lineNumber);
75
+ }
76
+
77
+ action store_tag_content {
78
+ listener.tag(substring(data, contentStart, p).trim(), currentLine);
79
+ }
80
+
81
+ action inc_line_number {
82
+ lineNumber++;
83
+ }
84
+
85
+ action last_newline {
86
+ lastNewline = p + 1;
87
+ }
88
+
89
+ action start_keyword {
90
+ if(keywordStart == -1) keywordStart = p;
91
+ }
92
+
93
+ action end_keyword {
94
+ keyword = substring(data, keywordStart, p).replaceFirst(":$","").trim();
95
+ keywordStart = -1;
96
+ }
97
+
98
+ action next_keyword_start {
99
+ nextKeywordStart = p;
100
+ }
101
+
102
+ action start_table {
103
+ p = p - 1;
104
+ rows = new ArrayList<List<String>>();
105
+ currentLine = lineNumber;
106
+ }
107
+
108
+ action start_row {
109
+ currentRow = new ArrayList<String>();
110
+ }
111
+
112
+ action begin_cell_content {
113
+ contentStart = p;
114
+ }
115
+
116
+ action store_cell_content {
117
+ currentRow.add(substring(data, contentStart, p).trim());
118
+ }
119
+
120
+ action store_row {
121
+ rows.add(currentRow);
122
+ }
123
+
124
+ action store_table {
125
+ if(!rows.isEmpty()) {
126
+ listener.table(rows, currentLine);
127
+ }
128
+ }
129
+
130
+ action end_feature {
131
+ if(cs < lexer_first_final) {
132
+ String content = currentLineContent(data, lastNewline);
133
+ throw new LexingError("Lexing error on line " + lineNumber);
134
+ }
135
+ }
136
+
137
+ include lexer_common "lexer_common.<%= i18n_language %>.rl";
138
+ }%%
139
+
140
+ private final Listener listener;
141
+
142
+ public <%= i18n_lexer_class_name %>(Listener listener) {
143
+ this.listener = listener;
144
+ }
145
+
146
+ %% write data noerror;
147
+
148
+ public void scan(CharSequence inputSequence) {
149
+ String input = inputSequence.toString() + "\n%_FEATURE_END_%";
150
+ byte[] data = input.getBytes();
151
+ int cs, p = 0, pe = data.length;
152
+ int eof = pe;
153
+
154
+ int lineNumber = 1;
155
+ int lastNewline = 0;
156
+
157
+ int contentStart = -1;
158
+ int currentLine = -1;
159
+ int startCol = -1;
160
+ int nextKeywordStart = -1;
161
+ int keywordStart = -1;
162
+ String keyword = null;
163
+ List<List<String>> rows = null;
164
+ List<String> currentRow = null;
165
+
166
+ %% write init;
167
+ %% write exec;
168
+ }
169
+
170
+ private String keywordContent(byte[] data, int p, int eof, int nextKeywordStart, int contentStart) {
171
+ int endPoint = (nextKeywordStart == -1 || (p == eof)) ? p : nextKeywordStart;
172
+ return substring(data, contentStart, endPoint);
173
+ }
174
+
175
+ private String multilineStrip(String text) {
176
+ StringBuffer result = new StringBuffer();
177
+ for(String s : text.split("\n")) {
178
+ result.append(s.trim()).append("\n");
179
+ }
180
+ return result.toString().trim();
181
+ }
182
+
183
+ private String unindent(int startCol, String text) {
184
+ return Pattern.compile("^ {0," + startCol + "}", Pattern.MULTILINE).matcher(text).replaceAll("");
185
+ }
186
+
187
+ private String currentLineContent(byte[] data, int lastNewline) {
188
+ return substring(data, lastNewline, data.length).trim();
189
+ }
190
+
191
+ private String substring(byte[] data, int start, int end) {
192
+ try {
193
+ return new String(data, start, end-start, "utf-8");
194
+ } catch(java.io.UnsupportedEncodingException e) {
195
+ throw new RuntimeException("Internal error", e);
196
+ }
197
+ }
198
+ }
@@ -0,0 +1,172 @@
1
+ module Gherkin
2
+ module RbLexer
3
+ class <%= i18n_lexer_class_name %> #:nodoc:
4
+ %%{
5
+ # patterns:
6
+ # * data[start...end].pack("c*").strip_of_some_sort
7
+ # * changing the end point of the range according to next_keyword_start
8
+ # * methods taking the machine state because Ragel doesn't seem to know about ivars
9
+
10
+ machine lexer;
11
+
12
+ action begin_content {
13
+ @content_start = p
14
+ @current_line = @line_number
15
+ }
16
+
17
+ action start_pystring {
18
+ @current_line = @line_number
19
+ @start_col = p - @last_newline
20
+ }
21
+
22
+ action begin_pystring_content {
23
+ @content_start = p
24
+ }
25
+
26
+ action store_pystring_content {
27
+ con = unindent(@start_col, data[@content_start...@next_keyword_start-1].pack("c*").sub(/(\r?\n)?( )*\Z/, ''))
28
+ @listener.py_string(con, @current_line)
29
+ }
30
+
31
+ action store_feature_content {
32
+ store_keyword_content(:feature, data, p, eof) { |con| multiline_strip(con) }
33
+ p = @next_keyword_start - 1 if @next_keyword_start
34
+ @next_keyword_start = nil
35
+ }
36
+
37
+ action store_background_content {
38
+ store_keyword_content(:background, data, p, eof) { |con| multiline_strip(con) }
39
+ p = @next_keyword_start - 1 if @next_keyword_start
40
+ @next_keyword_start = nil
41
+ }
42
+
43
+ action store_scenario_content {
44
+ store_keyword_content(:scenario, data, p, eof) { |con| multiline_strip(con) }
45
+ p = @next_keyword_start - 1 if @next_keyword_start
46
+ @next_keyword_start = nil
47
+ }
48
+
49
+ action store_scenario_outline_content {
50
+ store_keyword_content(:scenario_outline, data, p, eof) { |con| multiline_strip(con) }
51
+ p = @next_keyword_start - 1 if @next_keyword_start
52
+ @next_keyword_start = nil
53
+ }
54
+
55
+ action store_examples_content {
56
+ store_keyword_content(:examples, data, p, eof) { |con| multiline_strip(con) }
57
+ p = @next_keyword_start - 1 if @next_keyword_start
58
+ @next_keyword_start = nil
59
+ }
60
+
61
+ action store_step_content {
62
+ con = data[@content_start...p].pack("c*").strip
63
+ @listener.step(@keyword, con, @current_line)
64
+ }
65
+
66
+ action store_comment_content {
67
+ con = data[@content_start...p].pack("c*").strip
68
+ @listener.comment(con, @line_number)
69
+ }
70
+
71
+ action store_tag_content {
72
+ con = data[@content_start...p].pack("c*").strip
73
+ @listener.tag(con, @current_line)
74
+ }
75
+
76
+ action inc_line_number {
77
+ @line_number += 1
78
+ }
79
+
80
+ action last_newline {
81
+ @last_newline = p + 1
82
+ }
83
+
84
+ action start_keyword {
85
+ @keyword_start ||= p
86
+ }
87
+
88
+ action end_keyword {
89
+ @keyword = data[@keyword_start...p].pack("c*").sub(/:$/,'').strip
90
+ @keyword_start = nil
91
+ }
92
+
93
+ action next_keyword_start {
94
+ @next_keyword_start = p
95
+ }
96
+
97
+ action start_table {
98
+ p = p - 1
99
+ @rows = []
100
+ @current_line = @line_number
101
+ }
102
+
103
+ action start_row {
104
+ current_row = []
105
+ }
106
+
107
+ action begin_cell_content {
108
+ @content_start = p
109
+ }
110
+
111
+ action store_cell_content {
112
+ con = data[@content_start...p].pack("c*").strip
113
+ current_row << con
114
+ }
115
+
116
+ action store_row {
117
+ @rows << current_row
118
+ }
119
+
120
+ action store_table {
121
+ if @rows.size != 0
122
+ @listener.table(@rows, @current_line)
123
+ end
124
+ }
125
+
126
+ action end_feature {
127
+ if cs < lexer_first_final
128
+ content = current_line_content(data, p)
129
+ raise Lexer::LexingError.new("Lexing error on line %d: '%s'." % [@line_number, content])
130
+ end
131
+ }
132
+
133
+ include lexer_common "lexer_common.<%= i18n_language %>.rl";
134
+ }%%
135
+
136
+ def initialize(listener)
137
+ @listener = listener
138
+ %% write data;
139
+ end
140
+
141
+ def scan(data)
142
+ data = (data + "\n%_FEATURE_END_%").unpack("c*") # Explicit EOF simplifies things considerably
143
+ eof = pe = data.length
144
+
145
+ @line_number = 1
146
+ @last_newline = 0
147
+
148
+ %% write init;
149
+ %% write exec;
150
+ end
151
+
152
+ def multiline_strip(text)
153
+ text.split("\n").map{|s| s.strip}.join("\n").strip
154
+ end
155
+
156
+ def unindent(startcol, text)
157
+ text.gsub(/^ {0,#{startcol}}/, "")
158
+ end
159
+
160
+ def store_keyword_content(event, data, p, eof)
161
+ end_point = (!@next_keyword_start or (p == eof)) ? p : @next_keyword_start
162
+ con = yield data[@content_start...end_point].pack("c*")
163
+ @listener.send(event, @keyword, con, @current_line)
164
+ end
165
+
166
+ def current_line_content(data, p)
167
+ rest = data[@last_newline..-1]
168
+ rest[0..rest.index(10)||-1].pack("c*").strip
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,46 @@
1
+ %%{
2
+ machine lexer_common;
3
+
4
+ # Language specific
5
+ I18N_Feature = <%= i18n['feature'] %> >start_keyword %end_keyword;
6
+ I18N_Background = <%= i18n['background'] %> >start_keyword %end_keyword;
7
+ I18N_ScenarioOutline = <%= i18n['scenario_outline'] %> >start_keyword %end_keyword;
8
+ I18N_Scenario = <%= i18n['scenario'] %> >start_keyword %end_keyword;
9
+ I18N_Step = (<%= i18n['given'] %> | <%= i18n['when'] %> | <%= i18n['and'] %> | <%= i18n['then'] %> | <%= i18n['but'] %>) >start_keyword %end_keyword;
10
+ I18N_Examples = <%= i18n['examples'] %> >start_keyword %end_keyword;
11
+
12
+ EOF = '%_FEATURE_END_%'; # Explicit EOF added before scanning begins
13
+ EOL = ('\r'? '\n') @inc_line_number @last_newline;
14
+
15
+ FeatureHeadingEnd = EOL+ space* (I18N_Background | I18N_Scenario | I18N_ScenarioOutline | '@' | '#' | EOF) >next_keyword_start;
16
+ ScenarioHeadingEnd = EOL+ space* ( I18N_Scenario | I18N_ScenarioOutline | I18N_Step | '@' | '#' | EOF ) >next_keyword_start;
17
+ BackgroundHeadingEnd = EOL+ space* ( I18N_Scenario | I18N_ScenarioOutline | I18N_Step | '@' | '#'| EOF ) >next_keyword_start;
18
+ ScenarioOutlineHeadingEnd = EOL+ space* ( I18N_Scenario | I18N_Step | '@' | '#' | EOF ) >next_keyword_start;
19
+ ExamplesHeadingEnd = EOL+ space* '|' >next_keyword_start;
20
+
21
+ FeatureHeading = space* I18N_Feature %begin_content ^FeatureHeadingEnd* :>> FeatureHeadingEnd @store_feature_content;
22
+ BackgroundHeading = space* I18N_Background %begin_content ^BackgroundHeadingEnd* :>> BackgroundHeadingEnd @store_background_content;
23
+ ScenarioHeading = space* I18N_Scenario %begin_content ^ScenarioHeadingEnd* :>> ScenarioHeadingEnd @store_scenario_content;
24
+ ScenarioOutlineHeading = space* I18N_ScenarioOutline %begin_content ^ScenarioOutlineHeadingEnd* :>> ScenarioOutlineHeadingEnd @store_scenario_outline_content;
25
+ ExamplesHeading = space* I18N_Examples %begin_content ^ExamplesHeadingEnd* :>> ExamplesHeadingEnd @store_examples_content;
26
+
27
+ Step = space* I18N_Step %begin_content ^EOL+ %store_step_content EOL+;
28
+ Comment = space* '#' >begin_content ^EOL* %store_comment_content EOL+;
29
+
30
+ Tag = ( '@' [^@\r\n\t ]+ >begin_content ) %store_tag_content;
31
+ Tags = space* (Tag space*)+ EOL+;
32
+
33
+ StartTable = space* '|' >start_table;
34
+ EndTable = EOL space* ^('|') >next_keyword_start;
35
+ Cell = '|' (any - '|')* >begin_cell_content %store_cell_content;
36
+ Row = space* Cell* >start_row '|' :>> (space* EOL+ space*) %store_row;
37
+ Table = StartTable :>> Row+ %store_table <: EndTable?;
38
+
39
+ StartPyString = '"""' >start_pystring space* :>> EOL;
40
+ EndPyString = (space* '"""') >next_keyword_start;
41
+ PyString = space* StartPyString %begin_pystring_content (^EOL | EOL)* :>> EndPyString %store_pystring_content space* EOL+;
42
+
43
+ Tokens = (space | EOL)* (Tags | Comment | FeatureHeading | BackgroundHeading | ScenarioHeading | ScenarioOutlineHeading | ExamplesHeading | Step | Table | PyString)* (space | EOL)* EOF;
44
+
45
+ main := Tokens %end_feature @!end_feature;
46
+ }%%
@@ -0,0 +1,21 @@
1
+ #encoding: utf-8
2
+ unless defined?(JRUBY_VERSION)
3
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
4
+ require 'gherkin/c_lexer'
5
+
6
+ module Gherkin
7
+ module Lexer
8
+ describe "C Lexer" do
9
+ before do
10
+ @listener = Gherkin::SexpRecorder.new
11
+ @lexer = Gherkin::CLexer['en'].new(@listener)
12
+ end
13
+
14
+ it_should_behave_like "a Gherkin lexer"
15
+ it_should_behave_like "a Gherkin lexer lexing tags"
16
+ it_should_behave_like "a Gherkin lexer lexing py_strings"
17
+ it_should_behave_like "a Gherkin lexer lexing tables"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,43 @@
1
+ #Comment on line 1
2
+ @tag1 @tag2
3
+ #Comment on line 3
4
+ Feature: Feature Text
5
+ In order to test multiline forms
6
+ As a ragel writer
7
+ I need to check for complex combinations
8
+
9
+ #Comment on line 9
10
+
11
+ #Comment on line 11
12
+
13
+ Background:
14
+ Given this is a background step
15
+ And this is another one
16
+
17
+ @tag3 @tag4
18
+ Scenario: Reading a Scenario
19
+ Given there is a step
20
+ But not another step
21
+
22
+ @tag3
23
+ Scenario: Reading a second scenario
24
+ #Comment on line 24
25
+ Given a third step with a table
26
+ |a|b|
27
+ |c|d|
28
+ |e|f|
29
+ And I am still testing things
30
+ |g|h|
31
+ |e|r|
32
+ |k|i|
33
+ |n||
34
+ And I am done testing these tables
35
+ #Comment on line 29
36
+ Then I am happy
37
+
38
+ Scenario: Hammerzeit
39
+ Given All work and no play
40
+ """
41
+ Makes Homer something something
42
+ """
43
+ Then crazy