File Coverage

blib/lib/Parse/Eyapp/YATW.pm
Criterion Covered Total %
statement 132 141 93.6
branch 27 40 67.5
condition 5 6 83.3
subroutine 21 23 91.3
pod 0 15 0.0
total 185 225 82.2


line stmt bran cond sub pod time code
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     64   388 use strict;
  64         134  
  64         1490  
10 64     64   275 use warnings;
  64         119  
  64         1421  
11 64     64   273 use Carp;
  64         118  
  64         2721  
12 64     64   23580 use Data::Dumper;
  64         238649  
  64         3422  
13 64     64   435 use List::Util qw(first);
  64         145  
  64         73362  
14              
15             sub firstval(&@) {
16 0     0 0 0 my $handler = shift;
17            
18 0         0 return (grep { $handler->($_) } @_)[0]
  0         0  
19             }
20              
21             sub lastval(&@) {
22 40     40 0 85 my $handler = shift;
23            
24 40         98 return (grep { $handler->($_) } @_)[-1]
  147         394  
25             }
26              
27             sub valid_keys {
28 64     64 0 209 my %valid_args = @_;
29              
30 64         214 my @valid_args = keys(%valid_args);
31 64         153 local $" = ", ";
32 64         289 return "@valid_args"
33             }
34              
35             sub invalid_keys {
36 63     63 0 107 my $valid_args = shift;
37 63         105 my $args = shift;
38              
39 63     122   403 return (first { !exists($valid_args->{$_}) } keys(%$args));
  122         416  
40             }
41              
42              
43             our $VERSION = $Parse::Eyapp::Driver::VERSION;
44              
45             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             my %_new_yatw = (
56             PATTERN => 'CODE',
57             NAME => 'STRING',
58             );
59              
60             my $validkeys = valid_keys(%_new_yatw);
61              
62             sub new {
63 63     63 0 147 my $class = shift;
64 63         225 my %args = @_;
65              
66 63 50       242 croak "Error. Expected a code reference when building a tree walker. " unless (ref($args{PATTERN}) eq 'CODE');
67 63 50       211 if (defined($a = invalid_keys(\%_new_yatw, \%args))) {
68 0         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         231 $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         143 $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         161 $args{NODE} = [];
92              
93 63         280 bless \%args, $class;
94             }
95              
96             sub buildpatterns {
97 30     30 0 102 my $class = shift;
98            
99 30         62 my @family;
100 30         185 while (my ($n, $p) = splice(@_, 0,2)) {
101 59         221 push @family, Parse::Eyapp::YATW->new(NAME => $n, PATTERN => $p);
102             }
103 30 50       256 return wantarray? @family : $family[0];
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             sub m {
115 9     9 0 28 my $p = shift(); # pattern YATW object
116 9         25 my $t = shift; # tree
117 9         34 my $pattern = $p->{PATTERN}; # CODE ref
118              
119             # References to the found nodes are stored in @stack
120 9         68 my @stack = ( Parse::Eyapp::Node::Match->new(node=>$t, depth=>0, dewey => "") );
121 9         22 my @results;
122 9         22 do {
123 98         201 my $n = CORE::shift(@stack);
124 98         432 my %n = %$n;
125              
126 98         232 my $dewey = $n->{dewey};
127 98         172 my $d = $n->{depth};
128 98 100       2393 if ($pattern->($n{node})) {
129 40         134 $n->{family} = [ $p ];
130 40         111 $n->{patterns} = [ 0 ];
131              
132             # Is at this time that I have to compute the father
133 40     147   220 my $f = lastval { $dewey =~ m{^$_->{dewey}}} @results;
  147         1379  
134 40         165 $n->{father} = $f;
135             # ... and children
136 40 100       129 push @{$f->{children}}, $n if defined($f);
  31         85  
137 40         104 push @results, $n;
138             }
139 98         198 my $k = 0;
140             CORE::unshift @stack,
141             map {
142 89         191 local $a;
143 89         367 $a = Parse::Eyapp::Node::Match->new(node=>$_, depth=>$d+1, dewey=>"$dewey.$k" );
144 89         183 $k++;
145 89         412 $a;
146 98         326 } $n{node}->children();
147             } while (@stack);
148              
149 9 50       62 return wantarray? @results : $results[0];
150             }
151              
152             ######################### getter-setter for YATW objects ###########################
153              
154             sub pattern {
155 989     989 0 1410 my $self = shift;
156 989 50       2008 $self->{PATTERN} = shift if (@_);
157 989         12711 return $self->{PATTERN};
158             }
159              
160             sub name {
161 0     0 0 0 my $self = shift;
162 0 0       0 $self->{NAME} = shift if (@_);
163 0         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             sub delete {
177 36     36 0 179 my $self = shift;
178              
179 36         134 bless $self->{NODE}[0], 'Parse::Eyapp::Node::DELETE';
180             }
181            
182             sub make_delete_effective {
183 841     841 0 1175 my $self = shift;
184 841         1198 my $node = shift;
185              
186 841         1862 my $i = -1+$node->children;
187 841         2013 while ($i >= 0) {
188 876 100       2289 if (UNIVERSAL::isa($node->child($i), 'Parse::Eyapp::Node::DELETE')) {
189 36 50       52 $self->{CHANGES}++ if defined(splice(@{$node->{children}}, $i, 1));
  36         107  
190             }
191 876         1956 $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             sub unshift {
202 3     3 0 24 my ($self, $node, $k) = @_;
203 3 50       10 $k = 1 unless defined($k); # father by default
204              
205 3         6 my $ancestor = ${$self->{NODE}}[$k];
  3         7  
206 3 50       9 croak "unshift: does not exist ancestor $k of node ".Dumper(${$self->{NODE}}[0]) unless defined($ancestor);
  0         0  
207              
208             # Stringification of $ancestor. Hope it works
209             # operation, node to insert,
210 3         5 push @{$self->{PENDING_TASKS}{$ancestor}}, ['unshift', $node ];
  3         19  
211             }
212              
213             sub insert_before {
214 1     1 0 6 my ($self, $node) = @_;
215              
216 1         2 my $father = ${$self->{NODE}}[1];
  1         3  
217 1 50       3 croak "insert_before: does not exist father of node ".Dumper(${$self->{NODE}}[0]) unless defined($father);
  0         0  
218              
219             # operation, node to insert, before this node
220 1         2 push @{$self->{PENDING_TASKS}{$father}}, ['insert_before', $node, ${$self->{NODE}}[0] ];
  1         5  
  1         5  
221             }
222              
223             sub _delayed_insert_before {
224 1     1   3 my ($father, $node, $before) = @_;
225              
226 1         2 my $i = 0;
227 1         4 for ($father->children()) {
228 3 100       90 last if ($_ == $before);
229 2         6 $i++;
230             }
231 1         2 splice @{$father->{children}}, $i, 0, $node;
  1         4  
232             }
233              
234             sub do_pending_tasks {
235 841     841 0 1175 my $self = shift;
236 841         1123 my $node = shift;
237              
238 841         1537 my $mytasks = $self->{PENDING_TASKS}{$node};
239 841   100     2283 while ($mytasks and (my $job = shift @{$mytasks})) {
  7         30  
240 4         10 my @args = @$job;
241 4         8 my $task = shift @args;
242              
243             # change this for a jump table
244 4 100       15 if ($task eq 'unshift') {
    50          
245 3         6 CORE::unshift(@{$node->{children}}, @args);
  3         7  
246 3         10 $self->{CHANGES}++;
247             }
248             elsif ($task eq 'insert_before') {
249 1         4 _delayed_insert_before($node, @args);
250 1         5 $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             sub s {
259 989     989 0 1483 my $self = shift;
260 989 50       2193 my $node = $_[0] or croak("Error. Method __PACKAGE__::s requires a node");
261 989         1342 CORE::unshift @{$self->{NODE}}, $_[0];
  989         1784  
262             # father is $_[1]
263 989         1394 my $index = $_[2];
264              
265             # If is not a reference or can't children then simply check the matching and leave
266 989 100 66     4726 if (!ref($node) or !UNIVERSAL::can($node, "children")) {
267            
268 148 100       324 $self->{CHANGES}++ if $self->pattern->(
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         569 return;
275             };
276            
277             # Else, is not a leaf and is a regular Parse::Eyapp::Node
278             # Recursively transform subtrees
279 841         1310 my $i = 0;
280 841         1129 for (@{$node->{children}}) {
  841         1544  
281 882         2127 $self->s($_, $_[0], $i);
282 882         1565 $i++;
283             }
284            
285 841         1642 my $number_of_changes = $self->{CHANGES};
286             # Now is safe to delete children nodes that are no longer needed
287 841         2141 $self->make_delete_effective($node);
288              
289             # Safely do pending jobs for this node
290 841         1947 $self->do_pending_tasks($node);
291              
292             #node , father, childindex, and ...
293             #Change YATW object to be the first argument?
294 841 100       1830 if ($self->pattern->($_[0], $_[1], $index, $self)) {
295 52         731 $self->{CHANGES}++;
296             }
297 841         3225 shift @{$self->{NODE}};
  841         1548  
298             }
299              
300             1;
301