blib/lib/Wraith.pm

filter: Statementstmt 99.1% 122 / 123 Branchbran 81.2% 13 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 33 / 33 Podpod 0.0% 0 / 14 totaltotal 90.3% 168 / 186 CC 17 SCAR 6.7
CC
17
Cov
97%
__ANON__
4.1
deref
3.0
concat_impl
3.0
CRAP 1.9
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 require Exporter;
5  
6 2 our $VERSION = 0.12;
7  
8 {
9 package Wraith;
10  
11 2 our @ISA = qw( Exporter );
12 2 our @EXPORT_OK = qw( $literal $literals $token $many $succeed $fail $many_g $opt $satisfy );
13  
14 {
15 package inner_lazy;
16  
17 32 ▶ sub TIESCALAR {
Subroutine TIESCALAR: called CC=1 SCAR=0.0
18 32 my ($class, $val) = @_;
19 32 bless $_[1], $class
20 }
21  
22 28 ▶ sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
23 28 my ($self) = @_;
24 28 $self->()
25 }
26 }
27  
28 use overload
29 2 ▶ '>>' => "then_impl",
Subroutine BEGIN: called CC=1 SCAR=0.0
30 '|' => "alt_impl",
31 2 '**' => "using_impl";
32  
33 473 ▶ sub deref {
Subroutine deref: called CC=3 SCAR=11.0
Pod undocumented
34 473 my @args = @_;
35 473 for my $elt (@args) {
36 572 ▶ if (ref($elt) eq "Wraith_rule") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $elt eq "Wraith_rule") 66 506 572
37 66 $elt = $$elt;
38 }
39 }
40 @args
41 473 }
42  
43 202 ▶ sub concat_impl {
Subroutine concat_impl: called CC=3 SCAR=11.0
Pod undocumented
44 202 my @list_of_lists = @_;
45 202 my @list;
46  
47 202 for my $elt (@list_of_lists) {
48 405 push @list, $_ for @$elt;
49 }
50 \@list
51 202 };
52 2 our $concat = \&concat_impl;
53  
54 89 ▶ sub succeed_impl {
Subroutine succeed_impl: called CC=1 SCAR=0.0
Pod undocumented
55 89 my $v = $_[0];
56 bless
57 105 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
58 105 ▶ my $u = (ref($v) eq "ARRAY") ? $v : [ $v ];
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 49 56 105
59 105 [ [ $u, $_[0] ] ]
60 }
61 89 };
62 2 our $succeed = bless \&succeed_impl;
63  
64 96 ▶ sub fail_impl {
Subroutine fail_impl: called CC=1 SCAR=0.0
Pod undocumented
65 96 []
66 };
67 2 our $fail = bless \&fail_impl;
68  
69 25 ▶ sub satisfy_impl {
Subroutine satisfy_impl: called CC=2 SCAR=6.9
Pod undocumented
70 25 my ($p, $m) = @_;
71 25 ▶ $m = sub { $_[0] =~ /(.)(.*)/s } if not $m;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless $m 12 13 25
72 bless
73 152 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
74 152 ▶ if (my ($x, $xs) = $m->($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$m($_[0])) { } 61 91 152
75 61 ▶ if ($p->($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$p($x)) { } 56 5 61
76 56 return $succeed->($x)->($xs);
77 } else {
78 5 return $fail->($xs);
79 }
80 } else {
81 91 return $fail->( [] );
82 }
83 }
84 25 };
85 2 our $satisfy = bless \&satisfy_impl;
86  
87 11 ▶ sub literal_impl {
Subroutine literal_impl: called CC=1 SCAR=0.0
Pod undocumented
88 11 my $y = $_[0];
89 $satisfy->(
90 12 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
91 12 $y eq $_[0]
92 }
93 )
94 11 };
95 2 our $literal = bless \&literal_impl;
96  
97 1 ▶ sub literals_impl {
Subroutine literals_impl: called CC=1 SCAR=0.0
Pod undocumented
98 1 my $y = $_[0];
99 $satisfy->(
100 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
101 1 index($y, $_[0]) != -1
102 }
103 )
104 1 };
105 2 our $literals = bless \&literals_impl;
106  
107 13 ▶ sub token_impl {
Subroutine token_impl: called CC=2 SCAR=7.0
Pod undocumented
108 13 my ($tok, $skip) = @_;
109 13 ▶ $skip = '\s*' if not $skip;
Branch: 1 branch
BranchTrueFalseTotal
unless $skip 13 0 13
110 $satisfy->(
111 48 ▶ sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
112 139 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
113 139 $_[0] =~ /^$skip($tok)(.*)/s
114 }
115 )
116 13 };
117 2 our $token = bless \&token_impl;
118  
119 38 ▶ sub alt_impl {
Subroutine alt_impl: called CC=1 SCAR=0.0
Pod undocumented
120 38 my ($p1_, $p2_, $discard) = @_;
121 bless
122 99 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
123 99 my ($p1, $p2) = deref($p1_, $p2_);
124 99 my $inp = $_[0];
125 99 $concat->($p1->($inp), $p2->($inp))
126 }
127 38 }
128 2 our $alt = bless \&alt_impl;
129  
130 44 ▶ sub then_impl {
Subroutine then_impl: called CC=1 SCAR=0.0
Pod undocumented
131 44 my $arglist = \@_;
132 bless
133 169 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
134 169 my ($p1) = deref($arglist->[0]);
135 169 my $inp = $_[0];
136 169 my $reslist1 = $p1->($inp);
137 169 my $finlist = [];
138 169 for my $respair (@$reslist1) {
139 88 my ($p2) = deref($arglist->[1]);
140 88 my $reslist2 = $p2->($respair->[1]);
141 88 for my $finpair (@$reslist2) {
142 102 push @$finlist, [ $concat->($respair->[0], $finpair->[0]), $finpair->[1] ];
143 }
144 }
145 $finlist
146 169 }
147 44 }
148 2 our $then = bless \&then_impl;
149  
150 9 ▶ sub using_impl {
Subroutine using_impl: called CC=1 SCAR=0.0
Pod undocumented
151 9 my ($p_, $f, $discard) = @_;
152 bless
153 115 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
154 115 my ($p) = deref($p_);
155 115 my $inp = $_[0];
156 115 my $reslist = $p->($inp);
157 115 my $finlist = [];
158 115 for my $respair (@$reslist) {
159 102 push @$finlist, [ $f->($respair->[0]), $respair->[1] ];
160 }
161 $finlist
162 115 }
163 9 }
164 2 our $using = bless \&using_impl;
165  
166 32 ▶ sub many_impl {
Subroutine many_impl: called CC=1 SCAR=0.0
Pod undocumented
167 32 my $p = $_[0];
168 32 my $f;
169 32 ▶ tie $f, "inner_lazy", sub { many_impl($p) };
Subroutine __ANON__: called CC=1 SCAR=0.0
170 32 $alt->($then->($p, $f), $succeed->( [] ))
171 }
172 2 our $many = bless \&many_impl;
173  
174 1 ▶ sub many_g_impl {
Subroutine many_g_impl: called CC=1 SCAR=0.0
Pod undocumented
175 1 my $arglist = \@_;
176 bless
177 1 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
178 1 my ($p) = deref($arglist->[0]);
179 1 my $inp = $_[0];
180 1 my $finlist = [];
181 1 while (1) {
182 4 my $reslist = $p->($inp);
183 4 ▶ last if (not @$reslist);
Branch: 1 branch
BranchTrueFalseTotal
unless @$reslist 1 3 4
184 3 my $respair = shift @$reslist;
185 3 for my $elt (@$reslist) {
186 0 ▶ $respair = $elt if (length($respair->[1]) < length($elt->[1]));
Branch: 1 branch
BranchTrueFalseTotal
if length $respair->[1] < length $elt->[1] 0 0 0
187 }
188 3 push @$finlist, $respair->[0];
189 3 $inp = $respair->[1];
190 }
191 1 [ [ $concat->(@$finlist), $inp ] ]
192 }
193 1 }
194 2 our $many_g = bless \&many_g_impl;
195  
196 1 ▶ sub opt_impl {
Subroutine opt_impl: called CC=1 SCAR=0.0
Pod undocumented
197 1 my ($p) = deref($_[0]);
198 1 $alt->($p, $succeed->( [] ))
199 }
200 2 our $opt = bless \&opt_impl;
201 }
202  
203 {
204 2 package Wraith_rule;
205  
206 2 our @ISA = qw( Exporter Wraith );
207 2 our @EXPORT_OK = qw( );
208  
209 6 ▶ sub makerule {
Subroutine makerule: called CC=1 SCAR=0.0
210 6 bless $_[0]
211 }
212  
213 1 ▶ sub makerules {
Subroutine makerules: called CC=2 SCAR=6.9
214 1 my ($class, @args) = @_;
215 1 for my $elt (@args) {
216 6 $elt = makerule($elt);
217 }
218 @args
219 1 }
220 }
221  
222 2 1;
223  
224 __END__
↑ Summary