TokenStream/Lexer.pm

filter: Statementstmt 100% 56 / 56 Branchbran 92.8% 13 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 98.8% 85 / 86 CC 8 SCAR 6.2
CC
8
Cov
99%
__ANON__
3.0
next_token
3.0
_build__rules_rx
2.0
CRAP 1.9
« TokenStream.pm ↑ Summary TokenStream/Token.pm »
1 package Text::TokenStream::Lexer;
2  
3 2 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.04';
7  
8 2 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use List::Util qw(pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Text::TokenStream::Types qw(Identifier LexerRule);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Types::Standard qw(ArrayRef CycleTuple ScalarRef Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 has rules => (
16 is => 'ro',
17 isa => CycleTuple[Identifier, LexerRule],
18 required => 1,
19 );
20  
21 has whitespace => (
22 is => 'ro',
23 isa => ArrayRef[LexerRule],
24 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 2 );
26  
27 3 ▶has _whitespace_rx => (is => 'lazy', init_arg => undef, builder => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
28 3 my ($self) = @_;
29 3 ▶ my @whitespace = map ref() ? $_ : quotemeta, @{ $self->whitespace }
Branch: 2 branches
BranchTrueFalseTotal
my(@whitespace) = map((ref $_ ? $_ : quotemeta $_), @{$self->whitespace;}) or return qr/(*FAIL)/u 2 1 3
ref $_ ? : 4 0 4
30 or return qr/(*FAIL)/;
31 2 local $" = '|';
32 2 return qr/^(?:@whitespace)/;
33 2 });
34  
35 3 ▶has _rules_rx => (is => 'lazy', init_arg => undef, builder => sub {
Subroutine _build__rules_rx: called CC=2 SCAR=6.9
36 3 my ($self) = @_;
37 19 ▶ my @annotated_rules = pairmap { qr/$b(*MARK:$a)/ }
Subroutine __ANON__: called CC=1 SCAR=0.0
38 19 ▶ pairmap { $a => (ref $b ? $b : quotemeta $b) }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $b ? : 18 1 19
39 3 ▶ @{ $self->rules }
Branch: 1 branch
BranchTrueFalseTotal
my(@annotated_rules) = &pairmap(sub { qr/$b(*MARK:$a)/u; } , &pairmap(sub { $a, ref $b ? $b : quotemeta $b; } , @{$self->rules;})) or return qr/(*FAIL)/u 2 1 3
40 or return qr/(*FAIL)/;
41 2 local $" = '|';
42 2 qr/^(?|@annotated_rules)/;
43 2 });
44  
45 35 ▶sub skip_whitespace {
Subroutine skip_whitespace: called CC=2 SCAR=6.9
Pod documented
46 35 my ($self, $str_ref) = @_;
47 35 (ScalarRef[Str])->assert_valid($str_ref);
48  
49 35 my $ret = 0;
50 35 my $whitespace_rx = $self->_whitespace_rx;
51 35 ▶ $ret = 1 while $$str_ref =~ s/$whitespace_rx//;
Branch: 1 branch
BranchTrueFalseTotal
$$str_ref =~ s/$whitespace_rx//u and do { $ret = 1 } 28 35 63
52  
53 35 return $ret;
54 }
55  
56 35 ▶sub next_token {
Subroutine next_token: called CC=3 SCAR=11.0
Pod documented
57 35 my ($self, $str_ref) = @_;
58 35 (ScalarRef[Str])->assert_valid($str_ref);
59  
60 35 my $saw_whitespace = $self->skip_whitespace($str_ref);
61  
62 35 ▶ return undef if !length $$str_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless length $$str_ref 1 34 35
63  
64 34 ▶ if ($$str_ref !~ $self->_rules_rx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$str_ref =~ $self->_rules_rx) 1 33 34
65 1 my $text = substr $$str_ref, 0, 30;
66 1 confess("No matching rule; next text is: $text");
67 }
68  
69 33 my $type = our $REGMARK;
70 33 my $captures = { %+ };
71 33 my $text = substr($$str_ref, 0, $+[0], '');
72  
73 return {
74 33 type => $type,
75 captures => $captures,
76 text => $text,
77 cuddled => 0+!$saw_whitespace,
78 };
79 }
80  
81 2 1;
82 __END__
« TokenStream.pm ↑ Summary TokenStream/Token.pm »