Skip to content

Commit 090eacc

Browse files
committed
[Perl] implements MarkdownTokenMatcher
1 parent f1e3ad3 commit 090eacc

File tree

2 files changed

+282
-3
lines changed

2 files changed

+282
-3
lines changed

perl/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ SOURCE_FILES = $(shell find lib -name "*.pm" | grep -v $(GHERKIN_PARSER) | grep
88
GHERKIN = bin/gherkin
99
GHERKIN_GENERATE_TOKENS = bin/gherkin-generate-tokens
1010

11-
GOOD_FEATURE_FILES = $(shell find ../testdata/good -name "*.feature")
12-
BAD_FEATURE_FILES = $(shell find ../testdata/bad -name "*.feature")
11+
GOOD_FEATURE_FILES = $(shell find ../testdata/good -name "*.feature" -o -name "*.feature.md")
12+
BAD_FEATURE_FILES = $(shell find ../testdata/bad -name "*.feature" -o -name "*.feature.md")
1313

1414
TOKENS = $(patsubst ../testdata/%,acceptance/testdata/%.tokens,$(GOOD_FEATURE_FILES))
1515
ASTS = $(patsubst ../testdata/%,acceptance/testdata/%.ast.ndjson,$(GOOD_FEATURE_FILES))

perl/lib/Gherkin/MarkdownTokenMatcher.pm

Lines changed: 280 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,286 @@ package Gherkin::MarkdownTokenMatcher;
33
use strict;
44
use warnings;
55

6-
use base 'Gherkin::TokenMatcher';
6+
my $DEFAULT_DOC_STRING_SEPARATOR = q/^(```[`]*)(.*)/;
7+
my $KEYWORD_PREFIX_BULLET = q/^(\\s*[*+-]\\s*)/;
8+
my $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, ':',
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, ':',
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, ':',
97+
$token, ScenarioLine => $self->dialect->Scenario )
98+
|| $self->_match_title_line( $KEYWORD_PREFIX_HEADER, ':',
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, ':',
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, ':',
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(
126+
@tags,
127+
{
128+
column => 2 + length($`),
129+
text => $1,
130+
}
131+
);
132+
}
133+
return unless scalar(@tags);
134+
$self->_set_token_matched( $token,
135+
TagLine => { items => \@tags } );
136+
return 1;
137+
}
138+
139+
sub _match_title_line {
140+
my ( $self, $prefix, $keyword_suffix, $token, $token_type, $keywords ) = @_;
141+
my $regex = $prefix . '(' . join( '|', @{$keywords} ) . ')' . $keyword_suffix . '\s*(.*)';
142+
if ( $token->line->_trimmed_line_text =~ qr/$regex/ ) {
143+
my $indent = $token->line->indent + ( length($1) || 0 );
144+
my $keyword = $2;
145+
my $text = $3;
146+
$text =~ s/\s+$//;
147+
my $keyword_type;
148+
if ( exists $self->_keyword_types->{$keyword} ) {
149+
# only set the keyword type if this is a step keyword
150+
$keyword_type =
151+
( scalar( @{ $self->_keyword_types->{$keyword} } ) > 1 )
152+
? Cucumber::Messages::Step::KEYWORDTYPE_UNKNOWN
153+
: $self->_keyword_types->{$keyword}->[0];
154+
}
155+
$self->_set_token_matched( $token, $token_type,
156+
{ indent => $indent, keyword => $keyword, text => $text, keyword_type => $keyword_type } );
157+
return 1;
158+
}
159+
return;
160+
}
161+
162+
sub _set_token_matched {
163+
my ( $self, $token, $matched_type, $options ) = @_;
164+
$options->{'items'} ||= [];
165+
$token->matched_type($matched_type);
166+
167+
if ( defined $options->{'text'} ) {
168+
chomp $options->{'text'};
169+
$token->matched_text( $options->{'text'} );
170+
}
171+
172+
$token->matched_keyword( $options->{'keyword'} )
173+
if defined $options->{'keyword'};
174+
$token->matched_keyword_type( $options->{'keyword_type'} )
175+
if defined $options->{'keyword_type'};
176+
177+
if ( defined $options->{'indent'} ) {
178+
$token->matched_indent( $options->{'indent'} );
179+
} else {
180+
$token->matched_indent( $token->line ? $token->line->indent : 0 );
181+
}
182+
183+
$token->matched_items( $options->{'items'} )
184+
if defined $options->{'items'};
185+
186+
$token->location->{'column'} = $token->matched_indent + 1;
187+
$token->matched_gherkin_dialect( $self->dialect_name );
188+
return;
189+
}
190+
191+
sub match_EOF {
192+
my ( $self, $token ) = @_;
193+
if ( $token->is_eof ) {
194+
$self->_set_token_matched( $token, 'EOF' );
195+
return 1;
196+
}
197+
}
198+
199+
sub match_Empty {
200+
my ( $self, $token ) = @_;
201+
if (
202+
$token->line->is_empty
203+
|| ( !$self->match_TagLine($token)
204+
&& !$self->match_FeatureLine($token)
205+
&& !$self->match_ScenarioLine($token)
206+
&& !$self->match_BackgroundLine($token)
207+
&& !$self->match_ExamplesLine($token)
208+
&& !$self->match_RuleLine($token)
209+
&& !$self->match_TableRow($token)
210+
&& !$self->match_Comment($token)
211+
&& !$self->match_Language($token)
212+
&& !$self->match_DocStringSeparator($token)
213+
&& !$self->match_EOF($token)
214+
&& !$self->match_StepLine($token) )
215+
)
216+
{
217+
$self->_set_token_matched( $token,
218+
Empty => { indent => 0 } );
219+
return 1;
220+
}
221+
return;
222+
}
223+
224+
sub match_Comment {
225+
my ( $self, $token ) = @_;
226+
if ( $token->line->startswith('|')
227+
&& $self->_is_gfm_table_separator( $token->line->table_cells ) )
228+
{
229+
$self->_set_token_matched( $token,
230+
Comment => { text => $token->line->line_text, indent => 0 } );
231+
return 1;
232+
}
233+
return;
234+
}
235+
236+
sub _is_gfm_table_separator {
237+
my ( $self, $table_cells ) = @_;
238+
my @separator_values = grep { $_->{'text'} =~ m/^:?-+:?$/ } @{$table_cells};
239+
return scalar(@separator_values) > 0;
240+
}
241+
242+
sub match_Other {
243+
my ( $self, $token ) = @_;
244+
# take the entire line, except removing DocString indents
245+
my $text = $token->line->get_line_text( $self->_indent_to_remove );
246+
$self->_set_token_matched( $token,
247+
Other => { indent => 0, text => $text } );
248+
return 1;
249+
}
250+
251+
sub match_StepLine {
252+
my ( $self, $token ) = @_;
253+
return $self->_match_title_line( $KEYWORD_PREFIX_BULLET, '',
254+
$token, StepLine => $self->_non_star_step_keywords );
255+
}
256+
257+
sub match_DocStringSeparator {
258+
my ( $self, $token ) = @_;
259+
my $active_doc_string_separator = $self->_active_doc_string_separator;
260+
if ( $token->line->line_text =~ qr/$active_doc_string_separator/ ) {
261+
if ( $self->_active_doc_string_separator eq $DEFAULT_DOC_STRING_SEPARATOR ) {
262+
$self->_active_doc_string_separator( '^(' . $1 . ')$' );
263+
$self->_indent_to_remove( $token->line->indent );
264+
} else {
265+
$self->_active_doc_string_separator($DEFAULT_DOC_STRING_SEPARATOR);
266+
}
267+
$self->_set_token_matched( $token,
268+
DocStringSeparator => { text => '', keyword => $1 } );
269+
return 1;
270+
}
271+
return;
272+
}
273+
274+
sub match_TableRow {
275+
my ( $self, $token ) = @_;
276+
# Gherkin tables must be indented 2-5 spaces in order to be distinguidedn from non-Gherkin tables
277+
if ( $token->line->line_text =~ m/^\s\s\s?\s?\s?\|/ ) {
278+
my $table_cells = $token->line->table_cells;
279+
return if ( $self->_is_gfm_table_separator($table_cells) );
280+
$self->_set_token_matched( $token,
281+
TableRow => { items => $table_cells } );
282+
return 1;
283+
}
284+
return;
285+
}
7286

8287
1;
9288

0 commit comments

Comments
 (0)