blib/lib/Decl/Semantics/Code.pm

filter: Statementstmt 81.0% 171 / 211 Branchbran 70.9% 44 / 62 Conditioncond 51.8% 14 / 27 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 72.7% 16 / 22 Podpod 100% 15 / 15 totaltotal 74.7% 263 / 352 CC 44 SCAR 24.0
CC
44
Cov
76%
build_macro_payload
21.8
make_select
15.5
make_macro_code
12.3
CRAP 11.0
« blib/lib/Decl/DefaultParsers.pm ↑ Summary blib/lib/Decl/Parser.pm »
1 package Decl::Semantics::Code;
2
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 13 ▶use base qw(Decl::Node);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Decl::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 =head1 NAME
11
12 Decl::Semantics::Code - implements some code (perl or otherwise) in a declarative framework.
13
14 =head1 VERSION
15
16 Version 0.01
17
18 =cut
19
20 13 our $VERSION = '0.01';
21
22
23 =head1 SYNOPSIS
24
25 This class serves two purposes: first, it's an example of what a semantic node class should look like, and second, it
26 will probably end up being the class that builds most of the code references in a declarative program.
27
28 =head2 defines(), tags_defined()
29
30 Called by Decl::Semantics during import, to find out what tags this plugin claims to implement and the
31 parsers used to build its content.
32
33 =cut
34 0 ▶sub defines { ('on', 'do', 'perl', 'sub'); }
Subroutine defines: not called CC=1 SCAR=6.9
Pod documented
35 0 ▶our %build_handlers = ( perl => { node => sub { Decl::Semantics::Code->new (@_) }, body => 'none' } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
36 12 ▶sub tags_defined { Decl->new_data(<<EOF); }
Subroutine tags_defined: called CC=1 SCAR=0.0
Pod documented
37 on
38 do
39 sub
40 filter
41 perl (body=text)
42 EOF
43
44 =head2 build_payload, build_macro_payload, fixvars, fixcall, fixevent, fixfind, make_code, make_macro_code, parse_select, make_select, make_dml, make_output, make_ifnew
45
46 The C<build_payload> function is then called when this object's payload is built (i.e. in the stage when we're adding semantics to our
47 parsed syntax). The payload of a code object is its callable code result.
48
49 The C<make_code> function builds code in an event context; it actually calls C<make_macro_code>, which does the same in an arbitrary
50 node context that you supply (but that defaults to the event context of the code node).
51
52 The parent's payload will always have been created by the time this function is called.
53
54 The C<make_select> function is by far the most complex of our code generators, as it has to find an iterator source and build a while loop,
55 or a DBI database and build a query and select loop. The parsing is split out into C<parse_select> in order to make it usable from elsewhere.
56
57 The C<make_dml> function handles the non-select DBI keywords (just 'insert', 'update', and 'delete').
58
59 The C<fix*> functions munge various things around in our code generation scheme.
60
61 The C<make_output> function handles text blocks for output delineated with "<<".
62
63 The C<make_ifnew> is probably going overboard with specific select tweaks; I really need to start thinking harder about real macros in the code.
64
65 =cut
66
67 17 ▶sub fixvars { '$self->{v}->{\'' . $_[0] . '\'}' }
Subroutine fixvars: called CC=1 SCAR=0.0
Pod documented
68 4 ▶sub fixcall {
Subroutine fixcall: called CC=4 SCAR=20.1
Pod documented
69 4 ▶ return '$self->' . $_[0] if ($_[0] eq 'output' ||
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "output" or $_[0] eq "write" or $_[0] eq "log" 4 0 4
Condition33%2/6
$_[0] eq "output" or $_[0] eq "write" or $_[0] eq "log"
l!l&&r!l&&!r
400
$_[0] eq "output" or $_[0] eq "write"
l!l&&r!l&&!r
400
MC/DC0%0/3
$_[0] eq "output" or $_[0] eq "write" or $_[0] eq "log"
$_[0] eq "output" $_[0] eq "write" $_[0] eq "log"
Truth table25%1/4
A or B or C
A= $_[0] eq "output"B= $_[0] eq "write"C= $_[0] eq "log"
ABCresult
1XX1
01X1
0011
0000
70 $_[0] eq 'write' ||
71 $_[0] eq 'log');
72 0 '$cx->' . $_[0]
73 }
74 2 ▶sub fixevent { '$cx->do(\'' . $_[0] . '\')' }
Subroutine fixevent: called CC=1 SCAR=0.0
Pod documented
75
76 0 ▶sub fixfind { '$self->find_context(' . $_[0] . ')' }
Subroutine fixfind: not called CC=1 SCAR=6.9
Pod documented
77
78 13 our $next_counter = 1;
79
80 5 ▶sub parse_select { # 2011-08-27 - factored out of make_select below - made possible once more by the magic of unit testing!
Subroutine parse_select: called CC=4 SCAR=13.9
Pod documented
81 5 my ($foreach) = @_;
82 5 my @vars = ();
83 5 my $keyword = '';
84
85 5 ▶ if ($foreach =~ /^\s*(.*?)\s+in\s+(.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($foreach =~ /^\s*(.*?)\s+in\s+(.*?)\s*$/) 1 4 5
86 1 my ($target, $source) = ($1, $2);
87 1 @vars = split /\s*[, ]\s*/, $target;
88 1 return ('foreach', $target, $source, @vars);
89 }
90
91 4 ▶ if ($foreach =~ /^\s*(.*?)\s+from\s+(.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($foreach =~ /^\s*(.*?)\s+from\s+(.*?)\s*$/) 3 1 4
92 3 my ($target, $source) = ($1, $2);
93 3 my $t = $target;
94 3 $t =~ s/^(distinct|all)\s+//;
95 3 @vars = map { s/^.* //; $_ } split (/\s*,\s*/, $t);
96 3 return ('select', $target, $source, @vars)
97 }
98 1 ▶ if ($foreach !~ /\s/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($foreach =~ /\s/) 1 0 1
99 1 return ('foreach', '', $foreach);
100 }
101
102 0 return ('error');
103 }
104
105 5 ▶sub make_select {
Subroutine make_select: called CC=11 SCAR=27.4
Pod documented
106 5 my ($self, $foreach, $keyword) = @_;
107 5 my $cx = $self->event_context;
108
109 5 my ($target, $source);
110 5 my @vars = ();
111 5 my @last_vars = ();
112
113 5 ($keyword, $target, $source, @vars) = parse_select($foreach);
114 5 @last_vars = map { '_last_' . $_ . '_value' } @vars;
115
116 5 ▶ if ($keyword eq 'error') {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyword eq "error") 0 5 5
117 0 $self->error("'^foreach/select $foreach' can't be parsed");
118 0 return 'if (0) {';
119 }
120
121 5 my $unique = $next_counter++;
122 5 my $ret;
123
124 5 ▶ if ($keyword eq 'foreach') { # Normal data
Branch: 1 branch
BranchTrueFalseTotal
if ($keyword eq 'foreach') { } 2 3 5
125 2 my ($datasource, $type) = $self->find_data($source); # TODO: error handling if source not found.
126
127 2 ▶ if (not $target and $datasource->is ('data')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $target and $datasource->is("data")) 1 1 2
Condition66%2/3
not $target and $datasource->is("data")
!ll&&!rl&&r
101
MC/DC50%1/2
not $target and $datasource->is("data")
not $target $datasource->is("data")
Truth table66%2/3
not(A) and B
A= not $targetB= $datasource->is("data")
ABresult
0X0
100
111
128 # Take target from definition of data source.
129 1 push @vars, $datasource->parmlist;
130 1 push @last_vars, map { '_last_' . $_ . '_value' } $datasource->parmlist;
131 }
132
133 2 ▶ if ($type eq 'text') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'text') { } 0 2 2
elsif ($type eq 'data') { } 2 0 2
134 0 my $my = '';
135 0 ▶ if (@vars) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vars) { } 0 0 0
136 0 $target = 'my $' . shift @vars;
137 0 ▶ $my = 'my ($' . join (', $', @vars) . '); ' if @vars;
Branch: 1 branch
BranchTrueFalseTotal
if @vars 0 0 0
138 0 ▶ $my .= 'my ($' . join (', $', @last_vars) . '); ' if @last_vars;
Branch: 1 branch
BranchTrueFalseTotal
if @last_vars 0 0 0
139 } else {
140 0 $target = '$_';
141 }
142 0 $ret .= '{ ';
143 0 $ret .= 'my @text_node = $self->find_data(\'' . $source . '\'); ';
144 0 $ret .= 'my $iterator = $text_node[0]->iterate; ';
145 0 $ret .= 'while (' . $target . ' = $iterator->next) { ';
146 0 $ret .= $my;
147 } elsif ($type eq 'data') {
148 2 $ret .= '{ ';
149 2 $ret .= 'my @data_node = $self->find_data(\'' . $source . '\'); ';
150 2 $ret .= 'my $iterator = $data_node[0]->iterate; ';
151 2 $ret .= 'while (my $line = $iterator->next) { ';
152 2 $ret .= 'my ($' . join (', $', @vars) . ') = @$line;';
153 2 $ret .= 'my ($' . join (', $', @last_vars) . ') = @$line;';
154 } else {
155 0 $self->error ("node foreach not implemented yet");
156 0 $ret = 'if (0) {';
157 }
158 } else { # Database (e.g. DBI) select
159 # This is kind of the default mode for DBI; absent specification to the contrary, we find the first database handle and use it.
160 # But "source" is where everything is coming from, so if we can munge in in some way, this is where that will happen.
161
162 3 ▶ if ($vars[0] eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars[0] eq '*') { } 1 2 3
163 1 $ret .= '{ ';
164 1 $ret .= 'my $dbh = $self->find_context(\'database\')->payload; ';
165 1 $ret .= 'my $sth = $dbh->prepare ("select ' . $target . ' from ' . $source . '"); ';
166 1 $ret .= '$sth->execute(); ';
167 1 $ret .= 'while (my $row = $sth->fetchrow_hashref()) {';
168 } else {
169 2 $ret .= '{ ';
170 2 $ret .= 'my $dbh = $self->find_context(\'database\')->payload; ';
171 2 $ret .= 'my $sth = $dbh->prepare ("select ' . $target . ' from ' . $source . '"); ';
172 2 $ret .= '$sth->execute(); ';
173 2 $ret .= 'my ($' . join (', $', @vars) . '); ';
174 2 $ret .= 'my ($' . join (', $', @last_vars) . '); ';
175 2 $ret .= '$sth->bind_columns (\$'. join (', \$', @vars) . '); ';
176 2 $ret .= 'while ($sth->fetch()) {';
177 }
178 }
179
180 5 $ret;
181 }
182
183 0 ▶sub make_dml {
Subroutine make_dml: not called CC=1 SCAR=6.9
Pod documented
184 0 my ($self, $foreach, $keyword) = @_;
185 0 my $cx = $self->event_context;
186 }
187
188 1 ▶sub make_output {
Subroutine make_output: called CC=2 SCAR=7.1
Pod documented
189 1 my ($output, $flag) = @_;
190
191 1 my $r;
192 1 ▶ if ($flag eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag eq '"') { } 0 1 1
193 0 $r = '$self->output(<<"EOF");' . "\n";
194 } else {
195 1 $r = '$self->output($Decl::template_engine->express(<<\'EOF\', $cx));' . "\n";
196 }
197 1 $r .= $output;
198 1 $r .= "EOF\n";
199 1 return $r;
200 }
201
202 0 ▶sub make_ifnew {
Subroutine make_ifnew: not called CC=1 SCAR=6.9
Pod documented
203 0 my ($v) = @_;
204 0 return 'if (not defined $_last_' . $v . '_value or $_last_' . $v . '_value ne $' . $v . ') {' . "\n" .
205 ' $_last_' . $v . '_value = $' . $v . ';' . "\n";
206 }
207
208 1 ▶sub make_code {
Subroutine make_code: called CC=1 SCAR=0.0
Pod documented
209 1 my $self = shift;
210 1 my $code = shift;
211
212 1 make_macro_code($self, $code, undef, @_);
213 }
214
215 23 ▶sub make_macro_code {
Subroutine make_macro_code: called CC=12 SCAR=25.1
Pod documented
216 23 my $self = shift;
217 23 my $code = shift;
218 23 ▶ my $outer_cx = shift || $self->event_context;
Condition66%2/3
shift() || $self->event_context
l!l&&r!l&&!r
2210
MC/DC0%0/2
shift() || $self->event_context
shift() $self->event_context
Truth table66%2/3
A || B
A= shift()B= $self->event_context
ABresult
1X1
011
000
219
220 23 my $sem = $outer_cx->semantics;
221 23 my $subs = $self->subs();
222
223 23 my $preamble = 'my $cx = shift || $outer_cx;' . "\n";
224 23 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 18 23
225 5 $preamble .= 'my ($' . join (', $', @_) . ') = @_;' . "\n\n"; # I love generating code.
226 }
227 23 foreach my $subname (keys %$subs) {
228 2 $preamble .= 'local *' . $subname . ' = $subs->{\'' . $subname . '\'}->{sub};' . "\n";
229 }
230 23 $code = $preamble . $code;
231 23 $code =~ s/\^db( *)->/\$self->find_context('database')->dbh->/g;
232 23 $code =~ s/\$\^(\w+)/fixvars($1)/ge;
233 23 $code =~ s/\^!(\w+)/fixevent($1)/ge;
234 0 $code =~ s/\^\((.*?)\)/fixfind($1)/ge; # TODO: balanced parens would be a lot more convincing in that regexp...
235 23 $code =~ s/\^foreach (.*) {{/$self->make_select($1, 'foreach')/ge;
236 23 $code =~ s/\^select (.*) {{/$self->make_select($1, 'select')/ge;
237 0 $code =~ s/\^if-new (.*) {/make_ifnew($1)/ge;
238 0 $code =~ s/\^(insert .*);/$self->make_dml($1)/ge;
239 0 $code =~ s/\^(delete .*);/$self->make_dml($1)/ge;
240 0 $code =~ s/\^(update .*);/$self->make_dml($1)/ge;
241 23 $code =~ s/\^(\w+)/fixcall($1)/ge;
242
243 23 my $lcode = '';
244 23 my $mode = 0;
245 23 my $indent = 0;
246 23 my $output;
247 23 my $flag = '';
248 23 foreach my $line (split /\n/, $code) {
249 98 ▶ if ($mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode) { } 2 96 98
250 2 my $leader = substr($line, 0, $indent);
251 2 ▶ if ($leader =~ /^[\s<]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leader =~ /^[\s<]*$/) { } 1 1 2
252 1 $output .= substr($line, $indent) . "\n";
253 } else {
254 1 $lcode .= make_output($output, $flag);
255 1 $mode = 0;
256 1 $lcode .= $line . "\n";
257 }
258 } else {
259 96 ▶ if ($line =~ /^\s*<</) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*<</) { } 1 95 96
260 1 my $olen = length($line);
261 1 $line =~ s/^\s*<<//;
262 1 $flag = substr($line, 0, 1);
263 1 $line =~ s/$flag\s*//;
264 1 $indent = $olen - length ($line);
265 1 $output = $line . "\n";
266 1 $mode = 1;
267 } else {
268 95 $lcode .= $line . "\n";
269 }
270 }
271 }
272 23 ▶ if ($mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode) 0 23 23
273 0 $lcode .= make_output($output, $flag);
274 }
275
276 23 my $sub = eval "sub {" . $lcode . "\n}";
277 23 ▶ $self->error ($@) if $@; # TODO: man, this is just the wrong way to do this.
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 23 23
278 23 ▶ print STDERR $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 23 23
279
280 23 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 22 1 23
281 22 return ($sub, $lcode);
282 } else {
283 1 return $sub;
284 }
285 }
286
287 28 ▶sub build_payload { # TODO: split this out into build_code and build_payload
Subroutine build_payload: called CC=2 SCAR=6.9
Pod documented
288 28 my $self = shift;
289 28 my $is_event = shift; # @_ is now the list of 'my' variables the code expects, by name.
290 28 build_macro_payload($self, $is_event, undef, @_);
291 28 ▶ $self->{callable} = 'sub' if $self->is('sub');
Branch: 1 branch
BranchTrueFalseTotal
if $self->is("sub") 2 26 28
292 28 $self;
293 }
294
295 31 ▶sub build_macro_payload {
Subroutine build_macro_payload: called CC=15 SCAR=30.8
Pod documented
296 31 my $self = shift;
297 31 my $is_event = shift;
298 31 ▶ my $cx = shift || $self->event_context;
Condition33%1/3
shift() || $self->event_context
l!l&&r!l&&!r
0310
MC/DC0%0/2
shift() || $self->event_context
shift() $self->event_context
Truth table33%1/3
A || B
A= shift()B= $self->event_context
ABresult
1X1
011
000
299
300 31 ▶ return $self if $self->{built};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"built"} 1 30 31
301 30 $self->{built} = 1;
302
303 30 ▶ if (!@_) { # Didn't get any 'my' variables explicitly defined.
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 18 12 30
304 18 @_ = $self->optionlist;
305 }
306
307 # Here's the tricky part. We have to build some code and evaluate it when asked. This could get arbitrarily complex.
308 # If we have a code body, that's our code. If we have both a body and a "code" (i.e. a one-line bracketed body), then
309 # the "code" takes precedence (e.g. Wx toolbars).
310 30 ▶ if ($self->code) { # TODO: this wasn't covered by the unit tests!
Branch: 2 branches
BranchTrueFalseTotal
if ($self->code) { } 0 30 30
elsif ($self->body) { } 22 8 30
311 0 my $code = $self->code;
312 0 $code =~ s/^{//;
313 0 $code =~ s/}$//;
314 #print "code is $code\n";
315 0 ($self->{sub}, $self->{gencode}) = make_macro_code ($self, $code, $cx, @_);
316 #print STDERR "1gencode is " . $self->{gencode} . "\n";
317 0 $self->{callable} = 1;
318 0 $self->{owncode} = 1;
319 } elsif ($self->body) {
320 22 ($self->{sub}, $self->{gencode}) = make_macro_code ($self, $self->body, $cx, @_);
321 #print STDERR "gencode is " . $self->{gencode} . "\n";
322
323 22 $self->{callable} = 1;
324 22 $self->{owncode} = 1;
325 } else {
326 # No body means we're just going to build each of our children, and try to execute each of them in sequence when called.
327 # No body and no callable children means we're not callable either.
328 #print "making child-based caller:" . $self->myline . "\n";
329 8 my $child_code = 0;
330 8 foreach ($self->nodes) {
331 0 $_->build;
332 0 ▶ $child_code = $child_code || $_->{callable};
Condition0%0/3
$child_code || $_->{'callable'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$child_code || $_->{'callable'}
$child_code $_->{'callable'}
Truth table0%0/3
A || B
A= $child_codeB= $_->{'callable'}
ABresult
1X1
011
000
333 }
334
335 8 ▶ $self->{callable} = $child_code ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$child_code ? : 0 8 8
336 0 ▶ $self->{sub} = sub { $self->go(); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
337 8 $self->{owncode} = 0;
338 }
339
340 30 ▶ $self->{event} = $self->is ('on') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->is('on') ? : 4 26 30
341 30 ▶ if ($self->{callable} && ($is_event || ($self->is ('on') and $self->name))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"callable"} and $is_event || $self->is("on") && $self->name) 4 26 30
Condition77%7/9
$self->{"callable"} and $is_event || $self->is("on") && $self->name
!ll&&!rl&&r
8184
$is_event or $self->is("on") and $self->name
l!l&&r!l&&!r
0418
$self->is("on") and $self->name
!ll&&!rl&&r
1804
MC/DC50%2/4
$self->{"callable"} and $is_event || $self->is("on") && $self->name
$self->{"callable"} $is_event $self->is("on") $self->name
Truth table60%3/5
A and B or C and D
A= $self->{"callable"}B= $is_eventC= $self->is("on")D= $self->name
ABCDresult
0XXX0
100X0
10100
11XX1
10111
342 4 $cx->register_event ($self->name, $self->{sub});
343 }
344
345 30 ▶ $self->{payload} = $self->{sub} unless $self->{payload}; # TODO: this seems fishy.
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"payload"} 30 0 30
346 30 return $self;
347 }
348
349
350 =head1 AUTHOR
351
352 Michael Roberts, C<< <michael at vivtek.com> >>
353
354 =head1 BUGS
355
356 Please report any bugs or feature requests to C<bug-decl at rt.cpan.org>, or through
357 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Decl>. I will be notified, and then you'll
358 automatically be notified of progress on your bug as I make changes.
359
360 =head1 LICENSE AND COPYRIGHT
361
362 Copyright 2010 Michael Roberts.
363
364 This program is free software; you can redistribute it and/or modify it
365 under the terms of either: the GNU General Public License as published
366 by the Free Software Foundation; or the Artistic License.
367
368 See http://dev.perl.org/licenses/ for more information.
369
370 =cut
371
372 13 1; # End of Decl::Semantics::Code
« blib/lib/Decl/DefaultParsers.pm ↑ Summary blib/lib/Decl/Parser.pm »