| 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 |
|
| 17 |
# Tests that include a grammar, an input, and an resolution |
| 18 |
# error message, but no (or minimal?) semantics. |
| 19 |
# |
| 20 |
# The intent is that this file will contain tests of the |
| 21 |
# valuator's resolution phase |
| 22 |
|
| 23 |
use 5.010001; |
| 24 |
use strict; |
| 25 |
use warnings; |
| 26 |
|
| 27 |
use Test::More tests => 6; |
| 28 |
use English qw( -no_match_vars ); |
| 29 |
use lib 'inc'; |
| 30 |
use Marpa::R2::Test; |
| 31 |
use Marpa::R2; |
| 32 |
use Data::Dumper; |
| 33 |
|
| 34 |
our $DEBUG = 0; |
| 35 |
my @tests_data = (); |
| 36 |
|
| 37 |
sub My_Semantics::new {} |
| 38 |
|
| 39 |
#### |
| 40 |
|
| 41 |
{ |
| 42 |
my $grammar = \(<<'END_OF_SOURCE'); |
| 43 |
:start ::= test |
| 44 |
test ::= 'X' action => nowhere |
| 45 |
END_OF_SOURCE |
| 46 |
|
| 47 |
push @tests_data, [ $grammar, 'X', |
| 48 |
'Failure in value() method', |
| 49 |
<<'END_OF_MESSAGE', |
| 50 |
Could not resolve rule action named 'nowhere' |
| 51 |
Rule was test ::= 'X' |
| 52 |
Failed resolution of action "nowhere" to My_Semantics::nowhere |
| 53 |
END_OF_MESSAGE |
| 54 |
'Missing action' ]; |
| 55 |
} |
| 56 |
|
| 57 |
#### |
| 58 |
|
| 59 |
# Test trivial grammar with action -- |
| 60 |
# Problem found by Jean-Damien |
| 61 |
|
| 62 |
{ |
| 63 |
my $grammar = \(<<'END_OF_SOURCE'); |
| 64 |
:start ::= test |
| 65 |
test ::= action => main::not_null |
| 66 |
END_OF_SOURCE |
| 67 |
|
| 68 |
push @tests_data, [ $grammar, q{}, |
| 69 |
'not null', |
| 70 |
'Parse OK', 'Trivial grammar with action' ]; |
| 71 |
} |
| 72 |
|
| 73 |
#### |
| 74 |
|
| 75 |
{ |
| 76 |
|
| 77 |
# Marpa::R2::Display |
| 78 |
# name: inaccessible is fatal statement |
| 79 |
# start-after-line: END_OF_SOURCE |
| 80 |
# end-before-line: '^END_OF_SOURCE$' |
| 81 |
|
| 82 |
my $source = <<'END_OF_SOURCE'; |
| 83 |
|
| 84 |
inaccessible is fatal by default |
| 85 |
:default ::= action => [symbol, name, values] |
| 86 |
lexeme default = action => [symbol, name, value] |
| 87 |
start ::= stuff* |
| 88 |
stuff ::= a | b |
| 89 |
a ::= x |
| 90 |
b ::= x |
| 91 |
c ::= x |
| 92 |
x ::= 'x' |
| 93 |
END_OF_SOURCE |
| 94 |
|
| 95 |
# Marpa::R2::Display::End |
| 96 |
|
| 97 |
my $input = 'xxx'; |
| 98 |
my $expected_value = 'SLIF grammar failed'; |
| 99 |
|
| 100 |
push @tests_data, |
| 101 |
[ |
| 102 |
\$source, $input, $expected_value, |
| 103 |
"Inaccessible symbol: c\n", qq{test "inaccessible is fatal by default"} |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
### |
| 108 |
|
| 109 |
TEST: |
| 110 |
for my $test_data (@tests_data) { |
| 111 |
my ( $source, $input, $expected_value, $expected_result, $test_name ) = |
| 112 |
@{$test_data}; |
| 113 |
my ( $actual_value, $actual_result ); |
| 114 |
PROCESSING: { |
| 115 |
my $grammar; |
| 116 |
if (not defined eval { |
| 117 |
$grammar = |
| 118 |
Marpa::R2::Scanless::G->new( { source => $source } ); |
| 119 |
1; |
| 120 |
} |
| 121 |
) |
| 122 |
{ |
| 123 |
say $EVAL_ERROR if $DEBUG; |
| 124 |
my $abbreviated_error = $EVAL_ERROR; |
| 125 |
|
| 126 |
chomp $abbreviated_error; |
| 127 |
$abbreviated_error =~ s/^ Marpa[:][:]R2 \s+ exception \s+ at \s+ .* \z//xms; |
| 128 |
$actual_value = 'SLIF grammar failed'; |
| 129 |
$actual_result = $abbreviated_error; |
| 130 |
last PROCESSING; |
| 131 |
} ## end if ( not defined eval { $grammar = Marpa::R2::Scanless::G...}) |
| 132 |
my $recce = Marpa::R2::Scanless::R->new( |
| 133 |
{ grammar => $grammar, semantics_package => 'My_Semantics' } ); |
| 134 |
|
| 135 |
if ( not defined eval { $recce->read( \$input ); 1 } ) { |
| 136 |
say $EVAL_ERROR if $DEBUG; |
| 137 |
my $abbreviated_error = $EVAL_ERROR; |
| 138 |
chomp $abbreviated_error; |
| 139 |
$abbreviated_error =~ s/\n.*//xms; |
| 140 |
$actual_value = 'No parse'; |
| 141 |
$actual_result = $abbreviated_error; |
| 142 |
last PROCESSING; |
| 143 |
} ## end if ( not defined eval { $recce->read( \$input ); 1 }) |
| 144 |
my $value_ref ; |
| 145 |
if ( not defined eval { $value_ref = $recce->value(); 1 } ) { |
| 146 |
say $EVAL_ERROR if $DEBUG; |
| 147 |
my $abbreviated_error = $EVAL_ERROR; |
| 148 |
chomp $abbreviated_error; |
| 149 |
$abbreviated_error =~ s/^ Marpa[:][:]R2 \s+ exception \s+ at \s+ .* \z//xms; |
| 150 |
$actual_value = 'Failure in value() method'; |
| 151 |
$actual_result = $abbreviated_error; |
| 152 |
last PROCESSING; |
| 153 |
} |
| 154 |
if ( not defined $value_ref ) { |
| 155 |
$actual_value = 'No parse'; |
| 156 |
$actual_result = 'Input read to end but no parse'; |
| 157 |
last PROCESSING; |
| 158 |
} |
| 159 |
$actual_value = ${$value_ref}; |
| 160 |
$actual_result = 'Parse OK'; |
| 161 |
last PROCESSING; |
| 162 |
} ## end PROCESSING: |
| 163 |
|
| 164 |
Marpa::R2::Test::is( |
| 165 |
Data::Dumper::Dumper( \$actual_value ), |
| 166 |
Data::Dumper::Dumper( \$expected_value ), |
| 167 |
qq{Value of $test_name} |
| 168 |
); |
| 169 |
Marpa::R2::Test::is( $actual_result, $expected_result, |
| 170 |
qq{Result of $test_name} ); |
| 171 |
} ## end for my $test_data (@tests_data) |
| 172 |
|
| 173 |
# An example action, for tests which need one |
| 174 |
sub main::not_null { |
| 175 |
return 'not null'; |
| 176 |
} |
| 177 |
|
| 178 |
# vim: expandtab shiftwidth=4: |