| 1 |
#!perl |
| 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 |
# Ensure various coding errors are caught |
| 17 |
|
| 18 |
use 5.010001; |
| 19 |
use strict; |
| 20 |
use warnings; |
| 21 |
|
| 22 |
use Test::More tests => 7; |
| 23 |
|
| 24 |
use lib 'inc'; |
| 25 |
use Marpa::R2::Test; |
| 26 |
use Fatal qw( open close ); |
| 27 |
use English qw( -no_match_vars ); |
| 28 |
use Marpa::R2; |
| 29 |
|
| 30 |
our $DEFAULT_NULL_DESC = '[default null]'; |
| 31 |
our $NULL_DESC = '[null]'; |
| 32 |
|
| 33 |
my @features = qw( |
| 34 |
e_op_action default_action |
| 35 |
); |
| 36 |
|
| 37 |
my @tests = ( 'run phase warning', 'run phase error', 'run phase die', ); |
| 38 |
|
| 39 |
my %good_code = ( |
| 40 |
'e_op_action' => 'main::e_op_action', |
| 41 |
'e_pass_through' => 'main::e_pass_through', |
| 42 |
'e_number_action' => 'main::e_number_action', |
| 43 |
'default_action' => 'main::default_action', |
| 44 |
); |
| 45 |
|
| 46 |
# Code to produce a run phase warning |
| 47 |
sub run_phase_warning { |
| 48 |
my $x; |
| 49 |
warn 'Test Warning 1'; |
| 50 |
warn 'Test Warning 2'; |
| 51 |
$x++; |
| 52 |
return 1; |
| 53 |
} ## end sub run_phase_warning |
| 54 |
|
| 55 |
# Code to produce a run phase error |
| 56 |
sub run_phase_error { |
| 57 |
my $x = 0; |
| 58 |
$x = 1 / 0; |
| 59 |
return $x++; |
| 60 |
} |
| 61 |
|
| 62 |
# Code to produce a run phase die() |
| 63 |
sub run_phase_die { |
| 64 |
my $x = 0; |
| 65 |
die 'test call to die'; |
| 66 |
} |
| 67 |
|
| 68 |
my %test_arg; |
| 69 |
my %expected; |
| 70 |
for my $test (@tests) { |
| 71 |
for my $feature (@features) { |
| 72 |
$test_arg{$test}{$feature} = '1;'; |
| 73 |
$expected{$test}{$feature} = q{}; |
| 74 |
} |
| 75 |
} ## end for my $test (@tests) |
| 76 |
|
| 77 |
for my $feature (@features) { |
| 78 |
$test_arg{'run phase warning'}{$feature} = 'main::run_phase_warning'; |
| 79 |
$test_arg{'run phase error'}{$feature} = 'main::run_phase_error'; |
| 80 |
$test_arg{'run phase die'}{$feature} = 'main::run_phase_die'; |
| 81 |
} |
| 82 |
|
| 83 |
my $getting_headers = 1; |
| 84 |
my @headers; |
| 85 |
my $data = q{}; |
| 86 |
|
| 87 |
my $test_data = <<'END_OF_TEST_DATA'; |
| 88 |
|
| 89 |
| bad code run phase warning |
| 90 |
# this should be a run phase warning |
| 91 |
my $x = 0; |
| 92 |
warn "Test Warning 1"; |
| 93 |
warn "Test Warning 2"; |
| 94 |
$x++; |
| 95 |
1; |
| 96 |
__END__ |
| 97 |
|
| 98 |
| expected e_op_action run phase warning |
| 99 |
============================================================ |
| 100 |
* THERE WERE 2 WARNING(S) IN THE MARPA SEMANTICS: |
| 101 |
Marpa treats warnings as fatal errors |
| 102 |
* THIS IS WHAT MARPA WAS DOING WHEN THE PROBLEM OCCURRED: |
| 103 |
Computing value for rule: 3: F -> F MultOp F |
| 104 |
* WARNING MESSAGE NUMBER 0: |
| 105 |
Test Warning 1 at <LOCATION> |
| 106 |
* WARNING MESSAGE NUMBER 1: |
| 107 |
Test Warning 2 at <LOCATION> |
| 108 |
Marpa::R2 exception at <LOCATION> |
| 109 |
__END__ |
| 110 |
|
| 111 |
| expected default_action run phase warning |
| 112 |
============================================================ |
| 113 |
* THERE WERE 2 WARNING(S) IN THE MARPA SEMANTICS: |
| 114 |
Marpa treats warnings as fatal errors |
| 115 |
* THIS IS WHAT MARPA WAS DOING WHEN THE PROBLEM OCCURRED: |
| 116 |
Computing value for rule: 8: trailer -> Text |
| 117 |
* WARNING MESSAGE NUMBER 0: |
| 118 |
Test Warning 1 at <LOCATION> |
| 119 |
* WARNING MESSAGE NUMBER 1: |
| 120 |
Test Warning 2 at <LOCATION> |
| 121 |
Marpa::R2 exception at <LOCATION> |
| 122 |
__END__ |
| 123 |
|
| 124 |
| bad code run phase error |
| 125 |
# this should be a run phase error |
| 126 |
my $x = 0; |
| 127 |
$x = 711/0; |
| 128 |
$x++; |
| 129 |
1; |
| 130 |
__END__ |
| 131 |
|
| 132 |
| expected e_op_action run phase error |
| 133 |
============================================================ |
| 134 |
* THE MARPA SEMANTICS PRODUCED A FATAL ERROR |
| 135 |
* THIS IS WHAT MARPA WAS DOING WHEN THE PROBLEM OCCURRED: |
| 136 |
Computing value for rule: 3: F -> F MultOp F |
| 137 |
* THIS WAS THE FATAL ERROR MESSAGE: |
| 138 |
Illegal division by zero at <LOCATION> |
| 139 |
Marpa::R2 exception at <LOCATION> |
| 140 |
__END__ |
| 141 |
|
| 142 |
| expected default_action run phase error |
| 143 |
============================================================ |
| 144 |
* THE MARPA SEMANTICS PRODUCED A FATAL ERROR |
| 145 |
* THIS IS WHAT MARPA WAS DOING WHEN THE PROBLEM OCCURRED: |
| 146 |
Computing value for rule: 8: trailer -> Text |
| 147 |
* THIS WAS THE FATAL ERROR MESSAGE: |
| 148 |
Illegal division by zero at <LOCATION> |
| 149 |
Marpa::R2 exception at <LOCATION> |
| 150 |
__END__ |
| 151 |
|
| 152 |
| bad code run phase die |
| 153 |
# this is a call to die() |
| 154 |
my $x = 0; |
| 155 |
die 'test call to die'; |
| 156 |
$x++; |
| 157 |
1; |
| 158 |
__END__ |
| 159 |
|
| 160 |
| expected e_op_action run phase die |
| 161 |
============================================================ |
| 162 |
* THE MARPA SEMANTICS PRODUCED A FATAL ERROR |
| 163 |
* THIS IS WHAT MARPA WAS DOING WHEN THE PROBLEM OCCURRED: |
| 164 |
Computing value for rule: 3: F -> F MultOp F |
| 165 |
* THIS WAS THE FATAL ERROR MESSAGE: |
| 166 |
test call to die at <LOCATION> |
| 167 |
Marpa::R2 exception at <LOCATION> |
| 168 |
__END__ |
| 169 |
|
| 170 |
| expected default_action run phase die |
| 171 |
============================================================ |
| 172 |
* THE MARPA SEMANTICS PRODUCED A FATAL ERROR |
| 173 |
* THIS IS WHAT MARPA WAS DOING WHEN THE PROBLEM OCCURRED: |
| 174 |
Computing value for rule: 8: trailer -> Text |
| 175 |
* THIS WAS THE FATAL ERROR MESSAGE: |
| 176 |
test call to die at <LOCATION> |
| 177 |
Marpa::R2 exception at <LOCATION> |
| 178 |
__END__ |
| 179 |
|
| 180 |
END_OF_TEST_DATA |
| 181 |
|
| 182 |
## no critic (InputOutput::RequireBriefOpen) |
| 183 |
open my $test_data_fh, q{<}, \$test_data; |
| 184 |
## use critic |
| 185 |
LINE: while ( my $line = <$test_data_fh> ) { |
| 186 |
|
| 187 |
if ($getting_headers) { |
| 188 |
next LINE if $line =~ m/ \A \s* \Z/xms; |
| 189 |
if ( $line =~ s/ \A [|] \s+ //xms ) { |
| 190 |
chomp $line; |
| 191 |
push @headers, $line; |
| 192 |
next LINE; |
| 193 |
} |
| 194 |
else { |
| 195 |
$getting_headers = 0; |
| 196 |
$data = q{}; |
| 197 |
} |
| 198 |
} ## end if ($getting_headers) |
| 199 |
|
| 200 |
# getting data |
| 201 |
|
| 202 |
if ( $line =~ /\A__END__\Z/xms ) { |
| 203 |
HEADER: while ( my $header = pop @headers ) { |
| 204 |
if ( $header =~ s/\A expected \s //xms ) { |
| 205 |
my ( $feature, $test ) = |
| 206 |
( $header =~ m/\A (\S*) \s+ (.*) \Z/xms ); |
| 207 |
die |
| 208 |
"expected result given for unknown test, feature: $test, $feature" |
| 209 |
if not defined $expected{$test}{$feature}; |
| 210 |
$expected{$test}{$feature} = $data; |
| 211 |
next HEADER; |
| 212 |
} ## end if ( $header =~ s/\A expected \s //xms ) |
| 213 |
if ( $header =~ s/\A good \s code \s //xms ) { |
| 214 |
die 'Good code should no longer be in data section'; |
| 215 |
} |
| 216 |
if ( $header =~ s/\A bad \s code \s //xms ) { |
| 217 |
chomp $header; |
| 218 |
die "test code given for unknown test: $header" |
| 219 |
if not defined $test_arg{$header}; |
| 220 |
next HEADER; |
| 221 |
} ## end if ( $header =~ s/\A bad \s code \s //xms ) |
| 222 |
die "Bad header: $header"; |
| 223 |
} # HEADER |
| 224 |
$getting_headers = 1; |
| 225 |
$data = q{}; |
| 226 |
} # if $line |
| 227 |
|
| 228 |
$data .= $line; |
| 229 |
} ## end LINE: while ( my $line = <$test_data_fh> ) |
| 230 |
|
| 231 |
sub canonical { |
| 232 |
my $template = shift; |
| 233 |
|
| 234 |
# allow for this test file to change name |
| 235 |
# as long as it remains lower-case, with |
| 236 |
# _ or - |
| 237 |
$template =~ s{ |
| 238 |
\s at \s t[^.]+[.]t \s line \s \d+ [^\n]* |
| 239 |
}{ at <LOCATION>}gxms; |
| 240 |
return $template; |
| 241 |
} ## end sub canonical |
| 242 |
|
| 243 |
sub run_test { |
| 244 |
my $args = shift; |
| 245 |
|
| 246 |
my $e_op_action = $good_code{e_op_action}; |
| 247 |
my $e_pass_through = $good_code{e_pass_through}; |
| 248 |
my $e_number_action = $good_code{e_number_action}; |
| 249 |
my $default_action = $good_code{default_action}; |
| 250 |
|
| 251 |
### e_op_action default: $e_op_action |
| 252 |
### e_number_action default: $e_number_action |
| 253 |
|
| 254 |
ARG: for my $arg ( keys %{$args} ) { |
| 255 |
my $value = $args->{$arg}; |
| 256 |
my $run_test_arg = lc $arg; |
| 257 |
if ( $run_test_arg eq 'e_op_action' ) { |
| 258 |
$e_op_action = $value; |
| 259 |
next ARG; |
| 260 |
} |
| 261 |
if ( $run_test_arg eq 'e_number_action' ) { |
| 262 |
$e_number_action = $value; |
| 263 |
next ARG; |
| 264 |
} |
| 265 |
if ( $run_test_arg eq 'default_action' ) { |
| 266 |
$default_action = $value; |
| 267 |
next ARG; |
| 268 |
} |
| 269 |
die "unknown argument to run_test: $arg"; |
| 270 |
} ## end ARG: for my $arg ( keys %{$args} ) |
| 271 |
|
| 272 |
### e_op_action: $e_op_action |
| 273 |
### e_number_action: $e_number_action |
| 274 |
|
| 275 |
my $grammar = Marpa::R2::Grammar->new( |
| 276 |
{ start => 'S', |
| 277 |
rules => [ |
| 278 |
[ 'S', [qw/T trailer optional_trailer1 optional_trailer2/], ], |
| 279 |
[ 'T', [qw/T AddOp T/], $e_op_action, ], |
| 280 |
[ 'T', [qw/F/], $e_pass_through, ], |
| 281 |
[ 'F', [qw/F MultOp F/], $e_op_action, ], |
| 282 |
[ 'F', [qw/Number/], $e_number_action, ], |
| 283 |
[ 'optional_trailer1', [qw/trailer/], ], |
| 284 |
[ 'optional_trailer1', [], ], |
| 285 |
[ 'optional_trailer2', [], 'main::NULL_DESC' ], |
| 286 |
[ 'trailer', [qw/Text/], ], |
| 287 |
], |
| 288 |
default_action => $default_action, |
| 289 |
default_empty_action => 'main::DEFAULT_NULL_DESC', |
| 290 |
terminals => [qw(Number AddOp MultOp Text)], |
| 291 |
} |
| 292 |
); |
| 293 |
$grammar->precompute(); |
| 294 |
|
| 295 |
my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } ); |
| 296 |
|
| 297 |
$recce->read( Number => 2 ); |
| 298 |
$recce->read( MultOp => q{*} ); |
| 299 |
$recce->read( Number => 3 ); |
| 300 |
$recce->read( AddOp => q{+} ); |
| 301 |
$recce->read( Number => 4 ); |
| 302 |
$recce->read( MultOp => q{*} ); |
| 303 |
$recce->read( Number => 1 ); |
| 304 |
$recce->read( Text => q{trailer} ); |
| 305 |
|
| 306 |
$recce->end_input(); |
| 307 |
|
| 308 |
my $expected = '(((2*3)+(4*1))==10;trailer;[default null];[null])'; |
| 309 |
my $value_ref = $recce->value(); |
| 310 |
my $value = $value_ref ? ${$value_ref} : 'No parse'; |
| 311 |
Marpa::R2::Test::is( $value, $expected, 'Ambiguous Equation Value' ); |
| 312 |
|
| 313 |
return 1; |
| 314 |
|
| 315 |
} # sub run_test |
| 316 |
|
| 317 |
run_test( {} ); |
| 318 |
|
| 319 |
for my $test (@tests) { |
| 320 |
FEATURE: for my $feature (@features) { |
| 321 |
next FEATURE if not defined $expected{$test}{$feature}; |
| 322 |
my $test_name = "$test in $feature"; |
| 323 |
if ( eval { run_test( { $feature => $test_arg{$test}{$feature}, } ); } |
| 324 |
) |
| 325 |
{ |
| 326 |
Test::More::fail( |
| 327 |
"$test_name did not fail -- that shouldn't happen"); |
| 328 |
} ## end if ( eval { run_test( { $feature => $test_arg{$test}...})}) |
| 329 |
else { |
| 330 |
my $eval_error = $EVAL_ERROR; |
| 331 |
Marpa::R2::Test::is( canonical($eval_error), |
| 332 |
$expected{$test}{$feature}, $test_name ); |
| 333 |
} |
| 334 |
} ## end FEATURE: for my $feature (@features) |
| 335 |
} ## end for my $test (@tests) |
| 336 |
|
| 337 |
## no critic (Subroutines::RequireArgUnpacking) |
| 338 |
|
| 339 |
sub e_pass_through { |
| 340 |
shift; |
| 341 |
return $_[0]; |
| 342 |
} |
| 343 |
|
| 344 |
sub e_op_action { |
| 345 |
shift; |
| 346 |
my ( $right_string, $right_value ) = ( $_[2] =~ /^(.*)==(.*)$/xms ); |
| 347 |
my ( $left_string, $left_value ) = ( $_[0] =~ /^(.*)==(.*)$/xms ); |
| 348 |
my $op = $_[1]; |
| 349 |
my $value; |
| 350 |
if ( $op eq q{+} ) { |
| 351 |
$value = $left_value + $right_value; |
| 352 |
} |
| 353 |
elsif ( $op eq q{*} ) { |
| 354 |
$value = $left_value * $right_value; |
| 355 |
} |
| 356 |
elsif ( $op eq q{-} ) { |
| 357 |
$value = $left_value - $right_value; |
| 358 |
} |
| 359 |
else { |
| 360 |
die "Unknown op: $op"; |
| 361 |
} |
| 362 |
return '(' . $left_string . $op . $right_string . ')==' . $value; |
| 363 |
} ## end sub e_op_action |
| 364 |
|
| 365 |
sub e_number_action { |
| 366 |
shift; |
| 367 |
my $v0 = pop @_; |
| 368 |
return $v0 . q{==} . $v0; |
| 369 |
} |
| 370 |
|
| 371 |
sub default_action { |
| 372 |
shift; |
| 373 |
my $v_count = scalar @_; |
| 374 |
return q{} if $v_count <= 0; |
| 375 |
return $_[0] if $v_count == 1; |
| 376 |
return '(' . join( q{;}, ( map { $_ // 'undef' } @_ ) ) . ')'; |
| 377 |
} ## end sub default_action |
| 378 |
|
| 379 |
# Local Variables: |
| 380 |
# mode: cperl |
| 381 |
# cperl-indent-level: 4 |
| 382 |
# fill-column: 100 |
| 383 |
# End: |
| 384 |
# vim: expandtab shiftwidth=4: |