Eyapp/YATW.pm

filter: Statementstmt 93.8% 137 / 146 Branchbran 72.9% 35 / 48 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 91.3% 21 / 23 Podpod 0.0% 0 / 15 totaltotal 83.0% 201 / 242 CC 29 SCAR 12.9
CC
29
Cov
88%
s
7.0
name
6.0
do_pending_tasks
5.0
CRAP 3.6
« Eyapp/Options.pm ↑ Summary make_eyapp_c_tests.pl »
1 # Copyright © 2006, 2007, 2008, 2009, 2010, 2011, 2012 Casiano Rodriguez-Leon.
2 # Copyright © 2017 William N. Braswell, Jr.
3 # All Rights Reserved.
4 #
5 # Parse::Yapp is Copyright © 1998, 1999, 2000, 2001, Francois Desarmenien.
6 # Parse::Yapp is Copyright © 2017 William N. Braswell, Jr.
7 # All Rights Reserved.
8 package Parse::Eyapp::YATW;
9 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 64 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 64 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 64 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 64 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub firstval(&@) {
Subroutine firstval: not called CC=1 SCAR=6.9
Pod undocumented
16 0 my $handler = shift;
17
18 0 return (grep { $handler->($_) } @_)[0]
19 }
20  
21 40 ▶sub lastval(&@) {
Subroutine lastval: called CC=1 SCAR=0.0
Pod undocumented
22 40 my $handler = shift;
23
24 40 return (grep { $handler->($_) } @_)[-1]
25 }
26  
27 64 ▶sub valid_keys {
Subroutine valid_keys: called CC=1 SCAR=0.0
Pod undocumented
28 64 my %valid_args = @_;
29  
30 64 my @valid_args = keys(%valid_args);
31 64 local $" = ", ";
32 64 return "@valid_args"
33 }
34  
35 63 ▶sub invalid_keys {
Subroutine invalid_keys: called CC=1 SCAR=0.0
Pod undocumented
36 63 my $valid_args = shift;
37 63 my $args = shift;
38  
39 63 ▶ return (first { !exists($valid_args->{$_}) } keys(%$args));
Subroutine __ANON__: called CC=1 SCAR=0.0
40 }
41  
42  
43 64 our $VERSION = $Parse::Eyapp::Driver::VERSION;
44  
45 64 our $FILENAME=__FILE__;
46  
47 # TODO: Check args. Typical args:
48 # 'CHANGES' => 0,
49 # 'PATTERN' => sub { "DUMMY" },
50 # 'NAME' => 'fold',
51 # 'PATTERN_ARGS' => [],
52 # 'PENDING_TASKS' => {},
53 # 'NODE' => []
54  
55 64 my %_new_yatw = (
56 PATTERN => 'CODE',
57 NAME => 'STRING',
58 );
59  
60 64 my $validkeys = valid_keys(%_new_yatw);
61  
62 63 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod undocumented
63 63 my $class = shift;
64 63 my %args = @_;
65  
66 63 ▶ croak "Error. Expected a code reference when building a tree walker. " unless (ref($args{PATTERN}) eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"PATTERN"} eq "CODE" 0 63 63
67 63 ▶ if (defined($a = invalid_keys(\%_new_yatw, \%args))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($a = invalid_keys(\%_new_yatw, \%args))) 0 63 63
68 0 croak("Parse::Eyapp::YATW::new Error!: unknown argument $a. Valid arguments are: $validkeys")
69 }
70  
71  
72 # obsolete, I have to delete this
73 #$args{PATTERN_ARGS} = [] unless (ref($args{PATTERN_ARGS}) eq 'ARRAY');
74  
75 # Internal fields
76  
77 # Tell us if the node has changed after the visit
78 63 $args{CHANGES} = 0;
79
80 # PENDING_TASKS is a queue storing the tasks waiting for a "safe time/node" to do them
81 # Usually that time occurs when visiting the father of the node who generated the job
82 # (when asap criteria is applied).
83 # Keys are node references. Values are array references. Each entry defines:
84 # [ the task kind, the node where to do the job, and info related to the particular job ]
85 # Example: @{$self->{PENDING_TASKS}{$father}}, ['insert_before', $node, ${$self->{NODE}}[0] ];
86 63 $args{PENDING_TASKS} = {};
87  
88 # NODE is a stack storing the ancestor of the node being visited
89 # Example: my $ancestor = ${$self->{NODE}}[$k]; when k=1 is the father, k=2 the grandfather, etc.
90 # Example: CORE::unshift @{$self->{NODE}}, $_[0]; Finished the visit so take it out
91 63 $args{NODE} = [];
92  
93 63 bless \%args, $class;
94 }
95  
96 30 ▶sub buildpatterns {
Subroutine buildpatterns: called CC=3 SCAR=11.0
Pod undocumented
97 30 my $class = shift;
98
99 30 my @family;
100 30 ▶ while (my ($n, $p) = splice(@_, 0,2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 59 30 89
101 59 push @family, Parse::Eyapp::YATW->new(NAME => $n, PATTERN => $p);
102 }
103 30 ▶ return wantarray? @family : $family[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 30 0 30
104 }
105  
106 ####################################################################
107 # Usage : @r = $b{$_}->m($t)
108 # See Simple4.eyp and m_yatw.pl in the examples directory
109 # Returns : Returns an array of nodes matching the treeregexp
110 # The set of nodes is a Parse::Eyapp::Node::Match tree
111 # showing the relation between the matches
112 # Parameters : The tree (and the object of course)
113 # depth is no longer used: eliminate
114 9 ▶sub m {
Subroutine m: called CC=5 SCAR=16.1
Pod undocumented
115 9 my $p = shift(); # pattern YATW object
116 9 my $t = shift; # tree
117 9 my $pattern = $p->{PATTERN}; # CODE ref
118  
119 # References to the found nodes are stored in @stack
120 9 my @stack = ( Parse::Eyapp::Node::Match->new(node=>$t, depth=>0, dewey => "") );
121 9 my @results;
122 9 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
@stack and do { do { my $n = shift @stack; my(%n) = %$n; my $dewey = $n->{"dewey"}; my $d = $n->{"depth"}; if (&$pattern($n{"node"})) { $n->{"family"} = [$p]; $n->{"patterns"} = [0]; my $f = &lastval(sub { $dewey =~ /^$_->{"dewey"}/; } , @results); $n->{"father"} = $f; push @{$$f{"children"};}, $n if defined $f; push @results, $n; } ; my $k = 0; CORE::unshift @stack, map({local $a; $a = "Parse::Eyapp::Node::Match"->new("node", $_, "depth", $d + 1, "dewey", "$dewey.$k"); ++$k; $a;} $n{"node"}->children) } } 89 9 98
123 98 my $n = CORE::shift(@stack);
124 98 my %n = %$n;
125  
126 98 my $dewey = $n->{dewey};
127 98 my $d = $n->{depth};
128 98 ▶ if ($pattern->($n{node})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$pattern($n{"node"})) 40 58 98
129 40 $n->{family} = [ $p ];
130 40 $n->{patterns} = [ 0 ];
131  
132 # Is at this time that I have to compute the father
133 40 ▶ my $f = lastval { $dewey =~ m{^$_->{dewey}}} @results;
Subroutine __ANON__: called CC=1 SCAR=0.0
134 40 $n->{father} = $f;
135 # ... and children
136 40 ▶ push @{$f->{children}}, $n if defined($f);
Branch: 1 branch
BranchTrueFalseTotal
if defined $f 31 9 40
137 40 push @results, $n;
138 }
139 98 my $k = 0;
140 CORE::unshift @stack,
141 map {
142 89 local $a;
143 89 $a = Parse::Eyapp::Node::Match->new(node=>$_, depth=>$d+1, dewey=>"$dewey.$k" );
144 89 $k++;
145 89 $a;
146 98 } $n{node}->children();
147 } while (@stack);
148  
149 9 ▶ return wantarray? @results : $results[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9 0 9
150 }
151  
152 ######################### getter-setter for YATW objects ###########################
153  
154 989 ▶sub pattern {
Subroutine pattern: called CC=2 SCAR=7.1
Pod undocumented
155 989 my $self = shift;
156 989 ▶ $self->{PATTERN} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 989 989
157 989 return $self->{PATTERN};
158 }
159  
160 0 ▶sub name {
Subroutine name: not called CC=2 SCAR=17.9
Pod undocumented
161 0 my $self = shift;
162 0 ▶ $self->{NAME} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
163 0 return $self->{NAME};
164 }
165  
166 #sub pattern_args {
167 # my $self = shift;
168 #
169 # $self->{PATTERN_ARGS} = @_ if @_;
170 # return @{$self->{PATTERN_ARGS}};
171 #}
172  
173 ########################## PENDING TASKS management ################################
174  
175 # Purpose : Deletes the node that matched from the list of children of its father.
176 36 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
177 36 my $self = shift;
178  
179 36 bless $self->{NODE}[0], 'Parse::Eyapp::Node::DELETE';
180 }
181
182 841 ▶sub make_delete_effective {
Subroutine make_delete_effective: called CC=4 SCAR=13.9
Pod undocumented
183 841 my $self = shift;
184 841 my $node = shift;
185  
186 841 my $i = -1+$node->children;
187 841 ▶ while ($i >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 876 841 1717
188 876 ▶ if (UNIVERSAL::isa($node->child($i), 'Parse::Eyapp::Node::DELETE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($node->child($i), "Parse::Eyapp::Node::DELETE")) 36 840 876
189 36 ▶ $self->{CHANGES}++ if defined(splice(@{$node->{children}}, $i, 1));
Branch: 1 branch
BranchTrueFalseTotal
if defined splice(@{$$node{"children"};}, $i, 1) 36 0 36
190 }
191 876 $i--;
192 }
193 }
194  
195 ####################################################################
196 # Usage : my $b = Parse::Eyapp::Node->new( 'NUM(TERMINAL)', sub { $_[1]->{attr} = 4 });
197 # $yatw_pattern->unshift($b);
198 # Parameters : YATW object, node to insert,
199 # ancestor offset: 0 = root of the tree that matched, 1 = father, 2 = granfather, etc.
200  
201 3 ▶sub unshift {
Subroutine unshift: called CC=3 SCAR=11.4
Pod undocumented
202 3 my ($self, $node, $k) = @_;
203 3 ▶ $k = 1 unless defined($k); # father by default
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 3 0 3
204  
205 3 my $ancestor = ${$self->{NODE}}[$k];
206 0 ▶ croak "unshift: does not exist ancestor $k of node ".Dumper(${$self->{NODE}}[0]) unless defined($ancestor);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ancestor 0 3 3
207  
208 # Stringification of $ancestor. Hope it works
209 # operation, node to insert,
210 3 push @{$self->{PENDING_TASKS}{$ancestor}}, ['unshift', $node ];
211 }
212  
213 1 ▶sub insert_before {
Subroutine insert_before: called CC=2 SCAR=7.1
Pod undocumented
214 1 my ($self, $node) = @_;
215  
216 1 my $father = ${$self->{NODE}}[1];
217 0 ▶ croak "insert_before: does not exist father of node ".Dumper(${$self->{NODE}}[0]) unless defined($father);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $father 0 1 1
218  
219 # operation, node to insert, before this node
220 1 push @{$self->{PENDING_TASKS}{$father}}, ['insert_before', $node, ${$self->{NODE}}[0] ];
221 }
222  
223 1 ▶sub _delayed_insert_before {
Subroutine _delayed_insert_before: called CC=3 SCAR=11.0
224 1 my ($father, $node, $before) = @_;
225  
226 1 my $i = 0;
227 1 for ($father->children()) {
228 3 ▶ last if ($_ == $before);
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $before 1 2 3
229 2 $i++;
230 }
231 1 splice @{$father->{children}}, $i, 0, $node;
232 }
233  
234 841 ▶sub do_pending_tasks {
Subroutine do_pending_tasks: called CC=5 SCAR=16.1
Pod undocumented
235 841 my $self = shift;
236 841 my $node = shift;
237  
238 841 my $mytasks = $self->{PENDING_TASKS}{$node};
239 841 ▶ while ($mytasks and (my $job = shift @{$mytasks})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mytasks and my $job = shift @{$mytasks;}) 4 841 845
Condition100%3/3
$mytasks and my $job = shift @{$mytasks;}
!ll&&!rl&&r
83834
MC/DC100%2/2
$mytasks and my $job = shift @{$mytasks;}
$mytasks my $job = shift @{$mytasks;}
Truth table100%3/3
A and B
A= $mytasksB= my $job = shift @{$mytasks;}
ABresult
0X0
100
111
240 4 my @args = @$job;
241 4 my $task = shift @args;
242  
243 # change this for a jump table
244 4 ▶ if ($task eq 'unshift') {
Branch: 2 branches
BranchTrueFalseTotal
if ($task eq 'unshift') { } 3 1 4
elsif ($task eq 'insert_before') { } 1 0 1
245 3 CORE::unshift(@{$node->{children}}, @args);
246 3 $self->{CHANGES}++;
247 }
248 elsif ($task eq 'insert_before') {
249 1 _delayed_insert_before($node, @args);
250 1 $self->{CHANGES}++;
251 }
252 }
253 }
254  
255 ####################################################################
256 # Parameters : pattern, node, father of the node, index of the child in the children array
257 # YATW object. Probably too many
258 989 ▶sub s {
Subroutine s: called CC=7 SCAR=19.5
Pod undocumented
259 989 my $self = shift;
260 989 ▶ my $node = $_[0] or croak("Error. Method __PACKAGE__::s requires a node");
Branch: 1 branch
BranchTrueFalseTotal
my $node = $_[0] or croak("Error. Method __PACKAGE__::s requires a node") 989 0 989
261 989 CORE::unshift @{$self->{NODE}}, $_[0];
262 # father is $_[1]
263 989 my $index = $_[2];
264  
265 # If is not a reference or can't children then simply check the matching and leave
266 989 ▶ if (!ref($node) or !UNIVERSAL::can($node, "children")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $node and UNIVERSAL::can($node, "children")) 148 841 989
Condition66%2/3
ref $node and UNIVERSAL::can($node, "children")
!ll&&!rl&&r
0148841
MC/DC50%1/2
ref $node and UNIVERSAL::can($node, "children")
ref $node UNIVERSAL::can($node, "children")
Truth table66%2/3
A and B
A= ref $nodeB= UNIVERSAL::can($node, "children")
ABresult
0X0
100
111
267
268 148 ▶ $self->{CHANGES}++ if $self->pattern->(
Branch: 1 branch
BranchTrueFalseTotal
if $self->pattern->($_[0], $_[1], $index, $self) 33 115 148
269 $_[0], # Node being visited
270 $_[1], # Father of this node
271 $index, # Index of this node in @Father->children
272 $self, # The YATW pattern object
273 );
274 148 return;
275 };
276
277 # Else, is not a leaf and is a regular Parse::Eyapp::Node
278 # Recursively transform subtrees
279 841 my $i = 0;
280 841 for (@{$node->{children}}) {
281 882 $self->s($_, $_[0], $i);
282 882 $i++;
283 }
284
285 841 my $number_of_changes = $self->{CHANGES};
286 # Now is safe to delete children nodes that are no longer needed
287 841 $self->make_delete_effective($node);
288  
289 # Safely do pending jobs for this node
290 841 $self->do_pending_tasks($node);
291  
292 #node , father, childindex, and ...
293 #Change YATW object to be the first argument?
294 841 ▶ if ($self->pattern->($_[0], $_[1], $index, $self)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pattern->($_[0], $_[1], $index, $self)) 52 789 841
295 52 $self->{CHANGES}++;
296 }
297 841 shift @{$self->{NODE}};
298 }
299  
300 64 1;
301  
« Eyapp/Options.pm ↑ Summary make_eyapp_c_tests.pl »