Topiary.pm

filter: Statementstmt 89.5% 60 / 67 Branchbran 78.5% 22 / 28 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 88.2% 98 / 111 CC 17 SCAR 20.4
CC
17
Cov
87%
topiary
14.5
_consolidate_hash
4.0
delegation_class
3.0
CRAP 7.7
↑ Summary Topiary/Base.pm »
1 package Parse::RecDescent::Topiary;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 $VERSION = '0.05';
9 2 @ISA = qw(Exporter);
10 2 @EXPORT = qw(topiary);
11 2 @EXPORT_OK = qw(topiary delegation_class);
12 2 %EXPORT_TAGS = ( all => [qw/topiary delegation_class/] );
13 }
14  
15 =head1 NAME
16  
17 Parse::RecDescent::Topiary - tree surgery for Parse::RecDescent autotrees
18  
19 =head1 SYNOPSIS
20  
21 use Parse::RecDescent::Topiary;
22 my $parser = Parse::RecDescent->new($grammar);
23 ...
24 my $tree = topiary(
25 tree => $parser->mainrule,
26 namespace => 'MyModule::Foo',
27 ucfirst => 1
28 );
29  
30 =head1 DESCRIPTION
31  
32 L<Parse::RecDescent> has a mechanism for automatically generating parse trees.
33 What this does is to bless each resulting node into a package namespace
34 corresponding to the rule. This might not be desirable, for a couple of
35 reasons:
36  
37 =over 4
38  
39 =item *
40  
41 You probably don't want to pollute the top-level namespace with packages,
42 and you probably don't want your grammar rules to be named according to CPAN
43 naming conventions. Also, the namespaces could collide if an application has
44 two different RecDescent grammars, that share some rule names.
45  
46 =item *
47  
48 Parse::RecDescent merely blesses the data structures. It does not call a
49 constructor. Parse::RecDescent::Topiary calls C<new> for each class. A base
50 class, L<Parse::RecDescent::Topiary::Base> is provided in the distribution,
51 to construct hashref style objects. The user can always supply their own -
52 inside out or whatever.
53  
54 =back
55  
56 =head2 C<topiary>
57  
58 This is a function which recursively rebuilds an autotree returned by
59 L<Parse::RecDescent>, using constructors for each node.
60  
61 This exported function takes a list of option / value pairs:
62  
63 =over 4
64  
65 =item C<tree>
66  
67 Pass in the resulting autotree returned by a Parse::RecDescent object.
68  
69 =item C<namespace>
70  
71 If not specified, topiary will not use objects in the new parse tree. This
72 can be specified either as a single prefix value, or a list of namespaces
73 as an arrayref.
74  
75 As the tree is walked, each blessed node is used to form a candidate
76 class name, and if such a candidate class has a constructor, i.e. if
77 C<Foo::Bar::Token-E<gt>can('new')> returns true, this will be used to
78 construct the new node object (see L<delegation_class>).
79  
80 If a list of namespaces are given, each one is tried in turn, until a
81 C<new> method is found. If no constructor is found, the node is built
82 as a data structure, i.e. it is not blessed or constructed.
83  
84 =item C<ucfirst>
85  
86 Optional flag to upper case the first character of the rule when forming the
87 class name.
88  
89 =item C<consolidate>
90  
91 Optional flag that causes topiary to reduce the nesting, unambiguously, of
92 optionally quantified productions. The production foo(?) causes generation
93 of the hash entry 'foo(?)' containing an arrayref of either 0 or 1 elements
94 depending whether foo was present or not in the input string.
95  
96 If consolidate is a true value, topiary processes this entry, and either
97 generates a hash entry foo => foo_object if foo was present, or does not
98 generate a hash entry if it was absent.
99  
100 =item C<args>
101  
102 Optional user arguments passed in. These are available to the constructors,
103 and the default constructor will put them into the new objects as
104 $self->{__ARGS__}.
105  
106 =back
107  
108 =head2 C<delegation_class>
109  
110 @class_list = qw(Foo::Bar Foo::Baz);
111 my $class = delegation_class( 'Dongle', \@class_list, 'wiggle' );
112  
113 This subroutine is not exported by default, and is used internally by topiary.
114 C<$class> is set to C<Foo::Bar::Dongle> if
115 C<Foo::Bar::Dongle-E<gt>can('wiggle')> or set to C<Foo::Baz::Dongle> if
116 C<Foo::Baz::Dongle-E<gt>can('wiggle')> or return undef if no match is found.
117  
118 =head1 BUGS
119  
120 Please report bugs to http://rt.cpan.org
121  
122 =head1 AUTHOR
123  
124 Ivor Williams
125 CPAN ID: IVORW
126
127 ivorw@cpan.org
128
129  
130 =head1 COPYRIGHT
131  
132 This program is free software; you can redistribute
133 it and/or modify it under the same terms as Perl itself.
134  
135 The full text of the license can be found in the
136 LICENSE file included with this module.
137  
138  
139 =head1 SEE ALSO
140  
141 L<Parse::RecDescent>.
142  
143 =cut
144  
145 2 ▶use Params::Validate::Dummy qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
146 2 ▶use Module::Optional qw(Params::Validate :all);
Subroutine BEGIN: called CC=1 SCAR=0.0
147 2 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
148  
149 62 ▶sub topiary {
Subroutine topiary: called CC=12 SCAR=26.8
Pod documented
150 62 my %par = validate(
151 @_,
152 { tree => 1,
153 namespace => {
154 regex => qr/\w+(\:\:\w+)*/,
155 type => SCALAR | ARRAYREF,
156 default => '',
157 },
158 ucfirst => 0,
159 args => 0,
160 consolidate => 0,
161 }
162 );
163  
164 62 my $tree = $par{tree};
165 62 my $namespace = $par{namespace};
166 62 ▶ my @ns = ref($namespace) ? @$namespace : ($namespace);
Branch: 1 branch
BranchTrueFalseTotal
ref $namespace ? : 25 37 62
167 62 my $origpkg = blessed $tree;
168 62 my $class;
169 62 ▶ if ($origpkg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($origpkg) 27 35 62
170 27 ▶ $origpkg = ucfirst $origpkg if $par{ucfirst};
Branch: 1 branch
BranchTrueFalseTotal
if $par{"ucfirst"} 16 11 27
171 27 $class = delegation_class( $origpkg, \@ns, 'new' );
172 }
173  
174 62 ▶ my $type = reftype($tree) || '';
Condition100%2/2
builtin::reftype($tree) || ''
l!l
2735
MC/DC100%1/1
builtin::reftype($tree) || ''
builtin::reftype($tree)
Truth table100%2/2
A
A= builtin::reftype($tree)
Aresult
11
00
175 62 my $rv;
176 62 ▶ if ( $type eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 0 62 62
elsif ($type eq 'HASH') { } 27 35 62
177 0 my @proto = map { topiary( %par, tree => $_ ) } @$tree;
178 0 ▶ if ($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 0 0 0
179 0 ▶ if ( exists $par{args} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $par{"args"}) 0 0 0
180 0 push @proto, __ARGS__ => $par{args};
181 }
182 0 $rv = $class->new(@proto);
183 }
184 else {
185 0 $rv = \@proto;
186 }
187 }
188 elsif ( $type eq 'HASH' ) {
189  
190 #my %proto = map { $_, topiary( %par, tree => $tree->{$_} ) }
191 27 my %proto = map { _consolidate_hash( $_, $tree->{$_}, \%par ) }
192 keys %$tree;
193 27 ▶ if ($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 21 6 27
194 21 ▶ if ( exists $par{args} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $par{"args"}) 5 16 21
195 5 $proto{__ARGS__} = $par{args};
196 }
197 21 $rv = $class->new(%proto);
198 }
199 else {
200 6 $rv = \%proto;
201 }
202 }
203 else {
204 35 ▶ $rv = $class ? $class->new($tree) : $tree;
Branch: 1 branch
BranchTrueFalseTotal
$class ? : 0 35 35
205 }
206 62 return $rv;
207 }
208  
209 59 ▶sub _consolidate_hash {
Subroutine _consolidate_hash: called CC=4 SCAR=13.9
210 59 my ( $key, $tree, $args ) = @_;
211  
212 59 ▶ return $key, topiary( %$args, tree => $tree ) unless $args->{consolidate};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"consolidate"} 48 11 59
213 11 ▶ if ( $key =~ /(\w+)\(\?\)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /(\w+)\(\?\)$/) 2 9 11
214 2 ▶ return () unless @$tree;
Branch: 1 branch
BranchTrueFalseTotal
unless @$tree 1 1 2
215 1 return $1, topiary( %$args, tree => $tree->[0] );
216 }
217 9 return $key, topiary( %$args, tree => $tree );
218 }
219  
220 27 ▶sub delegation_class {
Subroutine delegation_class: called CC=3 SCAR=11.0
Pod documented
221 27 my ( $node, $plist, $method ) = @_;
222  
223 27 for my $prefix (@$plist) {
224 37 my $pclass = $prefix . '::' . $node;
225 37 ▶ next unless $pclass->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $pclass->can($method) 16 21 37
226 21 return $pclass;
227 }
228 6 undef;
229 }
230  
231 2 1;
232  
233 # The preceding line will help the module return a true value
234  
↑ Summary Topiary/Base.pm »