| 1 | package Gherkin; | ||
| 2 | $Gherkin::VERSION = '42.0.1'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Encode qw(encode_utf8 find_encoding); | ||
| 6 | use Scalar::Util qw( blessed ); | ||
| 7 | |||
| 8 | use Cucumber::Messages; | ||
| 9 | |||
| 10 | use Gherkin::AstBuilder; | ||
| 11 | use Gherkin::Parser; | ||
| 12 | use Gherkin::Pickles::Compiler; | ||
| 13 | use Gherkin::TokenMatcher; | ||
| 14 | use Gherkin::MarkdownTokenMatcher; | ||
| 15 | |||
| 16 | |||
| 17 | use Class::XSAccessor accessors => | ||
| 18 | [ qw/ include_source include_ast include_pickles predictable_ids | ||
| 19 | _producer /, ]; | ||
| 20 | |||
| 21 | |||
| 22 | sub new { | ||
| 23 | my ($class, %options) = @_; | ||
| 24 | |||
| 25 | return bless { | ||
| 26 | include_source => 1, | ||
| 27 | include_ast => 1, | ||
| 28 | include_pickles => 1, | ||
| 29 | %options, | ||
| 30 | }, $class; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub from_paths { | ||
| 34 | my ($class, $paths, $id_generator, $sink, %options) = @_; | ||
| 35 | |||
| 36 | my $gherkin = $class->new(%options); | ||
| 37 | for my $path (@{$paths}) { | ||
| 38 | # Note: There's a huge difference between ':utf8' and | ||
| 39 | # ':encoding(UTF-8)' in Perl: the latter causes strict UTF-8 conversion | ||
| 40 | # and fails hard if there are encoding problems. The former | ||
| 41 | # accommodates the errors and simply continues, allowing us to | ||
| 42 | # recode back to octets and then to the encoding indicated in the | ||
| 43 | # header using the "# encoding: ..." header. | ||
| 44 | ## no critic (RequireEncodingWithUTF8Layer) | ||
| 45 | open my $fh, '<:utf8', $path | ||
| 46 | or die "Unable to open gherkin document $path: $!"; | ||
| 47 | ## use critic | ||
| 48 | |||
| 49 | # local $/ = undef; --> unset 'end-of-line' marker: slurp entire file | ||
| 50 | # use the 'do' block to scope this binding to smallest possible scope | ||
| 51 | my $content = do { local $/ = undef; <$fh> }; | ||
| 52 | close $fh | ||
| 53 | or warn "Unable to close gherkin document $path: $!"; | ||
| 54 | |||
| 55 | $gherkin->from_source( | ||
| 56 | Cucumber::Messages::Envelope->new( | ||
| 57 | source => Cucumber::Messages::Source->new( | ||
| 58 | uri => $path, | ||
| 59 | data => $content, | ||
| 60 | media_type => $path =~ m/\.md$/ | ||
| 61 | ? Cucumber::Messages::Source::MEDIATYPE_TEXT_X_CUCUMBER_GHERKIN_MARKDOWN | ||
| 62 | : Cucumber::Messages::Source::MEDIATYPE_TEXT_X_CUCUMBER_GHERKIN_PLAIN, | ||
| 63 | ) | ||
| 64 | ), | ||
| 65 | $id_generator, | ||
| 66 | $sink); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | sub _parse_source_encoding_header { | ||
| 71 | my ($envelope) = @_; | ||
| 72 | my $source = $envelope->source; | ||
| 73 | my $header_end = 0; | ||
| 74 | my @header = grep { | ||
| 75 | not ($header_end ||= ($_ !~ m/^\s*#/)) | ||
| 76 | } split( /\n/, $source->data ); | ||
| 77 | my $encoding; | ||
| 78 | for my $line (@header) { | ||
| 79 | if ($line =~ m/\s*#\s+encoding:\s+(\S+)/) { | ||
| 80 | $encoding = $1; | ||
| 81 | last; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | if ($encoding) { | ||
| 85 | my $enc = find_encoding($encoding); | ||
| 86 | my $uri = $source->uri; | ||
| 87 | die "Header in $uri specifies unknown encoding $encoding" | ||
| 88 | unless $enc; | ||
| 89 | $source->data( $enc->decode(encode_utf8($source->data)) ); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _parser_error_message { | ||
| 94 | my ( $error, $uri ) = @_; | ||
| 95 | return Cucumber::Messages::Envelope->new( | ||
| 96 | parse_error => Cucumber::Messages::ParseError->new( | ||
| 97 | source => Cucumber::Messages::SourceReference->new( | ||
| 98 | uri => $uri, | ||
| 99 | location => Cucumber::Messages::Location->new( | ||
| 100 | line => $error->location->{line}, | ||
| 101 | column => $error->location->{column}, | ||
| 102 | ), | ||
| 103 | ), | ||
| 104 | message => $error->stringify, | ||
| 105 | ) | ||
| 106 | ); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub from_source { | ||
| 110 | my ($self, $envelope, $id_generator, $sink) = @_; | ||
| 111 | |||
| 112 | _parse_source_encoding_header($envelope); | ||
| 113 | if ($self->include_source) { | ||
| 114 | $sink->($envelope); | ||
| 115 | } | ||
| 116 | |||
| 117 | if ($self->include_ast or $self->include_pickles) { | ||
| 118 | my $source = $envelope->source; | ||
| 119 | my $parser = Gherkin::Parser->new( | ||
| 120 | Gherkin::AstBuilder->new($id_generator), | ||
| 121 | $source->media_type eq Cucumber::Messages::Source::MEDIATYPE_TEXT_X_CUCUMBER_GHERKIN_MARKDOWN | ||
| 122 | ? Gherkin::MarkdownTokenMatcher->new() | ||
| 123 | : Gherkin::TokenMatcher->new() | ||
| 124 | ); | ||
| 125 | my $data = $source->data; | ||
| 126 | |||
| 127 | local $@; | ||
| 128 | my $ast_msg; | ||
| 129 | if (eval { $ast_msg = $parser->parse( \$data, $source->uri); 1 }) { | ||
| 130 | $sink->($ast_msg) if $self->include_ast; | ||
| 131 | |||
| 132 | if ($self->include_pickles) { | ||
| 133 | Gherkin::Pickles::Compiler->compile( | ||
| 134 | $ast_msg, | ||
| 135 | $id_generator, | ||
| 136 | $sink); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | else { | ||
| 140 | if ( blessed $@ ) { | ||
| 141 | if ( $@->isa( 'Gherkin::Exceptions::CompositeParser' ) ) { | ||
| 142 | $sink->( _parser_error_message( $_, $source->uri ) ) | ||
| 143 | for ( @{ $@->errors } ); | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | elsif ( $@->isa( 'Gherkin::Exceptions::SingleParser' ) ) { | ||
| 147 | $sink->( _parser_error_message( $@, $source->uri ) ); | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | die $@; # rethrow | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | 1; | ||
| 158 | |||
| 159 | __END__ |