blib/lib/Lox/Parser.pm

filter: Statementstmt 100% 279 / 279 Branchbran 99.1% 121 / 122 Conditioncond 69.2% 9 / 13 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 49 / 49 Podpod 0.0% 0 / 39 totaltotal 90.5% 462 / 510 CC 68 SCAR 14.0
CC
68
Cov
99%
primary
11.0
statement
8.0
for_statement
7.0
CRAP 4.1
« blib/lib/Lox/Scanner.pm ↑ Summary blib/lib/Lox/AstPrinter.pm »
1 package Lox::Parser;
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::Bool;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 260 ▶use Lox::Expr;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 260 ▶use Lox::Nil;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 260 ▶use Lox::Stmt;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 260 ▶use Lox::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 260 ▶use Lox::TokenType;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 260 our $VERSION = 0.02;
11  
12 258 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
13 258 my ($class, $args) = @_;
14 return bless {
15 258 ▶ tokens => ($args->{tokens} || die 'requires an arrayref of tokens'),
Condition50%1/2
$args->{'tokens'} || die('requires an arrayref of tokens')
l!l
2580
MC/DC0%0/1
$args->{'tokens'} || die('requires an arrayref of tokens')
$args->{'tokens'}
Truth table50%1/2
A
A= $args->{'tokens'}
Aresult
11
00
16 errors => [],
17 current=> 0,
18 }, $class;
19 }
20  
21 180213 ▶sub tokens { $_[0]->{tokens} }
Subroutine tokens: called CC=1 SCAR=0.0
Pod undocumented
22 303 ▶sub errors :lvalue { $_[0]->{errors} }
Subroutine errors: called CC=1 SCAR=0.0
Pod undocumented
23 191220 ▶sub current :lvalue { $_[0]->{current} }
Subroutine current: called CC=1 SCAR=0.0
Pod undocumented
24  
25 258 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
Pod undocumented
26 258 my $self = shift;
27 258 my @statements;
28 258 ▶ while (!$self->is_at_end) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_at_end) 715 258 973
29 715 push @statements, $self->declaration;
30 }
31 258 return \@statements;
32 }
33  
34 1275 ▶sub declaration {
Subroutine declaration: called CC=6 SCAR=17.9
Pod undocumented
35 1275 my $self = shift;
36 1275 my $dec = eval {
37 1275 ▶ if ($self->check(FUN) && $self->check(IDENTIFIER, 1)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->check(27) and $self->check(19, 1)) { } 83 1192 1275
elsif ($self->match(24)) { } 103 1089 1192
elsif ($self->match(37)) { } 163 926 1089
Condition66%2/3
$self->check(27) and $self->check(19, 1)
!ll&&!rl&&r
1192083
MC/DC50%1/2
$self->check(27) and $self->check(19, 1)
$self->check(27) $self->check(19, 1)
Truth table66%2/3
A and B
A= $self->check(27)B= $self->check(19, 1)
ABresult
0X0
100
111
38 83 $self->advance;
39 83 $self->function_stmt('function');
40 }
41 elsif ($self->match(CLASS)) {
42 103 $self->class_declaration;
43 }
44 163 elsif ($self->match(VAR)) { $self->var_declaration }
45 926 else { $self->statement }
46 };
47 1275 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 1230 45 1275
48 1230 return $dec;
49 }
50 45 $self->synchronize;
51 45 return undef;
52 }
53  
54 103 ▶sub class_declaration {
Subroutine class_declaration: called CC=4 SCAR=13.9
Pod undocumented
55 103 my $self = shift;
56 103 my $name = $self->consume(IDENTIFIER, 'Expect class name');
57  
58 103 my $superclass = undef;
59 103 ▶ if ($self->match(LESS)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(17)) 32 71 103
60 32 $self->consume(IDENTIFIER, 'Expect superclass name');
61 31 $superclass = Lox::Expr::Variable->new({name => $self->previous});
62 }
63  
64 102 $self->consume(LEFT_BRACE, 'Expect \'{\' before class body');
65 102 my @methods = ();
66 102 ▶ while (!$self->check(RIGHT_BRACE) && !$self->is_at_end) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check(3) or $self->is_at_end) 93 101 194
Condition66%2/3
$self->check(3) or $self->is_at_end
l!l&&r!l&&!r
101093
MC/DC50%1/2
$self->check(3) or $self->is_at_end
$self->check(3) $self->is_at_end
Truth table66%2/3
A or B
A= $self->check(3)B= $self->is_at_end
ABresult
1X1
011
000
67 93 push @methods, $self->function_stmt('method');
68 }
69 101 $self->consume(RIGHT_BRACE, 'Expect \'}\' after class body');
70 101 return Lox::Stmt::Class->new({
71 superclass => $superclass,
72 methods => \@methods,
73 name => $name,
74 });
75 }
76  
77 1028 ▶sub statement {
Subroutine statement: called CC=8 SCAR=20.8
Pod undocumented
78 1028 my $self = shift;
79 1028 ▶ if ($self->match(FOR)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(28)) 24 1004 1028
80 24 return $self->for_statement;
81 }
82 1004 ▶ if ($self->match(BREAK)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(23)) 4 1000 1004
83 4 return $self->break_statement;
84 }
85 1000 ▶ if ($self->match(IF)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(29)) 48 952 1000
86 48 return $self->if_statement;
87 }
88 952 ▶ if ($self->match(WHILE)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(38)) 15 937 952
89 15 return $self->while_statement;
90 }
91 937 ▶ if ($self->match(LEFT_BRACE)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(2)) 66 871 937
92 66 return Lox::Stmt::Block->new({statements => $self->block});
93 }
94 871 ▶ if ($self->match(PRINT)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(32)) 470 401 871
95 470 return $self->print_statement;
96 }
97 401 ▶ if ($self->match(RETURN)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(33)) 89 312 401
98 89 return $self->return_statement;
99 }
100 312 return $self->expression_statement;
101 }
102  
103 24 ▶sub for_statement {
Subroutine for_statement: called CC=7 SCAR=19.5
Pod undocumented
104 24 my $self = shift;
105 24 $self->consume(LEFT_PAREN, "Expect '(' after 'for'");
106  
107 24 my $initializer;
108 24 ▶ if ($self->match(SEMICOLON)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->match(8)) { } 12 12 24
elsif ($self->match(37)) { } 10 2 12
109 12 $initializer = undef;
110 }
111 elsif ($self->match(VAR)) {
112 10 $initializer = $self->var_declaration;
113 }
114 else {
115 2 $initializer = $self->expression_statement;
116 }
117  
118 23 my $condition = Lox::Expr::Literal->new({value => 1});
119 23 ▶ if (!$self->check(SEMICOLON)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check(8)) 14 9 23
120 14 $condition = $self->expression;
121 }
122 22 $self->consume(SEMICOLON, 'Expect ";" after loop condition');
123  
124 22 my $increment = undef;
125 22 ▶ if (!$self->check(RIGHT_PAREN)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check(1)) 9 13 22
126 9 $increment = $self->expression;
127 }
128 21 $self->consume(RIGHT_PAREN, 'Expect ")" after for clauses');
129  
130 21 $self->{looping}++;
131 21 my $body = $self->statement;
132 18 ▶ if ($increment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($increment) 8 10 18
133 8 $body = Lox::Stmt::Block->new({statements =>
134 [$body, Lox::Stmt::Expression->new({expression => $increment})]});
135 }
136 18 $body = Lox::Stmt::While->new({condition => $condition, body => $body});
137  
138 18 ▶ if ($initializer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($initializer) 9 9 18
139 9 $body = Lox::Stmt::Block->new({statements => [$initializer, $body]});
140 }
141 18 $self->{looping}--;
142  
143 18 return $body;
144 }
145  
146 48 ▶sub if_statement {
Subroutine if_statement: called CC=2 SCAR=6.9
Pod undocumented
147 48 my $self = shift;
148 48 $self->consume(LEFT_PAREN, "Expect '(' after 'if'");
149 48 my $condition = $self->expression;
150 48 $self->consume(RIGHT_PAREN, "Expect ')' after condition");
151  
152 48 my $then_branch = $self->statement;
153 45 my $else_branch = undef;
154 45 ▶ if ($self->match(ELSE)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(25)) 18 27 45
155 18 $else_branch = $self->statement;
156 }
157 42 return Lox::Stmt::If->new({
158 condition => $condition,
159 then_branch => $then_branch,
160 else_branch => $else_branch,
161 });
162 }
163  
164 4 ▶sub break_statement {
Subroutine break_statement: called CC=2 SCAR=6.9
Pod undocumented
165 4 my $self = shift;
166 $self->error($self->previous, 'Can only break out of loops')
167 4 ▶ unless $self->{looping};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"looping"} 2 2 4
168 2 $self->consume(SEMICOLON, 'Expect ";" after "break"');
169 2 return Lox::Stmt::Break->new({});
170 }
171  
172 173 ▶sub var_declaration {
Subroutine var_declaration: called CC=2 SCAR=6.9
Pod undocumented
173 173 my $self = shift;
174 173 my $name = $self->consume(IDENTIFIER, "Expect variable name");
175 170 my $init = undef;
176 170 ▶ if ($self->match(EQUAL)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(13)) 152 18 170
177 152 $init = $self->expression;
178 }
179 167 $self->consume(SEMICOLON, 'Expect ";" after variable declaration');
180 167 return Lox::Stmt::Var->new({name => $name, initializer => $init});
181 }
182  
183 15 ▶sub while_statement {
Subroutine while_statement: called CC=1 SCAR=0.0
Pod undocumented
184 15 my $self = shift;
185 15 $self->consume(LEFT_PAREN, "Expect '(' after 'while'");
186 15 my $condition = $self->expression;
187 15 $self->consume(RIGHT_PAREN, "Expect ')' after condition");
188 15 $self->{looping}++;
189 15 my $while = Lox::Stmt::While->new({
190 condition => $condition,
191 body => $self->statement,
192 });
193 12 $self->{looping}--;
194 12 return $while;
195 }
196  
197 314 ▶sub expression_statement {
Subroutine expression_statement: called CC=1 SCAR=0.0
Pod undocumented
198 314 my $self = shift;
199 314 my $value = $self->expression;
200 288 $self->consume(SEMICOLON, 'Expect \';\' after expression');
201 286 return Lox::Stmt::Expression->new({expression => $value});
202 }
203  
204 176 ▶sub function_stmt {
Subroutine function_stmt: called CC=1 SCAR=0.0
Pod undocumented
205 176 my ($self, $kind) = @_;
206 176 my $name = $self->consume(IDENTIFIER, "Expect $kind name");
207 176 my ($parameters, $body) = $self->parse_function($kind);
208 172 return Lox::Stmt::Function->new({
209 name => $name,
210 params => $parameters,
211 body => $body,
212 });
213 }
214  
215 27 ▶sub function_expr {
Subroutine function_expr: called CC=1 SCAR=0.0
Pod undocumented
216 27 my ($self) = @_;
217 27 my ($parameters, $body) = $self->parse_function('lambda');
218 20 return Lox::Expr::Function->new({
219 params => $parameters,
220 body => $body,
221 });
222 }
223  
224 203 ▶sub parse_function {
Subroutine parse_function: called CC=4 SCAR=13.9
Pod undocumented
225 203 my ($self, $kind) = @_;
226 203 $self->consume(LEFT_PAREN, "Expect '(' after $kind declaration");
227 199 my @parameters;
228 199 ▶ if (!$self->check(RIGHT_PAREN)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check(1)) 72 127 199
229 72 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$self->match(4) and do { do { if (@parameters >= 255) { $self->error($self->peek, "Cannot have more than 255 parameters"); } ; push @parameters, $self->consume(19, "Expect parameter name") } } 868 69 937
230 940 ▶ if (@parameters >= 255) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parameters >= 255) 3 937 940
231 3 $self->error($self->peek, "Cannot have more than 255 parameters");
232 }
233  
234 937 push @parameters, $self->consume(IDENTIFIER, "Expect parameter name");
235 } while ($self->match(COMMA));
236 }
237 196 $self->consume(RIGHT_PAREN, "Expect ')' after parameters");
238  
239 194 $self->consume(LEFT_BRACE, "Expect '{' before $kind body");
240 192 $self->{functioning}++;
241 192 my $body = $self->block;
242 192 $self->{functioning}--;
243 192 return \@parameters, $body;
244 }
245  
246 258 ▶sub block {
Subroutine block: called CC=3 SCAR=11.0
Pod undocumented
247 258 my $self = shift;
248 258 my @statements;
249 258 ▶ while (!$self->check(RIGHT_BRACE) && !$self->is_at_end) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check(3) or $self->is_at_end) 560 258 818
Condition66%2/3
$self->check(3) or $self->is_at_end
l!l&&r!l&&!r
2580560
MC/DC50%1/2
$self->check(3) or $self->is_at_end
$self->check(3) $self->is_at_end
Truth table66%2/3
A or B
A= $self->check(3)B= $self->is_at_end
ABresult
1X1
011
000
250 560 push @statements, $self->declaration;
251 }
252 258 $self->consume(RIGHT_BRACE, "Expect '}' after block");
253 258 return \@statements;
254 }
255  
256 470 ▶sub print_statement {
Subroutine print_statement: called CC=1 SCAR=0.0
Pod undocumented
257 470 my $self = shift;
258 470 my $value = $self->expression;
259 469 $self->consume(SEMICOLON, 'Expect ";" after value');
260 469 return Lox::Stmt::Print->new({expression => $value});
261 }
262  
263 89 ▶sub return_statement {
Subroutine return_statement: called CC=3 SCAR=11.0
Pod undocumented
264 89 my $self = shift;
265 89 my $keyword = $self->previous;
266 $self->error($keyword, 'Can only return from functions')
267 89 ▶ unless $self->{functioning};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"functioning"} 1 88 89
268 88 ▶ my $value = $self->check(SEMICOLON) ? undef : $self->expression;
Branch: 1 branch
BranchTrueFalseTotal
$self->check(8) ? : 4 84 88
269 88 $self->consume(SEMICOLON, 'Expect ";" after return value');
270 88 return Lox::Stmt::Return->new({keyword => $keyword, value => $value});
271 }
272  
273 2237 ▶sub assignment {
Subroutine assignment: called CC=4 SCAR=13.9
Pod undocumented
274 2237 my $self = shift;
275 2237 my $expr = $self->_or;
276 2208 ▶ if ($self->match(EQUAL)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(13)) 163 2045 2208
277 163 my $equals = $self->previous;
278 163 my $value = $self->assignment;
279 # we parsed the left side THEN found an equals sign
280 # returns a new expr using the left side input
281 163 ▶ if (ref $expr eq 'Lox::Expr::Variable') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $expr eq 'Lox::Expr::Variable') { } 51 112 163
elsif (ref $expr eq 'Lox::Expr::Get') { } 108 4 112
282 51 return Lox::Expr::Assign->new({name => $expr->name, value => $value});
283 }
284 elsif (ref $expr eq 'Lox::Expr::Get') {
285 108 return Lox::Expr::Set->new({
286 object => $expr->object,
287 value => $value,
288 name => $expr->name,
289 });
290 }
291 4 $self->error($equals, 'Invalid assignment target');
292 }
293 2045 return $expr;
294 }
295  
296 2237 ▶sub _or {
Subroutine _or: called CC=2 SCAR=6.9
297 2237 my $self = shift;
298 2237 my $expr = $self->_and;
299  
300 2208 ▶ while ($self->match(OR)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(31)) 14 2208 2222
301 14 $expr = Lox::Expr::Logical->new({
302 left => $expr,
303 operator => $self->previous,
304 right => $self->_and,
305 });
306 }
307 2208 return $expr;
308 }
309  
310 2265 ▶sub _and {
Subroutine _and: called CC=2 SCAR=6.9
311 2265 my $self = shift;
312 2265 my $expr = $self->equality;
313  
314 2236 ▶ while ($self->match(AND)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(22)) 14 2236 2250
315 14 $expr = Lox::Expr::Logical->new({
316 left => $expr,
317 operator => $self->previous,
318 right => $self->_and,
319 });
320 }
321 2236 return $expr;
322 }
323  
324 2074 ▶sub expression { shift->assignment }
Subroutine expression: called CC=1 SCAR=0.0
Pod undocumented
325  
326 2265 ▶sub equality {
Subroutine equality: called CC=2 SCAR=6.9
Pod undocumented
327 2265 my $self = shift;
328 2265 my $expr = $self->comparison;
329 2236 ▶ while ($self->match(BANG_EQUAL, EQUAL_EQUAL)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(12, 14)) 67 2236 2303
330 67 $expr = Lox::Expr::Binary->new({
331 left => $expr,
332 operator => $self->previous,
333 right => $self->comparison,
334 });
335 }
336 2236 return $expr;
337 }
338  
339 2332 ▶sub comparison {
Subroutine comparison: called CC=2 SCAR=6.9
Pod undocumented
340 2332 my $self = shift;
341 2332 my $expr = $self->addition;
342 2303 ▶ while ($self->match(GREATER, GREATER_EQUAL, LESS, LESS_EQUAL)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(15, 16, 17, 18)) 51 2303 2354
343 51 $expr = Lox::Expr::Binary->new({
344 left => $expr,
345 operator => $self->previous,
346 right => $self->addition,
347 });
348 }
349 2303 return $expr;
350 }
351  
352 2383 ▶sub addition {
Subroutine addition: called CC=2 SCAR=6.9
Pod undocumented
353 2383 my $self = shift;
354 2383 my $expr = $self->multiplication;
355 2354 ▶ while ($self->match(MINUS, PLUS)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(6, 7)) 167 2354 2521
356 167 $expr = Lox::Expr::Binary->new({
357 left => $expr,
358 operator => $self->previous,
359 right => $self->multiplication,
360 });
361 }
362 2354 return $expr;
363 }
364  
365 2550 ▶sub multiplication {
Subroutine multiplication: called CC=2 SCAR=6.9
Pod undocumented
366 2550 my $self = shift;
367 2550 my $expr = $self->unary;
368 2521 ▶ while ($self->match(SLASH, STAR)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(9, 10)) 14 2521 2535
369 14 $expr = Lox::Expr::Binary->new({
370 left => $expr,
371 operator => $self->previous,
372 right => $self->unary,
373 });
374 }
375 2521 return $expr;
376 }
377  
378 2597 ▶sub unary {
Subroutine unary: called CC=2 SCAR=6.9
Pod undocumented
379 2597 my $self = shift;
380 2597 ▶ if ($self->match(BANG, MINUS)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match(11, 6)) 33 2564 2597
381 33 my $expr = Lox::Expr::Unary->new({
382 operator => $self->previous,
383 right => $self->unary,
384 });
385 33 return $expr;
386 }
387 2564 return $self->call;
388 }
389  
390 2564 ▶sub call {
Subroutine call: called CC=3 SCAR=11.0
Pod undocumented
391 2564 my $self = shift;
392 2564 my $expr = $self->primary;
393 2540 while (1) {
394 3055 ▶ if ($self->match(LEFT_PAREN)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->match(0)) { } 246 2809 3055
elsif ($self->match(5)) { } 274 2535 2809
395 246 $expr = $self->finish_call($expr);
396 }
397 elsif ($self->match(DOT)) {
398 274 $expr = Lox::Expr::Get->new({
399 object => $expr,
400 name => $self->consume(IDENTIFIER,'Expect property name after \'.\''),
401 });
402 }
403 else {
404 2535 last;
405 }
406 }
407 2535 return $expr;
408 }
409  
410 246 ▶sub finish_call {
Subroutine finish_call: called CC=4 SCAR=13.9
Pod undocumented
411 246 my ($self, $callee) = @_;
412 246 my @args;
413 246 ▶ if (!$self->check(RIGHT_PAREN)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check(1)) 85 161 246
414 85 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$self->match(4) and do { do { $self->error($self->peek, "Cannot have more than 255 arguments") if @args >= 255; push @args, $self->expression } } 872 82 954
415 957 ▶ $self->error($self->peek, 'Cannot have more than 255 arguments')
Branch: 1 branch
BranchTrueFalseTotal
if @args >= 255 3 954 957
416 if @args >= 255;
417 954 push @args, $self->expression;
418 } while ($self->match(COMMA));
419 }
420 243 my $paren = $self->consume(RIGHT_PAREN, 'Expect ")" after arguments');
421 243 return Lox::Expr::Call->new({
422 arguments => \@args,
423 callee => $callee,
424 paren => $paren,
425 });
426 }
427  
428 2564 ▶sub primary {
Subroutine primary: called CC=11 SCAR=24.0
Pod undocumented
429 2564 my $self = shift;
430 2564 ▶ if ($self->match(FALSE)) {
Branch: 10 branches
BranchTrueFalseTotal
if ($self->match(26)) { } 62 2502 2564
elsif ($self->match(36)) { } 69 2433 2502
elsif ($self->match(30)) { } 23 2410 2433
elsif ($self->match(21)) { } 396 2014 2410
elsif ($self->match(20)) { } 353 1661 2014
elsif ($self->match(34)) { } 21 1640 1661
elsif ($self->match(35)) { } 28 1612 1640
elsif ($self->match(19)) { } 1558 54 1612
elsif ($self->match(0)) { } 14 40 54
elsif ($self->match(27)) { } 27 13 40
431 62 return Lox::Expr::Literal->new({value => $False});
432 }
433 elsif ($self->match(TRUE)) {
434 69 return Lox::Expr::Literal->new({value => $True});
435 }
436 elsif ($self->match(NIL)) {
437 23 return Lox::Expr::Literal->new({value => $Nil});
438 }
439 elsif ($self->match(NUMBER)) {
440 396 return Lox::Expr::Literal->new({value => $self->previous->{literal}});
441 }
442 elsif ($self->match(STRING)) {
443 353 return Lox::Expr::Literal->new({value => Lox::String->new($self->previous->{literal})});
444 }
445 elsif ($self->match(SUPER)) {
446 21 my $keyword = $self->previous;
447 21 $self->consume(DOT, 'Expect \'.\' after \'super\'');
448 19 my $method = $self->consume(IDENTIFIER, 'Expect superclass method name');
449 18 return Lox::Expr::Super->new({keyword => $keyword, method => $method});
450 }
451 elsif ($self->match(THIS)) {
452 28 return Lox::Expr::This->new({keyword => $self->previous});
453 }
454 elsif ($self->match(IDENTIFIER)) {
455 1558 return Lox::Expr::Variable->new({name => $self->previous});
456 }
457 elsif ($self->match(LEFT_PAREN)) {
458 14 my $expr = $self->expression;
459 13 $self->consume(RIGHT_PAREN, 'Expect ")" after expression');
460 13 return Lox::Expr::Grouping->new({expression => $expr});
461 }
462 elsif ($self->match(FUN)) {
463 27 return $self->function_expr;
464 }
465 13 $self->error($self->peek, 'Expect expression');
466 }
467  
468 52503 ▶sub match {
Subroutine match: called CC=3 SCAR=11.0
Pod undocumented
469 52503 my ($self, @types) = @_;
470 52503 for my $t (@types) {
471 69380 ▶ if ($self->check($t)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check($t)) 6540 62840 69380
472 6540 $self->advance;
473 6540 return 1;
474 }
475 }
476 45963 return undef;
477 }
478  
479 4252 ▶sub consume {
Subroutine consume: called CC=2 SCAR=6.9
Pod undocumented
480 4252 my ($self, $type, $msg) = @_;
481 4252 ▶ return $self->advance if $self->check($type);
Branch: 1 branch
BranchTrueFalseTotal
if $self->check($type) 4233 19 4252
482 19 $self->error($self->peek, $msg);
483 }
484  
485 76580 ▶sub check {
Subroutine check: called CC=2 SCAR=6.9
Pod undocumented
486 76580 my ($self, $type, $offset) = @_;
487 76580 ▶ return $self->is_at_end ? undef : $self->peek($offset)->{type} == $type;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_at_end ? : 4 76576 76580
488 }
489  
490 11008 ▶sub advance {
Subroutine advance: called CC=2 SCAR=6.9
Pod undocumented
491 11008 my $self = shift;
492 11008 ▶ $self->current++ unless $self->is_at_end;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_at_end 11007 1 11008
493 11008 return $self->previous;
494 }
495  
496 89366 ▶sub is_at_end { shift->peek->{type} == EOF }
Subroutine is_at_end: called CC=1 SCAR=0.0
Pod undocumented
497  
498 166087 ▶sub peek {
Subroutine peek: called CC=2 SCAR=6.9
Pod undocumented
499 166087 my ($self, $offset) = @_;
500 166087 ▶ return $self->tokens->[ $self->current + ($offset//0) ];
Condition100%2/2
$offset // 0
l!l
83166004
MC/DC100%1/1
$offset // 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
501 }
502  
503 14126 ▶sub previous {
Subroutine previous: called CC=1 SCAR=0.0
Pod undocumented
504 14126 my $self = shift;
505 14126 return $self->tokens->[ $self->current - 1];
506 }
507  
508 45 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod undocumented
509 45 my ($self, $token, $msg) = @_;
510 45 push $self->errors->@*, [$token, $msg];
511 45 die $msg;
512 }
513  
514 45 ▶sub synchronize {
Subroutine synchronize: called CC=4 SCAR=13.9
Pod undocumented
515 45 my $self = shift;
516 45 $self->advance;
517 45 ▶ while (!$self->is_at_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_at_end) 117 35 152
518 117 ▶ return if $self->previous->{type} == SEMICOLON;
Branch: 1 branch
BranchTrueFalseTotal
if $self->previous->{"type"} == 8 10 107 117
519 107 my $next = $self->peek;
520 107 ▶ return if grep { $next == $_ } CLASS,FUN,VAR,FOR,IF,WHILE,PRINT,RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$next == $_;} 24, 27, 37, 28, 29, 38, 32, 33 0 107 107
521 107 $self->advance;
522 }
523 }
524  
525 260 1;
« blib/lib/Lox/Scanner.pm ↑ Summary blib/lib/Lox/AstPrinter.pm »