| 1 | package Gherkin::TokenScanner; | ||||||||||
| 2 | 2 | $Gherkin::TokenScanner::VERSION = '42.0.1'; | |||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Class::XSAccessor accessors => [qw/fh line_number/]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use Carp qw/croak/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use Gherkin::Line; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use Gherkin::Token; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 5 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 15 | 5 | my ( $class, $path_or_str ) = @_; | |||||||||
| 16 | |||||||||||
| 17 | # Perl convention is that a string reference is the string itself, but that | ||||||||||
| 18 | # a straight string is a path | ||||||||||
| 19 | ## no critic (RequireBriefOpen) | ||||||||||
| 20 | # the file is opened in this object constructor, | ||||||||||
| 21 | # and will be closed in the method next_line at the end | ||||||||||
| 22 | 5 | my $fh; | |||||||||
| 23 | 5 | ▶ | if ( ref $path_or_str eq 'SCALAR' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 3 | my $bytes = Encode::encode('UTF-8', ${ $path_or_str }); | |||||||||
| 25 | 3 | open $fh, '<:encoding(UTF-8)', \$bytes; | |||||||||
| 26 | } else { | ||||||||||
| 27 | 2 | ▶ | open( $fh, '<', $path_or_str ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | || croak "Can't open [$path_or_str] for reading"; | ||||||||||
| 29 | 2 | $fh->binmode(':utf8'); | |||||||||
| 30 | } | ||||||||||
| 31 | ## use critic | ||||||||||
| 32 | |||||||||||
| 33 | 5 | return bless { fh => $fh, line_number => 0 }, $class; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 42 | ▶ | sub next_line { | ||||||||
|
Subroutine
next_line: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 37 | 42 | my $self = shift; | |||||||||
| 38 | |||||||||||
| 39 | 42 | ▶ | return (undef, $self->line_number) if not defined $self->fh; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | |||||||||||
| 41 | 42 | my $line = $self->fh->getline; | |||||||||
| 42 | 42 | $self->line_number( $self->line_number + 1 ); | |||||||||
| 43 | |||||||||||
| 44 | 42 | ▶ | if (not defined $line) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 4 | $self->fh->close; | |||||||||
| 46 | 4 | $self->fh( undef ); | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 42 | return ($line, $self->line_number); | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 42 | ▶ | sub read { | ||||||||
|
Subroutine
read: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 53 | 42 | my $self = shift; | |||||||||
| 54 | 42 | my ($line, $line_number) = $self->next_line; | |||||||||
| 55 | |||||||||||
| 56 | 42 | my $location = { line => $line_number }; | |||||||||
| 57 | 42 | my $line_token = undef; | |||||||||
| 58 | 42 | ▶ | if (defined $line) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 38 | $line =~ s/\r$//; # \n as well as \r\n are considered lines separators | |||||||||
| 60 | 38 | $line_token = | |||||||||
| 61 | Gherkin::Line->new( | ||||||||||
| 62 | { line_text => $line, line_number => $line_number } | ||||||||||
| 63 | ); | ||||||||||
| 64 | } | ||||||||||
| 65 | 42 | return Gherkin::Token->new(line => $line_token, location => $location); | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 2 | 1; | |||||||||
| 69 | |||||||||||
| 70 | |||||||||||
| 71 | __END__ | ||||||||||