| 1 | package Text::TokenStream; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use v5.12; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.04'; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | ▶ | use List::Util qw(max); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use Types::Path::Tiny qw(Path); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use Types::Standard qw(ArrayRef Int Maybe ScalarRef Str); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use Text::TokenStream::Token; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use Text::TokenStream::Types qw(Lexer Position TokenClass); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | has input_name => (is => 'ro', isa => Maybe[Path], coerce => 1, default => undef); | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 1 | has input => (is => 'ro', isa => Str, required => 1); | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 1 | has lexer => ( | |||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||
| 22 | isa => Lexer, | ||||||||||||||||||||
| 23 | required => 1, | ||||||||||||||||||||
| 24 | handles => { next_lexer_token => 'next_token' }, | ||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | has token_class => ( | ||||||||||||||||||||
| 28 | is => 'lazy', | ||||||||||||||||||||
| 29 | isa => TokenClass, | ||||||||||||||||||||
| 30 | 0 | ▶ | builder => sub { 'Text::TokenStream::Token' }, | ||||||||||||||||||
|
Subroutine
_build_token_class: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 31 | 1 | ); | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 1 | ▶ | has _pending => (is => 'ro', isa => ArrayRef, default => sub { [] }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 1 | ▶ | has _input_ref => (is => 'lazy', isa => ScalarRef[Str], builder => sub { | ||||||||||||||||||
|
Subroutine
_build__input_ref: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 1 | my ($self) = @_; | |||||||||||||||||||
| 37 | 1 | my $copy = $self->input; | |||||||||||||||||||
| 38 | 1 | return \$copy; | |||||||||||||||||||
| 39 | 1 | }); | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 1 | has current_position => ( | |||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||
| 43 | writer => '_set_current_position', | ||||||||||||||||||||
| 44 | isa => Position, | ||||||||||||||||||||
| 45 | default => 0, | ||||||||||||||||||||
| 46 | init_arg => undef, | ||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 1 | with qw(Text::TokenStream::Role::Stream); | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 8 | ▶ | sub next { | ||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 52 | 8 | my ($self) = @_; | |||||||||||||||||||
| 53 | 8 | ▶ | $self->fill(1) or return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 8 | my $tok = shift @{ $self->_pending }; | |||||||||||||||||||
| 55 | 8 | $self->_set_current_position( $tok->position + length($tok->text) ); | |||||||||||||||||||
| 56 | 8 | return $tok; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 23 | ▶ | sub fill { | ||||||||||||||||||
|
Subroutine
fill: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 60 | 23 | my ($self, $n) = @_; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 23 | my $input_ref = $self->_input_ref; | |||||||||||||||||||
| 63 | 23 | my $input_len = length($self->input); | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 23 | my $pending = $self->_pending; | |||||||||||||||||||
| 66 | 23 | ▶ | while (@$pending < $n) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 11 | ▶ | my $tok = $self->next_lexer_token($input_ref) // return 0; | ||||||||||||||||||
|
Condition50%1/2
$self->next_lexer_token($input_ref) // (return 0)
MC/DC0%0/1
$self->next_lexer_token($input_ref) // (return 0)
$self->next_lexer_token($input_ref)
Truth table50%1/2
A
A= $self->next_lexer_token($input_ref)
| |||||||||||||||||||||
| 68 | 11 | my $position = $input_len - length($$input_ref) - length($tok->{text}); | |||||||||||||||||||
| 69 | 11 | push @$pending, $self->create_token(%$tok, position => $position); | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 23 | return 1; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 11 | ▶ | sub create_token { | ||||||||||||||||||
|
Subroutine
create_token: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 76 | 11 | my ($self, %data) = @_; | |||||||||||||||||||
| 77 | 11 | return $self->token_class->new(%data); | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 12 | ▶ | sub peek { | ||||||||||||||||||
|
Subroutine
peek: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 81 | 12 | my ($self) = @_; | |||||||||||||||||||
| 82 | 12 | ▶ | $self->fill(1) or return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 12 | return $self->_pending->[0]; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 2 | ▶ | sub skip_optional { | ||||||||||||||||||
|
Subroutine
skip_optional: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 87 | 2 | my ($self, $target) = @_; | |||||||||||||||||||
| 88 | 2 | ▶ | my $tok = $self->peek // return 0; | ||||||||||||||||||
|
Condition50%1/2
$self->peek // (return 0)
MC/DC0%0/1
$self->peek // (return 0)
$self->peek
Truth table50%1/2
A
A= $self->peek
| |||||||||||||||||||||
| 89 | 2 | ▶ | return 0 if !$tok->matches($target); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 1 | $self->next; # ignore return | |||||||||||||||||||
| 91 | 1 | return 1; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 2 | ▶ | sub looking_at { | ||||||||||||||||||
|
Subroutine
looking_at: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||
| 95 | 2 | my ($self, @targets) = @_; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 2 | ▶ | $self->fill(scalar @targets) or return 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 2 | my $pending = $self->_pending; | |||||||||||||||||||
| 100 | 2 | for my $i (0 .. $#targets) { | |||||||||||||||||||
| 101 | 3 | ▶ | return 0 if !$pending->[$i]->matches($targets[$i]); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 2 | return 1; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 3 | ▶ | sub next_of { | ||||||||||||||||||
|
Subroutine
next_of: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||
| 108 | 3 | my ($self, $target, $where) = @_; | |||||||||||||||||||
| 109 | 3 | ▶ | my $tok = $self->peek | ||||||||||||||||||
|
Condition33%1/3
$self->peek // $self->err(join(" ", "Missing token", grep(defined($_), $where)))
MC/DC0%0/2
$self->peek // $self->err(join(" ", "Missing token", grep(defined($_), $where)))
$self->peek
$self->err(join(" ", "Missing token", grep(defined($_), $where)))
Truth table33%1/3
A // B
A= $self->peekB= $self->err(join(" ", "Missing token", grep(defined($_), $where)))
| |||||||||||||||||||||
| 110 | // $self->err(join ' ', "Missing token", grep defined, $where); | ||||||||||||||||||||
| 111 | 3 | ▶ | $self->token_err($tok, join ' ', "Unexpected", $tok->type, "token", grep defined, $where) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | if !$tok->matches($target); | ||||||||||||||||||||
| 113 | 1 | return $self->next; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 2 | ▶ | sub _err { | ||||||||||||||||||
|
Subroutine
_err: called CC=4 SCAR=14.0
| |||||||||||||||||||||
| 117 | 2 | my ($self, $token, @message) = @_; | |||||||||||||||||||
| 118 | 2 | ▶ | my $position = $token ? $token->position : $self->current_position; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | 2 | my $marker = '^' x max(6, map length($_->text), grep defined, $token); | |||||||||||||||||||
| 120 | 2 | my $input = $self->input; | |||||||||||||||||||
| 121 | 2 | my $prefix = substr $input, 0, $position; | |||||||||||||||||||
| 122 | 2 | (my $line_prefix = $prefix) =~ s/^.*\n//s; | |||||||||||||||||||
| 123 | 2 | (my $space_prefix = $line_prefix) =~ tr/\t/ /c; | |||||||||||||||||||
| 124 | 2 | (my $line_suffix = substr $input, $position) =~ s/\r?\n.*//s; | |||||||||||||||||||
| 125 | 2 | my $line_number = 1 + ($prefix =~ tr/\n//); | |||||||||||||||||||
| 126 | 2 | my $column_number = 1 + length $line_prefix; | |||||||||||||||||||
| 127 | 2 | my $input_name = $self->input_name; | |||||||||||||||||||
| 128 | 2 | ▶ | my $file_line = defined $input_name ? "File $input_name, line" : "Line"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 2 | ▶ | @message = q[Something's wrong] if !@message; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 2 | my $message = join '', ( | |||||||||||||||||||
| 131 | "SORRY! $file_line $line_number, column $column_number: ", @message, "\n", | ||||||||||||||||||||
| 132 | $line_prefix, $line_suffix, "\n", | ||||||||||||||||||||
| 133 | $space_prefix, $marker, "\n", | ||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||
| 135 | 2 | die $message; | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 2 | ▶ | sub token_err { shift->_err( @_) } | ||||||||||||||||||
|
Subroutine
token_err: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 139 | 0 | ▶ | sub err { shift->_err(undef, @_) } | ||||||||||||||||||
|
Subroutine
err: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 1 | 1; | |||||||||||||||||||
| 142 | __END__ | ||||||||||||||||||||