blib/lib/B/DeparseTree/Node.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 132 SCAR 53.5
CC
14
Cov
0%
new
156.0
maybe_parens
12.0
parens_test
2.0
CRAP 210.0
1 # The underlying node structure of the abstract code tree built
2 # that is built.
3 # Copyright (c) 2015, 2018 Rocky Bernstein
4 use strict; use warnings;
5 package B::DeparseTree::Node;
6 use Carp;
7 use Config;
8 my $is_cperl = $Config::Config{usecperl};
9  
10 use Hash::Util qw[ lock_hash ];
11  
12 # Set of unary precedences
13 our %UNARY_PRECEDENCES = (
14 4 => 1, # right not
15 16 => 'sub, %, @', # "sub", "%", "@'
16 21 => '~', # steal parens (see maybe_parens_unop)
17 );
18  
19 unless ($is_cperl) {
20 lock_hash %UNARY_PRECEDENCES;
21 }
22  
23  
24 our $VERSION = '3.2.0';
25 our @ISA = qw(Exporter);
26 our @EXPORT = qw(
27 new($$$$)
28 parens_test($$$)
29 %UNARY_PRECEDENCES
30 update_other_ops($$)
31 );
32  
33 =head2 Node structure
34  
35 Fields in a node structure:
36  
37 =over
38  
39 *item B<type>
40  
41 The string name for the node. It can be used to determine the overall
42 structure. For example a 'binop' node will have a I<body> with a node
43 left-hand side, the string operation name and a I<body> right-hand
44 side. Right now the type names are a little funky, but over time I
45 hope these will less so.
46  
47 * item B<sep> (optional)
48  
49 A string indicating how to separate the the strings extracted from the
50 C<texts> field. The field is subject to format expansion. In particular
51 tt can have '%;' in it to indicate we are separating statements.
52 the body.
53  
54 * item B<texts>
55  
56 A reference to a list containing either:
57  
58 =over
59  
60 * item a tuple with a strings, and a op address
61 * a DeparseTreee::Node object
62  
63 =back
64  
65 * item B<text>
66  
67 Text representation of the node. Eventually this will diasppear
68 and, you'll use one of the node-to-string conversion routines.
69  
70 * item B<maybe_parens>
71  
72 If this node is embedded in the parent above, whether we need to add parenthesis.
73 The keys is a hash ref hash reference
74  
75 =over
76  
77 =item B<context>
78  
79 A number passed from the parent indicating its precedence context that
80 the expression is embedded it.
81  
82 =item B<precedence>
83  
84 A number as determined by the operator at this level.
85  
86 =item B<parens>
87  
88 'true' if we should to add parenthesis based on I<context> and
89 I<precedence> values; '' if not. We don't nest equal precedence
90 for unuary ops. The unary op precedence is given by
91 UNARY_OP_PRECEDENCE
92  
93 =back
94  
95 =back
96 =cut
97  
98  
99 sub parens_test($$$)
100 {
101 my ($obj, $cx, $prec) = @_;
102 return ($prec < $cx
103 # Unary ops which nest just fine
104 or ($prec == $cx && !exists $UNARY_PRECEDENCES{$cx}));
105 }
106  
107 sub new($$$$$)
108 {
109 my ($class, $op, $deparse, $data, $sep, $type, $opts) = @_;
110 my $addr = -1;
111 if (ref($op)) {
112 if (ref($op) eq 'B::DeparseTree') {
113 # use Enbugger 'trepan'; Enbugger->stop;
114 Carp::confess("Rocky got the order of \$self, and \$op confused again");
115 $addr = -2;
116 } else {
117 eval { $addr = $$op };
118 }
119 }
120 my $self = bless {
121 addr => $addr,
122 op => $op,
123 deparse => $deparse,
124 type => $type,
125 }, $class;
126  
127 $self->{sep} = $sep if defined $sep;
128 if (ref($data)) {
129 # Passed in a ref ARRAY
130 $self->{texts} = $data;
131 $self->{text} = $deparse->combine2str($sep, $data) if defined $sep;
132 } elsif (defined $data) {
133 # Passed in a string
134 $self->{text} = $data;
135 } else {
136 # Leave {text} and {texts} uninitialized
137 }
138  
139 foreach my $optname (qw(other_ops parent_ops child_pos maybe_parens
140 omit_next_semicolon position)) {
141 $self->{$optname} = $opts->{$optname} if $opts->{$optname};
142 }
143 if ($opts->{maybe_parens}) {
144 my ($obj, $context, $precedence) = @{$opts->{maybe_parens}};
145 my $parens = parens_test($obj, $context, $precedence);
146 $self->{maybe_parens} = {
147 context => $context,
148 precedence => $precedence,
149 force => $obj->{'parens'},
150 parens => $parens ? 'true' : ''
151 };
152 $self->{text} = "($self->{text})" if exists $self->{text} and $parens;
153 }
154 if ($opts->{prev_expr}) {
155 $self->{prev_expr} = $opts->{prev_expr};
156 }
157 return $self;
158 }
159  
160 # Possibly add () around $text depending on precedence $prec and
161 # context $cx. We return a string.
162 sub maybe_parens($$$$)
163 {
164 my($self, $info, $cx, $prec) = @_;
165 if (parens_test($info, $cx, $prec)) {
166 $info->{text} = $self->combine('', "(", $info->{text}, ")");
167 # In a unop, let parent reuse our parens; see maybe_parens_unop
168 if ($cx == 16) {
169 $info->{parens} = 'reuse';
170 } else {
171 $info->{parens} = 'true';
172 }
173 return $info->{text};
174 } else {
175 $info->{parens} = '';
176 return $info->{text};
177 }
178 }
179  
180 # Update $self->{other_ops} to add $info
181 sub update_other_ops($$)
182 {
183 my ($self, $info) = @_;
184 $self->{other_ops} ||= [];
185 my $other_ops = $self->{other_ops};
186 push @{$other_ops}, $info;
187 $self->{other_ops} = $other_ops;
188 }
189  
190 # Demo code
191 unless(caller) {
192 my $old_pkg = __PACKAGE__;
193 package B::DeparseTree::NodeDemo;
194 sub new($) {
195 my ($class) = @_;
196 bless {}, $class;
197 }
198 sub combine2str($$$) {
199 my ($self, $sep, $data) = @_;
200 join($sep, @$data);
201 }
202 my $deparse = __PACKAGE__->new();
203 my $node = $old_pkg->new('op', $deparse, ['X'], 'test', {});
204 print $node->{text}, "\n";
205 }
206 1;