| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 39 | 3 | ▶ | @{ $self->rules } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 63 | |||||||||||||||
| 64 | 34 | ▶ | if ($$str_ref !~ $self->_rules_rx) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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__ | ||||||||||||||