| 1 | # B::DeparseTree tree-building routines. | ||
| 2 | # Copyright (c) 2018 Rocky Bernstein | ||
| 3 | # All rights reserved. | ||
| 4 | # This module is free software; you can redistribute and/or modify | ||
| 5 | # it under the same terms as Perl itself. | ||
| 6 | |||
| 7 | # This is based on the module B::Deparse by Stephen McCamant. | ||
| 8 | # It has been extended save tree structure, and is addressible | ||
| 9 | # by opcode address. | ||
| 10 | |||
| 11 | # Note the package name. It is *not* B::DeparseTree::Tree. | ||
| 12 | # In the future there may be a version of this that doesn't | ||
| 13 | # save as much information, but just stores enough to extract | ||
| 14 | # a string, which would be a slightly more heavyweight version of | ||
| 15 | # B::Deparse. | ||
| 16 | package B::DeparseTree::SyntaxTree; | ||
| 17 | |||
| 18 | use B::DeparseTree::TreeNode; | ||
| 19 | |||
| 20 | our($VERSION, @EXPORT, @ISA); | ||
| 21 | $VERSION = '3.2.0'; | ||
| 22 | @ISA = qw(Exporter B::DeparseTree); | ||
| 23 | @EXPORT = qw( | ||
| 24 | combine | ||
| 25 | combine2str | ||
| 26 | get_info_and_str | ||
| 27 | expand_simple_spec | ||
| 28 | indent_less | ||
| 29 | indent_more | ||
| 30 | indent_value | ||
| 31 | info2str | ||
| 32 | info_from_list | ||
| 33 | info_from_template | ||
| 34 | info_from_string | ||
| 35 | info_from_text | ||
| 36 | template_engine | ||
| 37 | template2str | ||
| 38 | ); | ||
| 39 | |||
| 40 | sub combine($$$) | ||
| 41 | { | ||
| 42 | my ($self, $sep, $items) = @_; | ||
| 43 | # FIXME: loop over $item, testing type. | ||
| 44 | Carp::confess("should be a reference to a array: is $items") unless | ||
| 45 | ref $items eq 'ARRAY'; | ||
| 46 | my @result = (); | ||
| 47 | foreach my $item (@$items) { | ||
| 48 | my $add; | ||
| 49 | if (ref $item) { | ||
| 50 | if (ref $item eq 'ARRAY' and scalar(@$item) == 2) { | ||
| 51 | $add = [$item->[0], $item->[1]]; | ||
| 52 | } elsif (eval{$item->isa("B::DeparseTree::TreeNode")}) { | ||
| 53 | $add = [$item->{text}, $item->{addr}]; | ||
| 54 | # First item is text and second item is op address. | ||
| 55 | } else { | ||
| 56 | Carp::confess("don't know what to do with $item"); | ||
| 57 | } | ||
| 58 | } else { | ||
| 59 | $add = $item; | ||
| 60 | } | ||
| 61 | push @result, $sep if @result && $sep; | ||
| 62 | push @result, $add; | ||
| 63 | } | ||
| 64 | return @result; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub combine2str($$$) | ||
| 68 | { | ||
| 69 | my ($self, $sep, $items) = @_; | ||
| 70 | my $result = ''; | ||
| 71 | foreach my $item (@$items) { | ||
| 72 | $result .= $sep if $result; | ||
| 73 | if (ref $item) { | ||
| 74 | if (ref $item eq 'ARRAY' and scalar(@$item) == 2) { | ||
| 75 | # First item is text and second item is op address. | ||
| 76 | $result .= $self->info2str($item->[0]); | ||
| 77 | } elsif (eval{$item->isa("B::DeparseTree::TreeNode")}) { | ||
| 78 | if (exists $item->{fmt}) { | ||
| 79 | $result .= $self->template2str($item); | ||
| 80 | } else { | ||
| 81 | $result .= $self->info2str($item); | ||
| 82 | } | ||
| 83 | } else { | ||
| 84 | Carp::confess("Invalid ref item ref($item)"); | ||
| 85 | } | ||
| 86 | } else { | ||
| 87 | # FIXME: add this and remove errors | ||
| 88 | if (index($item, '@B::DeparseTree::TreeNode') > 0) { | ||
| 89 | Carp::confess("\@B::DeparseTree::TreeNode as an item is probably wrong"); | ||
| 90 | } | ||
| 91 | $result .= $item; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | return $result; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub expand_simple_spec($$) | ||
| 98 | { | ||
| 99 | my ($self, $fmt) = @_; | ||
| 100 | my $result = ''; | ||
| 101 | while ((my $k=index($fmt, '%')) >= 0) { | ||
| 102 | $result .= substr($fmt, 0, $k); | ||
| 103 | my $spec = substr($fmt, $k, 2); | ||
| 104 | $fmt = substr($fmt, $k+2); | ||
| 105 | |||
| 106 | if ($spec eq '%%') { | ||
| 107 | $result .= '%'; | ||
| 108 | } elsif ($spec eq '%+') { | ||
| 109 | $result .= $self->indent_more(); | ||
| 110 | } elsif ($spec eq '%-') { | ||
| 111 | $result .= $self->indent_less(); | ||
| 112 | } elsif ($spec eq '%|') { | ||
| 113 | $result .= $self->indent_value(); | ||
| 114 | } else { | ||
| 115 | Carp::confess("Unknown spec $spec") | ||
| 116 | } | ||
| 117 | } | ||
| 118 | $result .= $fmt if $fmt; | ||
| 119 | return $result; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub indent_less($$) { | ||
| 123 | my ($self, $check_level) = @_; | ||
| 124 | $check_level = 0 if !defined $check_level; | ||
| 125 | |||
| 126 | $self->{level} -= $self->{'indent_size'}; | ||
| 127 | my $level = $self->{level}; | ||
| 128 | if ($check_level < 0) { | ||
| 129 | Carp::confess("mismatched indent/dedent") if $check_level; | ||
| 130 | $level = 0; | ||
| 131 | $self->{level} = 0; | ||
| 132 | } | ||
| 133 | return $self->indent_value(); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub indent_more($) { | ||
| 137 | my ($self) = @_; | ||
| 138 | $self->{level} += $self->{'indent_size'}; | ||
| 139 | return $self->indent_value(); | ||
| 140 | } | ||
| 141 | |||
| 142 | sub indent_value($) { | ||
| 143 | my ($self) = @_; | ||
| 144 | my $level = $self->{level}; | ||
| 145 | if ($self->{'use_tabs'}) { | ||
| 146 | return "\t" x ($level / 8) . " " x ($level % 8); | ||
| 147 | } else { | ||
| 148 | return " " x $level; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | sub info2str($$) | ||
| 153 | { | ||
| 154 | my ($self, $item) = @_; | ||
| 155 | my $result = ''; | ||
| 156 | if (ref $item) { | ||
| 157 | if (ref $item eq 'ARRAY' and scalar(@$item) == 2) { | ||
| 158 | # This code is going away... | ||
| 159 | Carp::confess("fixme"); | ||
| 160 | $result = $item->[0]; | ||
| 161 | } elsif (eval{$item->isa("B::DeparseTree::TreeNode")}) { | ||
| 162 | if (exists $item->{fmt}) { | ||
| 163 | $result .= $self->template2str($item); | ||
| 164 | if ($item->{maybe_parens}) { | ||
| 165 | my $mp = $item->{maybe_parens}; | ||
| 166 | if ($mp->{force} || $mp->{parens}) { | ||
| 167 | $result = "($result)"; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } elsif (!exists $item->{texts} && exists $item->{text}) { | ||
| 171 | # Is a constant string | ||
| 172 | $result .= $item->{text}; | ||
| 173 | } else { | ||
| 174 | $result = $self->combine2str($item->{sep}, | ||
| 175 | $item->{texts}); | ||
| 176 | } | ||
| 177 | |||
| 178 | } else { | ||
| 179 | Carp::confess("Invalid ref item ref($item)"); | ||
| 180 | } | ||
| 181 | } else { | ||
| 182 | # FIXME: add this and remove errors | ||
| 183 | if (index($item, '@B::DeparseTree::TreeNode') > 0) { | ||
| 184 | Carp::confess("\@B::DeparseTree::TreeNode as an item is probably wrong"); | ||
| 185 | } | ||
| 186 | $result = $item; | ||
| 187 | } | ||
| 188 | return $result; | ||
| 189 | } | ||
| 190 | |||
| 191 | # Create an info structure from a list of strings | ||
| 192 | # FIXME: $deparse (or rather $self) should be first | ||
| 193 | sub info_from_list($$$$$$) | ||
| 194 | { | ||
| 195 | my ($op, $self, $texts, $sep, $type, $opts) = @_; | ||
| 196 | |||
| 197 | # Set undef in "texts" argument position because we are going to create | ||
| 198 | # our own text from the $texts. | ||
| 199 | my $info = B::DeparseTree::TreeNode->new($op, $self, $texts, undef, | ||
| 200 | $type, $opts); | ||
| 201 | $info->{sep} = $sep; | ||
| 202 | my $text = ''; | ||
| 203 | foreach my $item (@$texts) { | ||
| 204 | $text .= $sep if $text and $sep; | ||
| 205 | if(ref($item) eq 'ARRAY'){ | ||
| 206 | $text .= $item->[0]; | ||
| 207 | } elsif (eval{$item->isa("B::DeparseTree::TreeNode")}) { | ||
| 208 | $text .= $item->{text}; | ||
| 209 | } else { | ||
| 210 | $text .= $item; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | $info->{text} = $text; | ||
| 215 | if ($opts->{maybe_parens}) { | ||
| 216 | my ($obj, $context, $precedence) = @{$opts->{maybe_parens}}; | ||
| 217 | my $parens = B::DeparseTree::TreeNode::parens_test($obj, $context, $precedence); | ||
| 218 | $self->{maybe_parens} = { | ||
| 219 | context => $context, | ||
| 220 | precedence => $precedence, | ||
| 221 | force => $obj->{'parens'}, | ||
| 222 | parens => $parens ? 'true' : '' | ||
| 223 | }; | ||
| 224 | $info->{text} = "($info->{text})" if exists $info->{text} and $parens; | ||
| 225 | } | ||
| 226 | |||
| 227 | return $info | ||
| 228 | } | ||
| 229 | |||
| 230 | # Create an info structure a template pattern | ||
| 231 | sub info_from_template($$$$$) { | ||
| 232 | my ($self, $type, $op, $fmt, $indexes, $args, $opts) = @_; | ||
| 233 | $opts = {} unless defined($opts); | ||
| 234 | # if (ref($args) ne "ARRAY") { | ||
| 235 | # use Enbugger "trepan"; Enbugger->stop; | ||
| 236 | # } | ||
| 237 | my @args = @$args; | ||
| 238 | my $info = B::DeparseTree::TreeNode->new($op, $self, $args, undef, $type, $opts); | ||
| 239 | |||
| 240 | $indexes = [0..$#args] unless defined $indexes; | ||
| 241 | $info->{'indexes'} = $indexes; | ||
| 242 | my $text = $self->template_engine($fmt, $indexes, $args); | ||
| 243 | |||
| 244 | $info->{'fmt'} = $fmt; | ||
| 245 | $info->{'text'} = $self->template_engine($fmt, $indexes, $args); | ||
| 246 | |||
| 247 | if (! defined $op) { | ||
| 248 | $info->{addr} = ++$self->{'last_fake_addr'}; | ||
| 249 | $self->{optree}{$info->{addr}} = $info; | ||
| 250 | } | ||
| 251 | |||
| 252 | if ($opts->{'relink_children'}) { | ||
| 253 | # FIXME we should specify which children to relink | ||
| 254 | for (my $i=0; $i < scalar @$args; $i++) { | ||
| 255 | if ($args[$i]->isa("B::DeparseTree::TreeNode")) { | ||
| 256 | $args[$i]{parent} = $info->{addr}; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | # Link the parent of Deparse::Tree::TreeNodes to this node. | ||
| 262 | if ($opts->{'synthesized_nodes'}) { | ||
| 263 | foreach my $node (@{$opts->{'synthesized_nodes'}}) { | ||
| 264 | $node->{parent} = $info->{addr}; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | # Need to handle maybe_parens since B::DeparseNode couldn't do that | ||
| 269 | # as it was passed a ref ARRAY rather than a string. | ||
| 270 | if ($opts->{maybe_parens}) { | ||
| 271 | my ($obj, $context, $precedence) = @{$opts->{maybe_parens}}; | ||
| 272 | my $parens = B::DeparseTree::TreeNode::parens_test($obj, | ||
| 273 | $context, $precedence); | ||
| 274 | $info->{maybe_parens} = { | ||
| 275 | context => $context, | ||
| 276 | precedence => $precedence, | ||
| 277 | force => $obj->{'parens'}, | ||
| 278 | parens => $parens ? 'true' : '' | ||
| 279 | }; | ||
| 280 | $info->{text} = "($info->{text})" if exists $info->{text} and $parens; | ||
| 281 | } | ||
| 282 | |||
| 283 | return $info; | ||
| 284 | } | ||
| 285 | |||
| 286 | # Create an info structure from a single string | ||
| 287 | sub info_from_string($$$$$) | ||
| 288 | { | ||
| 289 | my ($self, $type, $op, $str, $opts) = @_; | ||
| 290 | $opts ||= {}; | ||
| 291 | return B::DeparseTree::TreeNode->new($op, $self, $str, undef, | ||
| 292 | $type, $opts); | ||
| 293 | } | ||
| 294 | |||
| 295 | # OBSOLETE: Create an info structure from a single string | ||
| 296 | # FIXME: remove this | ||
| 297 | sub info_from_text($$$$$) | ||
| 298 | { | ||
| 299 | my ($op, $self, $text, $type, $opts) = @_; | ||
| 300 | # Use this to smoke outt calls | ||
| 301 | # use Enbugger 'trepan'; Enbugger->stop; | ||
| 302 | return $self->info_from_string($type, $op, $text, $opts) | ||
| 303 | } | ||
| 304 | |||
| 305 | # List of suffix characters that are handled by "expand_simple_spec()". | ||
| 306 | use constant SIMPLE_SPEC => '%+-|'; | ||
| 307 | |||
| 308 | # Extract the string at $args[$index] and if | ||
| 309 | # we are looking for that position include where we are in | ||
| 310 | # that position | ||
| 311 | sub get_info_and_str($$$) | ||
| 312 | { | ||
| 313 | my ($self, $index, $args) = @_; | ||
| 314 | my $info = $args->[$index]; | ||
| 315 | my $str = $self->info2str($info); | ||
| 316 | return ($info, $str); | ||
| 317 | } | ||
| 318 | |||
| 319 | sub template_engine($$$$) | ||
| 320 | { | ||
| 321 | my ($self, $fmt, $indexes, $args, $find_addr) = @_; | ||
| 322 | |||
| 323 | # use Data::Dumper; | ||
| 324 | # print "-----\n"; | ||
| 325 | # p $args; | ||
| 326 | # print "'======\n"; | ||
| 327 | # print $fmt, "\n" | ||
| 328 | # print $args, "\n"; | ||
| 329 | |||
| 330 | my $i = 0; | ||
| 331 | $find_addr = -2 unless $find_addr; | ||
| 332 | |||
| 333 | my $start_fmt = $fmt; # used in error messages | ||
| 334 | my @args = @$args; | ||
| 335 | |||
| 336 | my $result = ''; | ||
| 337 | my $find_pos = undef; | ||
| 338 | while ((my $k=index($fmt, '%')) >= 0) { | ||
| 339 | $result .= substr($fmt, 0, $k); | ||
| 340 | my $spec = substr($fmt, $k, 2); | ||
| 341 | $fmt = substr($fmt, $k+2); | ||
| 342 | |||
| 343 | if (index(SIMPLE_SPEC, substr($spec, 1, 1)) >= 0) { | ||
| 344 | $result .= $self->expand_simple_spec($spec); | ||
| 345 | } elsif ($spec eq "%c") { | ||
| 346 | # Insert child entry | ||
| 347 | |||
| 348 | # FIXME: turn this into a subroutine. | ||
| 349 | if ($i >= scalar @{$indexes}) { | ||
| 350 | Carp::confess("Need another entry in args_spec for %c in fmt: $start_fmt"); | ||
| 351 | } | ||
| 352 | my $index = $indexes->[$i++]; | ||
| 353 | if ($index >= scalar @args) { | ||
| 354 | Carp::confess("$index in $start_fmt for %c is too large; should be less " . | ||
| 355 | "than scalar(@args)"); | ||
| 356 | } | ||
| 357 | my $str; | ||
| 358 | my ($info, $str) = $self->get_info_and_str($index, $args); | ||
| 359 | if (ref($info) && $info->{'addr'} == $find_addr) { | ||
| 360 | my $len = length($result); | ||
| 361 | $len++ if (exists $info->{maybe_parens} | ||
| 362 | && $info->{maybe_parens}{parens}); | ||
| 363 | $find_pos = [$len, length($str)]; | ||
| 364 | } | ||
| 365 | $result .= $str; | ||
| 366 | } elsif ($spec eq "%C") { | ||
| 367 | # Insert separator between child entry lists | ||
| 368 | my ($low, $high, $sub_spec) = @{$indexes->[$i++]}; | ||
| 369 | my $sep = $self->expand_simple_spec($sub_spec); | ||
| 370 | my $list = ''; | ||
| 371 | for (my $j=$low; $j<=$high; $j++) { | ||
| 372 | $result .= $sep if $j > $low; | ||
| 373 | |||
| 374 | # FIXME: Remove duplicate code | ||
| 375 | my ($info, $str) = $self->get_info_and_str($j, $args); | ||
| 376 | if (ref($info) && $info->{'addr'} == $find_addr) { | ||
| 377 | my $len = length($result); | ||
| 378 | $len++ if (exists $info->{maybe_parens} | ||
| 379 | && $info->{maybe_parens}{parens}); | ||
| 380 | $find_pos = [$len, length($str)]; | ||
| 381 | } | ||
| 382 | $result .= $str; | ||
| 383 | } | ||
| 384 | } elsif ($spec eq "%f") { | ||
| 385 | # Run maybe_parens_func | ||
| 386 | my $fn_name = shift @args; | ||
| 387 | my ($cx, $prec) = @{$indexes->[$i++]}; | ||
| 388 | my $params = $self->template_engine("%C", [[0, $#args], ', ']); | ||
| 389 | $result .= B::Deparse::maybe_parens_func($self, $fn_name, $params, $cx, $prec); | ||
| 390 | } elsif ($spec eq "%F") { | ||
| 391 | # Run a transformation function | ||
| 392 | if ($i >= scalar@$indexes) { | ||
| 393 | Carp::confess("Need another entry in args_spec for %%F fmt: $start_fmt"); | ||
| 394 | } | ||
| 395 | my ($arg_index, $transform_fn) = @{$indexes->[$i++]}; | ||
| 396 | if ($arg_index >= scalar @args) { | ||
| 397 | Carp::confess("argument index $arg_index in $start_fmt for %%F is too large; should be less than @$args"); | ||
| 398 | } | ||
| 399 | if (ref($transform_fn ne 'CODE')) { | ||
| 400 | Carp::confess("transformation function $transform_fn is not CODE"); | ||
| 401 | } | ||
| 402 | my ($arg) = $args[$arg_index]; | ||
| 403 | $result .= $transform_fn->($arg); | ||
| 404 | |||
| 405 | } elsif ($spec eq "%;") { | ||
| 406 | # Insert semicolons and indented newlines between statements. | ||
| 407 | # Don't insert them around empty strings - some OPs | ||
| 408 | # don't have an text associated with them. | ||
| 409 | # Finally, replace semicolon a the end of statement that | ||
| 410 | # end in "}" with a \n and proper indent. | ||
| 411 | my $sep = $self->expand_simple_spec(";\n%|"); | ||
| 412 | my $start_size = length($result); | ||
| 413 | for (my $j=0; $j< @args; $j++) { | ||
| 414 | my $old_result = $result; | ||
| 415 | if ($j > 0 && length($result) > $start_size) { | ||
| 416 | # Remove any prior ;\n | ||
| 417 | $result = substr($result, 0, -1) if substr($result, -1) eq "\n"; | ||
| 418 | $result = substr($result, 0, -1) if substr($result, -1) eq ";"; | ||
| 419 | ## The below needs to be done based on whether the previous construct is a compound statement or not. | ||
| 420 | ## That could be added in a trailing format specifier for it. | ||
| 421 | ## "sub {...}" and "$h = {..}" need a semicolon while "if () {...}" doesn't. | ||
| 422 | # if (substr($result, -1) eq "}" & $j < $#args) { | ||
| 423 | # # Omit ; from sep. FIXME: do this based on an option? | ||
| 424 | # $result .= substr($sep, 1); | ||
| 425 | # } else { | ||
| 426 | # $result .= $sep; | ||
| 427 | # } | ||
| 428 | $result .= $sep; | ||
| 429 | } | ||
| 430 | |||
| 431 | # FIXME: Remove duplicate code | ||
| 432 | my ($info, $str) = $self->get_info_and_str($j, $args); | ||
| 433 | if (ref($info) && $info->{'addr'} == $find_addr) { | ||
| 434 | my $len = length($result); | ||
| 435 | $len++ if exists $info->{maybe_parens} and $info->{maybe_parens}{parens}; | ||
| 436 | $find_pos = [length($result), length($str)]; | ||
| 437 | } | ||
| 438 | # Note: $str eq 0 add to $result | ||
| 439 | if (defined $str && $str ne '') { | ||
| 440 | $result .= $str | ||
| 441 | } else { | ||
| 442 | $result = $old_result; | ||
| 443 | } | ||
| 444 | } | ||
| 445 | # # FIXME: Add the final ';' based on an option? | ||
| 446 | # if ($result and not | ||
| 447 | # (substr($result, -1) eq ';' or | ||
| 448 | # (substr($result, -1) eq ';\n'))) { | ||
| 449 | # $result .= ';' if $result and substr($result, -1) ne ';'; | ||
| 450 | # } | ||
| 451 | |||
| 452 | } elsif ($spec eq "\cS") { | ||
| 453 | # FIXME: not handled yet | ||
| 454 | ; | ||
| 455 | } else { | ||
| 456 | # We have % with a non-special symbol. Just preserve those. | ||
| 457 | $result .= $spec; | ||
| 458 | } | ||
| 459 | } | ||
| 460 | $result .= $fmt if $fmt; | ||
| 461 | if ($find_addr != -2) { | ||
| 462 | # want result and position | ||
| 463 | return $result, $find_pos; | ||
| 464 | } | ||
| 465 | # want just result | ||
| 466 | return $result; | ||
| 467 | |||
| 468 | } | ||
| 469 | |||
| 470 | sub template2str($$) { | ||
| 471 | my ($self, $info) = @_; | ||
| 472 | return $self->template_engine($info->{fmt}, | ||
| 473 | $info->{indexes}, | ||
| 474 | $info->{texts}); | ||
| 475 | } | ||
| 476 | |||
| 477 | 1; |