lib/App/AnteREPL/Shorthand.pm

filter: Statementstmt 96.7% 147 / 152 Branchbran 93.1% 108 / 116 Conditioncond 75.7% 25 / 33 MC/DCmcdc 63.1% 12 / 19 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 4 totaltotal 90.7% 312 / 344 CC 71 SCAR 20.8
CC
71
Cov
93%
_value
14.2
_group
13.9
_definition
8.0
CRAP 8.0
« lib/App/AnteREPL.pm ↑ Summary script/anterepl »
1 25 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package App::AnteREPL::Shorthand;
6  
7 25 ▶use Text::Balanced qw(extract_bracketed extract_quotelike);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 25 my %keyword;
10  
11 # Ask this Perl about its own keywords without compiling any user code.
12 191 ▶sub perl_keyword {
Subroutine perl_keyword: called CC=3 SCAR=11.0
Pod undocumented
13 191 my ($name) = @_;
14 191 ▶ return $keyword{$name} if exists $keyword{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $keyword{$name} 131 60 191
15 60 local $@;
16 60 ▶ return $keyword{$name} = eval { my $prototype = prototype "CORE::$name"; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $prototype = prototype "CORE::$name"; 1 } } ? : 25 35 60
17 }
18  
19 31 ▶sub _fail {
Subroutine _fail: called CC=1 SCAR=0.0
20 31 die "Ambiguous shorthand: $_[0]. Try EVAL with explicit Perl syntax.\n";
21 }
22  
23 348 ▶sub _trim {
Subroutine _trim: called CC=1 SCAR=0.0
24 348 my ($text) = @_;
25 348 $text =~ s/\A\s+|\s+\z//g;
26 348 return $text;
27 }
28  
29 761 ▶sub _starts_quote {
Subroutine _starts_quote: called CC=2 SCAR=6.9
30 761 my ($text) = @_;
31 761 ▶ return $text =~ /\A['"`]/
Condition100%3/3
$text =~ /\A['"`]/u || $text =~ /\A(?:qq|qw|qx|qr|q|m|s|tr|y)(?=\s|[^\w\s])\s*(?!=>)\S/u
l!l&&r!l&&!r
3751673
MC/DC100%2/2
$text =~ /\A['"`]/u || $text =~ /\A(?:qq|qw|qx|qr|q|m|s|tr|y)(?=\s|[^\w\s])\s*(?!=>)\S/u
$text =~ /\A['"`]/u $text =~ /\A(?:qq|qw|qx|qr|q|m|s|tr|y)(?=\s|[^\w\s])\s*(?!=>)\S/u
Truth table100%3/3
A || B
A= $text =~ /\A['"`]/uB= $text =~ /\A(?:qq|qw|qx|qr|q|m|s|tr|y)(?=\s|[^\w\s])\s*(?!=>)\S/u
ABresult
1X1
011
000
32 || $text =~ /\A(?:qq|qw|qx|qr|q|m|s|tr|y)(?=\s|[^\w\s])\s*(?!=>)\S/;
33 }
34  
35 88 ▶sub _quote {
Subroutine _quote: called CC=3 SCAR=11.0
36 88 my ($text) = @_;
37 88 local $@;
38 88 my ($quote) = extract_quotelike("$text", '');
39 88 ▶ _fail('unclosed quoted value') unless defined $quote && length $quote;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $quote and length $quote 4 84 88
Condition66%2/3
defined $quote and length $quote
!ll&&!rl&&r
4084
MC/DC50%1/2
defined $quote and length $quote
defined $quote length $quote
Truth table66%2/3
A and B
A= defined $quoteB= length $quote
ABresult
0X0
100
111
40 84 return $quote;
41 }
42  
43 46 ▶sub _group {
Subroutine _group: called CC=13 SCAR=26.3
44 46 my ($text) = @_;
45 46 my $open = substr($text, 0, 1);
46 46 ▶ if ($open eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($open eq "(") 19 27 46
47 19 local $@;
48 19 my ($group) = extract_bracketed("$text", q{()[]{}'"`q}, '');
49 19 ▶ return $group if defined $group && length $group;
Branch: 1 branch
BranchTrueFalseTotal
if defined $group and length $group 19 0 19
Condition33%1/3
defined $group and length $group
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $group and length $group
defined $group length $group
Truth table33%1/3
A and B
A= defined $groupB= length $group
ABresult
0X0
100
111
50 0 ▶ _fail($@ =~ /Mismatched/ ? 'mismatched expression delimiters' : 'unclosed parenthesized expression');
Branch: 1 branch
BranchTrueFalseTotal
$@ =~ /Mismatched/u ? : 0 0 0
51 }
52 27 ▶ my $close = $open eq '[' ? ']' : '}';
Branch: 1 branch
BranchTrueFalseTotal
$open eq '[' ? : 19 8 27
53 27 my $position = 1;
54 27 ▶ while ($position < length $text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position < length $text) 122 3 125
55 122 my $rest = substr($text, $position);
56 122 my $char = substr($rest, 0, 1);
57 122 ▶ return substr($text, 0, $position + 1) if $char eq $close;
Branch: 1 branch
BranchTrueFalseTotal
if $char eq $close 21 101 122
58 101 ▶ _fail('mismatched container delimiters') if $char =~ /[)\]}]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[)\]}]/u 2 99 101
59 0 ▶ if ($rest =~ /\A\$[A-Za-z_][A-Za-z0-9_]*/) { $position += length $&; }
Branch: 4 branches
BranchTrueFalseTotal
if ($rest =~ /\A\$[A-Za-z_][A-Za-z0-9_]*/u) { } 0 99 99
elsif (_starts_quote($rest)) { } 16 83 99
elsif ($char =~ /[([{]/u) { } 3 80 83
elsif ($rest =~ /\A[\p{L}_][\p{L}\p{N}_]*/u) { } 23 57 80
60 16 elsif (_starts_quote($rest)) { $position += length _quote($rest); }
61 3 elsif ($char =~ /[([{]/) { $position += length _group($rest); }
62 23 elsif ($rest =~ /\A[\p{L}_][\p{L}\p{N}_]*/) { $position += length $&; }
63 57 else { ++$position; }
64 }
65 3 _fail('unclosed container');
66 }
67  
68 # Split only at the requested top-level separator. Perl expressions enclosed
69 # in parentheses are opaque: this scanner never compiles or evaluates them.
70 111 ▶sub _split {
Subroutine _split: called CC=8 SCAR=20.8
71 111 my ($text, $separator) = @_;
72 111 my @parts;
73 111 my ($start, $position) = (0, 0);
74 111 ▶ while ($position < length $text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position < length $text) 621 102 723
75 621 my $rest = substr($text, $position);
76 621 my $char = substr($rest, 0, 1);
77 621 ▶ if ($rest =~ /\A\$[A-Za-z_][A-Za-z0-9_]*/) { $position += length $&; }
Branch: 6 branches
BranchTrueFalseTotal
if ($rest =~ /\A\$[A-Za-z_][A-Za-z0-9_]*/u) { } 4 617 621
elsif (_starts_quote($rest)) { } 47 570 617
elsif ($char =~ /[([{]/u) { } 30 540 570
elsif ($char =~ /[)\]}]/u) { } 0 540 540
elsif (substr($rest, 0, length $separator) eq $separator) { } 93 447 540
elsif ($rest =~ /\A[\p{L}_][\p{L}\p{N}_]*/u) { } 153 294 447
78 47 elsif (_starts_quote($rest)) { $position += length _quote($rest); }
79 elsif ($char =~ /[([{]/) {
80 30 $position += length _group($rest);
81 }
82 0 elsif ($char =~ /[)\]}]/) { _fail('unexpected closing delimiter'); }
83 elsif (substr($rest, 0, length $separator) eq $separator) {
84 93 push @parts, substr($text, $start, $position - $start);
85 93 $position += length $separator;
86 93 $start = $position;
87 }
88 153 elsif ($rest =~ /\A[\p{L}_][\p{L}\p{N}_]*/) { $position += length $&; }
89 294 else { ++$position; }
90 }
91 102 push @parts, substr($text, $start);
92 102 return @parts;
93 }
94  
95 108 ▶sub _value {
Subroutine _value: called CC=14 SCAR=26.5
96 108 my ($text) = @_;
97 108 $text = _trim($text);
98 108 ▶ _fail('a value is missing') unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 1 107 108
99 107 ▶ return 'undef' if $text eq 'undef';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "undef" 1 106 107
100 106 ▶ return "'$text'" if $text =~ /\A[\p{L}_][\p{L}\p{N}_]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A[\p{L}_][\p{L}\p{N}_]*\z/u 51 55 106
101 55 ▶ return $text if $text =~ /\A[+-]?(?:(?:0|[1-9][0-9]*)(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A[+-]?(?:(?:0|[1-9][0-9]*)(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\z/u 7 48 55
102 48 ▶ return $text if $text =~ /\A\$[A-Za-z_][A-Za-z0-9_]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A\$[A-Za-z_][A-Za-z0-9_]*\z/u 3 45 48
103 45 ▶ if (_starts_quote($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_starts_quote($text)) 25 20 45
104 25 my $quote = _quote($text);
105 25 ▶ return $quote if _trim(substr($text, length $quote)) eq '';
Branch: 1 branch
BranchTrueFalseTotal
if _trim(substr($text, length $quote)) eq "" 25 0 25
106 0 _fail('a quoted value must occupy the whole item');
107 }
108 20 ▶ if ($text =~ /\A[([{]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\A[([{]/u) 13 7 20
109 13 my $group = _group($text);
110 13 my $rest = substr($text, length $group);
111 13 ▶ _fail('a container or parenthesized expression must occupy the whole item')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $group and length $group and _trim($rest) eq "" 1 12 13
Condition50%3/6
defined $group and length $group and _trim($rest) eq ""
!ll&&!rl&&r
0112
defined $group and length $group
!ll&&!rl&&r
0013
MC/DC33%1/3
defined $group and length $group and _trim($rest) eq ""
defined $group length $group _trim($rest) eq ""
Truth table50%2/4
A and B and C
A= defined $groupB= length $groupC= _trim($rest) eq ""
ABCresult
0XX0
10X0
1100
1111
112 unless defined $group && length $group && _trim($rest) eq '';
113 12 my $open = substr($group, 0, 1);
114 12 ▶ return $group if $open eq '(';
Branch: 1 branch
BranchTrueFalseTotal
if $open eq "(" 7 5 12
115 5 my $body = substr($group, 1, length($group) - 2);
116 5 ▶ return '[' . _array($body) . ']' if $open eq '[';
Branch: 1 branch
BranchTrueFalseTotal
if $open eq "[" 3 2 5
117 2 return '{' . _hash($body) . '}';
118 }
119 7 _fail("cannot read value '$text'; quote literal text or parenthesize a Perl expression");
120 }
121  
122 56 ▶sub _items {
Subroutine _items: called CC=5 SCAR=16.1
123 56 my ($text) = @_;
124 56 ▶ return () unless length _trim($text);
Branch: 1 branch
BranchTrueFalseTotal
unless length _trim($text) 4 52 56
125 52 my @items = _split($text, ',');
126 50 ▶ pop @items if @items > 1 && _trim($items[-1]) eq ''; # One trailing comma.
Branch: 1 branch
BranchTrueFalseTotal
if @items > 1 and _trim($items[-1]) eq "" 1 49 50
Condition100%3/3
@items > 1 and _trim($items[-1]) eq ""
!ll&&!rl&&r
22271
MC/DC100%2/2
@items > 1 and _trim($items[-1]) eq ""
@items > 1 _trim($items[-1]) eq ""
Truth table100%3/3
A and B
A= @items > 1B= _trim($items[-1]) eq ""
ABresult
0X0
100
111
127 50 ▶ _fail('a value is missing between commas') if grep { _trim($_) eq '' } @items;
Branch: 1 branch
BranchTrueFalseTotal
if grep {_trim($_) eq "";} @items 2 48 50
128 48 return @items;
129 }
130  
131 39 ▶sub _array {
Subroutine _array: called CC=1 SCAR=0.0
132 39 my ($text) = @_;
133 39 return join ', ', map { _value($_) } _items($text);
134 }
135  
136 17 ▶sub _hash {
Subroutine _hash: called CC=4 SCAR=13.9
137 17 my ($text) = @_;
138 17 my @pairs;
139 17 for my $item (_items($text)) {
140 24 my @pair = _split($item, '=>');
141 24 ▶ _fail('each hash item needs exactly one key => value pair') unless @pair == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @pair == 2 2 22 24
142 22 ▶ _fail('hash keys cannot be container literals') if _trim($pair[0]) =~ /\A[\[{]/;
Branch: 1 branch
BranchTrueFalseTotal
if _trim($pair[0]) =~ /\A[\[{]/u 1 21 22
143 # A pair always has one key and one value. Arrays belong in [...];
144 # parenthesized expressions in these two positions use scalar context.
145 21 push @pairs, 'scalar(' . _value($pair[0]) . ') => scalar(' . _value($pair[1]) . ')';
146 }
147 13 return join ', ', @pairs;
148 }
149  
150 63 ▶sub _definition {
Subroutine _definition: called CC=8 SCAR=20.8
151 63 my ($type, $name, $body) = @_;
152 63 ▶ if ($type eq 's') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "s") 12 51 63
153 12 ▶ _fail('a function body is missing; try E s name return expression') unless $body =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $body =~ /\S/u 1 11 12
154 11 ▶ _fail("'$name' is a Perl keyword; choose another function name") if perl_keyword($name);
Branch: 1 branch
BranchTrueFalseTotal
if perl_keyword($name) 1 10 11
155 10 return "sub $name {\n$body\n}";
156 }
157 51 ▶ return '@' . $name . ' = (' . _array($body) . ');' if $type eq 'a';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "a" 29 22 51
158 22 ▶ return '$' . $name . ' = [' . _array($body) . '];' if $type eq 'ar';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ar" 7 15 22
159 15 ▶ return '%' . $name . ' = (' . _hash($body) . ');' if $type eq 'h';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "h" 9 6 15
160 6 ▶ return '$' . $name . ' = {' . _hash($body) . '};' if $type eq 'hr';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hr" 6 0 6
161 0 die "Unknown internal shorthand type\n";
162 }
163  
164 744 ▶sub explicit {
Subroutine explicit: called CC=5 SCAR=16.1
Pod undocumented
165 744 my ($class, $line) = @_;
166 744 ▶ return unless $line =~ /\A\s*E(!?)\s+(s|a|ar|h|hr)(?=\s|\z)(.*)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\A\s*E(!?)\s+(s|a|ar|h|hr)(?=\s|\z)(.*)\z/su 686 58 744
167 58 my ($force, $type, $tail) = ($1, $2, $3);
168 58 ▶ $tail =~ s/;\s*\z// if $type ne 's';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "s" 42 16 58
169 58 ▶ _fail("$type needs a plain destination name, without a sigil")
Branch: 1 branch
BranchTrueFalseTotal
unless $tail =~ /\A\s+([A-Za-z_][A-Za-z0-9_]*)(?:\s+(.*))?\z/su 6 52 58
170 unless $tail =~ /\A\s+([A-Za-z_][A-Za-z0-9_]*)(?:\s+(.*))?\z/s;
171 52 ▶ my ($name, $body) = ($1, defined $2 ? $2 : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 47 5 52
172 52 return { code=>_definition($type, $name, $body), type=>$type, name=>$name, force=>$force, label=>'E' };
173 }
174  
175 421 ▶sub _implicit_candidate {
Subroutine _implicit_candidate: called CC=8 SCAR=20.8
176 421 my ($line, $is_function) = @_;
177 421 ▶ return unless $line =~ /\A\s*([a-z_][A-Za-z0-9_]*)(!?)\s+(.+)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\A\s*([a-z_][A-Za-z0-9_]*)(!?)\s+(.+)\z/su 260 161 421
178 161 my ($name, $force, $body) = ($1, $2, $3);
179 161 ▶ return if perl_keyword($name) || (!$force && $is_function && $is_function->($name));
Branch: 1 branch
BranchTrueFalseTotal
if perl_keyword($name) or not $force and $is_function and &$is_function($name) 143 18 161
Condition100%9/9
perl_keyword($name) or not $force and $is_function and &$is_function($name)
l!l&&r!l&&!r
136718
not $force and $is_function and &$is_function($name)
!ll&&!rl&&r
3157
not $force and $is_function
!ll&&!rl&&r
2122
MC/DC100%4/4
perl_keyword($name) or not $force and $is_function and &$is_function($name)
perl_keyword($name) not $force $is_function &$is_function($name)
Truth table100%5/5
A or not(B) and C and D
A= perl_keyword($name)B= not $forceC= $is_functionD= &$is_function($name)
ABCDresult
1XXX1
01111
00XX0
010X0
01100
180 18 ▶ return if $body =~ /\A=>/; # Perl's autoquoted hash key, not a destination.
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\A=>/u 1 17 18
181 17 ▶ return unless $body =~ /,|=>/;
Branch: 1 branch
BranchTrueFalseTotal
unless $body =~ /,|=>/u 2 15 17
182 15 return ($name, $body, $force);
183 }
184  
185 237 ▶sub implicit {
Subroutine implicit: called CC=5 SCAR=16.3
Pod undocumented
186 237 my ($class, $line, $is_function) = @_;
187 237 my ($name, $body, $force) = _implicit_candidate($line, $is_function);
188 237 ▶ return unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 226 11 237
189 11 $body =~ s/;\s*\z//; # One optional statement terminator, outside the values.
190 11 my @commas = _split($body, ',');
191 11 my @arrows = _split($body, '=>');
192 11 ▶ return unless @commas > 1 || @arrows > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @commas > 1 or @arrows > 1 0 11 11
Condition33%1/3
@commas > 1 or @arrows > 1
l!l&&r!l&&!r
1100
MC/DC0%0/2
@commas > 1 or @arrows > 1
@commas > 1 @arrows > 1
Truth table33%1/3
A or B
A= @commas > 1B= @arrows > 1
ABresult
1X1
011
000
193 11 ▶ my $type = @arrows > 1 ? 'h' : 'a';
Branch: 1 branch
BranchTrueFalseTotal
@arrows > 1 ? : 1 10 11
194 11 return { code=>_definition($type, $name, $body), type=>$type, name=>$name, force=>$force, label=>$name };
195 }
196  
197 193 ▶sub needs_more {
Subroutine needs_more: called CC=5 SCAR=16.1
Pod undocumented
198 193 my ($class, $line, $is_function) = @_;
199 193 my $body;
200 193 ▶ if ($line =~ /\A\s*E!?\s+(?:a|ar|h|hr)\s+[A-Za-z_][A-Za-z0-9_]*\s+(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A\s*E!?\s+(?:a|ar|h|hr)\s+[A-Za-z_][A-Za-z0-9_]*\s+(.*)\z/su) { } 9 184 193
201 9 $body = $1;
202 }
203 184 else { (undef, $body) = _implicit_candidate($line, $is_function); }
204 193 ▶ return unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 180 13 193
205 13 local $@;
206 13 my $ok = eval { _split($body, ','); 1 };
207 13 ▶ return !$ok && $@ =~ /unclosed/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!$ok && $@ =~ /unclosed/u ? : 6 7 13
Condition100%3/3
not $ok and $@ =~ /unclosed/u
!ll&&!rl&&r
616
MC/DC100%2/2
not $ok and $@ =~ /unclosed/u
not $ok $@ =~ /unclosed/u
Truth table100%3/3
not(A) and B
A= not $okB= $@ =~ /unclosed/u
ABresult
0X0
100
111
208 }
209  
210 25 1;
211  
212 __END__
« lib/App/AnteREPL.pm ↑ Summary script/anterepl »