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