| 1 |
#!/usr/bin/perl -w |
| 2 |
# Copyright 2022 Jeffrey Kegler |
| 3 |
# This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 4 |
# redistribute it and/or modify it under the terms of the GNU Lesser |
| 5 |
# General Public License as published by the Free Software Foundation, |
| 6 |
# either version 3 of the License, or (at your option) any later version. |
| 7 |
# |
| 8 |
# Marpa::R2 is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
# Lesser General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU Lesser |
| 14 |
# General Public License along with Marpa::R2. If not, see |
| 15 |
# http://www.gnu.org/licenses/. |
| 16 |
|
| 17 |
use 5.010001; |
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
|
| 21 |
use charnames ':full'; |
| 22 |
use Scalar::Util; |
| 23 |
use Data::Dumper (); |
| 24 |
use English qw( -no_match_vars ); |
| 25 |
use Test::More (); |
| 26 |
use lib 'pperl'; |
| 27 |
|
| 28 |
BEGIN { |
| 29 |
my $PPI_problem; |
| 30 |
CHECK_PPI: { |
| 31 |
if ( not eval { require PPI } ) { |
| 32 |
$PPI_problem = "PPI not installed: $EVAL_ERROR"; |
| 33 |
last CHECK_PPI; |
| 34 |
} |
| 35 |
if ( not PPI->VERSION(1.206) ) { |
| 36 |
$PPI_problem = 'PPI 1.206 not installed'; |
| 37 |
} |
| 38 |
} ## end CHECK_PPI: |
| 39 |
if ($PPI_problem) { |
| 40 |
Test::More::plan skip_all => $PPI_problem; |
| 41 |
} |
| 42 |
else { |
| 43 |
Test::More::plan tests => 12; |
| 44 |
} |
| 45 |
} ## end BEGIN |
| 46 |
|
| 47 |
use Marpa::R2; |
| 48 |
use Marpa::R2::Perl; |
| 49 |
|
| 50 |
our @OUTPUT = (); |
| 51 |
our %SYMTAB = ( SCALAR => {} ); |
| 52 |
|
| 53 |
sub DEBUG_dump { |
| 54 |
say {*STDERR} 'DEBUG: ', join "\n", @main::OUTPUT |
| 55 |
or die "Cannot print to STDERR: $ERRNO"; |
| 56 |
say {*STDERR} 'DEBUG: Symbol table: ', Data::Dumper::Dumper( \%SYMTAB ) |
| 57 |
or die "Cannot print to STDERR: $ERRNO"; |
| 58 |
return; |
| 59 |
} ## end sub DEBUG_dump |
| 60 |
|
| 61 |
# This code is about Perl GRAMMAR. |
| 62 |
# If you're writing |
| 63 |
# a Perl SEMANTICS, and looking for a place to start, |
| 64 |
# you probably don't want to start here. |
| 65 |
# The purpose of these semantics is to test the grammar -- no more. |
| 66 |
# They are probably good for nothing else. |
| 67 |
# |
| 68 |
# Here are some of the defects: |
| 69 |
# |
| 70 |
# 1. Not a 'safe' evaluator for code from untrusted sources. |
| 71 |
# 'eval' is used to interpret the string constants. |
| 72 |
# |
| 73 |
# 2. Most Perl semantics is not implementation and where |
| 74 |
# the implementation exists it often is at the toy level. |
| 75 |
# Basically, anything not needed to interpret |
| 76 |
# Data::Dumper output is ignored. |
| 77 |
# |
| 78 |
# 3. No optimization. It's fast enough for a test suite. |
| 79 |
# |
| 80 |
# 4. Etc., etc., etc. You get the idea. |
| 81 |
|
| 82 |
sub coerce_to_R { |
| 83 |
my ($tagged) = @_; |
| 84 |
my ( $side, $v ) = @{$tagged}; |
| 85 |
return $side eq 'R' ? $v : ${$v}; |
| 86 |
} |
| 87 |
|
| 88 |
sub do_term_lstop { |
| 89 |
my ( undef, $lstop, $list_tagged ) = @_; |
| 90 |
die "Unimplemented lstop: $lstop" if $lstop ne 'bless'; |
| 91 |
my $list_ref = coerce_to_R($list_tagged); |
| 92 |
return [ 'L', \\( bless $list_ref->[0], $list_ref->[1] ) ]; |
| 93 |
} ## end sub do_term_lstop |
| 94 |
|
| 95 |
# term_hi : term_hi ARROW '{' expr ';' '}' ; term_hi__arrow_hash /* somehref->{bar();} */ |
| 96 |
sub do_term_hi__arrow_hash { |
| 97 |
my ( undef, $term, undef, undef, $element ) = @_; |
| 98 |
|
| 99 |
my $element_ref = coerce_to_R($element); |
| 100 |
my $element_ref_type = Scalar::Util::reftype $element_ref; |
| 101 |
die "element in term->[element] is not an scalar: $element_ref_type" |
| 102 |
if $element_ref_type ne 'SCALAR'; |
| 103 |
|
| 104 |
my ( $term_side, $term_ref ) = @{$term}; |
| 105 |
if ( $term_side eq 'L' ) { |
| 106 |
$term_ref = ${$term_ref}; |
| 107 |
} |
| 108 |
if ( ( my $ref_type = Scalar::Util::reftype $term_ref) ne 'REF' |
| 109 |
or ( my $ref_ref_type = Scalar::Util::reftype ${$term_ref} ) ne |
| 110 |
'HASH' ) |
| 111 |
{ |
| 112 |
my $type = $ref_type eq 'REF' ? "REF to $ref_ref_type" : $ref_type; |
| 113 |
die "term in term->[element] is not an array ref: it is $type"; |
| 114 |
} ## end if ( ( my $ref_type = Scalar::Util::reftype $term_ref...)) |
| 115 |
return [ 'L', \\( ${$term_ref}->{ ${$element_ref} } ) ]; |
| 116 |
} ## end sub do_term_hi__arrow_hash |
| 117 |
|
| 118 |
# term_hi : term_hi ARROW '[' expr ']' ; term_hi__arrow_array /* somearef->[$element] */ |
| 119 |
sub do_term_hi__arrow_array { |
| 120 |
my ( undef, $term, undef, undef, $element ) = @_; |
| 121 |
|
| 122 |
my $element_ref = coerce_to_R($element); |
| 123 |
my $element_ref_type = Scalar::Util::reftype $element_ref; |
| 124 |
die "element in term->[element] is not an scalar: $element_ref_type" |
| 125 |
if $element_ref_type ne 'SCALAR'; |
| 126 |
|
| 127 |
my ( $term_side, $term_ref ) = @{$term}; |
| 128 |
if ( $term_side eq 'L' ) { |
| 129 |
$term_ref = ${$term_ref}; |
| 130 |
} |
| 131 |
if ( ( my $ref_type = Scalar::Util::reftype $term_ref) ne 'REF' |
| 132 |
or ( my $ref_ref_type = Scalar::Util::reftype ${$term_ref} ) ne |
| 133 |
'ARRAY' ) |
| 134 |
{ |
| 135 |
my $type = $ref_type eq 'REF' ? "REF to $ref_ref_type" : $ref_type; |
| 136 |
die "term in term->[element] is not an array ref: it is $type"; |
| 137 |
} ## end if ( ( my $ref_type = Scalar::Util::reftype $term_ref...)) |
| 138 |
return [ 'L', \\( ${$term_ref}->[ ${$element_ref} ] ) ]; |
| 139 |
} ## end sub do_term_hi__arrow_array |
| 140 |
|
| 141 |
# term_hi : scalar '{' expr ';' '}' ; hash_index /* $foo->{bar();} */ |
| 142 |
# term_hi : term_hi '{' expr ';' '}' ; hash_index_r /* $foo->[bar]->{baz;} */ |
| 143 |
sub do_hash_index { |
| 144 |
my ( undef, $term, undef, $element ) = @_; |
| 145 |
|
| 146 |
my $element_ref = coerce_to_R($element); |
| 147 |
my $element_ref_type = Scalar::Util::reftype $element_ref; |
| 148 |
die "element in term->[element] is not an scalar: $element_ref_type" |
| 149 |
if $element_ref_type ne 'SCALAR'; |
| 150 |
|
| 151 |
my ( $term_side, $term_ref ) = @{$term}; |
| 152 |
if ( $term_side eq 'R' ) { |
| 153 |
die 'rvalue term in scalar[element] not implemented'; |
| 154 |
} |
| 155 |
if ( ( my $ref_type = Scalar::Util::reftype ${$term_ref} ) ne 'REF' |
| 156 |
or ( my $ref_ref_type = Scalar::Util::reftype ${ ${$term_ref} } ) ne |
| 157 |
'HASH' ) |
| 158 |
{ |
| 159 |
my $type = $ref_type eq 'REF' ? "REF to $ref_ref_type" : $ref_type; |
| 160 |
die "scalar in scalar[element] is not an hash ref: it is $type"; |
| 161 |
} ## end if ( ( my $ref_type = Scalar::Util::reftype ${$term_ref...})) |
| 162 |
return [ 'L', \\( ${ ${$term_ref} }->{ ${$element_ref} } ) ]; |
| 163 |
} ## end sub do_hash_index |
| 164 |
|
| 165 |
sub do_array_index { |
| 166 |
my ( undef, $term, undef, $element ) = @_; |
| 167 |
|
| 168 |
my $element_ref = coerce_to_R($element); |
| 169 |
my $element_ref_type = Scalar::Util::reftype $element_ref; |
| 170 |
die "element in term->[element] is not an scalar: $element_ref_type" |
| 171 |
if $element_ref_type ne 'SCALAR'; |
| 172 |
|
| 173 |
my ( $term_side, $term_ref ) = @{$term}; |
| 174 |
if ( $term_side eq 'R' ) { |
| 175 |
die 'rvalue term in scalar[element] not implemented'; |
| 176 |
} |
| 177 |
if ( ( my $ref_type = Scalar::Util::reftype ${$term_ref} ) ne 'REF' |
| 178 |
or ( my $ref_ref_type = Scalar::Util::reftype ${ ${$term_ref} } ) ne |
| 179 |
'ARRAY' ) |
| 180 |
{ |
| 181 |
my $type = $ref_type eq 'REF' ? "REF to $ref_ref_type" : $ref_type; |
| 182 |
die "scalar in scalar[element] is not an hash ref: it is $type"; |
| 183 |
} ## end if ( ( my $ref_type = Scalar::Util::reftype ${$term_ref...})) |
| 184 |
return [ 'L', \\( ${ ${$term_ref} }->[ ${$element_ref} ] ) ]; |
| 185 |
} ## end sub do_array_index |
| 186 |
|
| 187 |
sub do_argexpr { |
| 188 |
my ( undef, $argexpr, undef, $term ) = @_; |
| 189 |
my $argexpr_ref = coerce_to_R($argexpr); |
| 190 |
my @result; |
| 191 |
PROCESS_BY_REFTYPE: { |
| 192 |
my $reftype = Scalar::Util::reftype $argexpr_ref; |
| 193 |
if ( $reftype eq 'REF' ) { |
| 194 |
push @result, ${$argexpr_ref}; |
| 195 |
last PROCESS_BY_REFTYPE; |
| 196 |
} |
| 197 |
if ( $reftype eq 'SCALAR' ) { |
| 198 |
push @result, ${$argexpr_ref}; |
| 199 |
last PROCESS_BY_REFTYPE; |
| 200 |
} |
| 201 |
if ( $reftype eq 'ARRAY' ) { |
| 202 |
push @result, @{$argexpr_ref}; |
| 203 |
last PROCESS_BY_REFTYPE; |
| 204 |
} |
| 205 |
if ( $reftype eq 'HASH' ) { |
| 206 |
push @result, %{$argexpr_ref}; |
| 207 |
last PROCESS_BY_REFTYPE; |
| 208 |
} |
| 209 |
die "Unknown argexpr type: $_"; |
| 210 |
} ## end PROCESS_BY_REFTYPE: |
| 211 |
my $term_ref = coerce_to_R($term); |
| 212 |
PROCESS_BY_REFTYPE: { |
| 213 |
my $reftype = Scalar::Util::reftype $term_ref; |
| 214 |
if ( $reftype eq 'REF' ) { |
| 215 |
push @result, ${$term_ref}; |
| 216 |
last PROCESS_BY_REFTYPE; |
| 217 |
} |
| 218 |
if ( $reftype eq 'SCALAR' ) { |
| 219 |
push @result, ${$term_ref}; |
| 220 |
last PROCESS_BY_REFTYPE; |
| 221 |
} |
| 222 |
if ( $reftype eq 'ARRAY' ) { |
| 223 |
push @result, @{$term_ref}; |
| 224 |
last PROCESS_BY_REFTYPE; |
| 225 |
} |
| 226 |
if ( $reftype eq 'HASH' ) { |
| 227 |
push @result, %{$term_ref}; |
| 228 |
last PROCESS_BY_REFTYPE; |
| 229 |
} |
| 230 |
die "Unknown term type: $_"; |
| 231 |
} ## end PROCESS_BY_REFTYPE: |
| 232 |
return [ 'L', \\@result ]; |
| 233 |
} ## end sub do_argexpr |
| 234 |
|
| 235 |
# scalar assignment only |
| 236 |
sub do_assign { |
| 237 |
my ( undef, $lhs, undef, $rhs ) = @_; |
| 238 |
my ( $side, $lhs_ref ) = @{$lhs}; |
| 239 |
|
| 240 |
my $rhs_ref = coerce_to_R($rhs); |
| 241 |
|
| 242 |
# If the LHS is actually an rvalue, |
| 243 |
# it is the name of a variable |
| 244 |
# passed up from a 'scalar' rule. |
| 245 |
# In this 'toy' semantics, that's how |
| 246 |
# variables are "declared". |
| 247 |
if ( $side eq 'R' ) { |
| 248 |
my $name = ${$lhs_ref}; |
| 249 |
if ( not defined $name or ref $name ) { |
| 250 |
die 'assignment to non-lvalue: ', Data::Dumper::Dumper($name); |
| 251 |
} |
| 252 |
my $v = ${$rhs_ref}; |
| 253 |
$SYMTAB{SCALAR}->{$name} = \$v; |
| 254 |
$lhs_ref = \( $SYMTAB{SCALAR}->{$name} ); |
| 255 |
return [ 'L', $lhs_ref ]; |
| 256 |
} ## end if ( $side eq 'R' ) |
| 257 |
|
| 258 |
if ( Scalar::Util::readonly ${ ${$lhs_ref} } ) { |
| 259 |
die 'lhs is read only!'; |
| 260 |
} |
| 261 |
${ ${$lhs_ref} } = ${$rhs_ref}; |
| 262 |
return [ 'L', $lhs_ref ]; |
| 263 |
} ## end sub do_assign |
| 264 |
|
| 265 |
sub do_THING { |
| 266 |
my ( undef, $value ) = @_; |
| 267 |
## no critic (BuiltinFunctions::ProhibitStringyEval) |
| 268 |
$value = eval $value; |
| 269 |
return [ 'R', \$value ]; |
| 270 |
} |
| 271 |
|
| 272 |
sub do_anon_array { |
| 273 |
my ( undef, undef, $expr ) = @_; |
| 274 |
my $value_ref = coerce_to_R($expr); |
| 275 |
my @result = (); |
| 276 |
PROCESS_BY_REFTYPE: { |
| 277 |
my $reftype = Scalar::Util::reftype $value_ref; |
| 278 |
if ( $reftype eq 'SCALAR' ) { |
| 279 |
push @result, ${$value_ref}; |
| 280 |
last PROCESS_BY_REFTYPE; |
| 281 |
} |
| 282 |
if ( $reftype eq 'REF' ) { |
| 283 |
push @result, ${$value_ref}; |
| 284 |
last PROCESS_BY_REFTYPE; |
| 285 |
} |
| 286 |
if ( $reftype eq 'ARRAY' ) { |
| 287 |
push @result, @{$value_ref}; |
| 288 |
last PROCESS_BY_REFTYPE; |
| 289 |
} |
| 290 |
if ( $reftype eq 'HASH' ) { |
| 291 |
push @result, %{$value_ref}; |
| 292 |
last PROCESS_BY_REFTYPE; |
| 293 |
} |
| 294 |
die "Unknown expr type: ref to $_"; |
| 295 |
} ## end PROCESS_BY_REFTYPE: |
| 296 |
return [ 'L', \\[@result] ]; |
| 297 |
} ## end sub do_anon_array |
| 298 |
|
| 299 |
sub do_anon_empty_array { |
| 300 |
return [ 'L', \\[] ]; |
| 301 |
} |
| 302 |
|
| 303 |
sub do_anon_hash { |
| 304 |
my ( undef, undef, $expr ) = @_; |
| 305 |
my $value_ref = coerce_to_R($expr); |
| 306 |
my $result; |
| 307 |
PROCESS_BY_REFTYPE: { |
| 308 |
my $reftype = Scalar::Util::reftype $value_ref; |
| 309 |
die 'expr for anon hash cannot be REF' if $reftype eq 'REF'; |
| 310 |
die 'expr for anon hash cannot be SCALAR' if $reftype eq 'SCALAR'; |
| 311 |
if ( $reftype eq 'ARRAY' ) { |
| 312 |
$result = { @{$value_ref} }; |
| 313 |
last PROCESS_BY_REFTYPE; |
| 314 |
} |
| 315 |
if ( $reftype eq 'HASH' ) { |
| 316 |
$result = \%{$value_ref}; |
| 317 |
last PROCESS_BY_REFTYPE; |
| 318 |
} |
| 319 |
die "Unknown expr type: ref to $_"; |
| 320 |
} ## end PROCESS_BY_REFTYPE: |
| 321 |
return [ 'R', \$result ]; |
| 322 |
} ## end sub do_anon_hash |
| 323 |
|
| 324 |
sub do_anon_empty_hash { |
| 325 |
return [ 'R', \{} ]; |
| 326 |
} |
| 327 |
|
| 328 |
# This assume that all 'my' variables |
| 329 |
# are just ways to create |
| 330 |
# undef lvalue's -- which is how |
| 331 |
# Data::Dumper uses them |
| 332 |
sub do_term_my { |
| 333 |
my $v = undef; |
| 334 |
return [ 'L', \\$v ]; |
| 335 |
} |
| 336 |
|
| 337 |
# Very simplified here -- |
| 338 |
# References are dereferenced and passed up. |
| 339 |
# All scalars not |
| 340 |
# already defined are returned as strings. |
| 341 |
# It is assumed that they will either be the only |
| 342 |
# thing on the LHS of an assignment, or in |
| 343 |
# a my declaration. Data::Dumper uses my |
| 344 |
# declarations to create undef's so the scalar |
| 345 |
# names |
| 346 |
# that go up to term_my's will be thrown away. |
| 347 |
sub do_scalar { |
| 348 |
my ( undef, $dollar, $tagged_ob ) = @_; |
| 349 |
my ( $side, $ob_ref ) = @{$tagged_ob}; |
| 350 |
if ( $side eq 'R' ) { |
| 351 |
my $name = ${$ob_ref}; |
| 352 |
my $scalars = $SYMTAB{SCALAR}; |
| 353 |
if ( exists $scalars->{$name} ) { |
| 354 |
return [ 'L', \$scalars->{$name} ]; |
| 355 |
} |
| 356 |
return [ 'R', \$name ]; |
| 357 |
} ## end if ( $side eq 'R' ) |
| 358 |
$ob_ref = ${$ob_ref}; |
| 359 |
my $ob = ${$ob_ref}; |
| 360 |
if ( ref $ob ) { |
| 361 |
return [ 'L', \$ob ]; |
| 362 |
} |
| 363 |
return [ 'R', $ob ]; |
| 364 |
} ## end sub do_scalar |
| 365 |
|
| 366 |
sub do_uniop { |
| 367 |
my ( undef, $op ) = @_; |
| 368 |
die "Unknown uniop: $op" if $op ne 'undef'; |
| 369 |
return [ 'R', \undef ]; |
| 370 |
} |
| 371 |
|
| 372 |
# refgen is always an rvalue |
| 373 |
sub do_refgen { |
| 374 |
my ( undef, undef, $s1 ) = @_; |
| 375 |
return [ 'R', \coerce_to_R($s1) ]; |
| 376 |
} |
| 377 |
|
| 378 |
# prog should always return an rvalue |
| 379 |
sub do_prog { |
| 380 |
my ( undef, $s1 ) = @_; |
| 381 |
return [ 'R', coerce_to_R($s1) ]; |
| 382 |
} |
| 383 |
|
| 384 |
sub symbol_1 { |
| 385 |
my ( undef, $s1 ) = @_; |
| 386 |
return $s1; |
| 387 |
} |
| 388 |
|
| 389 |
sub symbol_2 { |
| 390 |
my ( undef, undef, $s2 ) = @_; |
| 391 |
return $s2; |
| 392 |
} |
| 393 |
|
| 394 |
sub token_1 { |
| 395 |
my ( undef, $a ) = @_; |
| 396 |
return [ 'R', \$a ]; |
| 397 |
} |
| 398 |
|
| 399 |
my %unwrapped = ( |
| 400 |
and_expr__t => \&symbol_1, |
| 401 |
anon_empty_hash => \&do_anon_empty_hash, |
| 402 |
anon_hash => \&do_anon_hash, |
| 403 |
argexpr__comma => \&symbol_1, |
| 404 |
argexpr => \&do_argexpr, |
| 405 |
argexpr__t => \&symbol_1, |
| 406 |
array_index => \&do_array_index, |
| 407 |
array_index_r => \&do_array_index, |
| 408 |
block => \&symbol_2, |
| 409 |
do_block => \&symbol_2, |
| 410 |
expr => \&symbol_1, |
| 411 |
hash_index => \&do_hash_index, |
| 412 |
hash_index_r => \&do_hash_index, |
| 413 |
indirob__block => \&symbol_1, |
| 414 |
indirob__WORD => \&token_1, |
| 415 |
lineseq__line => \&symbol_2, |
| 416 |
line__sideff => \&symbol_2, |
| 417 |
listexpr => \&symbol_1, |
| 418 |
myterm_scalar => \&symbol_1, |
| 419 |
or_expr__t => \&symbol_1, |
| 420 |
prog => \&do_prog, |
| 421 |
refgen => \&do_refgen, |
| 422 |
scalar => \&do_scalar, |
| 423 |
sideff => \&symbol_1, |
| 424 |
term_addop__t => \&symbol_1, |
| 425 |
term_andand__t => \&symbol_1, |
| 426 |
term_arrow__t => \&symbol_1, |
| 427 |
term_assign => \&do_assign, |
| 428 |
term_assign_lstop => \&do_assign, |
| 429 |
term_assign__t => \&symbol_1, |
| 430 |
term_bitandop__t => \&symbol_1, |
| 431 |
term_bitorop__t => \&symbol_1, |
| 432 |
term_cond__t => \&symbol_1, |
| 433 |
term_dotdot__t => \&symbol_1, |
| 434 |
term_eqop__t => \&symbol_1, |
| 435 |
term_hi__anon_array => \&do_anon_array, |
| 436 |
term_hi__anon_empty_array => \&do_anon_empty_array, |
| 437 |
term_hi__arrow_array => \&do_term_hi__arrow_array, |
| 438 |
term_hi__arrow_hash => \&do_term_hi__arrow_hash, |
| 439 |
term_hi__parens => \&symbol_2, |
| 440 |
term_hi__scalar => \&symbol_1, |
| 441 |
term_hi__subscripted => \&symbol_1, |
| 442 |
term_hi__THING => \&do_THING, |
| 443 |
term_increment__t => \&symbol_1, |
| 444 |
term_listop__t => \&symbol_1, |
| 445 |
term_lstop => \&do_term_lstop, |
| 446 |
term_matchop__t => \&symbol_1, |
| 447 |
term_mulop__t => \&symbol_1, |
| 448 |
term_my => \&do_term_my, |
| 449 |
term_notop__t => \&symbol_1, |
| 450 |
term_oror__t => \&symbol_1, |
| 451 |
term_powop__t => \&symbol_1, |
| 452 |
term_relop__t => \&symbol_1, |
| 453 |
term_require__t => \&symbol_1, |
| 454 |
term_shiftop__t => \&symbol_1, |
| 455 |
term__t => \&symbol_1, |
| 456 |
term_uminus__t => \&symbol_1, |
| 457 |
term_uniop__t => \&symbol_1, |
| 458 |
uniop => \&do_uniop, |
| 459 |
); |
| 460 |
|
| 461 |
sub gen_closure { |
| 462 |
my ( $lhs, $rhs, $action ) = @_; |
| 463 |
my $closure = $unwrapped{$action}; |
| 464 |
die "lhs=$lhs: $closure is not a closure" |
| 465 |
if defined $closure and ref $closure ne 'CODE'; |
| 466 |
if ( not defined $closure and scalar @{$rhs} <= 0 ) { |
| 467 |
$closure = sub { undef; } |
| 468 |
} |
| 469 |
return sub { |
| 470 |
if ( not defined $closure ) { |
| 471 |
die qq{No action ("$action") defined for }, |
| 472 |
"$lhs ::= " . ( join q{ }, map { $_ // q{-} } @{$rhs} ); |
| 473 |
} |
| 474 |
my $v = $closure->(@_); |
| 475 |
local $Data::Dumper::Terse = 1; |
| 476 |
local $Data::Dumper::Indent = 0; |
| 477 |
|
| 478 |
# local $Data::Dumper::Maxdepth = 4; |
| 479 |
push @main::OUTPUT, |
| 480 |
"$lhs ::= " |
| 481 |
. ( join q{ }, map { $_ // q{-} } @{$rhs} ) . q{; } |
| 482 |
. Data::Dumper::Dumper( \$v ); |
| 483 |
$v; |
| 484 |
}; |
| 485 |
} ## end sub gen_closure |
| 486 |
|
| 487 |
my %symbol = (); |
| 488 |
my %closure = (); |
| 489 |
|
| 490 |
## Tests from dumper.t |
| 491 |
|
| 492 |
my $parser = Marpa::R2::Perl->new( { closures => \&gen_closure } ); |
| 493 |
|
| 494 |
# Perlcritic cannot figure out that $a and $b are not magic variables |
| 495 |
# for a sort comparison |
| 496 |
|
| 497 |
# Trivial |
| 498 |
if (1) { |
| 499 |
my $a = 1; |
| 500 |
test( [$a], [qw(a)] ); |
| 501 |
} |
| 502 |
|
| 503 |
if (1) { |
| 504 |
my @c = ('c'); |
| 505 |
my $c = \@c; |
| 506 |
my $b = {}; |
| 507 |
my $a = [ 1, $b, $c ]; |
| 508 |
$b->{a} = $a; |
| 509 |
$b->{b} = $a->[1]; |
| 510 |
$b->{c} = $a->[2]; |
| 511 |
|
| 512 |
test( [ $a, $b, $c ], [qw(a b c)] ); |
| 513 |
} ## end if (1) |
| 514 |
|
| 515 |
if (1) { |
| 516 |
my $foo = { |
| 517 |
"abc\N{NULL}\'\efg" => "mno\N{NULL}", |
| 518 |
'reftest' => \\1, |
| 519 |
}; |
| 520 |
|
| 521 |
test( [$foo], [qw($foo)] ); |
| 522 |
} ## end if (1) |
| 523 |
|
| 524 |
if (1) { |
| 525 |
my $foo = 5; |
| 526 |
my @foo = ( -10, \$foo ); |
| 527 |
my %foo = ( a => 1, b => \$foo, c => \@foo ); |
| 528 |
$foo{d} = \%foo; |
| 529 |
$foo[2] = \%foo; |
| 530 |
|
| 531 |
test( [ \%foo ], [qw($foo)] ); |
| 532 |
} ## end if (1) |
| 533 |
|
| 534 |
if (1) { |
| 535 |
my @dogs = qw( Fido Wags ); |
| 536 |
my %kennel = ( |
| 537 |
First => \$dogs[0], |
| 538 |
Second => \$dogs[1], |
| 539 |
); |
| 540 |
$dogs[2] = \%kennel; |
| 541 |
my $mutts = \%kennel; |
| 542 |
eval { |
| 543 |
test( [ \@dogs, \%kennel, $mutts ], [qw($dogs $kennel $mutts)] ); |
| 544 |
1; |
| 545 |
} |
| 546 |
or die "Eval failed: $EVAL_ERROR"; |
| 547 |
} ## end if (1) |
| 548 |
|
| 549 |
if (1) { |
| 550 |
my $a = []; |
| 551 |
$a->[1] = \$a->[0]; |
| 552 |
test( [$a], [qw($a)] ); |
| 553 |
} |
| 554 |
|
| 555 |
if (1) { |
| 556 |
my $a = \\\\\'foo'; |
| 557 |
my $b = ${ ${$a} }; |
| 558 |
test( [ $a, $b ], [qw($a $b)] ); |
| 559 |
} |
| 560 |
|
| 561 |
if (1) { |
| 562 |
## no critic (Variables::RequireLocalizedPunctuationVars) |
| 563 |
my $b; |
| 564 |
my $a = [ { a => \$b }, { b => undef } ]; |
| 565 |
$b = [ { c => \$b }, { d => \$a } ]; |
| 566 |
test( [ $a, $b ], [qw($a $b)] ); |
| 567 |
} ## end if (1) |
| 568 |
|
| 569 |
if (1) { |
| 570 |
my $a = [ [ [ [ \\\\\'foo' ] ] ] ]; |
| 571 |
my $b = $a->[0][0]; |
| 572 |
my $c = ${ ${ $b->[0][0] } }; |
| 573 |
test( [ $a, $b, $c ], [qw($a $b $c)] ); |
| 574 |
} ## end if (1) |
| 575 |
|
| 576 |
if (1) { |
| 577 |
my $f = 'pearl'; |
| 578 |
my $e = [$f]; |
| 579 |
my $d = { 'e' => $e }; |
| 580 |
my $c = [$d]; |
| 581 |
my $b = { 'c' => $c }; |
| 582 |
my $a = { 'b' => $b }; |
| 583 |
test( [ $a, $b, $c, $d, $e, $f ], [qw($a $b $c $d $e $f)] ); |
| 584 |
} ## end if (1) |
| 585 |
|
| 586 |
if (1) { |
| 587 |
## no critic (Variables::RequireLocalizedPunctuationVars) |
| 588 |
my $a; |
| 589 |
$a = \$a; |
| 590 |
my $b = [$a]; |
| 591 |
test( [$b], [qw($b)] ); |
| 592 |
} ## end if (1) |
| 593 |
|
| 594 |
## Test from Randal Schwartz |
| 595 |
|
| 596 |
if (1) { |
| 597 |
my $x = bless { fred => 'flintstone' }, 'x'; |
| 598 |
my $y = bless \$x, 'y'; |
| 599 |
test( [ $x, $y ], [qw($x $y)] ); |
| 600 |
} |
| 601 |
|
| 602 |
## no critic (Subroutines::RequireArgUnpacking) |
| 603 |
sub test { |
| 604 |
|
| 605 |
my $input = Data::Dumper->new(@_)->Purity(1)->Sortkeys(1)->Dump; |
| 606 |
|
| 607 |
# Table by type and name of data |
| 608 |
# All data is kept as refs. |
| 609 |
# For orthogonality, that includes scalars. |
| 610 |
%SYMTAB = (); |
| 611 |
@OUTPUT = (); |
| 612 |
|
| 613 |
my $value_ref = $parser->parse( \$input ); |
| 614 |
if ( not defined $value_ref ) { |
| 615 |
die 'Perl parse failed'; |
| 616 |
} |
| 617 |
my @pointers = (); |
| 618 |
my @names = (); |
| 619 |
for my $type ( sort keys %SYMTAB ) { |
| 620 |
my $sigil = |
| 621 |
$type eq 'SCALAR' ? q{$} |
| 622 |
: $type eq 'REF' ? q{$} |
| 623 |
: $type eq 'ARRAY' ? q{@} |
| 624 |
: $type eq 'HASH' ? q{@} |
| 625 |
: q{!}; |
| 626 |
my $symbols_by_name = $SYMTAB{$type}; |
| 627 |
for my $name ( sort keys %{$symbols_by_name} ) { |
| 628 |
my $ref = $symbols_by_name->{$name}; |
| 629 |
|
| 630 |
# The testing convention is to pass scalars directly |
| 631 |
$type eq 'SCALAR' and $ref = ${$ref}; |
| 632 |
push @pointers, $ref; |
| 633 |
push @names, "$sigil$name"; |
| 634 |
} ## end for my $name ( sort keys %{$symbols_by_name} ) |
| 635 |
} ## end for my $type ( sort keys %SYMTAB ) |
| 636 |
my $output = |
| 637 |
Data::Dumper->new( \@pointers, \@names )->Purity(1)->Sortkeys(1) |
| 638 |
->Dump; |
| 639 |
Test::More::is( $output, $input ); |
| 640 |
return; |
| 641 |
|
| 642 |
} ## end sub test |
| 643 |
|
| 644 |
# vim: expandtab shiftwidth=4: |