blib/lib/Ordeal/Model/Parser.pm

filter: Statementstmt 82.5% 156 / 189 Branchbran 53.1% 34 / 64 Conditioncond 25.0% 3 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 89.2% 58 / 65 Podpod 100% 1 / 1 totaltotal 74.8% 253 / 338 SCAR 45.5
CC
48
Cov
73%
__ANON__
20.0
__ANON__
12.8
___log
12.0
CRAP 94.2
↑ Summary blib/lib/Ordeal/Model/Evaluator.pm »
1 package Ordeal::Model::Parser;
2  
3 # vim: ts=3 sts=3 sw=3 et ai :
4  
5 5 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict; # redundant, but still useful to document
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 { our $VERSION = '0.003'; }
9 5 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use constant SHOW_CHARS => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use constant ELLIPSIS => '...';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use experimental qw< signatures postderef >;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶no warnings qw< experimental::signatures experimental::postderef >;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 ▶use Exporter qw< import >;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 our @EXPORT_OK = qw< PARSE >;
19  
20 ▶sub PARSE ($text) {
Subroutine PARSE: called CC=4 SCAR=19.2
Pod documented
21 2 state $expression = _expression();
22 2 my $ast = $expression->(\$text);
23 2 my $pos = pos $text;
24 2 my ($blanks, $rest) = substr($text, $pos) =~ m{\A (\s*) (.*) }mxs;
25 2 ▶ if (length $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest) 0 2 2
26 0 ▶ $pos += length($blanks // '');
Condition0%0/2
$blanks // ""
l!l
00
MC/DC0%0/1
$blanks // ""
$blanks
Truth table0%0/2
A
A= $blanks
Aresult
11
00
27 0 my $prest = $rest;
28 0 ▶ $prest = length($rest) > SHOW_CHARS
Branch: 1 branch
BranchTrueFalseTotal
length $rest > 20 ? : 0 0 0
29 ? (substr($rest, 0, SHOW_CHARS - length ELLIPSIS) . ELLIPSIS)
30 : $rest;
31 0 ouch 400, "unknown sequence starting at $pos '$prest'", $rest;
32 }
33 2 return $ast;
34 }
35  
36 ########################################################################
37 # Generic parsing facilities
38  
39 ▶sub __alternator (@alternatives) {
Subroutine __alternator: called CC=1 SCAR=0.0
40 ▶ return sub ($rtext) {
Subroutine __ANON__: called CC=3 SCAR=11.0
41 22 __ews($rtext);
42 22 for my $alt (@alternatives) {
43 46 ▶ next unless defined(my $retval = $alt->($rtext));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $retval = &$alt($rtext)) 34 12 46
44 12 return $retval;
45 }
46 10 return;
47 6 };
48 }
49  
50 78 ▶sub __ews ($rtext) { return __ewsr()->($rtext) }
Subroutine __ews: called CC=1 SCAR=0.0
51 78 ▶sub __ewsr { state $retval = __regexper(qr{\s+}) }
Subroutine __ewsr: called CC=1 SCAR=0.0
52  
53 ▶sub __exact ($what, @retval) {
Subroutine __exact: called CC=1 SCAR=0.0
54 7 my $wlen = length $what;
55 ▶ return sub ($rtext) {
Subroutine __ANON__: called CC=4 SCAR=13.9
56 30 ▶ my $pos = pos($$rtext) // 0;
Condition100%2/2
pos $$rtext // 0
l!l
282
MC/DC100%1/1
pos $$rtext // 0
pos $$rtext
Truth table100%2/2
A
A= pos $$rtext
Aresult
11
00
57 30 ▶ return if length($$rtext) - $pos < $wlen;
Branch: 1 branch
BranchTrueFalseTotal
if length($$rtext) - $pos < $wlen 8 22 30
58 22 ▶ return if substr($$rtext, $pos, $wlen) ne $what;
Branch: 1 branch
BranchTrueFalseTotal
if substr($$rtext, $pos, $wlen) ne $what 14 8 22
59 8 pos($$rtext) = $pos + $wlen;
60 8 return [@retval];
61 7 };
62 }
63  
64 1 ▶sub __lister ($what, $sep = undef) {
Subroutine __lister: called CC=4 SCAR=15.2
65 1 ▶ $sep = __exact($sep) if defined($sep) && ! ref($sep);
Branch: 1 branch
BranchTrueFalseTotal
if defined $sep and not ref $sep 1 0 1
Condition33%1/3
defined $sep and not ref $sep
!ll&&!rl&&r
001
MC/DC0%0/2
defined $sep and not ref $sep
defined $sep not ref $sep
Truth table33%1/3
A and not(B)
A= defined $sepB= not ref $sep
ABresult
0X0
100
111
66 ▶ return sub ($rtext) {
Subroutine __ANON__: called CC=3 SCAR=11.9
67 2 __ews($rtext);
68 2 ▶ defined(my $base = $what->($rtext)) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $base = &$what($rtext)) or return 0 2 2
69 ▶ my $rest = __starer(
Subroutine __ANON__: called CC=3 SCAR=13.3
70 sub ($rtext) {
71 2 ▶ if ($sep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sep) 2 0 2
72 2 __ews($rtext);
73 2 ▶ $sep->($rtext) or return; # check & discard
Branch: 1 branch
BranchTrueFalseTotal
&$sep($rtext) or return 2 0 2
74 }
75 0 __ews($rtext);
76 0 $what->($rtext);
77 }
78 2 )->($rtext);
79 2 ▶ $sep->($rtext) if $sep; # optional ending
Branch: 1 branch
BranchTrueFalseTotal
if $sep 2 0 2
80 2 unshift $rest->@*, $base;
81 2 return $rest;
82 1 };
83 }
84  
85 ▶sub __regexper ($rx) {
Subroutine __regexper: called CC=1 SCAR=0.0
86 ▶ return sub ($rtext) {
Subroutine __ANON__: called CC=2 SCAR=6.9
87 96 ▶ my (undef, $retval) = $$rtext =~ m{\G()$rx}cgmxs or return;
Branch: 1 branch
BranchTrueFalseTotal
my(undef, $retval) = $$rtext =~ /\G()$rx/cgmsux or return 92 4 96
88 4 return [$retval];
89 7 };
90 }
91  
92 sub __resolver { # probably unneeded
93 ▶ state $retval = sub ($what) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
94 0 ▶ return $what if ref $what;
Branch: 1 branch
BranchTrueFalseTotal
if ref $what 0 0 0
95 0 return __PACKAGE__->can($what);
96 0 ▶ };
Subroutine __resolver: not called CC=1 SCAR=6.9
97 }
98  
99 ▶sub __sequencer (@items) {
Subroutine __sequencer: called CC=1 SCAR=0.0
100 ▶ return sub ($rtext) {
Subroutine __ANON__: called CC=4 SCAR=13.9
101 26 my $pos = pos $$rtext;
102 26 my @retval;
103 26 for my $item (@items) {
104 38 my $ews = __ews($rtext);
105 38 ▶ $item = __exact($item) unless ref $item;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item 4 34 38
106 38 ▶ if (defined(my $piece = $item->($rtext))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $piece = &$item($rtext))) { } 20 18 38
107 20 push @retval, $piece;
108 }
109 else { # fail
110 18 pos($$rtext) = $pos;
111 18 return;
112 }
113 }
114 8 return \@retval;
115 9 };
116 }
117  
118 6 ▶sub __starer ($what, $min = 0) {
Subroutine __starer: called CC=2 SCAR=7.0
119 ▶ return sub ($rtext) {
Subroutine __ANON__: called CC=4 SCAR=14.0
120 10 my $pos = pos $$rtext;
121 10 my @retval;
122 10 my $local_min = $min;
123 10 while ('possible') {
124 14 __ews($rtext);
125 14 ▶ defined(my $piece = $what->($rtext)) or last;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $piece = &$what($rtext)) or last 10 4 14
126 4 push @retval, $piece;
127 4 ▶ if ($local_min > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_min > 0) { } 0 4 4
128 0 --$local_min;
129 }
130 else {
131 4 $pos = pos $$rtext;
132 }
133 }
134 10 pos($$rtext) = $pos; # "undo" last try/tries
135 10 ▶ return if $local_min > 0; # failed to match at least $min
Branch: 1 branch
BranchTrueFalseTotal
if $local_min > 0 0 10 10
136 10 return \@retval;
137 6 };
138 }
139  
140 ########################################################################
141 # Specific grammar
142  
143 sub _addend {
144 ▶ state $r = sub ($rtext) {
Subroutine __ANON__: called CC=4 SCAR=13.9
145 2 state $op = __regexper(qr{([*x])});
146 2 state $seq = __sequencer(
147 __starer(__sequencer(_positive_int(), $op)),
148 _atom(),
149 __starer(__sequencer($op, _positive_int())),
150 );
151 2 ▶ my $match = $seq->($rtext) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $match = &$seq($rtext) or return 0 2 2
152 2 my ($pre, $retval, $post) = $match->@*;
153 2 $retval = ___mult($retval, reverse($_->@*)) for reverse($pre->@*);
154 2 $retval = ___mult($retval, ($_->@*)) for ($post->@*);
155 2 return $retval;
156 }
157 1 ▶}
Subroutine _addend: called CC=1 SCAR=0.0
158  
159 sub _atom {
160 1 ▶ state $base = _atom_base();
Subroutine _atom: called CC=1 SCAR=0.0
161 1 state $unaries = __starer(_atom_unary());
162 ▶ state $retval = sub ($rtext) {
Subroutine __ANON__: called CC=3 SCAR=11.1
163 2 ▶ my $retval = $base->($rtext) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $retval = &$base($rtext) or return 0 2 2
164 2 for my $unary ($unaries->($rtext)->@*) {
165 4 my ($op, @rest) = $unary->@*;
166 4 $retval = [$op, $retval, @rest];
167 }
168 2 return $retval;
169 1 };
170 }
171  
172 sub _atom_base {
173 ▶ state $sub_expression = sub ($rtext) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
174 0 state $seq = __sequencer('(', _expression(), ')');
175 0 ▶ my $match = $seq->($rtext) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $match = &$seq($rtext) or return 0 0 0
176 0 return $match->[1];
177 1 ▶ };
Subroutine _atom_base: called CC=1 SCAR=0.0
178 1 state $retval = __alternator(
179 _identifier(),
180 $sub_expression,
181 );
182 }
183  
184 sub _atom_unary {
185 1 ▶ state $r = __alternator(_sslicer(), _slicer(), _sorter(), _shuffler());
Subroutine _atom_unary: called CC=1 SCAR=0.0
186 }
187  
188 sub _expression {
189 ▶ state $r = sub ($rtext) {
Subroutine __ANON__: called CC=3 SCAR=11.6
190 2 state $addend = _addend();
191 2 state $seq = __sequencer(
192 $addend,
193 __starer(__sequencer(__regexper(qr{([-+])}), $addend)),
194 );
195 2 state $name_for = {'+' => 'sum', '-' => 'subtract'};
196 2 ▶ my $match = $seq->($rtext) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $match = &$seq($rtext) or return 0 2 2
197 2 my ($retval, $transformations) = $match->@*;
198 2 for my $t ($transformations->@*) {
199 0 my ($op, $addend) = $t->@*;
200 0 $retval = [$name_for->{"$op->@*"}, $retval, $addend];
201 }
202 2 return $retval;
203 1 ▶ };
Subroutine _expression: called CC=1 SCAR=0.0
204 }
205  
206 sub _identifier {
207 ▶ state $retval = sub ($rtext) {
Subroutine __ANON__: called CC=2 SCAR=7.1
208 2 state $alts = __alternator(_token(), _quoted_string());
209 2 ▶ my $rv = $alts->($rtext) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rv = &$alts($rtext) or return 0 2 2
210 2 return [resolve => $rv->[0]];
211 1 ▶ };
Subroutine _identifier: called CC=1 SCAR=0.0
212 }
213  
214 3 ▶sub _int { state $r = __alternator(_simple_int(), _random_int()) }
Subroutine _int: called CC=1 SCAR=0.0
215  
216 1 ▶sub _int_item { state $r = __alternator(_int_sr(), _int_range(), _int()) }
Subroutine _int_item: called CC=1 SCAR=0.0
217  
218 2 ▶sub _int_item_list { state $r = __lister(_int_item(), ',') }
Subroutine _int_item_list: called CC=1 SCAR=0.0
219  
220 1 ▶sub _int_range { state $r = _ranger((_int()) x 2) }
Subroutine _int_range: called CC=1 SCAR=0.0
221  
222 sub _int_sr {
223 ▶ state $r = sub ($rtext) {
Subroutine __ANON__: called CC=2 SCAR=7.0
224 2 state $seq = __sequencer('#', _positive_int());
225 2 ▶ my $list = $seq->($rtext) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $list = &$seq($rtext) or return 0 2 2
226 2 my ($n) = ___promote_simple_ints($list->[1]);
227 2 return [range => 0 => [math_subtract => $n => 1]];
228 }
229 1 ▶}
Subroutine _int_sr: called CC=1 SCAR=0.0
230  
231 sub _positive_int {
232 3 ▶ state $r = __alternator(_positive_simple_int(), _positive_random_int());
Subroutine _positive_int: called CC=1 SCAR=0.0
233  
234 }
235  
236 sub _positive_random_int {
237 ▶ state $r = sub ($rtext) {
Subroutine __ANON__: called CC=4 SCAR=25.5
238 2 state $ri = _random_int();
239 2 my $pos = pos $$rtext;
240 2 ▶ my $r = $ri->($rtext) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $r = &$ri($rtext) or return 2 0 2
241  
242 0 state $is_positive;
243 ▶ $is_positive ||= sub ($x) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
244 0 ▶ return $x > 0 unless ref $x;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x 0 0 0
245 0 for my $i (1 .. $#$x) { # skip 1st item in array
246 0 ▶ return unless $is_positive->($x->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless &$is_positive($x->[$i]) 0 0 0
247 }
248 0 return 1; # all checks were good
249 0 ▶ };
Condition0%0/3
$is_positive ||= sub ($x) {
l!l&&r!l&&!r
000
MC/DC0%0/2
$is_positive ||= sub ($x) { return $x > 0 unless ref $x; foreach my $i (1 .. $x->$#*) { return unless &$is_positive($x->[$i]); } ; return 1; }
$is_positive sub ($x) { return $x > 0 unless ref $x; foreach my $i (1 .. $x->$#*) { return unless &$is_positive($x->[$i]); } ; return 1; }
Truth table0%0/3
A ||= B
A= $is_positiveB= sub ($x) { return $x > 0 unless ref $x; foreach my $i (1 .. $x->$#*) { return unless &$is_positive($x->[$i]); } ; return 1; }
ABresult
1X1
011
000
250 0 ▶ return $r if $is_positive->($r);
Branch: 1 branch
BranchTrueFalseTotal
if &$is_positive($r) 0 0 0
251  
252 0 return;
253 1 ▶ };
Subroutine _positive_random_int: called CC=1 SCAR=0.0
254 }
255  
256 1 ▶sub _positive_simple_int { state $r = __regexper(qr{([1-9][0-9]*)}) }
Subroutine _positive_simple_int: called CC=1 SCAR=0.0
257  
258 1 ▶sub _quoted_string { state $r = __regexper(qr{"((?:[^\\"]|\\.)*)"}) }
Subroutine _quoted_string: called CC=1 SCAR=0.0
259  
260 sub _random_int {
261 ▶ state $r = sub ($rtext) {
Subroutine __ANON__: called CC=2 SCAR=8.1
262 8 state $seq = __sequencer('{', _int_item_list(), '}');
263 8 ▶ my $list = $seq->($rtext) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $list = &$seq($rtext) or return 8 0 8
264 0 return [random => ___promote_simple_ints($list->[1]->@*)];
265 2 ▶ };
Subroutine _random_int: called CC=1 SCAR=0.0
266 }
267  
268 ▶sub _ranger ($t1, $t2) {
Subroutine _ranger: called CC=1 SCAR=2.6
269 1 my $ranger = __sequencer($t1, '..', $t2);
270 ▶ return sub ($rtext) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
271 0 ▶ my $range = $ranger->($rtext) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $range = &$ranger($rtext) or return 0 0 0
272 0 return [range => ___promote_simple_ints($range->@[0, 2])];
273 1 };
274 }
275  
276 1 ▶sub _shuffler { state $r = __exact('@', 'shuffle') }
Subroutine _shuffler: called CC=1 SCAR=0.0
277  
278 1 ▶sub _simple_int { state $r = __regexper(qr{(0|-?[1-9][0-9]*)}) }
Subroutine _simple_int: called CC=1 SCAR=0.0
279  
280 sub _slicer {
281 ▶ state $r = sub ($rtext) {
Subroutine __ANON__: called CC=2 SCAR=6.9
282 6 state $slicer = __sequencer('[', _int_item_list(), ']');
283 6 ▶ my $slice = $slicer->($rtext) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $slice = &$slicer($rtext) or return 4 2 6
284 2 return [slice => ___promote_simple_ints($slice->[1]->@*)];
285 1 ▶ };
Subroutine _slicer: called CC=1 SCAR=0.0
286 }
287  
288 1 ▶sub _sorter { state $r = __exact('!', 'sort') }
Subroutine _sorter: called CC=1 SCAR=0.0
289  
290 sub _sslicer {
291 ▶ state $r = sub ($rtext) {
Subroutine __ANON__: called CC=2 SCAR=9.2
292 6 state $catcher = _int();
293 6 ▶ my $catched = $catcher->($rtext) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $catched = &$catcher($rtext) or return 6 0 6
294 0 my ($n) = ___promote_simple_ints($catched);
295 0 return [slice => [range => 0 => [math_subtract => $n => 1]]];
296 1 ▶ };
Subroutine _sslicer: called CC=1 SCAR=0.0
297 }
298  
299 1 ▶sub _token { state $r = __regexper(qr{([a-zA-Z]\w*)}) }
Subroutine _token: called CC=1 SCAR=0.0
300  
301  
302  
303 ########################################################################
304 # Convenience functions
305 ▶sub ___mult ($atom, $op, $n) {
Subroutine ___mult: not called CC=1 SCAR=6.9
306 0 state $name_for = {'*' => 'repeat', 'x' => 'replicate'};
307 0 return [$name_for->{"$op->@*"}, $atom, ___promote_simple_ints($n)];
308 }
309  
310 ▶sub ___promote_simple_ints (@list) {
Subroutine ___promote_simple_ints: called CC=2 SCAR=6.9
311 4 ▶ map {($_->@* <= 1) ? ($_->@*) : $_} @list;
Branch: 1 branch
BranchTrueFalseTotal
@$_ <= 1 ? : 2 2 4
312 }
313  
314 0 ▶sub ___log ($rtext, $prefix = '') {
Subroutine ___log: not called CC=3 SCAR=24.8
315 0 ▶ my $pos = pos($$rtext) // 0;
Condition0%0/2
pos $$rtext // 0
l!l
00
MC/DC0%0/1
pos $$rtext // 0
pos $$rtext
Truth table0%0/2
A
A= pos $$rtext
Aresult
11
00
316 0 my $rest = substr $$rtext, $pos;
317 }
318  
319 5 1;
↑ Summary blib/lib/Ordeal/Model/Evaluator.pm »