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