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

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 142 CC 14 SCAR 34.1
CC
14
Cov
0%
combine
56.0
new
42.0
maybe_parens
12.0
CRAP 30.2
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  
8 our($VERSION, @EXPORT, @ISA);
9 $VERSION = '1.0.0';
10 @ISA = qw(Exporter);
11 @EXPORT = qw(new($$$$ from_str($$$) from_list($$$) parens_test($$$)));
12  
13 =head2 Node structure
14  
15 Fields in a node structure:
16  
17 =over
18  
19 *item B<type>
20  
21 The string name for the node. It can be used to determine the overall
22 structure. For example a 'binop' node will have a I<body> with a node
23 left-hand side, the string operation name and a I<body> right-hand
24 side. Right now the type names are a little funky, but over time I
25 hope these will less so.
26  
27 * item B<sep>
28  
29 A string indicating how to separate the the strings derived from
30 the body. To indicate statement separation, the separator is ';' and the
31 B<indent_type> is '{'. The indent program can also use the type
32 to help out with statement boundaries.
33  
34 * item B<texts>
35  
36 A reference to a list containing either strings, a Node references, or Hash references
37 containing the keys I<sep> and a I<body>.
38  
39 * item B<body>
40  
41 A reference to a list of a Node references. Eventually this will this and
42 texts will be merged.
43  
44 * item B<text>
45  
46 Text representation of the node until. Eventually this will diasppear
47 and, you'll use one of the node-to-string conversion routines.
48  
49 * item B<maybe_parens>
50  
51 If this node is embedded in the parent above, whether we need to add parenthesis.
52 The keys is a hash ref hash reference
53  
54 =over
55  
56 =item B<context>
57  
58 A number passed from the parent indicating its precidence context
59  
60 =item B<precidence>
61  
62 A number as determined by the operator at this level.
63  
64 =item B<parens>
65  
66 'true' if we should to add parenthesis based on I<context> and
67 I<precidence> values; '' if not.
68  
69 =back
70  
71 =back
72 =cut
73  
74  
75 sub parens_test($$$)
76 {
77 my ($obj, $cx, $prec) = @_;
78 return ($prec < $cx or
79 $obj->{'parens'} or
80 # unary ops nest just fine
81 $prec == $cx and $cx != 4 and $cx != 16 and $cx != 21)
82 }
83  
84 sub new($$$$$)
85 {
86 my ($class, $op, $deparse, $texts, $sep, $type, $opts) = @_;
87 my $addr = -1;
88 if (ref($op)) {
89 if (ref($op) eq 'B::DeparseTree') {
90 # use Enbugger 'trepan'; Enbugger->stop;
91 Carp::confess("Rocky got the order of \$self, and \$op confused again");
92 $addr = -2;
93 } else {
94 eval { $addr = $$op };
95 }
96 }
97 my $self = bless {
98 addr => $addr,
99 op => $op,
100 deparse => $deparse,
101 texts => $texts,
102 type => $type,
103 sep => $sep,
104 }, $class;
105  
106 $self->{text} = $self->combine($sep, $texts);
107  
108 foreach my $optname (qw(other_ops body parent_ops child_pos)) {
109 $self->{$optname} = $opts->{$optname} if $opts->{$optname};
110 }
111 if ($opts->{maybe_parens}) {
112 my ($obj, $context, $precidence) = @{$opts->{maybe_parens}};
113 my $parens = parens_test($obj, $context, $precidence);
114 $self->{maybe_parens} = {
115 context => $context,
116 precidence => $precidence,
117 force => $obj->{'parens'},
118 parens => $parens ? 'true' : ''
119 };
120 }
121 return $self;
122 }
123  
124 # Simplified class constructors
125 sub from_str($$$$$)
126 {
127 my ($op, $self, $str, $type, $opts) = @_;
128 __PACKAGE__->new({body=>[$str]}, '', $type, $opts);
129 }
130  
131 sub from_list($$$$$$)
132 {
133 my ($op, $self, $list, $sep, $type, $opts) = @_;
134 __PACKAGE__->new({body=>$list}, $sep, $type, $opts);
135 }
136  
137 sub combine($$$)
138 {
139 my ($self, $sep, $items) = @_;
140 # FIXME: loop over $item, testing type.
141 return $items unless ref $items;
142 Carp::confess("should be a reference to a hash: is $items") unless
143 ref $items eq 'ARRAY';
144 my $result = '';
145 foreach my $item (@{$items}) {
146 my $add;
147 if (ref $item) {
148 if (ref $item eq 'ARRAY' and scalar(@$item) == 2) {
149 # First item is text and second item is op address.
150 $add = $item->[0];
151 } elsif (eval{$item->isa("B::DeparseTree::Node")}) {
152 $add = $item->{text};
153 # First item is text and second item is op address.
154 } else {
155 $add = $self->combine($item->{sep}, $item->{texts});
156 }
157 } else {
158 $add = $item;
159 }
160 if ($result) {
161 $result .= ($sep . $add);
162 } else {
163 $result = $add;
164 }
165 }
166 return $result;
167 }
168  
169 # FIXME: replace with routines to build text on from the tree
170 #
171 sub text($)
172 {
173 return shift()->{text};
174 }
175  
176  
177 # Possibly add () around $text depending on precidence $prec and
178 # context $cx. We return a string.
179 sub maybe_parens($$$$)
180 {
181 my($self, $info, $cx, $prec) = @_;
182 if (parens_test($info, $cx, $prec)) {
183 $info->{text} = $self->combine('', "(", $info->{text}, ")");
184 # In a unop, let parent reuse our parens; see maybe_parens_unop
185 if ($cx == 16) {
186 $info->{text} = "\cS" . $info->{text};
187 $info->{parens} = 'reuse';
188 } else {
189 $info->{parens} = 'true';
190 }
191 return $info->{text};
192 } else {
193 $info->{parens} = '';
194 return $info->{text};
195 }
196 }
197  
198 # Demo code
199 unless(caller) {
200 my $deparse = undef;
201 *fs = \&B::DeparseTree::Node::from_str;
202 *fl = \&B::DeparseTree::Node::from_list;
203 my @list = ();
204 push @list, fs('root', undef, "X", 'string', {}),
205 fl('root', undef, ['A', 'B'], ':', 'simple-list', {});
206 push @list, fl('root', undef, \@list, '||', 'compound-list', {});
207 foreach my $item (@list) {
208 print $item->text, "\n";
209 }
210 }
211 1;