lib/Zuzu/CLI.pm

filter: Statementstmt 50.5% 197 / 390 Branchbran 30.4% 53 / 174 Conditioncond 16.3% 10 / 61 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 85.2% 29 / 34 Podpod 0.0% 0 / 1 totaltotal 41.5% 289 / 696 CC 115 SCAR 37.5
CC
115
Cov
42%
_run_repl
121.2
_repl_structural_depth
91.9
_is_probably_incomplete_parse_error
90.0
CRAP 42.5
« blib/lib/Zuzu/Lexer.pm ↑ Summary blib/lib/Zuzu/Module/Net/SMTP.pm »
1 package Zuzu::CLI;
2  
3 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 our $VERSION = '0.007002';
8  
9 10 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Getopt::Long qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 Configure
12 GetOptionsFromArray
13 10 );
14  
15 10 ▶use Zuzu;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use Zuzu::Lexer;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use Zuzu::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use Zuzu::Parser::_Impl;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Zuzu::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use Zuzu::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use Zuzu::Value::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 9 ▶sub run {
Subroutine run: called CC=15 SCAR=34.3
Pod undocumented
24 9 my ( @argv ) = @_;
25 9 ▶ @argv = @ARGV if ! @argv;
Branch: 1 branch
BranchTrueFalseTotal
unless @argv 0 9 9
26  
27 9 binmode(*STDOUT, ':utf8');
28 9 binmode(*STDERR, ':utf8');
29  
30 9 my ( $options, $args, $usage_error ) = _parse_options( \@argv );
31 9 ▶ if ( defined $usage_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $usage_error) 0 9 9
32 0 _print_usage($usage_error);
33 0 return 2;
34 }
35  
36 9 my ( $deny, $deny_module_list, $preload, $disabled_visitors, $value_error )
37 = _normalize_option_lists($options);
38 9 ▶ if ( defined $value_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_error) 0 9 9
39 0 _print_usage($value_error);
40 0 return 2;
41 }
42 9 $options->{disabled_visitors} = $disabled_visitors;
43  
44 9 ▶ Zuzu::Runtime->clear_persistent_ast_cache if $options->{clear_cache};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"clear_cache"} 0 9 9
45  
46 9 ▶ if ( $options->{show_version} or $options->{show_version_verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"show_version"} or $options->{"show_version_verbose"}) 0 9 9
Condition33%1/3
$options->{"show_version"} or $options->{"show_version_verbose"}
l!l&&r!l&&!r
009
MC/DC0%0/2
$options->{"show_version"} or $options->{"show_version_verbose"}
$options->{"show_version"} $options->{"show_version_verbose"}
Truth table33%1/3
A or B
A= $options->{"show_version"}B= $options->{"show_version_verbose"}
ABresult
1X1
011
000
47 0 _print_version( $options, $deny, $deny_module_list );
48 0 return 0;
49 }
50  
51 9 my ( $script, $source, $script_error ) = _prepare_source(
52 $options,
53 $args,
54 $preload,
55 );
56 9 ▶ if ( defined $script_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $script_error) 0 9 9
57 0 _print_usage($script_error);
58 0 return 2;
59 }
60  
61 9 $Zuzu::Runtime::DEBUG_LEVEL = $options->{debug_level};
62 9 $ENV{ZUZU_DEBUG_LEVEL} = $options->{debug_level};
63  
64 my $runtime = Zuzu::Runtime->new(
65 9 lib => [ @{ $options->{include_dirs} }, @Zuzu::Runtime::DEFAULT_LIB ],
66 deny => $deny,
67 deny_modules => $deny_module_list,
68 persistent_ast_cache => !$options->{no_cache},
69 9 disabled_visitors => $disabled_visitors,
70 );
71  
72 9 ▶ if ( $options->{repl_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"repl_mode"}) 1 8 9
73 1 _run_repl($runtime);
74 1 return 0;
75 }
76  
77 8 my $parser = Zuzu::Parser->new( disabled_visitors => $disabled_visitors );
78 8 my $ok = eval {
79 8 my $ast = $parser->parse( $source, $script );
80 8 $runtime->evaluate($ast);
81  
82 6 ▶ if ( $runtime->has_function('__main__') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($runtime->has_function("__main__")) 0 6 6
83 0 my $call_args = Zuzu::Value::Array->new( items => [ @$args ] );
84 0 ▶ if ( $runtime->function_is_async('__main__') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($runtime->function_is_async('__main__')) { } 0 0 0
85 0 $runtime->call( '__main__', $call_args );
86 }
87 else {
88 0 $runtime->call_unawaited( '__main__', $call_args );
89 }
90 }
91 6 1;
92 };
93 7 ▶ if ( ! $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 6 7
94 1 my $err = $@;
95 1 ▶ if ( ref($err) eq 'HASH' and $err->{_zuzu_throw} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err eq "HASH" and $err->{"_zuzu_throw"}) 0 1 1
Condition33%1/3
ref $err eq "HASH" and $err->{"_zuzu_throw"}
!ll&&!rl&&r
100
MC/DC0%0/2
ref $err eq "HASH" and $err->{"_zuzu_throw"}
ref $err eq "HASH" $err->{"_zuzu_throw"}
Truth table33%1/3
A and B
A= ref $err eq "HASH"B= $err->{"_zuzu_throw"}
ABresult
0X0
100
111
96 0 ▶ my $value = defined $err->{value} ? $err->{value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $err->{'value'} ? : 0 0 0
97 0 my $text = _render_thrown_value( $runtime, $value );
98 0 print STDERR "$text\n";
99 0 $runtime->finish;
100 0 return 255;
101 }
102 1 $runtime->finish;
103 1 die $err;
104 }
105  
106 6 $runtime->finish;
107  
108 6 return 0;
109 }
110  
111 9 ▶sub _parse_options {
Subroutine _parse_options: called CC=6 SCAR=23.5
112 9 my ( $argv ) = @_;
113  
114 9 my $options = {
115 debug_level => 0,
116 include_dirs => [],
117 deny_capabilities => [],
118 deny_modules => [],
119 inline_snippets => [],
120 preload_modules => [],
121 no_visitors => [],
122 no_cache => 0,
123 clear_cache => 0,
124 repl_mode => 0,
125 show_version => 0,
126 show_version_verbose => 0,
127 };
128 9 my $debug_opt;
129  
130 9 Configure(
131 'no_ignore_case',
132 'bundling',
133 'require_order',
134 );
135 my $ok = GetOptionsFromArray(
136 $argv,
137 'd:i' => \$debug_opt,
138 'I=s@' => $options->{include_dirs},
139 'deny=s@' => $options->{deny_capabilities},
140 'denymodule=s@' => $options->{deny_modules},
141 'no-visitor=s@' => $options->{no_visitors},
142 'e=s@' => $options->{inline_snippets},
143 'M=s@' => $options->{preload_modules},
144 'no-cache' => \$options->{no_cache},
145 'clear-cache' => \$options->{clear_cache},
146 'R|repl' => \$options->{repl_mode},
147 'h|help' => \$options->{show_help},
148 'v' => \$options->{show_version},
149 'V' => \$options->{show_version_verbose},
150 9 );
151 9 ▶ if ( ! $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 9 9
152 0 return ( undef, undef, undef );
153 }
154  
155 9 ▶ if ( $options->{show_help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"show_help"}) 0 9 9
156 0 return ( undef, undef, '' );
157 }
158  
159 9 ▶ if ( defined $debug_opt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $debug_opt) 0 9 9
160 0 ▶ $debug_opt = 1 if $debug_opt eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $debug_opt eq "" 0 0 0
161 0 ▶ return ( undef, undef, 'Debug level must be a non-negative integer' )
Branch: 1 branch
BranchTrueFalseTotal
unless $debug_opt =~ /\A(?:0|[1-9][0-9]*)\z/ 0 0 0
162 if $debug_opt !~ /\A(?:0|[1-9][0-9]*)\z/;
163 0 $options->{debug_level} = 0 + $debug_opt;
164 }
165  
166 9 return ( $options, $argv, undef );
167 }
168  
169 9 ▶sub _normalize_option_lists {
Subroutine _normalize_option_lists: called CC=4 SCAR=15.6
170 9 my ( $options ) = @_;
171  
172 9 my @deny = _flatten_trimmed_csv( @{ $options->{deny_capabilities} } );
173 9 my @deny_module_list = _flatten_trimmed_csv( @{ $options->{deny_modules} } );
174 9 my @preload = _flatten_trimmed_csv( @{ $options->{preload_modules} } );
175 9 my @disabled_visitors = _flatten_trimmed_csv( @{ $options->{no_visitors} } );
176  
177 9 for my $entry ( @deny, @deny_module_list, @preload, @disabled_visitors ) {
178 0 ▶ if ( $entry =~ /\A\s*\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /\A\s*\z/) 0 0 0
179 0 return ( undef, undef, undef, undef, 'Option values may not contain whitespace only' );
180 }
181 }
182  
183 9 my @known_visitors;
184 eval {
185 9 @known_visitors = Zuzu::Parser->normalize_disabled_visitors(
186 @disabled_visitors,
187 );
188 9 1;
189 9 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { @known_visitors = "Zuzu::Parser"->normalize_disabled_visitors(@disabled_visitors); 1 } } or do { my $error = $@; chomp $error; $error =~ s/ at \S+ line \d+\.?\z//; return undef, undef, undef, undef, $error } 9 0 9
190 0 my $error = $@;
191 0 chomp $error;
192 0 $error =~ s/ at \S+ line \d+\.?\z//;
193 0 return ( undef, undef, undef, undef, $error );
194 };
195  
196 9 return ( \@deny, \@deny_module_list, \@preload, \@known_visitors, undef );
197 }
198  
199 36 ▶sub _flatten_trimmed_csv {
Subroutine _flatten_trimmed_csv: called CC=2 SCAR=11.6
200 36 my ( @raw ) = @_;
201  
202 my @values = map {
203 0 s/^\s+//r =~ s/\s+$//r
204 } grep {
205 0 ▶ defined $_ and $_ ne ''
Condition0%0/3
defined $_ && $_ ne ''
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && $_ ne ''
defined $_ $_ ne ''
Truth table0%0/3
A && B
A= defined $_B= $_ ne ''
ABresult
0X0
100
111
206 } map {
207 0 split /,/
208 } @raw;
209  
210 36 return @values;
211 }
212  
213 9 ▶sub _prepare_source {
Subroutine _prepare_source: called CC=8 SCAR=25.1
214 9 my ( $options, $args, $preload ) = @_;
215  
216 9 ▶ if ( $options->{repl_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"repl_mode"}) 1 8 9
217 1 ▶ if ( @{ $options->{inline_snippets} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$options->{"inline_snippets"};}) 0 1 1
218 0 return ( undef, undef, '-R/--repl cannot be combined with -e snippets' );
219 }
220 1 ▶ if ( @$args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args) 0 1 1
221 0 return ( undef, undef, '-R/--repl does not accept a script path or argv values' );
222 }
223 1 return ( undef, undef, undef );
224 }
225  
226 8 ▶ if ( @{ $options->{inline_snippets} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$options->{"inline_snippets"};}) 0 8 8
227 my @prelude = map {
228 0 "from $_ import *;"
229 } @$preload;
230 0 my $source = join "\n", @prelude, @{ $options->{inline_snippets} };
231 0 return ( '(command line)', $source, undef );
232 }
233  
234 8 my $script = shift @$args;
235 8 ▶ if ( ! defined $script ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $script) 0 8 8
236 0 return ( undef, undef, '' );
237 }
238  
239 8 ▶ open my $fh, '<:encoding(UTF-8)', $script
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $script or die "Could not open '${script}': $!\n" 8 0 8
240 or die "Could not open '$script': $!\n";
241 8 my $source = do { local $/; <$fh> };
242 8 close $fh;
243  
244 8 ▶ if ( @$preload ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$preload) 0 8 8
245 my @prelude = map {
246 0 "from $_ import *;"
247 } @$preload;
248 0 $source = join "\n", @prelude, $source;
249 }
250  
251 8 return ( $script, $source, undef );
252 }
253  
254 0 ▶sub _print_usage {
Subroutine _print_usage: not called CC=3 SCAR=24.8
255 0 my ( $message ) = @_;
256  
257 0 ▶ if ( defined $message and $message ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message and $message ne "") 0 0 0
Condition0%0/3
defined $message and $message ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $message and $message ne ""
defined $message $message ne ""
Truth table0%0/3
A and B
A= defined $messageB= $message ne ""
ABresult
0X0
100
111
258 0 print STDERR $message, "\n";
259 }
260 0 print STDERR "Usage: zuzu.pl [options] path/to/script.zzs [arg ...]\n";
261 0 print STDERR " zuzu.pl [options] -e 'code' [arg ...]\n";
262 0 print STDERR "Options:\n";
263 0 print STDERR " -d[=N] set debug level (default: 1 if omitted)\n";
264 0 print STDERR " -I/path/to/lib add module include directory\n";
265 0 print STDERR " --deny=CAP deny runtime capability (repeatable)\n";
266 0 print STDERR " --denymodule=MODULE deny a specific module (repeatable)\n";
267 0 print STDERR " --no-visitor=NAME disable an AST visitor (repeatable)\n";
268 0 print STDERR " -e 'code' evaluate inline code (repeatable)\n";
269 0 print STDERR " -Mmodule preload module with wildcard import\n";
270 0 print STDERR " --no-cache disable the persistent module AST cache\n";
271 0 print STDERR " --clear-cache clear the persistent module AST cache before running\n";
272 0 print STDERR " -R, --repl start interactive REPL shell\n";
273 0 print STDERR " -h, --help show this help\n";
274 0 print STDERR " -v print version\n";
275 0 print STDERR " -V print verbose version details\n";
276  
277 0 return;
278 }
279  
280 0 ▶sub _print_version {
Subroutine _print_version: not called CC=5 SCAR=34.0
281 0 my ( $options, $deny, $deny_module_list ) = @_;
282  
283 my $runtime = Zuzu::Runtime->new(
284 0 ▶ lib => [ @{ $options->{include_dirs} }, @Zuzu::Runtime::DEFAULT_LIB ],
Condition0%0/2
$options->{"disabled_visitors"} // []
l!l
00
MC/DC0%0/1
$options->{"disabled_visitors"} // []
$options->{"disabled_visitors"}
Truth table0%0/2
A
A= $options->{"disabled_visitors"}
Aresult
11
00
285 deny => $deny,
286 deny_modules => $deny_module_list,
287 persistent_ast_cache => !$options->{no_cache},
288 0 disabled_visitors => $options->{disabled_visitors} // [],
289 );
290  
291 0 print "zuzu.pl version $Zuzu::VERSION\n";
292 0 ▶ if ( $options->{show_version_verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"show_version_verbose"}) 0 0 0
293 0 print "\n";
294 0 print "lib search paths:\n";
295 0 for my $path ( @{ $runtime->lib } ) {
296 0 print " $path\n";
297 }
298 0 print "\n";
299 0 print "builtin modules:\n";
300 0 for my $module ( sort keys %{ $runtime->builtin } ) {
301 0 print " $module\n";
302 }
303 }
304  
305 0 return;
306 }
307  
308 2 ▶sub _repl_print_prompt {
Subroutine _repl_print_prompt: called CC=1 SCAR=0.0
309 2 my ( $continuation ) = @_;
310  
311 2 print STDOUT _repl_prompt_coloured($continuation);
312  
313 2 return;
314 }
315  
316 3 ▶sub _repl_prompt_label {
Subroutine _repl_prompt_label: called CC=4 SCAR=19.1
317 3 my ( $continuation ) = @_;
318  
319 return $continuation
320 ? 'zuzu (...)> '
321 : 'zuzu (^_^)> '
322 3 ▶ if ! $ENV{ZUZU_EMOJI};
Branch: 2 branches
BranchTrueFalseTotal
unless $ENV{"ZUZU_EMOJI"} 3 0 3
$continuation ? : 0 3 3
323  
324 0 ▶ return $continuation
Branch: 1 branch
BranchTrueFalseTotal
$continuation ? : 0 0 0
325 ? 'zuzu 🦝 ⏳ > '
326 : 'zuzu 🦝 💤 > ';
327 }
328  
329 2 ▶sub _repl_prompt_coloured {
Subroutine _repl_prompt_coloured: called CC=2 SCAR=7.1
330 2 my ( $continuation ) = @_;
331  
332 2 ▶ my $colour = $continuation ? "\e[1;35m" : "\e[1;36m";
Branch: 1 branch
BranchTrueFalseTotal
$continuation ? : 0 2 2
333 2 return $colour . _repl_prompt_label($continuation) . "\e[0m";
334 }
335  
336 1 ▶sub _repl_print_output {
Subroutine _repl_print_output: called CC=1 SCAR=0.0
337 1 my ( $message ) = @_;
338  
339 1 print STDOUT "\e[1;32m", $message, "\e[0m\n";
340  
341 1 return;
342 }
343  
344 0 ▶sub _repl_print_error {
Subroutine _repl_print_error: not called CC=2 SCAR=17.9
345 0 my ( $message ) = @_;
346  
347 0 ▶ my $colour = $message =~ /\bwarn(?:ing)?\b/i
Branch: 1 branch
BranchTrueFalseTotal
$message =~ /\bwarn(?:ing)?\b/i ? : 0 0 0
348 ? "\e[1;33m"
349 : "\e[1;31m";
350 0 print STDERR $colour, $message, "\e[0m\n";
351  
352 0 return;
353 }
354  
355 1 ▶sub _repl_render_value {
Subroutine _repl_render_value: called CC=5 SCAR=25.2
356 1 my ( $runtime, $value ) = @_;
357  
358 1 ▶ return 'Null' if ! defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 1 1
359 1 ▶ return "$value" if ! ref($value);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 1 0 1
360 0 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::Boolean') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")) 0 0 0
Condition0%0/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
builtin::blessed($value) $value->isa("Zuzu::Value::Boolean")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Boolean")
ABresult
0X0
100
111
361 0 return "$value";
362 }
363  
364 0 return $runtime->_type_name($value);
365 }
366  
367 0 ▶sub _is_probably_incomplete_parse_error {
Subroutine _is_probably_incomplete_parse_error: not called CC=9 SCAR=45.0
368 0 my ( $error, $source ) = @_;
369  
370 0 ▶ return 0 if ! blessed($error);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($error) 0 0 0
371 0 ▶ return 0 if ! $error->isa('Zuzu::Error::Compile');
Branch: 1 branch
BranchTrueFalseTotal
unless $error->isa("Zuzu::Error::Compile") 0 0 0
372 0 ▶ return 1 if ( $error->message // '' ) =~ /\AUnterminated\b/;
Branch: 1 branch
BranchTrueFalseTotal
if ($error->message // "") =~ /\AUnterminated\b/ 0 0 0
Condition0%0/2
$error->message // ""
l!l
00
MC/DC0%0/1
$error->message // ""
$error->message
Truth table0%0/2
A
A= $error->message
Aresult
11
00
373  
374 0 my $line_count = () = $source =~ /\n/g;
375 0 $line_count++;
376 0 ▶ return 0 if ! defined $error->line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error->line 0 0 0
377 0 ▶ return 0 if $error->line < $line_count;
Branch: 1 branch
BranchTrueFalseTotal
if $error->line < $line_count 0 0 0
378  
379 0 ▶ return 1 if ( $error->message // '' ) =~ /\AExpected\b/;
Branch: 1 branch
BranchTrueFalseTotal
if ($error->message // "") =~ /\AExpected\b/ 0 0 0
Condition0%0/2
$error->message // ""
l!l
00
MC/DC0%0/1
$error->message // ""
$error->message
Truth table0%0/2
A
A= $error->message
Aresult
11
00
380  
381 0 return 0;
382 }
383  
384 1 ▶sub _try_parse_with_optional_semicolon {
Subroutine _try_parse_with_optional_semicolon: called CC=5 SCAR=26.9
385 1 my ( $runtime, $source ) = @_;
386  
387 1 my $parse_ok = eval {
388 1 _parse_repl_source( $runtime, $source );
389 1 1;
390 };
391 1 ▶ if ( $parse_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse_ok) 1 0 1
392 1 return ( 1, undef, $source );
393 }
394 0 my $first_error = $@;
395  
396 0 my $trimmed = $source;
397 0 $trimmed =~ s/\s+\z//;
398 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $trimmed =~ /[;{}]\z/ and $trimmed =~ /\S/) 0 0 0
Condition0%0/3
not $trimmed =~ /[;{}]\z/ and $trimmed =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
not $trimmed =~ /[;{}]\z/ and $trimmed =~ /\S/
not $trimmed =~ /[;{}]\z/ $trimmed =~ /\S/
Truth table0%0/3
not(A) and B
A= not $trimmed =~ /[;{}]\z/B= $trimmed =~ /\S/
ABresult
0X0
100
111
399 $trimmed !~ /[;{}]\z/
400 and $trimmed =~ /\S/
401 ) {
402 0 my $with_semicolon = $trimmed . ';';
403 0 my $semicolon_ok = eval {
404 0 _parse_repl_source( $runtime, $with_semicolon );
405 0 1;
406 };
407 0 ▶ if ( $semicolon_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($semicolon_ok) 0 0 0
408 0 return ( 1, undef, $with_semicolon );
409 }
410 }
411  
412 0 return ( 0, $first_error, $source );
413 }
414  
415 1 ▶sub _repl_structural_depth {
Subroutine _repl_structural_depth: called CC=17 SCAR=45.2
416 1 my ( $source ) = @_;
417  
418 1 my $depth = 0;
419 1 my $in_single = 0;
420 1 my $in_double = 0;
421 1 my $escaped = 0;
422  
423 1 for my $ch ( split //, $source ) {
424 1 ▶ if ( $escaped ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escaped) 0 1 1
425 0 $escaped = 0;
426 0 next;
427 }
428 1 ▶ if ( $in_single ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_single) 0 1 1
429 0 ▶ if ( $ch eq '\\' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 0 0 0
430 0 $escaped = 1;
431 0 next;
432 }
433 0 ▶ if ( $ch eq "'" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 0 0 0
434 0 $in_single = 0;
435 }
436 0 next;
437 }
438 1 ▶ if ( $in_double ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_double) 0 1 1
439 0 ▶ if ( $ch eq '\\' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 0 0 0
440 0 $escaped = 1;
441 0 next;
442 }
443 0 ▶ if ( $ch eq '"' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 0 0 0
444 0 $in_double = 0;
445 }
446 0 next;
447 }
448 1 ▶ if ( $ch eq "'" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "'") 0 1 1
449 0 $in_single = 1;
450 0 next;
451 }
452 1 ▶ if ( $ch eq '"' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 0 1 1
453 0 $in_double = 1;
454 0 next;
455 }
456 1 ▶ if ( $ch eq '{' or $ch eq '(' or $ch eq '[' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "{" or $ch eq "(" or $ch eq "[") 0 1 1
Condition33%2/6
$ch eq "{" or $ch eq "(" or $ch eq "["
l!l&&r!l&&!r
001
$ch eq "{" or $ch eq "("
l!l&&r!l&&!r
001
MC/DC0%0/3
$ch eq "{" or $ch eq "(" or $ch eq "["
$ch eq "{" $ch eq "(" $ch eq "["
Truth table25%1/4
A or B or C
A= $ch eq "{"B= $ch eq "("C= $ch eq "["
ABCresult
1XX1
01X1
0011
0000
457 0 $depth++;
458 0 next;
459 }
460 1 ▶ if ( $ch eq '}' or $ch eq ')' or $ch eq ']' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "}" or $ch eq ")" or $ch eq "]") 0 1 1
Condition33%2/6
$ch eq "}" or $ch eq ")" or $ch eq "]"
l!l&&r!l&&!r
001
$ch eq "}" or $ch eq ")"
l!l&&r!l&&!r
001
MC/DC0%0/3
$ch eq "}" or $ch eq ")" or $ch eq "]"
$ch eq "}" $ch eq ")" $ch eq "]"
Truth table25%1/4
A or B or C
A= $ch eq "}"B= $ch eq ")"C= $ch eq "]"
ABCresult
1XX1
01X1
0011
0000
461 0 $depth--;
462 }
463 }
464  
465 1 return $depth;
466 }
467  
468 2 ▶sub _repl_prelude_for_runtime {
Subroutine _repl_prelude_for_runtime: called CC=7 SCAR=28.7
469 2 my ( $runtime ) = @_;
470  
471 2 my @decls;
472 2 ▶ for my $name ( sort keys %{ $runtime->{_global}{slots} // {} } ) {
Condition50%1/2
$runtime->{"_global"}{"slots"} // {}
l!l
20
MC/DC0%0/1
$runtime->{"_global"}{"slots"} // {}
$runtime->{"_global"}{"slots"}
Truth table50%1/2
A
A= $runtime->{"_global"}{"slots"}
Aresult
11
00
473 70 ▶ next if $runtime->{_builtin_global_names}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime->{"_builtin_global_names"}{$name} 70 0 70
474 0 ▶ next if $name =~ /\A__/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A__/ 0 0 0
475 0 ▶ next if $name !~ /\A[_A-Za-z][_A-Za-z0-9]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A[_A-Za-z][_A-Za-z0-9]*\z/ 0 0 0
476 0 ▶ next if Zuzu::Util::is_keyword($name);
Branch: 1 branch
BranchTrueFalseTotal
if Zuzu::Util::is_keyword($name) 0 0 0
477 0 push @decls, "let $name := null;";
478 }
479  
480 2 return @decls;
481 }
482  
483 1 ▶sub _repl_init_line_editor {
Subroutine _repl_init_line_editor: called CC=5 SCAR=31.0
484 1 ▶ return if ! -t STDIN;
Branch: 1 branch
BranchTrueFalseTotal
unless -t STDIN 1 0 1
485 0 ▶ return if ! -t STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
unless -t STDOUT 0 0 0
486  
487 0 my $ok = eval {
488 0 require Term::ReadLine;
489 0 1;
490 };
491 0 ▶ return if ! $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
492  
493 0 my $term = Term::ReadLine->new('zuzu');
494 0 ▶ return if ! $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $term 0 0 0
495 0 return $term;
496 }
497  
498 3 ▶sub _repl_read_line {
Subroutine _repl_read_line: called CC=7 SCAR=25.7
499 3 my ( $editor, $continuation, $history_state ) = @_;
500  
501 3 ▶ if ($editor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($editor) 1 2 3
502 1 my $line = $editor->readline( _repl_prompt_label($continuation) );
503 1 ▶ return undef if ! defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 0 1
504  
505 0 ▶ if ( $line =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\S/) 0 0 0
506 0 my $last = $history_state->{last_entry};
507 0 ▶ if ( ! defined $last or $last ne $line ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $last or $last ne $line) 0 0 0
Condition0%0/3
not defined $last or $last ne $line
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $last or $last ne $line
not defined $last $last ne $line
Truth table0%0/3
not(A) or B
A= not defined $lastB= $last ne $line
ABresult
1X1
011
000
508 0 $editor->addhistory($line);
509 0 $history_state->{last_entry} = $line;
510 }
511 }
512 0 return $line;
513 }
514  
515 2 _repl_print_prompt($continuation);
516 2 my $line = readline STDIN;
517 2 ▶ return undef if ! defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 1 2
518 1 chomp $line;
519 1 return $line;
520 }
521  
522 2 ▶sub _parse_repl_source {
Subroutine _parse_repl_source: called CC=3 SCAR=12.5
523 2 my ( $runtime, $source ) = @_;
524  
525 2 my @prelude = _repl_prelude_for_runtime($runtime);
526 2 ▶ my $combined = @prelude
Branch: 1 branch
BranchTrueFalseTotal
@prelude ? : 0 2 2
527 ? join( "\n", @prelude, $source )
528 : $source;
529  
530 2 my $lexer = Zuzu::Lexer->new(
531 src => $combined,
532 filename => '<repl>',
533 );
534 2 my $impl = Zuzu::Parser::_Impl->new(
535 lexer => $lexer,
536 filename => '<repl>',
537 );
538  
539 2 my $ast = $impl->parse_program;
540 2 ▶ if ( @prelude ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@prelude) 0 2 2
541 0 my @stmts = @{ $ast->statements };
542 0 splice @stmts, 0, scalar @prelude;
543 0 $ast->statements( \@stmts );
544 }
545  
546 2 return $ast;
547 }
548  
549 1 ▶sub _run_repl {
Subroutine _run_repl: called CC=19 SCAR=48.0
550 1 my ( $runtime ) = @_;
551  
552 1 my @buffer;
553 1 my $expecting_more = 0;
554 1 my $line_editor = _repl_init_line_editor();
555 1 my %history_state;
556  
557 1 while ( 1 ) {
558 2 my $line = _repl_read_line(
559 $line_editor,
560 $expecting_more,
561 \%history_state,
562 );
563 2 ▶ last if ! defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 1 2
564 1 ▶ next if ! @buffer and $line =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not @buffer and $line =~ /\A\s*\z/ 0 1 1
Condition33%1/3
not @buffer and $line =~ /\A\s*\z/
!ll&&!rl&&r
010
MC/DC0%0/2
not @buffer and $line =~ /\A\s*\z/
not @buffer $line =~ /\A\s*\z/
Truth table33%1/3
not(A) and B
A= not @bufferB= $line =~ /\A\s*\z/
ABresult
0X0
100
111
565  
566 1 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting_more and @buffer and $line =~ /\A\s*;\s*\z/) 0 1 1
Condition33%2/6
$expecting_more and @buffer and $line =~ /\A\s*;\s*\z/
!ll&&!rl&&r
100
$expecting_more and @buffer
!ll&&!rl&&r
100
MC/DC0%0/3
$expecting_more and @buffer and $line =~ /\A\s*;\s*\z/
$expecting_more @buffer $line =~ /\A\s*;\s*\z/
Truth table25%1/4
A and B and C
A= $expecting_moreB= @bufferC= $line =~ /\A\s*;\s*\z/
ABCresult
0XX0
10X0
1100
1111
567 $expecting_more
568 and @buffer
569 and $line =~ /\A\s*;\s*\z/
570 ) {
571 0 my $source = join "\n", @buffer;
572 0 my ( $ok, $err, $effective_source )
573 = _try_parse_with_optional_semicolon( $runtime, $source );
574 0 ▶ if ( ! $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
575 0 _repl_print_error("$err");
576 0 @buffer = ();
577 0 $expecting_more = 0;
578 0 next;
579 }
580  
581 eval {
582 0 my $ast = _parse_repl_source( $runtime, $effective_source );
583 0 my $result = $runtime->evaluate($ast);
584 0 _repl_print_output( _repl_render_value( $runtime, $result ) );
585 0 1;
586 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $ast = _parse_repl_source($runtime, $effective_source); my $result = $runtime->evaluate($ast); _repl_print_output(_repl_render_value($runtime, $result)); 1 } } or do { my $err_eval = $@; if (ref $err_eval eq "HASH" and $err_eval->{"_zuzu_throw"}) { my $value = defined $err_eval->{"value"} ? $err_eval->{"value"} : ""; _repl_print_error(_render_thrown_value($runtime, $value)); } else { _repl_print_error("$err_eval"); } } 0 0 0
587 0 my $err_eval = $@;
588 0 ▶ if ( ref($err_eval) eq 'HASH' and $err_eval->{_zuzu_throw} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err_eval eq 'HASH' and $err_eval->{'_zuzu_throw'}) { } 0 0 0
Condition0%0/3
ref $err_eval eq "HASH" and $err_eval->{"_zuzu_throw"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $err_eval eq "HASH" and $err_eval->{"_zuzu_throw"}
ref $err_eval eq "HASH" $err_eval->{"_zuzu_throw"}
Truth table0%0/3
A and B
A= ref $err_eval eq "HASH"B= $err_eval->{"_zuzu_throw"}
ABresult
0X0
100
111
589 0 ▶ my $value = defined $err_eval->{value} ? $err_eval->{value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $err_eval->{'value'} ? : 0 0 0
590 0 _repl_print_error( _render_thrown_value( $runtime, $value ) );
591 }
592 else {
593 0 _repl_print_error("$err_eval");
594 }
595 };
596  
597 0 @buffer = ();
598 0 $expecting_more = 0;
599 0 next;
600 }
601  
602 1 push @buffer, $line;
603 1 my $source = join "\n", @buffer;
604 1 ▶ if ( _repl_structural_depth($source) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_repl_structural_depth($source) > 0) 0 1 1
605 0 $expecting_more = 1;
606 0 next;
607 }
608 1 my ( $ok, $err, $effective_source )
609 = _try_parse_with_optional_semicolon( $runtime, $source );
610 1 ▶ if ( ! $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 1 1
611 0 ▶ if ( _is_probably_incomplete_parse_error( $err, $source ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_probably_incomplete_parse_error($err, $source)) 0 0 0
612 0 $expecting_more = 1;
613 0 next;
614 }
615  
616 0 _repl_print_error("$err");
617 0 @buffer = ();
618 0 $expecting_more = 0;
619 0 next;
620 }
621  
622 eval {
623 1 my $ast = _parse_repl_source( $runtime, $effective_source );
624 1 my $result = $runtime->evaluate($ast);
625 1 _repl_print_output( _repl_render_value( $runtime, $result ) );
626 1 1;
627 1 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $ast = _parse_repl_source($runtime, $effective_source); my $result = $runtime->evaluate($ast); _repl_print_output(_repl_render_value($runtime, $result)); 1 } } or do { my $err_eval = $@; if (ref $err_eval eq "HASH" and $err_eval->{"_zuzu_throw"}) { my $value = defined $err_eval->{"value"} ? $err_eval->{"value"} : ""; _repl_print_error(_render_thrown_value($runtime, $value)); } else { _repl_print_error("$err_eval"); } } 1 0 1
628 0 my $err_eval = $@;
629 0 ▶ if ( ref($err_eval) eq 'HASH' and $err_eval->{_zuzu_throw} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err_eval eq 'HASH' and $err_eval->{'_zuzu_throw'}) { } 0 0 0
Condition0%0/3
ref $err_eval eq "HASH" and $err_eval->{"_zuzu_throw"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $err_eval eq "HASH" and $err_eval->{"_zuzu_throw"}
ref $err_eval eq "HASH" $err_eval->{"_zuzu_throw"}
Truth table0%0/3
A and B
A= ref $err_eval eq "HASH"B= $err_eval->{"_zuzu_throw"}
ABresult
0X0
100
111
630 0 ▶ my $value = defined $err_eval->{value} ? $err_eval->{value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $err_eval->{'value'} ? : 0 0 0
631 0 _repl_print_error( _render_thrown_value( $runtime, $value ) );
632 }
633 else {
634 0 _repl_print_error("$err_eval");
635 }
636 };
637  
638 1 @buffer = ();
639 1 $expecting_more = 0;
640 }
641  
642 1 print STDOUT "\n";
643 1 return;
644 }
645  
646 0 ▶sub _render_thrown_value {
Subroutine _render_thrown_value: not called CC=6 SCAR=37.4
647 0 my ( $runtime, $value ) = @_;
648  
649 0 my $text = $runtime->_to_String( $value );
650 0 ▶ return $text if not blessed($value);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) 0 0 0
651 0 ▶ return $text if not $value->isa( 'Zuzu::Value::Object' );
Branch: 1 branch
BranchTrueFalseTotal
unless $value->isa("Zuzu::Value::Object") 0 0 0
652  
653 0 ▶ my $slots = $value->slots // {};
Condition0%0/2
$value->slots // {}
l!l
00
MC/DC0%0/1
$value->slots // {}
$value->slots
Truth table0%0/2
A
A= $value->slots
Aresult
11
00
654 0 my $file = $slots->{file};
655 0 my $line = $slots->{line};
656 0 ▶ return $text if not defined $file or not defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and defined $line 0 0 0
Condition0%0/3
defined $file and defined $line
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file and defined $line
defined $file defined $line
Truth table0%0/3
A and B
A= defined $fileB= defined $line
ABresult
0X0
100
111
657  
658 0 return "$text at $file, line $line";
659 }
660  
661 10 1;
662  
663 =pod
664  
665 =head1 COPYRIGHT AND LICENCE
666  
667 B<< Zuzu::CLI >> is copyright Toby Inkster.
668  
669 It is free software; you may redistribute it and/or modify it under
670 the terms of either the Artistic License 1.0 or the GNU General Public
671 License version 2.
672  
673 =cut
« blib/lib/Zuzu/Lexer.pm ↑ Summary blib/lib/Zuzu/Module/Net/SMTP.pm »