@@ -3,7 +3,284 @@ package Gherkin::MarkdownTokenMatcher;
33use strict;
44use warnings;
55
6- use base ' Gherkin::TokenMatcher' ;
6+ our $DEFAULT_DOC_STRING_SEPARATOR = q/ ^(```[`]*)(.*)/ ;
7+ our $KEYWORD_PREFIX_BULLET = q/ ^(\\s*[*+-]\\s*)/ ;
8+ our $KEYWORD_PREFIX_HEADER = q/ ^(#{1,6}\\s)/ ;
9+
10+ use Class::XSAccessor accessors => [
11+ qw/ dialect _default_dialect_name _indent_to_remove _active_doc_string_separator _keyword_types
12+ _matched_FeatureLine _non_star_step_keywords/ ,
13+ ];
14+
15+ use Gherkin::Dialect;
16+
17+ sub new {
18+ my ( $class , $options ) = @_ ;
19+ $options -> {' dialect' } ||= Gherkin::Dialect-> new( { dialect => ' en' } );
20+ my $self = bless $options , $class ;
21+ $self -> _default_dialect_name( $self -> dialect_name );
22+ my @non_star_step_keywords = map {
23+ grep { $_ ne ' * ' }
24+ @{ $self -> dialect-> $_ }
25+ } qw/ Given When Then And But/ ;
26+ $self -> _non_star_step_keywords( \@non_star_step_keywords );
27+ $self -> reset ();
28+ return $self ;
29+ }
30+
31+ sub _add_keyword_type_mappings {
32+ my ( $keyword_types , $keywords , $type ) = @_ ;
33+
34+ for my $keyword ( @{$keywords } ) {
35+ if ( not exists $keyword_types -> {$keyword } ) {
36+ $keyword_types -> {$keyword } = [];
37+ }
38+ push @{ $keyword_types -> {$keyword } }, $type ;
39+ }
40+ return ;
41+ }
42+
43+ sub dialect_name { return $_ [0]-> dialect-> dialect; }
44+
45+ sub change_dialect {
46+ my $self = shift ;
47+ $self -> dialect-> change_dialect(@_ );
48+
49+ my $keyword_types = {};
50+ _add_keyword_type_mappings( $keyword_types , $self -> dialect-> Given,
51+ Cucumber::Messages::Step::KEYWORDTYPE_CONTEXT );
52+ _add_keyword_type_mappings( $keyword_types , $self -> dialect-> When,
53+ Cucumber::Messages::Step::KEYWORDTYPE_ACTION );
54+ _add_keyword_type_mappings( $keyword_types , $self -> dialect-> Then,
55+ Cucumber::Messages::Step::KEYWORDTYPE_OUTCOME );
56+ _add_keyword_type_mappings( $keyword_types , [ @{ $self -> dialect-> And }, @{ $self -> dialect-> But } ],
57+ Cucumber::Messages::Step::KEYWORDTYPE_CONJUNCTION );
58+ $self -> _keyword_types($keyword_types );
59+ return ;
60+ }
61+
62+ sub reset {
63+ my $self = shift ;
64+ $self -> change_dialect( $self -> _default_dialect_name );
65+ $self -> _indent_to_remove(0);
66+ $self -> _active_doc_string_separator($DEFAULT_DOC_STRING_SEPARATOR );
67+ return ;
68+ }
69+
70+ sub match_FeatureLine {
71+ my ( $self , $token ) = @_ ;
72+ return if $self -> _matched_FeatureLine;
73+
74+ # We first try to match "# Feature: blah"
75+ my $result = $self -> _match_title_line( $KEYWORD_PREFIX_HEADER , q{ :} ,
76+ $token , FeatureLine => $self -> dialect-> Feature );
77+ # If we didn't match "# Feature: blah", we still match this line
78+ # as a FeatureLine.
79+ # The reason for this is that users may not want to be constrained by having this as their fist line.
80+ unless ($result ) {
81+ $self -> _set_token_matched( $token ,
82+ FeatureLine => { text => $token -> line-> _trimmed_line_text } );
83+ }
84+ $self -> _matched_FeatureLine(1);
85+ return 1;
86+ }
87+
88+ sub match_RuleLine {
89+ my ( $self , $token ) = @_ ;
90+ return $self -> _match_title_line( $KEYWORD_PREFIX_HEADER , q{ :} ,
91+ $token , RuleLine => $self -> dialect-> Rule );
92+ }
93+
94+ sub match_ScenarioLine {
95+ my ( $self , $token ) = @_ ;
96+ return $self -> _match_title_line( $KEYWORD_PREFIX_HEADER , q{ :} ,
97+ $token , ScenarioLine => $self -> dialect-> Scenario )
98+ || $self -> _match_title_line( $KEYWORD_PREFIX_HEADER , q{ :} ,
99+ $token , ' ScenarioLine' => $self -> dialect-> ScenarioOutline );
100+ }
101+
102+ sub match_BackgroundLine {
103+ my ( $self , $token ) = @_ ;
104+ return $self -> _match_title_line( $KEYWORD_PREFIX_HEADER , q{ :} ,
105+ $token , BackgroundLine => $self -> dialect-> Background );
106+ }
107+
108+ sub match_ExamplesLine {
109+ my ( $self , $token ) = @_ ;
110+ return $self -> _match_title_line( $KEYWORD_PREFIX_HEADER , q{ :} ,
111+ $token , ExamplesLine => $self -> dialect-> Examples );
112+ }
113+
114+ sub match_Language {
115+ my ( $self , $token ) = @_ ;
116+ # We've made a deliberate choice not to support `# language: [ISO 639-1]` headers or similar
117+ # in Markdown. Users should specify a language globally.
118+ return ;
119+ }
120+
121+ sub match_TagLine {
122+ my ( $self , $token ) = @_ ;
123+ my @tags = ();
124+ while ( $token -> line-> line_text =~ m / `(@[^`]+)`/ g ) {
125+ push @tags ,
126+ {
127+ column => 2 + length $` ,
128+ text => $1 ,
129+ };
130+ }
131+ return unless scalar @tags ;
132+ $self -> _set_token_matched( $token ,
133+ TagLine => { items => \@tags } );
134+ return 1;
135+ }
136+
137+ sub _match_title_line {
138+ my ( $self , $prefix , $keyword_suffix , $token , $token_type , $keywords ) = @_ ;
139+ my $regex = $prefix . ' (' . join ( q{ |} , @{$keywords } ) . ' )' . $keyword_suffix . ' \s*(.*)' ;
140+ if ( $token -> line-> _trimmed_line_text =~ qr /$regex / ) {
141+ my $indent = $token -> line-> indent + ( length $1 || 0 );
142+ my $keyword = $2 ;
143+ my $text = $3 ;
144+ $text =~ s /\s +$// ;
145+ my $keyword_type ;
146+ if ( exists $self -> _keyword_types-> {$keyword } ) {
147+ # only set the keyword type if this is a step keyword
148+ $keyword_type =
149+ ( scalar @{ $self -> _keyword_types-> {$keyword } } > 1 )
150+ ? Cucumber::Messages::Step::KEYWORDTYPE_UNKNOWN
151+ : $self -> _keyword_types-> {$keyword }-> [0];
152+ }
153+ $self -> _set_token_matched( $token , $token_type ,
154+ { indent => $indent , keyword => $keyword , text => $text , keyword_type => $keyword_type } );
155+ return 1;
156+ }
157+ return ;
158+ }
159+
160+ sub _set_token_matched {
161+ my ( $self , $token , $matched_type , $options ) = @_ ;
162+ $options -> {' items' } ||= [];
163+ $token -> matched_type($matched_type );
164+
165+ if ( defined $options -> {' text' } ) {
166+ chomp $options -> {' text' };
167+ $token -> matched_text( $options -> {' text' } );
168+ }
169+
170+ $token -> matched_keyword( $options -> {' keyword' } )
171+ if defined $options -> {' keyword' };
172+ $token -> matched_keyword_type( $options -> {' keyword_type' } )
173+ if defined $options -> {' keyword_type' };
174+
175+ if ( defined $options -> {' indent' } ) {
176+ $token -> matched_indent( $options -> {' indent' } );
177+ } else {
178+ $token -> matched_indent( $token -> line ? $token -> line-> indent : 0 );
179+ }
180+
181+ $token -> matched_items( $options -> {' items' } )
182+ if defined $options -> {' items' };
183+
184+ $token -> location-> {' column' } = $token -> matched_indent + 1;
185+ $token -> matched_gherkin_dialect( $self -> dialect_name );
186+ return ;
187+ }
188+
189+ sub match_EOF {
190+ my ( $self , $token ) = @_ ;
191+ if ( $token -> is_eof ) {
192+ $self -> _set_token_matched( $token , ' EOF' );
193+ return 1;
194+ }
195+ }
196+
197+ sub match_Empty {
198+ my ( $self , $token ) = @_ ;
199+ if (
200+ $token -> line-> is_empty
201+ || ( !$self -> match_TagLine($token )
202+ && !$self -> match_FeatureLine($token )
203+ && !$self -> match_ScenarioLine($token )
204+ && !$self -> match_BackgroundLine($token )
205+ && !$self -> match_ExamplesLine($token )
206+ && !$self -> match_RuleLine($token )
207+ && !$self -> match_TableRow($token )
208+ && !$self -> match_Comment($token )
209+ && !$self -> match_Language($token )
210+ && !$self -> match_DocStringSeparator($token )
211+ && !$self -> match_EOF($token )
212+ && !$self -> match_StepLine($token ) )
213+ )
214+ {
215+ $self -> _set_token_matched( $token ,
216+ Empty => { indent => 0 } );
217+ return 1;
218+ }
219+ return ;
220+ }
221+
222+ sub match_Comment {
223+ my ( $self , $token ) = @_ ;
224+ if ( $token -> line-> startswith(q{ |} )
225+ && $self -> _is_gfm_table_separator( $token -> line-> table_cells ) )
226+ {
227+ $self -> _set_token_matched( $token ,
228+ Comment => { text => $token -> line-> line_text, indent => 0 } );
229+ return 1;
230+ }
231+ return ;
232+ }
233+
234+ sub _is_gfm_table_separator {
235+ my ( $self , $table_cells ) = @_ ;
236+ my @separator_values = grep { $_ -> {' text' } =~ m / ^:?-+:?$ / } @{$table_cells };
237+ return scalar @separator_values ;
238+ }
239+
240+ sub match_Other {
241+ my ( $self , $token ) = @_ ;
242+ # take the entire line, except removing DocString indents
243+ my $text = $token -> line-> get_line_text( $self -> _indent_to_remove );
244+ $self -> _set_token_matched( $token ,
245+ Other => { indent => 0, text => $text } );
246+ return 1;
247+ }
248+
249+ sub match_StepLine {
250+ my ( $self , $token ) = @_ ;
251+ return $self -> _match_title_line( $KEYWORD_PREFIX_BULLET , q{ } ,
252+ $token , StepLine => $self -> _non_star_step_keywords );
253+ }
254+
255+ sub match_DocStringSeparator {
256+ my ( $self , $token ) = @_ ;
257+ my $active_doc_string_separator = $self -> _active_doc_string_separator;
258+ if ( $token -> line-> line_text =~ qr /$active_doc_string_separator / ) {
259+ if ( $self -> _active_doc_string_separator eq $DEFAULT_DOC_STRING_SEPARATOR ) {
260+ $self -> _active_doc_string_separator( ' ^(' . $1 . ' )$' );
261+ $self -> _indent_to_remove( $token -> line-> indent );
262+ } else {
263+ $self -> _active_doc_string_separator($DEFAULT_DOC_STRING_SEPARATOR );
264+ }
265+ $self -> _set_token_matched( $token ,
266+ DocStringSeparator => { text => q{ } , keyword => $1 } );
267+ return 1;
268+ }
269+ return ;
270+ }
271+
272+ sub match_TableRow {
273+ my ( $self , $token ) = @_ ;
274+ # Gherkin tables must be indented 2-5 spaces in order to be distinguidedn from non-Gherkin tables
275+ if ( $token -> line-> line_text =~ m / ^\s\s\s ?\s ?\s ?\| / ) {
276+ my $table_cells = $token -> line-> table_cells;
277+ return if ( $self -> _is_gfm_table_separator($table_cells ) );
278+ $self -> _set_token_matched( $token ,
279+ TableRow => { items => $table_cells } );
280+ return 1;
281+ }
282+ return ;
283+ }
7284
82851;
9286
0 commit comments