| 1 | 50 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 50 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | =head1 NAME | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | Attean::TreeRewriter - Walk and rewrite subtrees | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 VERSION | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | This document describes Attean::TreeRewriter version 0.040 | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use v5.14; | ||||||||||||||||||||||||||||
| 15 | use Attean; | ||||||||||||||||||||||||||||
| 16 | my $w = Attean::TreeRewriter->new(); | ||||||||||||||||||||||||||||
| 17 | my ($rewritten, $tree) = $w->rewrite($tree, $thunk); | ||||||||||||||||||||||||||||
| 18 | if ($rewritten) { | ||||||||||||||||||||||||||||
| 19 | ... | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | The Attean::TreeRewriter class walks the nodes of query trees and rewrites | ||||||||||||||||||||||||||||
| 25 | sub-trees based on handlers that have been registered prior to rewriting. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 ROLES | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | None. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 METHODS | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =over 4 | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 50 | package Attean::TreeRewriter 0.040 { | |||||||||||||||||||||||||||
| 38 | 50 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 50 | ▶ | use Types::Standard qw(CodeRef ArrayRef Str); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 50 | ▶ | use Scalar::Util qw(blessed refaddr); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 50 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 50 | with 'MooX::Log::Any'; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 2 | ▶ | has types => (is => 'rw', isa => ArrayRef[Str], default => sub { ['Attean::API::DirectedAcyclicGraph'] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 12 | ▶ | has pre_handlers => (is => 'rw', isa => ArrayRef[CodeRef], default => sub { [] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =item C<< register_pre_handler( \&code ) >> | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Register a handler that will be called for each sub-tree during tree rewriting. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | The function will be called as C<< &code( $tree, $parent_node, $thunk ) >> where | ||||||||||||||||||||||||||||
| 52 | C<< $thunk >> is an opaque value passed to C<< rewrite >>. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | The function must return a list C<< ($handled, $descend, $rewritten) >>. | ||||||||||||||||||||||||||||
| 55 | C<< $handled >> is a boolean indicating whether the handler function rewrote | ||||||||||||||||||||||||||||
| 56 | the sub-tree, which is returned as C<< $rewritten >>. The C<< $descend >> | ||||||||||||||||||||||||||||
| 57 | boolean value indicates whether the the tree rewriting should continue downwards | ||||||||||||||||||||||||||||
| 58 | in the tree. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 12 | ▶ | sub register_pre_handler { | ||||||||||||||||||||||||||
|
Subroutine
register_pre_handler: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 64 | 12 | my $code = shift; | |||||||||||||||||||||||||||
| 65 | 12 | push(@{ $self->pre_handlers }, $code); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 41 | ▶ | sub _fire_pre_handlers { | ||||||||||||||||||||||||||
|
Subroutine
_fire_pre_handlers: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 69 | 41 | my $self = shift; | |||||||||||||||||||||||||||
| 70 | 41 | my ($t, $parent, $thunk) = @_; | |||||||||||||||||||||||||||
| 71 | 41 | my $main_descend = 0; | |||||||||||||||||||||||||||
| 72 | 41 | foreach my $cb (@{ $self->pre_handlers }) { | |||||||||||||||||||||||||||
| 73 | 41 | my ($handled, $descend, $rewritten) = $cb->($t, $parent, $thunk); | |||||||||||||||||||||||||||
| 74 | 41 | ▶ | unless (defined($descend)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | $descend = 1; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 41 | ▶ | if ($handled) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 78 | 9 | return ($descend, $rewritten); | |||||||||||||||||||||||||||
| 79 | } elsif ($descend) { | ||||||||||||||||||||||||||||
| 80 | 32 | $main_descend = 1; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 32 | return ($main_descend, undef); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =item C<< rewrite( $tree, $thunk, \%seen, $parent ) >> | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Rewrites the given C<< $tree >> using the registered handler functions. | ||||||||||||||||||||||||||||
| 89 | C<< $thunk >> is passed through to each handler function. | ||||||||||||||||||||||||||||
| 90 | C<< %seen >> is currently unused. | ||||||||||||||||||||||||||||
| 91 | C<< $parent >> is passed through to the handler functions as the value of the | ||||||||||||||||||||||||||||
| 92 | pseudo-parent tree node for C<< $tree >>. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Returns a list C<< ($handled, $tree) >> with C<< $handled >> indicating whether | ||||||||||||||||||||||||||||
| 95 | rewriting was performed, with the corresponding rewritten C<< $tree >>. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 58 | ▶ | sub rewrite { | ||||||||||||||||||||||||||
|
Subroutine
rewrite: called CC=21 SCAR=30.6
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 58 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 58 | my $tree = shift; | |||||||||||||||||||||||||||
| 102 | 58 | my $thunk = shift; | |||||||||||||||||||||||||||
| 103 | 58 | ▶ | my $seen = shift || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 104 | 58 | my $parent = shift; | |||||||||||||||||||||||||||
| 105 | 58 | my $ok = 0; | |||||||||||||||||||||||||||
| 106 | # if ($seen->{ refaddr($tree) }++) { | ||||||||||||||||||||||||||||
| 107 | # return (0, $tree); | ||||||||||||||||||||||||||||
| 108 | # } | ||||||||||||||||||||||||||||
| 109 | 58 | foreach my $type (@{ $self->types }) { | |||||||||||||||||||||||||||
| 110 | 113 | ▶ | if (blessed($tree) and $tree->does($type)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($tree) and $tree->does($type)
MC/DC100%2/2
builtin::blessed($tree) and $tree->does($type)
builtin::blessed($tree)
$tree->does($type)
Truth table100%3/3
A and B
A= builtin::blessed($tree)B= $tree->does($type)
| |||||||||||||||||||||||||||||
| 111 | 41 | $ok++; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 58 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 17 | $self->log->debug(ref($tree) . ' does not conform to any rewrite roles'); | |||||||||||||||||||||||||||
| 116 | 17 | return (0, $tree); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 41 | my ($descend, $rewritten) = $self->_fire_pre_handlers($tree, $parent, $thunk); | |||||||||||||||||||||||||||
| 120 | 41 | ▶ | if ($rewritten) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 9 | ▶ | if (refaddr($rewritten) == refaddr($tree)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | return (0, $tree); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 9 | ▶ | if ($descend) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | (undef, my $rewritten2) = $self->rewrite($rewritten, $thunk, $seen, $parent); | |||||||||||||||||||||||||||
| 126 | 0 | my $changed = (refaddr($rewritten) != refaddr($rewritten2)); | |||||||||||||||||||||||||||
| 127 | 0 | return ($changed, $rewritten2); | |||||||||||||||||||||||||||
| 128 | } else { | ||||||||||||||||||||||||||||
| 129 | 9 | return (1, $rewritten); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 32 | ▶ | if ($descend) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 32 | my @children; | |||||||||||||||||||||||||||
| 134 | my %attributes; | ||||||||||||||||||||||||||||
| 135 | 32 | my $changed = 0; | |||||||||||||||||||||||||||
| 136 | 32 | ▶ | if ($tree->does('Attean::API::DirectedAcyclicGraph')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 28 | my @c = @{ $tree->children }; | |||||||||||||||||||||||||||
| 138 | 28 | foreach my $i (0 .. $#c) { | |||||||||||||||||||||||||||
| 139 | 17 | my $p = $c[$i]; | |||||||||||||||||||||||||||
| 140 | 17 | my ($childchanged, $child) = $self->rewrite($p, $thunk, $seen, $tree); | |||||||||||||||||||||||||||
| 141 | 17 | ▶ | push(@children, $childchanged ? $child : $p); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 17 | ▶ | if ($childchanged) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 11 | $self->log->debug("Child $p changed for parent $tree"); | |||||||||||||||||||||||||||
| 144 | 11 | $changed = 1; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 32 | ▶ | if ($tree->can('tree_attributes')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 24 | foreach my $attr ($tree->tree_attributes) { | |||||||||||||||||||||||||||
| 151 | 29 | my $p = $tree->$attr(); | |||||||||||||||||||||||||||
| 152 | 29 | ▶ | if (ref($p) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 7 | my @patterns; | |||||||||||||||||||||||||||
| 154 | 7 | foreach my $pp (@$p) { | |||||||||||||||||||||||||||
| 155 | # warn "- $attr: $pp\n"; | ||||||||||||||||||||||||||||
| 156 | 7 | my ($childchanged, $child) = $self->rewrite($pp, $thunk, $seen, $tree); | |||||||||||||||||||||||||||
| 157 | 7 | ▶ | if ($childchanged) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 5 | $changed = 1; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 7 | push(@patterns, $child); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 7 | $attributes{$attr} = \@patterns; | |||||||||||||||||||||||||||
| 163 | } else { | ||||||||||||||||||||||||||||
| 164 | # warn "- $attr: $p\n"; | ||||||||||||||||||||||||||||
| 165 | 22 | my ($childchanged, $child) = $self->rewrite($p, $thunk, $seen, $tree); | |||||||||||||||||||||||||||
| 166 | 22 | $attributes{$attr} = $child; | |||||||||||||||||||||||||||
| 167 | 22 | ▶ | if ($childchanged) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 6 | $changed = 1; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 32 | ▶ | if ($changed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 16 | my $class = ref($tree); | |||||||||||||||||||||||||||
| 175 | 16 | $rewritten = $class->new( %attributes, children => \@children ); | |||||||||||||||||||||||||||
| 176 | # (undef, $rewritten) = $self->rewrite($rewritten, $thunk, $seen, $parent); | ||||||||||||||||||||||||||||
| 177 | 16 | return (1, $rewritten); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 16 | return (0, $tree); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 50 | 1; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | __END__ | ||||||||||||||||||||||||||||