| 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 |
# Test grammar exceptions -- make sure problems actually |
| 18 |
# are detected. These tests are for problems which are supposed |
| 19 |
# to abend. |
| 20 |
|
| 21 |
use 5.010001; |
| 22 |
use strict; |
| 23 |
use warnings; |
| 24 |
use English qw( -no_match_vars ); |
| 25 |
use Test::More tests => 7; |
| 26 |
use Fatal qw(open close); |
| 27 |
|
| 28 |
use lib 'inc'; |
| 29 |
use Marpa::R2::Test; |
| 30 |
use Marpa::R2; |
| 31 |
|
| 32 |
## no critic (Subroutines::RequireArgUnpacking) |
| 33 |
|
| 34 |
sub default_action { |
| 35 |
shift; |
| 36 |
my $v_count = scalar @_; |
| 37 |
return q{} if $v_count <= 0; |
| 38 |
return $_[0] if $v_count == 1; |
| 39 |
return '(' . join( q{;}, @_ ) . ')'; |
| 40 |
} ## end sub default_action |
| 41 |
|
| 42 |
## use critic |
| 43 |
|
| 44 |
# NOTE: trace_result not used or tested yet. |
| 45 |
sub test_grammar { |
| 46 |
my ( $test_name, $grammar_args, $expected_error, $trace_result ) = @_; |
| 47 |
my $trace; |
| 48 |
my $memory; |
| 49 |
my $added_args = {}; |
| 50 |
if ($trace_result) { |
| 51 |
$trace = q{}; |
| 52 |
## no critic (InputOutput::RequireBriefOpen) |
| 53 |
open $memory, q{>}, \$trace; |
| 54 |
$added_args = { trace_file_handle => $memory }; |
| 55 |
} ## end if ($trace_result) |
| 56 |
my $eval_ok = eval { |
| 57 |
my $grammar = Marpa::R2::Grammar->new( $grammar_args, $added_args ); |
| 58 |
$grammar->precompute(); |
| 59 |
1; |
| 60 |
}; |
| 61 |
my $eval_error = $EVAL_ERROR; |
| 62 |
defined $trace_result and close $memory; |
| 63 |
DETERMINE_TEST_RESULT: { |
| 64 |
if ($eval_ok) { |
| 65 |
Test::More::fail("Failed to catch problem: $test_name"); |
| 66 |
last DETERMINE_TEST_RESULT; |
| 67 |
} |
| 68 |
$eval_error =~ s/ ^ Marpa::R2 \s+ exception \s+ at \s+ .* \z //xms; |
| 69 |
if ( $eval_error eq $expected_error ) { |
| 70 |
Test::More::pass("Successfully caught problem: $test_name"); |
| 71 |
last DETERMINE_TEST_RESULT; |
| 72 |
} |
| 73 |
my $diag_message = |
| 74 |
"Failed to find expected message, was expecting:\n"; |
| 75 |
my $temp; |
| 76 |
$temp = $expected_error; |
| 77 |
$temp =~ s/^/=== /xmsg; |
| 78 |
chomp $temp; |
| 79 |
$diag_message .= "$temp\n"; |
| 80 |
$diag_message .= "This was the message actually received:\n"; |
| 81 |
$temp = $eval_error; |
| 82 |
$temp =~ s/^/=== /xmsg; |
| 83 |
chomp $temp; |
| 84 |
$diag_message .= "$temp\n"; |
| 85 |
|
| 86 |
# $diag_message =~ s/^Marpa::R2 \s+ exception \s+ at .* $//xms; |
| 87 |
Test::More::diag($diag_message); |
| 88 |
Test::More::fail("Unexpected message: $test_name"); |
| 89 |
} ## end DETERMINE_TEST_RESULT: |
| 90 |
return if not defined $trace_result; |
| 91 |
if ( index( $trace, $trace_result ) < 0 ) { |
| 92 |
my $diag_message = |
| 93 |
"Failed to get expected trace result, was expecting:\n"; |
| 94 |
my $temp; |
| 95 |
$temp = $trace_result; |
| 96 |
$temp =~ s/^/=== /xmsg; |
| 97 |
chomp $temp; |
| 98 |
$diag_message .= "$temp\n"; |
| 99 |
$diag_message .= "This were the traces actually received:\n"; |
| 100 |
$temp = $eval_error; |
| 101 |
$temp =~ s/^/=== /xmsg; |
| 102 |
chomp $temp; |
| 103 |
$diag_message .= "$temp\n"; |
| 104 |
Test::More::diag($diag_message); |
| 105 |
Test::More::fail("Unexpected trace: $test_name"); |
| 106 |
} ## end if ( index( $trace, $trace_result ) < 0 ) |
| 107 |
else { |
| 108 |
Test::More::pass("Tracing OK: $test_name"); |
| 109 |
} |
| 110 |
return; |
| 111 |
} ## end sub test_grammar |
| 112 |
|
| 113 |
my $counted_nullable_grammar = { |
| 114 |
rules => [ |
| 115 |
{ lhs => 'S', |
| 116 |
rhs => ['Seq'], |
| 117 |
min => 0, |
| 118 |
}, |
| 119 |
{ lhs => 'Seq', |
| 120 |
rhs => [qw(A B)], |
| 121 |
}, |
| 122 |
{ lhs => 'A' }, |
| 123 |
{ lhs => 'B' }, |
| 124 |
], |
| 125 |
start => 'S', |
| 126 |
}; |
| 127 |
|
| 128 |
test_grammar( |
| 129 |
'counted nullable', |
| 130 |
$counted_nullable_grammar, |
| 131 |
qq{Nullable symbol "Seq" is on RHS of counted rule\n} |
| 132 |
. qq{Counted nullables confuse Marpa -- please rewrite the grammar\n} |
| 133 |
); |
| 134 |
|
| 135 |
my $duplicate_rule_grammar = { |
| 136 |
rules => [ |
| 137 |
{ lhs => 'Top', rhs => ['Dup'] }, |
| 138 |
{ lhs => 'Dup', |
| 139 |
rhs => ['Item'], |
| 140 |
}, |
| 141 |
{ lhs => 'Dup', |
| 142 |
rhs => ['Item'], |
| 143 |
}, |
| 144 |
{ lhs => 'Item', rhs => ['a'] }, |
| 145 |
], |
| 146 |
start => 'Top', |
| 147 |
}; |
| 148 |
test_grammar( 'duplicate rule', |
| 149 |
$duplicate_rule_grammar, qq{Duplicate rule: Dup -> Item\n} ); |
| 150 |
|
| 151 |
my $unique_lhs_grammar = { |
| 152 |
rules => [ |
| 153 |
{ lhs => 'Top', rhs => ['Dup'] }, |
| 154 |
{ lhs => 'Dup', |
| 155 |
rhs => ['Item'], |
| 156 |
min => 0, |
| 157 |
}, |
| 158 |
{ lhs => 'Dup', |
| 159 |
rhs => ['Item'], |
| 160 |
}, |
| 161 |
{ lhs => 'Item', rhs => ['a'] }, |
| 162 |
], |
| 163 |
start => 'Top', |
| 164 |
}; |
| 165 |
test_grammar( 'unique_lhs', |
| 166 |
$unique_lhs_grammar, qq{LHS of sequence rule would not be unique: Dup -> Item\n} ); |
| 167 |
|
| 168 |
my $nulling_terminal_grammar = { |
| 169 |
rules => [ |
| 170 |
{ lhs => 'Top', rhs => ['Bad'] }, |
| 171 |
{ lhs => 'Top', rhs => ['Good'] }, |
| 172 |
{ lhs => 'Bad', rhs => [] }, |
| 173 |
], |
| 174 |
start => 'Top', |
| 175 |
terminals => ['Good', 'Bad'], |
| 176 |
}; |
| 177 |
test_grammar( |
| 178 |
'nulling terminal grammar', |
| 179 |
$nulling_terminal_grammar, |
| 180 |
<<'END_OF_MESSAGE' |
| 181 |
Nulling symbol "Bad" is also a terminal |
| 182 |
A terminal symbol cannot also be a nulling symbol |
| 183 |
END_OF_MESSAGE |
| 184 |
); |
| 185 |
|
| 186 |
my $no_start_grammar = { |
| 187 |
rules => [ { lhs => 'Top', rhs => ['Bad'] }, ], |
| 188 |
terminals => ['Bad'], |
| 189 |
}; |
| 190 |
test_grammar( 'no start symbol', $no_start_grammar, "No start symbol specified in grammar\n" ); |
| 191 |
|
| 192 |
my $start_not_lhs_grammar = { |
| 193 |
rules => [ { lhs => 'Top', rhs => ['Bad'] }, ], |
| 194 |
terminals => ['Bad'], |
| 195 |
start => 'Bad', |
| 196 |
}; |
| 197 |
test_grammar( 'start symbol not on lhs', |
| 198 |
$start_not_lhs_grammar, qq{Start symbol "Bad" not on LHS of any rule\n} ); |
| 199 |
|
| 200 |
my $unproductive_start_grammar = { |
| 201 |
rules => [ |
| 202 |
{ lhs => 'Top', rhs => ['Bad'] }, |
| 203 |
{ lhs => 'Bad', rhs => ['Worse'] }, |
| 204 |
{ lhs => 'Worse', rhs => ['Bad'] }, |
| 205 |
{ lhs => 'Top', rhs => ['Good'] }, |
| 206 |
], |
| 207 |
terminals => ['Good'], |
| 208 |
start => 'Bad', |
| 209 |
}; |
| 210 |
test_grammar( |
| 211 |
'unproductive start symbol', |
| 212 |
$unproductive_start_grammar, |
| 213 |
qq{Unproductive start symbol: "Bad"\n} |
| 214 |
); |
| 215 |
|
| 216 |
1; # In case used as "do" file |
| 217 |
|
| 218 |
# Local Variables: |
| 219 |
# mode: cperl |
| 220 |
# cperl-indent-level: 4 |
| 221 |
# fill-column: 100 |
| 222 |
# End: |
| 223 |
# vim: expandtab shiftwidth=4: |