blib/lib/Lox/Scanner.pm

filter: Statementstmt 86.6% 149 / 172 Branchbran 93.5% 116 / 124 Conditioncond 78.9% 15 / 19 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 75.8% 22 / 29 Podpod 0.0% 0 / 25 totaltotal 81.3% 310 / 381 CC 71 SCAR 24.2
CC
71
Cov
89%
scan_token
22.1
chomp_identifier
19.0
match
12.0
CRAP 11.3
« blib/lib/Lox/Interpreter.pm ↑ Summary blib/lib/Lox/Parser.pm »
1 package Lox::Scanner;
2 260 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 260 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 260 ▶use Lox::TokenType;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 260 ▶use Lox::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 260 our $VERSION = 0.02;
7  
8 261 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
9 261 my ($class, $args) = @_;
10 return bless {
11 source => $args->{source},
12 261 tokens => [],
13 current => 0,
14 column => 0,
15 line => 1,
16 }, $class;
17 }
18  
19 261 ▶sub tokens { $_[0]->{tokens} }
Subroutine tokens: called CC=1 SCAR=0.0
Pod undocumented
20  
21 0 ▶sub print {
Subroutine print: not called CC=2 SCAR=17.9
Pod undocumented
22 0 my ($self) = @_;
23 0 for ($self->{tokens}->@*) {
24 printf "% 3d % 3d % -15s %s\n",
25 0 $_->{line}, $_->{column}, Lox::TokenType::type($_->{type}), $_->{lexeme};
26 }
27 }
28  
29 261 ▶sub scan_tokens {
Subroutine scan_tokens: called CC=4 SCAR=14.0
Pod undocumented
30 261 my $self = shift;
31 261 ▶ while (!$self->is_at_end) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_at_end) 29618 261 29879
32 29618 $self->scan_token;
33 }
34 # handle non-terminated input
35 261 ▶ unless ($self->{tokens}->@* && $self->{tokens}[-1]->type eq EOF) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"tokens"}} and $self->{"tokens"}[-1]->type eq 40) 261 0 261
Condition66%2/3
@{$self->{"tokens"}} and $self->{"tokens"}[-1]->type eq 40
!ll&&!rl&&r
52560
MC/DC0%0/2
@{$self->{"tokens"}} and $self->{"tokens"}[-1]->type eq 40
@{$self->{"tokens"}} $self->{"tokens"}[-1]->type eq 40
Truth table66%2/3
A and B
A= @{$self->{"tokens"}}B= $self->{"tokens"}[-1]->type eq 40
ABresult
0X0
100
111
36 261 $self->chomp_eof('');
37 }
38 }
39  
40 29618 ▶sub scan_token {
Subroutine scan_token: called CC=22 SCAR=31.0
Pod undocumented
41 29618 my $self = shift;
42 29618 my $c = $self->advance;
43  
44 29618 ▶ if ($c eq '') {
Branch: 21 branches
BranchTrueFalseTotal
if ($c eq '') { } 0 29618 29618
elsif ($c eq '(') { } 553 29065 29618
elsif ($c eq ')') { } 553 28512 29065
elsif ($c eq '{') { } 380 28132 28512
elsif ($c eq '}') { } 380 27752 28132
elsif ($c eq ',') { } 1746 26006 27752
elsif ($c eq '.') { } 295 25711 26006
elsif ($c eq '-') { } 44 25667 25711
elsif ($c eq '+') { } 140 25527 25667
elsif ($c eq ';') { } 1076 24451 25527
elsif ($c eq '*') { } 7 24444 24451
elsif ($c eq '!') { } 37 24407 24444
elsif ($c eq '.') { } 0 24407 24407
elsif ($c eq '=') { } 364 24043 24407
elsif ($c eq '>') { } 18 24025 24043
elsif ($c eq '<') { } 65 23960 24025
elsif ($c eq '/') { } 1460 22500 23960
elsif ($c eq '"') { } 358 22142 22500
elsif ($c =~ /\d/) { } 399 21743 22142
elsif ($c =~ /\w/) { } 4592 17151 21743
elsif ($c =~ /\s/) { } 17150 1 17151
45 0 $self->chomp_eof($c);
46 }
47 elsif ($c eq '(') {
48 553 $self->add_token(lexeme=>$c, type=>LEFT_PAREN);
49 }
50 elsif ($c eq ')') {
51 553 $self->add_token(lexeme=>$c, type=>RIGHT_PAREN);
52 }
53 elsif ($c eq '{') {
54 380 $self->add_token(lexeme=>$c, type=>LEFT_BRACE);
55 }
56 elsif ($c eq '}') {
57 380 $self->add_token(lexeme=>$c, type=>RIGHT_BRACE);
58 }
59 elsif ($c eq ',') {
60 1746 $self->add_token(lexeme=>$c, type=>COMMA);
61 }
62 elsif ($c eq '.') {
63 295 $self->add_token(lexeme=>$c, type=>DOT);
64 }
65 elsif ($c eq '-') {
66 44 $self->add_token(lexeme=>$c, type=>MINUS);
67 }
68 elsif ($c eq '+') {
69 140 $self->add_token(lexeme=>$c, type=>PLUS);
70 }
71 elsif ($c eq ';') {
72 1076 $self->add_token(lexeme=>$c, type=>SEMICOLON);
73 }
74 elsif ($c eq '*') {
75 7 $self->add_token(lexeme=>$c, type=>STAR);
76 }
77 elsif ($c eq '!') {
78 37 $self->chomp_bang($c);
79 }
80 elsif ($c eq '.') {
81 0 $self->chomp_method($c);
82 }
83 elsif ($c eq '=') {
84 364 $self->chomp_equal($c);
85 }
86 elsif ($c eq '>') {
87 18 $self->chomp_greater($c);
88 }
89 elsif ($c eq '<') {
90 65 $self->chomp_less($c);
91 }
92 elsif ($c eq '/') {
93 1460 $self->chomp_slash($c);
94 }
95 elsif ($c eq '"') {
96 358 $self->chomp_string($c);
97 }
98 elsif ($c =~ /\d/) {
99 399 $self->chomp_number($c);
100 }
101 elsif ($c =~ /\w/) {
102 4592 $self->chomp_identifier($c);
103 }
104 elsif ($c =~ /\s/) {
105 # whitespace no-op
106 }
107 else {
108 1 $self->lex_error($c);
109 }
110 }
111  
112 55889 ▶sub is_at_end {
Subroutine is_at_end: called CC=1 SCAR=0.0
Pod undocumented
113 55889 my $self = shift;
114 55889 return $self->{current} >= length $self->{source};
115 }
116  
117 0 ▶sub match {
Subroutine match: not called CC=3 SCAR=24.8
Pod undocumented
118 0 my ($self, $expected_char) = @_;
119 0 ▶ if (!$self->is_at_end && $self->peek eq $expected_char) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_at_end and $self->peek eq $expected_char) 0 0 0
Condition0%0/3
not $self->is_at_end and $self->peek eq $expected_char
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->is_at_end and $self->peek eq $expected_char
not $self->is_at_end $self->peek eq $expected_char
Truth table0%0/3
not(A) and B
A= not $self->is_at_endB= $self->peek eq $expected_char
ABresult
0X0
100
111
120 0 $self->{current}++;
121 0 return 1;
122 }
123 0 return undef;
124 }
125  
126  
127 63904 ▶sub advance {
Subroutine advance: called CC=2 SCAR=6.9
Pod undocumented
128 63904 my $self = shift;
129 63904 my $next = substr $self->{source}, $self->{current}++, 1;
130 63904 ▶ if ($next eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($next eq "\n") { } 3614 60290 63904
131 3614 $self->{column} = 0;
132 3614 $self->{line}++;
133 }
134 else {
135 60290 $self->{column}++;
136 }
137 63904 return $next;
138 }
139  
140 43174 ▶sub peek {
Subroutine peek: called CC=2 SCAR=6.9
Pod undocumented
141 43174 my ($self, $length) = @_;
142 43174 ▶ return substr $self->{source}, $self->{current}, $length||1;
Condition100%2/2
$length || 1
l!l
37842796
MC/DC100%1/1
$length || 1
$length
Truth table100%2/2
A
A= $length
Aresult
11
00
143 }
144  
145 261 ▶sub chomp_eof {
Subroutine chomp_eof: called CC=1 SCAR=0.0
Pod undocumented
146 261 my ($self, $c) = @_;
147 261 $self->add_token(lexeme=>$c, type=>EOF);
148 }
149  
150 0 ▶sub chomp_left_brace {
Subroutine chomp_left_brace: not called CC=1 SCAR=6.9
Pod undocumented
151 0 my ($self, $c) = @_;
152 }
153  
154 0 ▶sub chomp_right_brace {
Subroutine chomp_right_brace: not called CC=1 SCAR=6.9
Pod undocumented
155 0 my ($self, $c) = @_;
156 0 $self->add_token(lexeme=>$c, type=>RIGHT_BRACE);
157 }
158 0 ▶sub chomp_left_paren {
Subroutine chomp_left_paren: not called CC=1 SCAR=6.9
Pod undocumented
159 0 my ($self, $c) = @_;
160 0 $self->add_token(lexeme=>$c, type=>LEFT_PAREN);
161 }
162  
163 0 ▶sub chomp_right_paren {
Subroutine chomp_right_paren: not called CC=1 SCAR=6.9
Pod undocumented
164 0 my ($self, $c) = @_;
165 0 $self->add_token(lexeme=>$c, type=>RIGHT_PAREN);
166 }
167  
168 4592 ▶sub chomp_identifier {
Subroutine chomp_identifier: called CC=19 SCAR=29.4
Pod undocumented
169 4592 my ($self, $c) = @_;
170 4592 my $column = $self->{column};
171 4592 ▶ while ($self->peek =~ /[A-Za-z0-9_]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->peek =~ /[A-Za-z0-9_]/) 9924 4592 14516
172 9924 $c .= $self->advance;
173 }
174 4592 my $type;
175 4592 ▶ if ($c eq 'and') {
Branch: 17 branches
BranchTrueFalseTotal
if ($c eq 'and') { } 14 4578 4592
elsif ($c eq 'break') { } 4 4574 4578
elsif ($c eq 'class') { } 107 4467 4574
elsif ($c eq 'else') { } 18 4449 4467
elsif ($c eq 'false') { } 63 4386 4449
elsif ($c eq 'for') { } 24 4362 4386
elsif ($c eq 'fun') { } 110 4252 4362
elsif ($c eq 'if') { } 48 4204 4252
elsif ($c eq 'nil') { } 24 4180 4204
elsif ($c eq 'or') { } 14 4166 4180
elsif ($c eq 'print') { } 470 3696 4166
elsif ($c eq 'return') { } 89 3607 3696
elsif ($c eq 'super') { } 21 3586 3607
elsif ($c eq 'this') { } 29 3557 3586
elsif ($c eq 'true') { } 69 3488 3557
elsif ($c eq 'var') { } 177 3311 3488
elsif ($c eq 'while') { } 15 3296 3311
176 14 $type = AND;
177 }
178 elsif ($c eq 'break') {
179 4 $type = BREAK;
180 }
181 elsif ($c eq 'class') {
182 107 $type = CLASS;
183 }
184 elsif ($c eq 'else') {
185 18 $type = ELSE;
186 }
187 elsif ($c eq 'false') {
188 63 $type = FALSE;
189 }
190 elsif ($c eq 'for') {
191 24 $type = FOR;
192 }
193 elsif ($c eq 'fun') {
194 110 $type = FUN;
195 }
196 elsif ($c eq 'if') {
197 48 $type = IF;
198 }
199 elsif ($c eq 'nil') {
200 24 $type = NIL;
201 }
202 elsif ($c eq 'or') {
203 14 $type = OR;
204 }
205 elsif ($c eq 'print') {
206 470 $type = PRINT;
207 }
208 elsif ($c eq 'return') {
209 89 $type = RETURN;
210 }
211 elsif ($c eq 'super') {
212 21 $type = SUPER;
213 }
214 elsif ($c eq 'this') {
215 29 $type = THIS;
216 }
217 elsif ($c eq 'true') {
218 69 $type = TRUE;
219 }
220 elsif ($c eq 'var') {
221 177 $type = VAR;
222 }
223 elsif ($c eq 'while') {
224 15 $type = WHILE;
225 }
226 else {
227 3296 $type = IDENTIFIER;
228 }
229 4592 $self->add_token(lexeme=>$c, column=>$column, type=>$type);
230 }
231  
232 0 ▶sub chomp_class {
Subroutine chomp_class: not called CC=2 SCAR=17.9
Pod undocumented
233 0 my ($self, $word) = @_;
234 0 my $column = $self->{column};
235 0 ▶ while ($self->peek =~ /[\w\d]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->peek =~ /[\w\d]/) 0 0 0
236 0 $word .= $self->advance;
237 }
238 0 $self->add_token(lexeme=>$word, column=>$column, type=>CLASS);
239 }
240  
241  
242 399 ▶sub chomp_number {
Subroutine chomp_number: called CC=5 SCAR=16.1
Pod undocumented
243 399 my ($self, $c) = @_;
244 399 my $column = $self->{column};
245 399 ▶ $c .= $self->advance while ($self->peek =~ /\d/);
Branch: 1 branch
BranchTrueFalseTotal
$self->peek =~ /\d/ and do { $c .= $self->advance } 41 399 440
246 399 ▶ if ($self->peek eq '.' && $self->peek(2) =~ /\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->peek eq "." and $self->peek(2) =~ /\d/) 8 391 399
Condition100%3/3
$self->peek eq "." and $self->peek(2) =~ /\d/
!ll&&!rl&&r
38748
MC/DC100%2/2
$self->peek eq "." and $self->peek(2) =~ /\d/
$self->peek eq "." $self->peek(2) =~ /\d/
Truth table100%3/3
A and B
A= $self->peek eq "."B= $self->peek(2) =~ /\d/
ABresult
0X0
100
111
247 8 $c .= $self->advance;
248 8 ▶ $c .= $self->advance while ($self->peek =~ /\d/);
Branch: 1 branch
BranchTrueFalseTotal
$self->peek =~ /\d/ and do { $c .= $self->advance } 15 8 23
249 }
250 399 $self->add_token(lexeme=>$c, type=>NUMBER, literal=>$c, column=>$column);
251 }
252  
253 358 ▶sub chomp_string {
Subroutine chomp_string: called CC=5 SCAR=16.1
Pod undocumented
254 358 my ($self, $c) = @_;
255 358 my $column = $self->{column};
256 358 my $word = '';
257  
258 358 ▶ while (!$self->is_at_end && $self->peek ne '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_at_end and $self->peek ne "\"") 1666 358 2024
Condition100%3/3
not $self->is_at_end and $self->peek ne "\""
!ll&&!rl&&r
13571666
MC/DC100%2/2
not $self->is_at_end and $self->peek ne "\""
not $self->is_at_end $self->peek ne "\""
Truth table100%3/3
not(A) and B
A= not $self->is_at_endB= $self->peek ne "\""
ABresult
0X0
100
111
259 1666 my $next = $self->advance;
260 1666 ▶ if ($next eq '\\') { # handle \"
Branch: 1 branch
BranchTrueFalseTotal
if ($next eq "\\") 0 1666 1666
261 0 $next .= $self->advance;
262 }
263 1666 $word .= $next;
264 }
265 358 ▶ if ($self->is_at_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_at_end) 1 357 358
266 1 $self->lex_error('EOF', 'Unterminated string');
267 1 return;
268 }
269 357 $self->advance;
270 357 $self->add_token(
271 lexeme=>"\"$word\"", type=>STRING, literal=>$word, column=>$column);
272 }
273  
274 37 ▶sub chomp_bang {
Subroutine chomp_bang: called CC=2 SCAR=6.9
Pod undocumented
275 37 my ($self, $c) = @_;
276 37 my $type = BANG;
277 37 ▶ if ($self->peek eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->peek eq "=") 21 16 37
278 21 $c .= $self->advance;
279 21 $type = BANG_EQUAL;
280 }
281 37 $self->add_token(lexeme => $c, type => $type);
282 }
283  
284 364 ▶sub chomp_equal {
Subroutine chomp_equal: called CC=2 SCAR=6.9
Pod undocumented
285 364 my ($self, $c) = @_;
286 364 my $type = EQUAL;
287 364 ▶ if ($self->peek eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->peek eq "=") 46 318 364
288 46 $c .= $self->advance;
289 46 $type = EQUAL_EQUAL;
290 }
291 364 $self->add_token(lexeme => $c, type => $type);
292 }
293  
294 18 ▶sub chomp_greater {
Subroutine chomp_greater: called CC=2 SCAR=6.9
Pod undocumented
295 18 my ($self, $c) = @_;
296 18 my $type = GREATER;
297 18 ▶ if ($self->peek eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->peek eq "=") 9 9 18
298 9 $c .= $self->advance;
299 9 $type = GREATER_EQUAL;
300 }
301 18 $self->add_token(lexeme => $c, type => $type);
302 }
303  
304 65 ▶sub chomp_less {
Subroutine chomp_less: called CC=2 SCAR=6.9
Pod undocumented
305 65 my ($self, $c) = @_;
306 65 my $type = LESS;
307 65 ▶ if ($self->peek eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->peek eq "=") 8 57 65
308 8 $c .= $self->advance;
309 8 $type = LESS_EQUAL;
310 }
311 65 $self->add_token(lexeme => $c, type => $type);
312 }
313  
314 1460 ▶sub chomp_slash {
Subroutine chomp_slash: called CC=10 SCAR=23.0
Pod undocumented
315 1460 my ($self, $c) = @_;
316 1460 ▶ if ($self->peek eq '/') { # single-line comment
Branch: 2 branches
BranchTrueFalseTotal
if ($self->peek eq '/') { } 1448 12 1460
elsif ($self->peek eq '*') { } 5 7 12
317 1448 ▶ while (!$self->is_at_end && $self->peek ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_at_end and $self->peek ne "\n") 21993 1448 23441
Condition100%3/3
not $self->is_at_end and $self->peek ne "\n"
!ll&&!rl&&r
2144621993
MC/DC100%2/2
not $self->is_at_end and $self->peek ne "\n"
not $self->is_at_end $self->peek ne "\n"
Truth table100%3/3
not(A) and B
A= not $self->is_at_endB= $self->peek ne "\n"
ABresult
0X0
100
111
318 21993 $self->advance;
319 }
320 }
321 elsif ($self->peek eq '*') { # multi-line comment
322 5 my $multiline = 1;
323 5 ▶ while (!$self->is_at_end) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_at_end) 186 1 187
324 186 ▶ if ($self->peek(2) eq '*/') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->peek(2) eq '*/') { } 6 180 186
elsif ($self->peek(2) eq '/*') { } 2 178 180
325 6 $multiline--;
326 6 $self->advance;
327 6 $self->advance;
328 }
329 elsif ($self->peek(2) eq '/*') {
330 2 $multiline++;
331 2 $self->advance;
332 2 $self->advance;
333 }
334 186 ▶ last unless $multiline;
Branch: 1 branch
BranchTrueFalseTotal
unless $multiline 4 182 186
335 182 $self->advance;
336 }
337 5 ▶ if ($multiline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiline) 1 4 5
338 1 $self->lex_error('EOF', 'Unterminated multi-line comment');
339 }
340 }
341 else {
342 7 $self->add_token(lexeme => $c, type => SLASH);
343 }
344 }
345  
346 11274 ▶sub add_token {
Subroutine add_token: called CC=1 SCAR=0.0
Pod undocumented
347 11274 my ($self, %args) = @_;
348 push $self->{tokens}->@*, Lox::Token->new(
349 {
350 literal => undef,
351 column => $self->{column},
352 line => $self->{line},
353 11274 %args,
354 });
355 }
356  
357 3 ▶sub lex_error {
Subroutine lex_error: called CC=2 SCAR=6.9
Pod undocumented
358 3 my ($self, $c, $msg) = @_;
359 3 ▶ $msg //= "unexpected character: $c";
Condition100%2/2
$msg //= "unexpected character: $c"
l!l
21
MC/DC100%1/1
$msg //= "unexpected character: $c"
$msg
Truth table100%2/2
A
A= $msg
Aresult
11
00
360 my $t = Lox::Token->new({
361 literal => undef,
362 lexeme => $c,
363 column => $self->{column},
364 line => $self->{line},
365 3 type => ERROR});
366 3 Lox::error($t, $msg);
367 }
368  
369 260 1;
« blib/lib/Lox/Interpreter.pm ↑ Summary blib/lib/Lox/Parser.pm »