blib/lib/Parser/Combinators.pm

filter: Statementstmt 61.5% 285 / 463 Branchbran 48.7% 118 / 242 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 62.5% 40 / 64 Podpod 0.0% 0 / 35 totaltotal 55.1% 446 / 809 CC 129 SCAR 28.1
CC
129
Cov
57%
__ANON__
90.0
__ANON__
56.0
__ANON__
56.0
CRAP 16.6
↑ Summary
1 package Parser::Combinators;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = '0.05';
6  
7 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 @Parser::Combinators::EXPORT = qw(
10 sequence
11 choice
12 try
13 maybe
14 regex
15 parens
16 char
17 sepBy
18 oneOf
19 word
20 natural
21 symbol
22 greedyUpto
23 upto
24 many
25 many1
26 whiteSpace
27 comma
28 matches
29 unwrap
30 empty
31 getParseTree
32 bindP
33 returnP
34 $V
35 );
36  
37 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 our $V= 0;
39  
40 # Forward declarations with prototypes
41  
42 #sub symbol ($);
43 #sub char($);
44 #sub maybe($);
45 #sub sepBy($$);
46 #sub oneOf($);
47  
48  
49 # I want to write the parser using lists, because sequencing is the most common operation.
50 # So I need a function to generate the actual parser from the lists
51 # which is actually a sequence of parsers
52 # The first arg is a list ref, the second arg is an optional code ref to process the returned list of matches
53 0 ▶sub sequence_ORIG {
Subroutine sequence_ORIG: not called CC=1 SCAR=6.9
Pod undocumented
54 0 (my $plst, my $proc)=@_;
55 0 ▶ my $gen = sub { (my $str)=@_;
Subroutine __ANON__: not called CC=9 SCAR=45.0
56 0 ▶ say "* sequence( '$str' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
57 0 my $matches=[];
58 0 my $st=1;
59 0 my $str2='';
60 0 my $ms=undef; # Why not []?
61 0 for my $p (@{$plst}) {
62 0 ▶ if (ref($p) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $p eq 'CODE') { } 0 0 0
elsif (ref $p eq 'HASH') { } 0 0 0
63 0 ($st, $str, $ms)=$p->($str);
64 } elsif (ref($p) eq 'HASH') {
65 0 my %hp=%{$p};
66 0 (my $k, my $pp) = each %hp;
67 0 ($st, $str, my $mms)=$pp->($str);
68 0 $ms = {$k => $mms};
69 } else { # assuming it's ARRAY
70 0 my $p2 = sequence($p);
71 0 ($st, $str, $ms)=$p2->($str)
72 }
73 0 ▶ if (!$st) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($st) 0 0 0
74 0 return (0,$str,[]);
75 }
76 0 push @{$matches},$ms;# if scalar @{ $ms };
77 }
78 0 ▶ if (defined($proc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proc) { } 0 0 0
79 0 ▶ if( ref($proc) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proc eq 'CODE') { } 0 0 0
80 0 return (1,$str,$proc->($matches));
81 } else {
82 0 ▶ say 'TROUBLE: <',show($plst),'><',show($proc),'>' if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
83 0 return (1,$str,$matches);
84 }
85 } else {
86 0 return (1,$str,$matches)
87 }
88 0 };
89 0 return $gen;
90 }
91  
92 19 ▶sub foldl {
Subroutine foldl: called CC=2 SCAR=6.9
Pod undocumented
93 19 (my $f, my $acc, my $ls)=@_;
94 19 for my $elt (@{$ls}) {
95 51 $acc = $f->($acc,$elt);
96 }
97 19 return $acc;
98 }
99  
100 11 ▶sub sequence {
Subroutine sequence: called CC=1 SCAR=0.2
Pod undocumented
101 11 (my $plst, my $proc)=@_;
102 19 ▶ my $gen = sub { (my $str)=@_;
Subroutine __ANON__: called CC=6 SCAR=19.6
103 19 ▶ say "* sequence( '$str' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 19 0 19
104 51 ▶ my $f = sub { (my $acc, my $p) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
105 51 (my $st1, my $str1, my $matches) = @{ $acc };
106 51 (my $st2, my $str2, my $ms) = apply($p,$str1);
107 51 ▶ if ($st2*$st1==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st2 * $st1 == 0) { } 8 43 51
108 8 return [0,$str1,[]];
109 } else {
110 43 return [1,$str2,[ @{$matches},$ms]];
111 }
112 19 };
113 19 (my $status, $str, my $matches) = @{ foldl($f, [1,$str,[]],$plst) };
114 19 ▶ if ($status == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status == 0) { } 4 15 19
elsif (defined $proc) { } 0 15 15
115 4 return (0,$str,[]);
116 } elsif (defined($proc)) {
117 0 ▶ if( ref($proc) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proc eq 'CODE') { } 0 0 0
118 0 return (1,$str,$proc->($matches));
119 } else {
120 0 ▶ say 'TROUBLE: <',show($plst),'><',show($proc),'>' if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
121 0 return (1,$str,$matches);
122 }
123 } else {
124 15 return (1,$str,$matches)
125 }
126 11 };
127 11 return $gen;
128 }
129  
130 0 ▶sub sequence_noproc {
Subroutine sequence_noproc: not called CC=1 SCAR=6.9
Pod undocumented
131 0 (my $plst )=@_;
132 0 ▶ my $gen = sub { (my $str)=@_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
133 0 ▶ say "* sequence( '$str' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
134 0 ▶ my $f = sub { (my $acc, my $p) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
135 0 (my $st1, my $str1, my $matches) = @{ $acc };
136 0 (my $st2, my $str2, my $ms) = do {
137 0 ▶ if (ref($p) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $p eq 'CODE') { } 0 0 0
elsif (ref $p eq 'HASH') { } 0 0 0
138 0 $p->($str1);
139 } elsif (ref($p) eq 'HASH') {
140 0 my %hp=%{$p};
141 0 (my $k, my $pp) = each %hp;
142 0 (my $st, my $str, my $mms)=$pp->($str1);
143 0 my $ms = {$k => $mms};
144 0 ($st, $str, $ms)
145 } else { # assuming it's ARRAY
146 0 my $p2 = sequence($p);
147 0 $p2->($str1)
148 }
149 };
150 0 ▶ if ($st2*$st1==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st2 * $st1 == 0) { } 0 0 0
151 0 return [0,$str1,[]];
152 } else {
153 0 return [1,$str2,[ @{$matches},$ms]];
154 }
155 0 };
156 0 (my $status, $str, my $matches) = @{ foldl($f, [1,$str,[]],$plst) };
157 0 ▶ if ($status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 0) { } 0 0 0
158 0 return (0,$str,[]);
159 } else {
160 0 return (1,$str,$matches)
161 }
162 0 };
163 0 return $gen;
164 }
165 # In the best tradition, bind() and return()
166 0 ▶sub bindP {
Subroutine bindP: not called CC=1 SCAR=6.9
Pod undocumented
167 0 (my $p1,my $p2) =@_;
168 0 ▶ my $gen = sub {(my $str1) =@_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
169 0 ▶ say "* bindP( \'$str1\' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
170 0 my $matches=undef;
171 0 (my $st1,my $str2,my $m1) = $p1->( $str1 );
172 0 push @{$matches},$m1;
173 0 ▶ if ($st1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st1) { } 0 0 0
174 0 say "bind: p1( $str1 ) matched,[$m1] try p2( $str2 )";
175 0 (my $st2,my $str3, my $m2) = $p2->( $str2 );
176 0 ▶ if(ref($m2) eq 'ARRAY' ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $m2 eq 'ARRAY') { } 0 0 0
elsif (defined $m2) { } 0 0 0
177 0 $matches =[ @{$matches},@{$m2} ];
178 } elsif (defined $m2) {
179 0 push @{$matches},$m2;
180 }
181 0 return ($st2,$str3,$matches);
182 } else {
183 0 return (0,$str1,undef);
184 }
185 0 };
186 0 return $gen;
187 }
188  
189 # Only we can't call it 'return' so let's call it enter :-)
190 0 ▶sub returnP {
Subroutine returnP: not called CC=1 SCAR=6.9
Pod undocumented
191 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
192 0 (my $str) = @_;
193 0 return (0,$str,undef);
194 }
195 0 }
196  
197 # Choice: try every parser in the list until one succeeds or return fail. '<|>' in Parsec
198 # FIXME: Prototype does not guarantee that parens can be omitted. Should make it binary for that.
199 1 ▶sub choice ($$;@) {
Subroutine choice: called CC=1 SCAR=0.1
Pod undocumented
200 1 my @parsers=@_;
201 2 ▶ my $gen = sub { (my $str)= @_;
Subroutine __ANON__: called CC=6 SCAR=18.6
202 2 ▶ say "* choice('$str')" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 2 0 2
203 2 for my $p (@parsers) {
204 3 my $status=0;
205 3 ($status, $str, my $matches) = apply($p,$str);
206 3 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 2 1 3
207 2 ▶ say "choice: remainder => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 2 0 2
208 2 ▶ say "choice: matches => [".show($matches)."]" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 2 0 2
209 2 return ($status, $str, $matches);
210 }
211 }
212 0 return (0, $str, []);
213 1 };
214 1 return $gen;
215 }
216 # Normally, when a parser parses a string, it removes the portion that matched. If you want to keep the string, wrap the parser in try()
217 0 ▶sub try {
Subroutine try: not called CC=1 SCAR=6.9
Pod undocumented
218 0 (my $p)=@_;
219 0 ▶ my $gen = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
220 0 (my $str)=@_;
221 0 ▶ say "* try( '$str' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
222 0 (my $status, my $rest, my $matches)=$p->($str);
223 0 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 0 0 0
224 0 ▶ say "try: remainder => <$rest>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
225 0 ▶ say "try: matches => [".show($matches)."]" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
226 0 return (1, $rest, $matches);
227 } else {
228 0 ▶ say "try: match failed => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
229 0 return (0, $str, $matches);
230 }
231 0 };
232 0 return $gen;
233 }
234  
235 # maybe() is like try() but always succeeds
236 # it returns the matches and the consumed string or the orig string and no matches
237 3 ▶sub maybe { (my $p)=@_;
Subroutine maybe: called CC=1 SCAR=0.0
Pod undocumented
238 9 ▶ my $gen = sub { (my $str)=@_;
Subroutine __ANON__: called CC=5 SCAR=16.4
239 9 ▶ say "* maybe('$str')" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 9 0 9
240 9 (my $status, my $rest, my $matches)=apply($p,$str);
241 9 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 6 3 9
242 6 ▶ say "maybe matches: [".show($matches)."]" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 6 0 6
243 6 return (1, $rest, $matches);
244 } else {
245 3 ▶ say "maybe: no matches for <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 3 0 3
246 3 return (1, $str, undef);
247 }
248 3 };
249 3 return $gen;
250 }
251  
252  
253 3 ▶sub parens { (my $p)= @_;
Subroutine parens: called CC=1 SCAR=0.1
Pod undocumented
254 9 ▶ my $gen = sub { (my $str)=@_;
Subroutine __ANON__: called CC=9 SCAR=23.0
255 9 ▶ say "* parens($str)" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 9 0 9
256 9 my $matches=undef;
257 9 (my $status, my $str3, my $ch)=char('(')->($str);
258 9 ▶ if ($status==1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 1) { } 6 3 9
259 6 my $str4 = $str3; $str4=~s/\s*//;
260 6 (my $st,my $str4s,$matches)=$p->($str4);
261 6 ▶ say "parens: remainder => <$str4s>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 6 0 6
262 6 ▶ say "parens: matches => [".show($matches)."]" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 6 0 6
263 6 $status*=$st;
264 6 ▶ if ($status==1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 1) { } 6 0 6
265 6 (my $st, my $str5, my $ch)=char(')')->($str4s);
266 # if ($st==1) {
267 # $str=~s/\s*//;
268 # }
269 6 $status*=$st;
270 6 ▶ if ($status==1) { # OK!
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 1) { } 6 0 6
271 6 my $str6 = $str5; $str6=~s/^\s*//;
272 6 ▶ say "parens: remainder => <$str5>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 6 0 6
273 6 ▶ say "parens: matches => ".show($matches)."" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 6 0 6
274 6 return (1,$str6, $matches);
275 } else { # parse failed on closing paren
276 0 return (0,$str5, $matches);
277 }
278 } else { # parse failed on $ref
279 0 return (0,$str4, $matches);
280 }
281 } else { # parse failed on opening paren
282 3 return (0,$str3,undef);
283 }
284 3 };
285 3 return $gen;
286 }
287  
288 29 ▶sub char { (my $ch)=@_;
Subroutine char: called CC=1 SCAR=0.0
Pod undocumented
289 29 ▶ my $gen = sub { (my $str)=@_;
Subroutine __ANON__: called CC=5 SCAR=16.1
290 29 ▶ say "* char('$ch', '$str')" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 27 2 29
291 29 ▶ if (substr($str,0,1) eq $ch) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($str, 0, 1) eq $ch) { } 19 10 29
292 19 ▶ say "char: matched \'$ch\' " if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 18 1 19
293 19 ▶ say "char: remainder <".substr($str,1).">" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 18 1 19
294 19 return (1,substr($str,1),$ch);
295 } else {
296 10 return (0,$str,undef);
297 }
298 29 };
299 29 return $gen;
300 }
301  
302 2 ▶sub sepBy ($$) { (my $sep, my $p)=@_;
Subroutine sepBy: called CC=1 SCAR=0.0
Pod undocumented
303 6 ▶ my $gen = sub { (my $str)=@_;
Subroutine __ANON__: called CC=6 SCAR=18.2
304 6 my $matches=[];
305 6 ▶ say "* sepBy('$sep', '$str')" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 6 0 6
306 6 (my $status,my $str1,my $m)=$p->($str);
307 6 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 6 0 6
308 6 push @{$matches},$m;
309 6 ▶ say "sepBy: remainder => <$str1>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 6 0 6
310 6 ($status,my $str2,$m)=char($sep)->($str1);
311 6 ▶ while( $status ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 6 6 12
312 6 my $str2s=$str2;$str2s=~s/^\s*//;
313 6 (my $st,my $str3,$m)=$p->($str2s);
314 6 push @{$matches},$m;
315 6 ($status,$str2,$m)=char($sep)->($str3);
316 }
317 6 ▶ say "sepBy matches => [".show($matches)."]" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 6 0 6
318 6 return (1, $str2, $matches);
319 } else { # first match failed.
320 0 return (0,$str1,undef);
321 }
322 2 };
323 2 return $gen;
324 }
325 # This is a lexeme parser, so it skips trailing whitespace
326 # Should be called "identifier" I think
327 6 ▶sub word {
Subroutine word: called CC=1 SCAR=0.0
Pod undocumented
328 13 ▶ my $gen = sub { (my $str)=@_;
Subroutine __ANON__: called CC=6 SCAR=17.9
329 13 ▶ say "* word( '$str' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 10 3 13
330 13 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(\w+)/) { } 12 1 13
331 $str=~/^(\w+)/
332 ) {
333 12 my $m=$1;
334 12 my $matches=$m;
335 12 $str=~s/^$m\s*//;
336 12 ▶ say "word: remainder => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 10 2 12
337 12 ▶ say "word: matches => [$matches]" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 10 2 12
338 12 return (1,$str, $matches);
339 } else {
340 1 ▶ say "word: match failed => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 1 1
341 1 return (0,$str, undef); # assumes $status is 0|1, $str is string, $matches is [string]
342 }
343 6 };
344 6 return $gen;
345 }
346 0 ▶sub identifier {
Subroutine identifier: not called CC=1 SCAR=6.9
Pod undocumented
347 0 word();
348 }
349  
350 # matches an unsigned integer
351 5 ▶sub natural {
Subroutine natural: called CC=1 SCAR=0.0
Pod undocumented
352 6 ▶ my $gen = sub { (my $str)=@_;
Subroutine __ANON__: called CC=6 SCAR=17.9
353 6 ▶ say "* natural( '$str' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 3 3 6
354 6 my $status=0;
355 6 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(\d+)/) { } 4 2 6
356 $str=~/^(\d+)/
357 ) {
358 4 my $m=$1;
359 4 my $matches=$m;
360 4 $status=1;
361 4 $str=~s/^$m\s*//;
362 4 ▶ say "natural: remainder => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 2 2 4
363 4 ▶ say "natural: matches => [$matches]" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 2 2 4
364 4 return ($status,$str, $matches);
365 } else {
366 2 ▶ say "natural: match failed => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 1 1 2
367 2 return ($status,$str, undef); # assumes $status is 0|1, $str is string, $matches is [string]
368 }
369 5 };
370 5 return $gen;
371 }
372  
373 # As in Parsec, parses a literal and removes trailing whitespace
374 11 ▶sub symbol ($) { (my $lit_str) = @_;
Subroutine symbol: called CC=1 SCAR=0.0
Pod undocumented
375 11 $lit_str=~s/(\W)/\\$1/g;
376 17 ▶ my $gen = sub { (my $str)=@_;
Subroutine __ANON__: called CC=6 SCAR=17.9
377 17 ▶ say "* symbol( '$lit_str', '$str' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 11 6 17
378 17 my $status=0;
379 17 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\s*$lit_str\s*/) { } 14 3 17
380 $str=~/^\s*$lit_str\s*/
381 ) {
382 14 my $m=$1;
383 14 my $matches=$lit_str;
384 14 $status=1;
385 14 $str=~s/^\s*$lit_str\s*//;
386 14 ▶ say "symbol: remainder => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 9 5 14
387 14 ▶ say "symbol: matches => [$matches]" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 9 5 14
388 14 return ($status,$str, $matches);
389 } else {
390 3 ▶ say "symbol: match failed => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 2 1 3
391 3 return ($status,$str, undef);
392 }
393 11 };
394 11 return $gen;
395 }
396  
397 # This parser parses anything up to the last occurence ofa given literal and trailing whitespace
398 1 ▶sub greedyUpto {
Subroutine greedyUpto: called CC=1 SCAR=0.3
Pod undocumented
399 1 (my $lit_str) = @_;
400 1 $lit_str=~s/(\W)/\\$1/g;
401 1 ▶ my $gen = sub {
Subroutine __ANON__: called CC=7 SCAR=22.6
402 1 (my $str)=@_;
403 1 ▶ say "* greedyUpto( \'$lit_str\', \'$str\' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 1 1
404 1 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(.*)\s*$lit_str\s*/) { } 1 0 1
405 $str=~/^(.*)\s*$lit_str\s*/
406 ) {
407 1 my $m=$1;
408 1 $m=~s/\s*$//;
409 1 ▶ my $matches= $m eq '' ? undef : $m;
Branch: 1 branch
BranchTrueFalseTotal
$m eq '' ? : 0 1 1
410 1 $str=~s/^.*$lit_str\s*//;
411 1 ▶ say "greedyUpto: remainder => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 1 1
412 1 ▶ say "greedyUpto: matches => [$matches]" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 1 1
413 1 return (1,$str, $matches);
414 } else {
415 0 ▶ say "greedyUpto: match failed => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
416 0 return (0,$str, undef);
417 }
418 1 };
419 1 return $gen;
420 }
421  
422 # This parser parses anything up to the last occurence of a given literal and trailing whitespace
423 1 ▶sub upto {
Subroutine upto: called CC=1 SCAR=0.4
Pod undocumented
424 1 (my $lit_str )= @_;
425 1 $lit_str=~s/(\W)/\\$1/g;
426 1 ▶ my $gen = sub {
Subroutine __ANON__: called CC=7 SCAR=23.0
427 1 (my $str)=@_;
428 1 ▶ say "upto1 \'$lit_str\': <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 1 1
429 1 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(.*?)\s*$lit_str\s*/) { } 1 0 1
430 $str=~/^(.*?)\s*$lit_str\s*/
431 ) {
432 1 my $m=$1;
433 1 ▶ my $matches= $m eq '' ? undef : $m;
Branch: 1 branch
BranchTrueFalseTotal
$m eq '' ? : 0 1 1
434 1 $str=~s/^.*?$lit_str\s*//;
435 1 ▶ say "upto: remainder => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 1 1
436 1 ▶ say "upto: matches => [$matches]" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 1 1
437 1 return (1,$str, $matches);
438 } else {
439 0 ▶ say "upto: match failed => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
440 0 return (0,$str, undef);
441 }
442 1 };
443 1 return $gen;
444 }
445  
446  
447 # `many`, as in Parsec, parses 0 or more the specified parsers
448 0 ▶sub many {
Subroutine many: not called CC=1 SCAR=6.9
Pod undocumented
449 0 (my $parser) = @_;
450 0 ▶ my $gen = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
451 0 (my $str)=@_;
452 0 ▶ print "* many( '$str' )\n" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
453 0 (my $status,$str,my $m)=$parser->($str);
454 0 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 0 0 0
455 0 my $matches = [$m];
456 0 ▶ while( $status==1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 1) 0 0 0
457 0 ($status,$str,$m)=$parser->($str);
458 0 push @{$matches},$m;
459 }
460 0 ▶ print "many: remainder => <$str>\n" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
461 0 ▶ print "many: matches => [".show($matches)."]\n" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
462 0 return (1, $str, $matches);
463 } else { # first match failed.
464 0 ▶ print "many: first match failed => <$str>\n" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
465 0 return (1, $str,undef);
466 }
467 0 };
468 0 return $gen;
469 }
470  
471  
472 # `many1`, as in Parsec, parses 1 or more the specified parsers
473 0 ▶sub many1 {
Subroutine many1: not called CC=1 SCAR=6.9
Pod undocumented
474 0 (my $parser) = @_;
475 0 ▶ my $gen = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
476 0 (my $str)=@_;
477 0 my $matches=[];
478 0 ▶ say "* many( '$str' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
479 0 (my $status,$str,my $m)=$parser->($str);
480 0 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 0 0 0
481 0 push @{$matches},$m;
482 0 ▶ while( $status==1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 1) 0 0 0
483 0 (my $status,$str,$m)=$parser->($str);
484 0 push @{$matches},$m;
485 }
486 0 ▶ say "many: remainder => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
487 0 ▶ say "many: matches => [".show($matches)."]" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
488 } else { # first match failed.
489 0 ▶ say "many: first match failed => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
490 0 return (0, $str,undef);
491 }
492 0 return (1, $str, $matches);
493 0 };
494 0 return $gen;
495 }
496  
497 3 ▶sub comma {
Subroutine comma: called CC=1 SCAR=0.0
Pod undocumented
498 7 ▶ my $gen = sub { (my $str) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
499 7 ▶ say "* comma( '$str' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 6 1 7
500 7 my $st = ($str=~s/^\s*,\s*//);
501 7 ▶ if ($st) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st) { } 5 2 7
502 5 ▶ say "comma: match" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 4 1 5
503 } else {
504 2 ▶ say "comma: match failed" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 2 0 2
505 }
506 7 return ($st, $str, undef);
507 3 };
508 3 return $gen;
509 }
510  
511 0 ▶sub semi {
Subroutine semi: not called CC=1 SCAR=6.9
Pod undocumented
512 0 ▶ my $gen = sub { (my $str) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
513 0 ▶ say "* semi( '$str' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
514 0 my $st = ($str=~s/^\s*;\s*//);
515 0 return ($st, $str, undef);
516 0 };
517 0 return $gen;
518 }
519  
520  
521  
522 # strip leading whitespace, always success
523 4 ▶sub whiteSpace {
Subroutine whiteSpace: called CC=1 SCAR=0.0
Pod undocumented
524 6 ▶ my $gen = sub { (my $str)=@_;
Subroutine __ANON__: called CC=2 SCAR=6.9
525 6 ▶ say "* whiteSpace( \'$str\' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 3 3 6
526 6 $str=~s/^(\s*)//;
527 6 my $m=$1;
528 6 return (1,$str,$m)
529 4 };
530 4 return $gen;
531 }
532  
533 0 ▶sub oneOf { (my $patt_lst) = @_;
Subroutine oneOf: not called CC=1 SCAR=6.9
Pod undocumented
534 0 ▶ my $gen = sub { (my $str)= @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
535 0 ▶ say "* oneOf([".join('|',@{$patt_lst})."],'$str')" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
536 0 for my $p (@{$patt_lst}) {
537 0 (my $status, $str, my $matches)= symbol($p)->($str);
538 0 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 0 0 0
539 0 ▶ say "choice: remainder => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
540 0 ▶ say "choice: matches => [".show($matches)."]" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
541 0 return (1, $str, $matches);
542 }
543 }
544 0 return (0, $str, undef);
545 0 };
546 0 return $gen;
547 }
548  
549 # Enough rope: this parser will parse whatever the regex is, stripping trailing whitespace
550 1 ▶sub regex { (my $regex_str) = @_;
Subroutine regex: called CC=1 SCAR=0.4
Pod undocumented
551 6 ▶ my $gen = sub { (my $str)=@_;
Subroutine __ANON__: called CC=6 SCAR=20.8
552 6 ▶ say "* regex( '/$regex_str/', '$str' )" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 6 0 6
553 6 my $matches=undef;
554 6 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/($regex_str)\s*//) { } 6 0 6
555 $str=~s/($regex_str)\s*//
556 ) {
557 6 my $m=$1;
558 6 $matches=$m;
559 6 ▶ say "regex: remainder => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 6 0 6
560 6 ▶ say "regex: matches => [$matches]" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 6 0 6
561 6 return (1,$str, $matches);
562 } else {
563 0 ▶ say "regex: match failed => <$str>" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $Parser::Combinators::V 0 0 0
564 }
565 0 return (0,$str, $matches); # assumes $status is 0|1, $str is string, $matches is [string]
566 1 };
567 1 return $gen;
568 }
569  
570 63 ▶sub apply { (my $p, my $str) = @_;
Subroutine apply: called CC=4 SCAR=13.9
Pod undocumented
571  
572 63 ▶ if (ref($p) eq 'CODE') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $p eq 'CODE') { } 39 24 63
elsif (ref $p eq 'ARRAY') { } 6 18 24
elsif (ref $p eq 'HASH') { } 18 0 18
573 39 return $p->($str);
574 } elsif (ref($p) eq 'ARRAY') {
575 6 return sequence($p)->($str);
576 } elsif (ref($p) eq 'HASH') {
577 18 my %hp = %{$p};
578 18 (my $k, my $pp) = each %hp;
579 18 (my $status, my $str2, my $mms)=$pp->($str);
580 18 my $matches = {$k => $mms};
581 18 return ($status, $str2, $matches);
582 } else {
583 0 die show($p);
584 }
585 }
586  
587  
588 0 ▶sub matches {
Subroutine matches: not called CC=1 SCAR=6.9
Pod undocumented
589 0 return @{$_[0]};
590 }
591  
592 0 ▶sub unwrap {
Subroutine unwrap: not called CC=1 SCAR=6.9
Pod undocumented
593 0 (my $elt_in_array)=@_;
594 0 my $elt = shift @{$elt_in_array};
595 0 return $elt;
596 }
597  
598 0 ▶sub empty {
Subroutine empty: not called CC=2 SCAR=17.9
Pod undocumented
599 0 (my $elt_in_array)=@_;
600 0 ▶ return (@{$elt_in_array} ) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@{$elt_in_array;} ? : 0 0 0
601 }
602  
603 # This function returns labeled items in the parse tree.
604 # It is rather aggressive in removing unlabeled items
605 16 ▶sub get_tree_as_lists { (my $list) = @_;
Subroutine get_tree_as_lists: called CC=11 SCAR=24.0
Pod undocumented
606 16 my $hlist=[];
607 16 for my $elt (@{$list}) {
608 41 ▶ if (ref($elt) eq 'ARRAY' and scalar @{$elt}>0) { # non-empty list
Branch: 2 branches
BranchTrueFalseTotal
if (ref $elt eq 'ARRAY' and scalar @{$elt;} > 0) { } 8 33 41
elsif (ref $elt eq 'HASH') { } 15 18 33
Condition66%2/3
ref $elt eq "ARRAY" and scalar @{$elt;} > 0
!ll&&!rl&&r
3308
MC/DC50%1/2
ref $elt eq "ARRAY" and scalar @{$elt;} > 0
ref $elt eq "ARRAY" scalar @{$elt;} > 0
Truth table66%2/3
A and B
A= ref $elt eq "ARRAY"B= scalar @{$elt;} > 0
ABresult
0X0
100
111
609 8 push @{ $hlist }, get_tree_as_lists($elt);
610 } elsif (ref($elt) eq 'HASH') { # hash: need to process the rhs of the pair
611 15 (my $k, my $v) = each %{$elt};
612 15 ▶ if (ref($v) ne 'ARRAY') { # not an array => wrap in array and redo
Branch: 2 branches
BranchTrueFalseTotal
if (ref $v ne 'ARRAY') { } 6 9 15
elsif (@{$v;} == 1) { } 2 7 9
613 6 push @{$hlist}, {$k => $v};
614 9 } elsif (@{$v}==1) { # a single-elt array
615 2 push @{$hlist}, {$k => $v->[0]};
616 } else {
617 my $pv =[
618 map {
619 16 ▶ if (ref($_) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 1 15 16
elsif (ref $_ eq 'HASH') { } 4 11 15
elsif (defined $_) { } 10 1 11
620 1 get_tree_as_lists($_)
621 } elsif ( ref($_) eq 'HASH') {
622 4 get_tree_as_lists([$_])
623 10 } elsif (defined $_) { $_ }
624 7 } @{$v}
625 ];
626 7 push @{$hlist}, {$k => $pv};
627 }
628 }
629 }
630 16 ▶ return scalar @{$hlist}==1 ? $hlist->[0] : $hlist;
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$hlist;} == 1 ? : 13 3 16
631 }
632  
633 14 ▶sub is_list_of_objects { (my $mlo) =$_;
Subroutine is_list_of_objects: called CC=3 SCAR=11.0
Pod undocumented
634 14 ▶ if (ref($mlo) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mlo eq 'ARRAY') { } 7 7 14
635 7 my @tmlo=@{$mlo};
636 7 my @l=grep { ref($_) ne 'HASH' } @tmlo;
637 7 ▶ return scalar(@l)?0:1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @l ? : 5 2 7
638 } else {
639 7 return 0;
640 }
641 }
642  
643 5 ▶sub l2m { (my $hlist) =@_;
Subroutine l2m: called CC=3 SCAR=11.0
Pod undocumented
644 5 my $hmap = {};
645 5 my @hmap_vals = map { (my $k, my $v)=%{$_}; $v } @{$hlist};
646 5 my @hmap_keys = map { (my $k, my $v)=%{$_}; $k } @{$hlist};
647 5 ▶ my @hmap_rvals = map { is_list_of_objects($_) ? l2m($_) : $_ } @hmap_vals;
Branch: 1 branch
BranchTrueFalseTotal
is_list_of_objects($_) ? : 2 12 14
648 # my @hmap_keys_vals = map { each %{$_} } @{$hlist}
649 5 for my $k ( @hmap_keys ) {
650 14 my $rv = shift @hmap_rvals;
651 14 $hmap->{$k}=$rv;
652 }
653 # my %{$hmap} = @hmap_keys_vals;
654 5 return $hmap;
655 }
656  
657
658
659 3 ▶sub getParseTree { (my $m) =@_;
Subroutine getParseTree: called CC=1 SCAR=0.0
Pod undocumented
660 3 return l2m(get_tree_as_lists($m));
661 }
662  
663 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
664 0 (my $p, my $str) = @_;
665 0 (my $st, my $rest, my $m) = apply($p,$str);
666 0 getParseTree($m);
667 }
668  
669 26 ▶sub show { (my $data)=@_;
Subroutine show: called CC=1 SCAR=0.0
Pod undocumented
670 26 $Data::Dumper::Indent=0;
671 26 $Data::Dumper::Terse =1;
672 26 return Dumper($data);
673 }
674  
675 2 1;
676  
677 __END__
↑ Summary