line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::TokenStream; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
76817
|
use v5.12; |
|
1
|
|
|
|
|
8
|
|
4
|
1
|
|
|
1
|
|
556
|
use Moo; |
|
1
|
|
|
|
|
11079
|
|
|
1
|
|
|
|
|
6
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
1540
|
use List::Util qw(max); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
98
|
|
9
|
1
|
|
|
1
|
|
529
|
use Types::Path::Tiny qw(Path); |
|
1
|
|
|
|
|
114440
|
|
|
1
|
|
|
|
|
8
|
|
10
|
1
|
|
|
1
|
|
390
|
use Types::Standard qw(ArrayRef Int Maybe ScalarRef Str); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
5
|
|
11
|
1
|
|
|
1
|
|
1607
|
use Text::TokenStream::Token; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
41
|
|
12
|
1
|
|
|
1
|
|
7
|
use Text::TokenStream::Types qw(Lexer Position TokenClass); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
668
|
use namespace::clean; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has input_name => (is => 'ro', isa => Maybe[Path], coerce => 1, default => undef); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has input => (is => 'ro', isa => Str, required => 1); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
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
|
|
|
0
|
|
0
|
builder => sub { 'Text::TokenStream::Token' }, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has _pending => (is => 'ro', isa => ArrayRef, default => sub { [] }); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has _input_ref => (is => 'lazy', isa => ScalarRef[Str], builder => sub { |
36
|
1
|
|
|
1
|
|
14
|
my ($self) = @_; |
37
|
1
|
|
|
|
|
8
|
my $copy = $self->input; |
38
|
1
|
|
|
|
|
17
|
return \$copy; |
39
|
|
|
|
|
|
|
}); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
has current_position => ( |
42
|
|
|
|
|
|
|
is => 'ro', |
43
|
|
|
|
|
|
|
writer => '_set_current_position', |
44
|
|
|
|
|
|
|
isa => Position, |
45
|
|
|
|
|
|
|
default => 0, |
46
|
|
|
|
|
|
|
init_arg => undef, |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
with qw(Text::TokenStream::Role::Stream); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Only to be called if the buffer has at least one token |
52
|
|
|
|
|
|
|
sub _next { |
53
|
8
|
|
|
8
|
|
15
|
my ($self) = @_; |
54
|
8
|
|
|
|
|
14
|
my $tok = shift @{ $self->_pending }; |
|
8
|
|
|
|
|
16
|
|
55
|
8
|
|
|
|
|
160
|
$self->_set_current_position( $tok->position + length($tok->text) ); |
56
|
8
|
|
|
|
|
234
|
return $tok; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub next { |
60
|
6
|
|
|
6
|
1
|
1781
|
my ($self) = @_; |
61
|
6
|
50
|
|
|
|
13
|
$self->fill(1) or return undef; |
62
|
6
|
|
|
|
|
14
|
return $self->_next; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub fill { |
66
|
21
|
|
|
21
|
1
|
1454
|
my ($self, $n) = @_; |
67
|
|
|
|
|
|
|
|
68
|
21
|
|
|
|
|
443
|
my $input_ref = $self->_input_ref; |
69
|
21
|
|
|
|
|
223
|
my $input_len = length($self->input); |
70
|
|
|
|
|
|
|
|
71
|
21
|
|
|
|
|
38
|
my $pending = $self->_pending; |
72
|
21
|
|
|
|
|
52
|
while (@$pending < $n) { |
73
|
11
|
|
50
|
|
|
261
|
my $tok = $self->next_lexer_token($input_ref) // return 0; |
74
|
11
|
|
|
|
|
31
|
my $position = $input_len - length($$input_ref) - length($tok->{text}); |
75
|
11
|
|
|
|
|
38
|
push @$pending, $self->create_token(%$tok, position => $position); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
21
|
|
|
|
|
323
|
return 1; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub create_token { |
82
|
11
|
|
|
11
|
1
|
47
|
my ($self, %data) = @_; |
83
|
11
|
|
|
|
|
196
|
return $self->token_class->new(%data); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub peek { |
87
|
12
|
|
|
12
|
1
|
1427
|
my ($self) = @_; |
88
|
12
|
50
|
|
|
|
24
|
$self->fill(1) or return undef; |
89
|
12
|
|
|
|
|
48
|
return $self->_pending->[0]; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub skip_optional { |
93
|
2
|
|
|
2
|
1
|
6
|
my ($self, $target) = @_; |
94
|
2
|
|
50
|
|
|
5
|
my $tok = $self->peek // return 0; |
95
|
2
|
100
|
|
|
|
8
|
return 0 if !$tok->matches($target); |
96
|
1
|
|
|
|
|
4
|
$self->_next; |
97
|
1
|
|
|
|
|
5
|
return 1; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub looking_at { |
101
|
2
|
|
|
2
|
1
|
1365
|
my ($self, @targets) = @_; |
102
|
|
|
|
|
|
|
|
103
|
2
|
50
|
|
|
|
7
|
$self->fill(scalar @targets) or return 0; |
104
|
|
|
|
|
|
|
|
105
|
2
|
|
|
|
|
6
|
my $pending = $self->_pending; |
106
|
2
|
|
|
|
|
7
|
for my $i (0 .. $#targets) { |
107
|
3
|
50
|
|
|
|
14
|
return 0 if !$pending->[$i]->matches($targets[$i]); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
2
|
|
|
|
|
17
|
return 1; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub next_of { |
114
|
3
|
|
|
3
|
1
|
3237
|
my ($self, $target, $where) = @_; |
115
|
3
|
|
33
|
|
|
8
|
my $tok = $self->peek |
116
|
|
|
|
|
|
|
// $self->err(join ' ', "Missing token", grep defined, $where); |
117
|
3
|
100
|
|
|
|
13
|
$self->token_err($tok, join ' ', "Unexpected", $tok->type, "token", grep defined, $where) |
118
|
|
|
|
|
|
|
if !$tok->matches($target); |
119
|
1
|
|
|
|
|
4
|
return $self->_next; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub _err { |
123
|
2
|
|
|
2
|
|
7
|
my ($self, $token, @message) = @_; |
124
|
2
|
50
|
|
|
|
9
|
my $position = $token ? $token->position : $self->current_position; |
125
|
2
|
|
|
|
|
14
|
my $marker = '^' x max(6, map length($_->text), grep defined, $token); |
126
|
2
|
|
|
|
|
6
|
my $input = $self->input; |
127
|
2
|
|
|
|
|
5
|
my $prefix = substr $input, 0, $position; |
128
|
2
|
|
|
|
|
13
|
(my $line_prefix = $prefix) =~ s/^.*\n//s; |
129
|
2
|
|
|
|
|
8
|
(my $space_prefix = $line_prefix) =~ tr/\t/ /c; |
130
|
2
|
|
|
|
|
12
|
(my $line_suffix = substr $input, $position) =~ s/\r?\n.*//s; |
131
|
2
|
|
|
|
|
5
|
my $line_number = 1 + ($prefix =~ tr/\n//); |
132
|
2
|
|
|
|
|
5
|
my $column_number = 1 + length $line_prefix; |
133
|
2
|
|
|
|
|
6
|
my $input_name = $self->input_name; |
134
|
2
|
50
|
|
|
|
6
|
my $file_line = defined $input_name ? "File $input_name, line" : "Line"; |
135
|
2
|
50
|
|
|
|
5
|
@message = q[Something's wrong] if !@message; |
136
|
2
|
|
|
|
|
11
|
my $message = join '', ( |
137
|
|
|
|
|
|
|
"SORRY! $file_line $line_number, column $column_number: ", @message, "\n", |
138
|
|
|
|
|
|
|
$line_prefix, $line_suffix, "\n", |
139
|
|
|
|
|
|
|
$space_prefix, $marker, "\n", |
140
|
|
|
|
|
|
|
); |
141
|
2
|
|
|
|
|
14
|
die $message; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
2
|
|
|
2
|
1
|
27
|
sub token_err { shift->_err( @_) } |
145
|
0
|
|
|
0
|
1
|
|
sub err { shift->_err(undef, @_) } |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
__END__ |