| 1 | use YAML::XS; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | our $OPT_log; | ||
| 5 | our $OPT_match; | ||
| 6 | package STD::Actions; | ||
| 7 | use Scalar::Util 'refaddr'; | ||
| 8 | |||
| 9 | # Generic ast translation done via autoload | ||
| 10 | |||
| 11 | our $AUTOLOAD; | ||
| 12 | my $SEQ = 1; | ||
| 13 | our %GENCLASS; | ||
| 14 | |||
| 15 | sub AUTOLOAD { | ||
| 16 | my $self = shift; | ||
| 17 | my $match = shift; | ||
| 18 | return if @_; # not interested in tagged reductions | ||
| 19 | return if $match->{_ast}{_specific} and ref($match->{_ast}) =~ /^VAST/; | ||
| 20 | print STDERR "AUTOLOAD $AUTOLOAD\n" if $OPT_log; | ||
| 21 | my $r = hoistast($match); | ||
| 22 | (my $class = $AUTOLOAD) =~ s/^STD::Actions/VAST/; | ||
| 23 | $class =~ s/__S_\d\d\d/__S_/ and $r->{_specific} = 1; | ||
| 24 | if ($class =~ /::(infix|prefix|postfix|postcircumfix|dotty|regex_infix)__S_/) { | ||
| 25 | $r->{_op} = $class; | ||
| 26 | $class =~ s/::(infix|prefix|postfix|postcircumfix|dotty|regex_infix)__S_/::SYM_$1__S_/; | ||
| 27 | } | ||
| 28 | gen_class($class); | ||
| 29 | bless $r, $class unless ref($r) =~ /^VAST/; | ||
| 30 | $r->{MATCH} = $match if $OPT_match; | ||
| 31 | $match->{'_ast'} = $r; | ||
| 32 | } | ||
| 33 | |||
| 34 | # propagate ->{'_ast'} nodes upward | ||
| 35 | # (untransformed STD nodes in output indicate bugs) | ||
| 36 | |||
| 37 | sub hoistast { | ||
| 38 | my $node = shift; | ||
| 39 | my $text = $node->Str; | ||
| 40 | my %r; | ||
| 41 | my @all; | ||
| 42 | my %allused; | ||
| 43 | my @fake; | ||
| 44 | for my $k (keys %$node) { | ||
| 45 | print STDERR $node->{_reduced} // 'ANON', " $k\n" if $OPT_log; | ||
| 46 | my $v = $node->{$k}; | ||
| 47 | if ($k eq 'O') { | ||
| 48 | for my $key (keys %$v) { | ||
| 49 | $r{$key} = $$v{$key}; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | elsif ($k eq 'PRE') { | ||
| 53 | } | ||
| 54 | elsif ($k eq 'POST') { | ||
| 55 | } | ||
| 56 | elsif ($k eq 'SIGIL') { | ||
| 57 | $r{SIGIL} = $v; | ||
| 58 | } | ||
| 59 | elsif ($k eq 'sym') { | ||
| 60 | if (ref $v) { | ||
| 61 | if (ref($v) eq 'ARRAY') { | ||
| 62 | $r{SYM} = $v; | ||
| 63 | } | ||
| 64 | elsif (ref($v) eq 'HASH') { | ||
| 65 | $r{SYM} = $v; | ||
| 66 | } | ||
| 67 | elsif ($v->{_pos}) { | ||
| 68 | $r{SYM} = $v->Str; | ||
| 69 | } | ||
| 70 | else { | ||
| 71 | $r{SYM} = $v->TEXT; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | else { | ||
| 75 | $r{SYM} = $v; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | elsif ($k eq '_arity') { | ||
| 79 | $r{ARITY} = $v; | ||
| 80 | } | ||
| 81 | elsif ($k eq '~CAPS') { | ||
| 82 | # print "CAPS ref ". ref($v) . "\n"; | ||
| 83 | if (ref $v) { | ||
| 84 | for (@$v) { | ||
| 85 | next unless ref $_; # XXX skip keys? | ||
| 86 | push @all, $_->{'_ast'} if defined $_->{'_ast'} | ||
| 87 | and !($allused{refaddr $_}++); | ||
| 88 | # don't generate multiple entries for a multi-named | ||
| 89 | # capture | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | elsif ($k eq '_from') { | ||
| 94 | $r{BEG} = $v; | ||
| 95 | $r{END} = $node->{_pos}; | ||
| 96 | if (exists $::MEMOS[$v]{'ws'}) { | ||
| 97 | my $wsstart = $::MEMOS[$v]{'ws'}; | ||
| 98 | $r{WS} = $v - $wsstart if defined $wsstart and $wsstart < $v | ||
| 99 | } | ||
| 100 | } | ||
| 101 | elsif ($k =~ /^[a-zA-Z]/) { | ||
| 102 | if ($k eq 'noun') { # trim off PRE and POST | ||
| 103 | $r{BEG} = $v->{_from}; | ||
| 104 | $r{END} = $v->{_pos}; | ||
| 105 | } | ||
| 106 | if (ref($v) eq 'ARRAY') { | ||
| 107 | my $zyg = []; | ||
| 108 | for my $z (@$v) { | ||
| 109 | if (ref $z) { | ||
| 110 | if (ref($z) eq 'ARRAY') { | ||
| 111 | push @$zyg, $z; | ||
| 112 | push @fake, @$z; | ||
| 113 | } | ||
| 114 | elsif (exists $z->{'_ast'}) { | ||
| 115 | my $zy = $z->{'_ast'}; | ||
| 116 | push @fake, $zy; | ||
| 117 | push @$zyg, $zy; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | push @$zyg, $z; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | $r{$k} = $zyg; | ||
| 125 | # $r{zygs}{$k} = $SEQ++ if @$zyg and $k ne 'sym'; | ||
| 126 | } | ||
| 127 | elsif (ref($v) eq 'HASH') { | ||
| 128 | $r{$k} = $v; | ||
| 129 | } | ||
| 130 | elsif (ref($v)) { | ||
| 131 | if ($v->isa('STD::Cursor') && !$v->{_reduced}) { | ||
| 132 | $r{$k} = $v->{'_ast'} //= hoistast($v); | ||
| 133 | bless $r{$k}, 'VAST::Str'; | ||
| 134 | next; | ||
| 135 | } | ||
| 136 | elsif (exists $v->{'_ast'}) { | ||
| 137 | push @fake, $v->{'_ast'}; | ||
| 138 | $r{$k} = $v->{'_ast'}; | ||
| 139 | } | ||
| 140 | elsif (exists $v->{'_from'}) { | ||
| 141 | $r{$k}{BEG} = $v->{'_from'}; | ||
| 142 | $r{$k}{END} = $v->{'_pos'}; | ||
| 143 | $r{$k}{TEXT} = $v->Str; | ||
| 144 | } | ||
| 145 | else { | ||
| 146 | # NAME or decl or sig or... | ||
| 147 | $r{$k} = $v; | ||
| 148 | next; | ||
| 149 | } | ||
| 150 | # $r{zygs}{$k} = $SEQ++; | ||
| 151 | unless (ref($r{$k}) =~ /^VAST/) { | ||
| 152 | my $class = "VAST::$k"; | ||
| 153 | gen_class($class); | ||
| 154 | bless $r{$k}, $class unless ref($r{$k}) =~ /^VAST/; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | $r{$k} = $v; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | if (@all == 1 and defined $all[0]) { | ||
| 163 | $r{'.'} = $all[0]; | ||
| 164 | } | ||
| 165 | elsif (@all) { | ||
| 166 | $r{'.'} = \@all; | ||
| 167 | } | ||
| 168 | elsif (@fake) { | ||
| 169 | $r{'.'} = \@fake; | ||
| 170 | } | ||
| 171 | else { | ||
| 172 | $r{TEXT} = $text; | ||
| 173 | } | ||
| 174 | \%r; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub hoist { | ||
| 178 | my $match = shift; | ||
| 179 | |||
| 180 | my %r; | ||
| 181 | my $v = $match->{O}; | ||
| 182 | if ($v) { | ||
| 183 | for my $key (keys %$v) { | ||
| 184 | $r{$key} = $$v{$key}; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | if ($match->{sym}) { | ||
| 188 | # $r{sym} = $match->{sym}; | ||
| 189 | } | ||
| 190 | if ($match->{ADV}) { | ||
| 191 | $r{ADV} = $match->{ADV}; | ||
| 192 | } | ||
| 193 | \%r; | ||
| 194 | } | ||
| 195 | |||
| 196 | sub CHAIN { | ||
| 197 | my $self = shift; | ||
| 198 | my $match = shift; | ||
| 199 | my $r = hoistast($match); | ||
| 200 | |||
| 201 | my $class = 'VAST::Chaining'; | ||
| 202 | |||
| 203 | gen_class($class); | ||
| 204 | $r = bless $r, $class; | ||
| 205 | $match->{'_ast'} = $r; | ||
| 206 | } | ||
| 207 | |||
| 208 | sub LIST { | ||
| 209 | my $self = shift; | ||
| 210 | my $match = shift; | ||
| 211 | my $r = hoist($match); | ||
| 212 | |||
| 213 | my @list = @{$match->{list}}; | ||
| 214 | my @delims = @{$match->{delims}}; | ||
| 215 | $r->{'args'} = [ map { $_->{_ast} } @list ]; | ||
| 216 | my @all; | ||
| 217 | while (@delims) { | ||
| 218 | my $term = shift @list; | ||
| 219 | push @all, $term->{_ast}; | ||
| 220 | my $infix = shift @delims; | ||
| 221 | push @all, $infix->{_ast}; | ||
| 222 | } | ||
| 223 | push @all, $list[0]->{_ast} if @list; | ||
| 224 | pop @all while @all and not $all[-1]{END}; | ||
| 225 | $r->{BEG} = $all[0]{BEG}; | ||
| 226 | $r->{END} = $all[-1]{END} // $r->{BEG}; | ||
| 227 | $r->{'infix'} = $all[-2]; # assume final one is most representative | ||
| 228 | $r->{'.'} = \@all; | ||
| 229 | |||
| 230 | my $base = ucfirst $match->{O}{dba} // $match->{sym} // 'termish'; | ||
| 231 | $base =~ s/ /_/g; | ||
| 232 | $base =~ s/^/VAST::/; | ||
| 233 | |||
| 234 | my $class = | ||
| 235 | $match->{delims}[0]{_ast}{infix}{_op} // | ||
| 236 | $match->{delims}[0]{_ast}{regex_infix}{_op} // | ||
| 237 | warn ::Dump($match); | ||
| 238 | gen_class($class, $base); | ||
| 239 | $r = bless $r, $class; | ||
| 240 | $match->{'_ast'} = $r; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub POSTFIX { | ||
| 244 | my $self = shift; | ||
| 245 | my $match = shift; | ||
| 246 | my $r = hoist($match); | ||
| 247 | my $arg = $match->{arg}->{_ast}; | ||
| 248 | $r->{'arg'} = $arg; | ||
| 249 | $r->{postop} = $match->{postop}{_ast} if exists $match->{postop}; | ||
| 250 | my $a = $r->{'.'} = [$arg,$match->{_ast}]; | ||
| 251 | $r->{BEG} = $a->[0]->{BEG} // $match->{_from}; | ||
| 252 | $r->{END} = $a->[-1]->{END} // $match->{_pos}; | ||
| 253 | |||
| 254 | my $base = ucfirst $match->{O}{dba} // $match->{sym} // 'termish'; | ||
| 255 | $base =~ s/ /_/g; | ||
| 256 | $base =~ s/^/VAST::/; | ||
| 257 | |||
| 258 | my $class; | ||
| 259 | if ($match->{fake}) { | ||
| 260 | $class = $base; | ||
| 261 | $base = ''; | ||
| 262 | } | ||
| 263 | else { | ||
| 264 | $class = | ||
| 265 | $match->{_ast}{postop}{postfix}{_op} // | ||
| 266 | $match->{_ast}{postop}{postcircumfix}{_op} // | ||
| 267 | $match->{_ast}{dotty}{_op} // | ||
| 268 | warn ::Dump($match); | ||
| 269 | } | ||
| 270 | |||
| 271 | gen_class($class, $base); | ||
| 272 | $r = bless $r, $class; | ||
| 273 | $match->{'_ast'} = $r; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub PREFIX { | ||
| 277 | my $self = shift; | ||
| 278 | my $match = shift; | ||
| 279 | my $r = hoist($match); | ||
| 280 | my $arg = $match->{arg}->{_ast}; | ||
| 281 | $r->{'postop'} = $match->{postop}->{_ast} if exists $match->{postop}; | ||
| 282 | $r->{'arg'} = $arg; | ||
| 283 | my $a = $r->{'.'} = [$match->{_ast},$arg]; | ||
| 284 | |||
| 285 | $r->{BEG} = $a->[0]->{BEG} // $match->{_from}; | ||
| 286 | $r->{END} = $a->[-1]->{END} // $match->{_pos}; | ||
| 287 | |||
| 288 | my $base = ucfirst $match->{O}{dba} // $match->{sym} // 'termish'; | ||
| 289 | $base =~ s/ /_/g; | ||
| 290 | $base =~ s/^/VAST::/; | ||
| 291 | |||
| 292 | my $class; | ||
| 293 | if ($match->{fake}) { | ||
| 294 | $class = $base; | ||
| 295 | $base = ''; | ||
| 296 | } | ||
| 297 | else { | ||
| 298 | $class = | ||
| 299 | $match->{_ast}{prefix}{_op} // | ||
| 300 | $match->{_ast}{prefix_postfix_meta_operator}{_op} // | ||
| 301 | $match->{_ast}{prefix_circumfix_meta_operator}{_op} // | ||
| 302 | warn ::Dump($match); | ||
| 303 | } | ||
| 304 | |||
| 305 | gen_class($class,$base); | ||
| 306 | $r = bless $r, $class; | ||
| 307 | $match->{'_ast'} = $r; | ||
| 308 | } | ||
| 309 | |||
| 310 | sub INFIX { | ||
| 311 | my $self = shift; | ||
| 312 | my $match = shift; | ||
| 313 | my $r = hoist($match); | ||
| 314 | my $left = $match->{left}->{_ast}; | ||
| 315 | my $right = $match->{right}->{_ast}; | ||
| 316 | if ($match->{middle}) { # ternary | ||
| 317 | my $middle = $match->{middle}->{_ast}; | ||
| 318 | $r->{'args'} = [$left,$middle,$right]; | ||
| 319 | } | ||
| 320 | else { | ||
| 321 | $r->{'args'} = [$left,$right]; | ||
| 322 | } | ||
| 323 | my $a = $r->{'.'} = [$left,$match->{_ast},$right]; | ||
| 324 | $r->{BEG} = $a->[0]->{BEG} // $match->{_from}; | ||
| 325 | $r->{END} = $a->[-1]->{END} // $match->{_pos}; | ||
| 326 | $r->{'infix'} = $a->[1]; | ||
| 327 | |||
| 328 | my $base = ucfirst $match->{O}{dba} // $match->{sym} // 'termish'; | ||
| 329 | $base =~ s/ /_/g; | ||
| 330 | $base =~ s/^/VAST::/; | ||
| 331 | |||
| 332 | my $class; | ||
| 333 | if ($match->{fake}) { | ||
| 334 | $class = $base; | ||
| 335 | $base = ''; | ||
| 336 | } | ||
| 337 | else { | ||
| 338 | $class = | ||
| 339 | $match->{_ast}{infix}{_op} // | ||
| 340 | $match->{_ast}{regex_infix}{_op} // | ||
| 341 | warn ::Dump($match); | ||
| 342 | } | ||
| 343 | |||
| 344 | gen_class($class, $base); | ||
| 345 | $r = bless $r, $class; | ||
| 346 | $match->{'_ast'} = $r; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub nibbler { | ||
| 350 | my $self = shift; | ||
| 351 | my $match = shift; | ||
| 352 | my $r = hoist($match); | ||
| 353 | if ($match->{nibbles}) { | ||
| 354 | my @dot; | ||
| 355 | for my $n ( @{ $match->{nibbles} } ) { | ||
| 356 | if (ref $n eq 'Str') { | ||
| 357 | push @dot, bless($n,"VAST::Str"); | ||
| 358 | } | ||
| 359 | elsif (ref $n eq 'VAST::Str') { | ||
| 360 | push @dot, $n; | ||
| 361 | } | ||
| 362 | elsif (ref $n eq 'ARRAY') { | ||
| 363 | push @dot, $n->[0]{_ast}; | ||
| 364 | } | ||
| 365 | elsif ($n->{_ast}) { | ||
| 366 | push @dot, $n->{_ast}; | ||
| 367 | } | ||
| 368 | elsif ($n->{EXPR}) { | ||
| 369 | push @dot, $n->{EXPR}->{_ast}; | ||
| 370 | } | ||
| 371 | else { | ||
| 372 | warn "Oops", ::Dump($n); | ||
| 373 | exit; | ||
| 374 | } | ||
| 375 | } | ||
| 376 | my $a = $r->{'.'} = \@dot; | ||
| 377 | $r->{BEG} = $a->[0]->{BEG} // $match->{_from}; | ||
| 378 | $r->{END} = $a->[-1]->{END} // $match->{_pos}; | ||
| 379 | } | ||
| 380 | elsif ($match->{EXPR}) { # regex? | ||
| 381 | $r->{'.'} = $match->{EXPR}->{_ast}; | ||
| 382 | $r->{BEG} = $r->{'.'}->{BEG} // $match->{_from}; | ||
| 383 | $r->{END} = $r->{'.'}->{END} // $match->{_pos}; | ||
| 384 | } | ||
| 385 | |||
| 386 | my $class = 'VAST::nibbler'; | ||
| 387 | # print STDERR ::Dump($r); | ||
| 388 | gen_class($class); | ||
| 389 | $r = bless $r, $class; | ||
| 390 | $match->{'_ast'} = $r; | ||
| 391 | } | ||
| 392 | |||
| 393 | sub EXPR { | ||
| 394 | return; | ||
| 395 | } | ||
| 396 | |||
| 397 | sub termish { | ||
| 398 | my $self = shift; | ||
| 399 | my $match = shift; | ||
| 400 | $match->{'_ast'} = $match->{term}{'_ast'}; | ||
| 401 | } | ||
| 402 | |||
| 403 | sub gen_class { | ||
| 404 | my $class = shift; | ||
| 405 | my $base = shift() // 'VAST::Base'; | ||
| 406 | # say $class; | ||
| 407 | no strict 'refs'; | ||
| 408 | if (@{$class . '::ISA'}) { | ||
| 409 | print STDERR "Existing class $class\n" if $OPT_log; | ||
| 410 | return; | ||
| 411 | } | ||
| 412 | $GENCLASS{$class} = $base; | ||
| 413 | print STDERR "Creating class $class\n" if $OPT_log; | ||
| 414 | @{$class . '::ISA'} = $base; | ||
| 415 | } | ||
| 416 | |||
| 417 | 1; |