| 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 |
# Note: Converted from gabend.t |
| 18 |
|
| 19 |
# Test grammar exceptions -- make sure problems actually |
| 20 |
# are detected. These tests are for problems which are supposed |
| 21 |
# to abend. |
| 22 |
|
| 23 |
use 5.010001; |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use English qw( -no_match_vars ); |
| 28 |
use Test::More tests => 14; |
| 29 |
use Fatal qw(open close); |
| 30 |
use POSIX qw(setlocale LC_ALL); |
| 31 |
|
| 32 |
POSIX::setlocale(LC_ALL, "C"); |
| 33 |
|
| 34 |
use lib 'inc'; |
| 35 |
use Marpa::R2::Test; |
| 36 |
use Marpa::R2; |
| 37 |
|
| 38 |
## no critic (Subroutines::RequireArgUnpacking) |
| 39 |
|
| 40 |
sub default_action { |
| 41 |
shift; |
| 42 |
my $v_count = scalar @_; |
| 43 |
return q{} if $v_count <= 0; |
| 44 |
return $_[0] if $v_count == 1; |
| 45 |
return '(' . join( q{;}, @_ ) . ')'; |
| 46 |
} ## end sub default_action |
| 47 |
|
| 48 |
## use critic |
| 49 |
|
| 50 |
sub test_grammar { |
| 51 |
my ( $test_name, $dsl, $expected_error ) = @_; |
| 52 |
my $trace; |
| 53 |
my $memory; |
| 54 |
my $eval_ok = eval { |
| 55 |
my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } ); |
| 56 |
1; |
| 57 |
}; |
| 58 |
my $eval_error = $EVAL_ERROR; |
| 59 |
if ($eval_ok) { |
| 60 |
Test::More::fail("Failed to catch problem: $test_name"); |
| 61 |
} |
| 62 |
else { |
| 63 |
$eval_error =~ s/ ^ Marpa::R2 \s+ exception \s+ at \s+ .* \z //xms; |
| 64 |
Marpa::R2::Test::is( $eval_error, $expected_error, |
| 65 |
"Successfully caught problem: $test_name" ); |
| 66 |
} |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
if (1) { |
| 71 |
my $counted_nullable_grammar = <<'END_OF_DSL'; |
| 72 |
S ::= Seq* |
| 73 |
Seq ::= A |
| 74 |
A ::= |
| 75 |
END_OF_DSL |
| 76 |
|
| 77 |
test_grammar( |
| 78 |
'counted nullable', |
| 79 |
$counted_nullable_grammar, |
| 80 |
qq{Nullable symbol "Seq" is on RHS of counted rule\n} |
| 81 |
. qq{Counted nullables confuse Marpa -- please rewrite the grammar\n} |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
if (1) { |
| 86 |
my $duplicate_rule_grammar = <<'END_OF_DSL'; |
| 87 |
Top ::= Dup |
| 88 |
Dup ::= Item |
| 89 |
Dup ::= Item |
| 90 |
Item ::= a |
| 91 |
END_OF_DSL |
| 92 |
test_grammar( 'duplicate rule', |
| 93 |
$duplicate_rule_grammar, <<'EOS'); |
| 94 |
Duplicate rule: Dup -> Item |
| 95 |
EOS |
| 96 |
} |
| 97 |
|
| 98 |
if (1) { |
| 99 |
my $unique_lhs_grammar = <<'END_OF_DSL'; |
| 100 |
Top ::= Dup |
| 101 |
Dup ::= Item* |
| 102 |
Dup ::= Item |
| 103 |
Item ::= a |
| 104 |
END_OF_DSL |
| 105 |
test_grammar( 'unique_lhs', $unique_lhs_grammar, <<'EOS'); |
| 106 |
LHS of sequence rule would not be unique: Dup -> Item |
| 107 |
EOS |
| 108 |
} |
| 109 |
|
| 110 |
# Duplicate precedenced LHS: 2 precedenced rules |
| 111 |
if (1) { |
| 112 |
my $unique_lhs_grammar = <<'END_OF_DSL'; |
| 113 |
Top ::= Dup |
| 114 |
Dup ::= Dup '+' Dup || Dup '-' Dup || Item1 |
| 115 |
Dup ::= Dup '*' Dup || Dup '/' Dup || Item2 |
| 116 |
Item1 ::= a |
| 117 |
Item2 ::= a |
| 118 |
a ~ 'a' |
| 119 |
END_OF_DSL |
| 120 |
test_grammar( 'dup precedenced lhs', $unique_lhs_grammar, <<'EOS'); |
| 121 |
Duplicate rule: Dup -> Dup[0] |
| 122 |
EOS |
| 123 |
} |
| 124 |
|
| 125 |
# Duplicate precedenced LHS: precedenced, then empty |
| 126 |
if (1) { |
| 127 |
my $bad_lexeme_grammar = <<'END_OF_DSL'; |
| 128 |
Top ::= Bad |
| 129 |
Top ::= Good |
| 130 |
Bad ::= |
| 131 |
Bad ~ [\d\D] |
| 132 |
Good ~ [\d\D] |
| 133 |
END_OF_DSL |
| 134 |
test_grammar( |
| 135 |
'lexeme on G1 LHS', |
| 136 |
$bad_lexeme_grammar, |
| 137 |
<<'END_OF_MESSAGE' |
| 138 |
<Bad> is a lexeme but it is not a legal lexeme in G1: |
| 139 |
Lexemes must be G1 symbols that do not appear on a G1 LHS. |
| 140 |
END_OF_MESSAGE |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
if (1) { |
| 145 |
my $bad_lexeme_grammar = <<'END_OF_DSL'; |
| 146 |
Top ::= A |
| 147 |
A ::= |
| 148 |
Bad ~ [\d\D] |
| 149 |
END_OF_DSL |
| 150 |
test_grammar( |
| 151 |
'lexeme not on G1 RHS', |
| 152 |
$bad_lexeme_grammar, |
| 153 |
<<'END_OF_MESSAGE' |
| 154 |
<Bad> is a lexeme but it is not a legal lexeme in G1: |
| 155 |
Lexemes must be G1 symbols that do not appear on a G1 LHS. |
| 156 |
END_OF_MESSAGE |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
if (1) { |
| 161 |
my $bad_lexeme_grammar = <<'END_OF_DSL'; |
| 162 |
Top ::= Bad | Good |
| 163 |
Good ~ [\d\D] |
| 164 |
END_OF_DSL |
| 165 |
test_grammar( |
| 166 |
'lexeme not on L0 LHS', |
| 167 |
$bad_lexeme_grammar, |
| 168 |
<<'END_OF_MESSAGE' |
| 169 |
A lexeme in G1 is not a lexeme in any of the lexers: Bad |
| 170 |
END_OF_MESSAGE |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
if (1) { |
| 175 |
my $bad_lexeme_grammar = <<'END_OF_DSL'; |
| 176 |
Top ::= Bad | Good |
| 177 |
Good ~ Bad |
| 178 |
Bad ~ [\d\D] |
| 179 |
END_OF_DSL |
| 180 |
test_grammar( |
| 181 |
'lexeme on L0 RHS', |
| 182 |
$bad_lexeme_grammar, |
| 183 |
<<'END_OF_MESSAGE' |
| 184 |
A lexeme in G1 is not a lexeme in any of the lexers: Bad |
| 185 |
END_OF_MESSAGE |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
if (1) { |
| 190 |
my $bad_lexeme_grammar = <<'END_OF_DSL'; |
| 191 |
:lexeme ~ <Bad> |
| 192 |
Top ::= Bad |
| 193 |
Bad ::= Good |
| 194 |
Bad ~ [\d\D] |
| 195 |
Good ~ [\d\D] |
| 196 |
END_OF_DSL |
| 197 |
test_grammar( |
| 198 |
'declared lexeme on G1 LHS', |
| 199 |
$bad_lexeme_grammar, |
| 200 |
<<'END_OF_MESSAGE' |
| 201 |
<Bad> is a lexeme but it is not a legal lexeme in G1: |
| 202 |
Lexemes must be G1 symbols that do not appear on a G1 LHS. |
| 203 |
END_OF_MESSAGE |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
if (1) { |
| 208 |
my $bad_lexeme_grammar = <<'END_OF_DSL'; |
| 209 |
:lexeme ~ <Bad> |
| 210 |
Top ::= A |
| 211 |
A ::= |
| 212 |
Bad ~ [\d\D] |
| 213 |
END_OF_DSL |
| 214 |
test_grammar( |
| 215 |
'declared lexeme not on G1 RHS', |
| 216 |
$bad_lexeme_grammar, |
| 217 |
<<'END_OF_MESSAGE' |
| 218 |
<Bad> is a lexeme but it is not a legal lexeme in G1: |
| 219 |
Lexemes must be G1 symbols that do not appear on a G1 LHS. |
| 220 |
END_OF_MESSAGE |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
if (1) { |
| 225 |
my $bad_lexeme_grammar = <<'END_OF_DSL'; |
| 226 |
:lexeme ~ <Bad> |
| 227 |
Top ::= Bad | Good |
| 228 |
Good ~ [\d\D] |
| 229 |
END_OF_DSL |
| 230 |
test_grammar( |
| 231 |
'declared lexeme not on L0 LHS', |
| 232 |
$bad_lexeme_grammar, |
| 233 |
<<'END_OF_MESSAGE' |
| 234 |
A lexeme in G1 is not a lexeme in any of the lexers: Bad |
| 235 |
END_OF_MESSAGE |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
if (1) { |
| 240 |
my $bad_lexeme_grammar = <<'END_OF_DSL'; |
| 241 |
:lexeme ~ <Bad> |
| 242 |
Top ::= Bad | Good |
| 243 |
Good ~ Bad |
| 244 |
Bad ~ [\d\D] |
| 245 |
END_OF_DSL |
| 246 |
test_grammar( |
| 247 |
'declared lexeme on L0 RHS', |
| 248 |
$bad_lexeme_grammar, |
| 249 |
<<'END_OF_MESSAGE' |
| 250 |
A lexeme in G1 is not a lexeme in any of the lexers: Bad |
| 251 |
END_OF_MESSAGE |
| 252 |
); |
| 253 |
} |
| 254 |
|
| 255 |
if (1) { |
| 256 |
my $start_not_lhs_grammar = <<'END_OF_DSL'; |
| 257 |
inaccessible is fatal by default |
| 258 |
:start ::= Bad |
| 259 |
Top ::= Bad |
| 260 |
Bad ~ [\d\D] |
| 261 |
END_OF_DSL |
| 262 |
test_grammar( |
| 263 |
'start symbol not on lhs', |
| 264 |
$start_not_lhs_grammar, |
| 265 |
qq{Inaccessible symbol: Top\n} |
| 266 |
); |
| 267 |
} |
| 268 |
|
| 269 |
if (1) { |
| 270 |
my $unproductive_start_grammar = <<'END_OF_DSL'; |
| 271 |
:start ::= Bad |
| 272 |
Top ::= Bad |
| 273 |
Bad ::= Worse |
| 274 |
Worse ::= Bad |
| 275 |
Top ::= Good |
| 276 |
Good ~ [\d\D] |
| 277 |
END_OF_DSL |
| 278 |
test_grammar( |
| 279 |
'unproductive start symbol', |
| 280 |
$unproductive_start_grammar, |
| 281 |
qq{Unproductive start symbol\n} |
| 282 |
); |
| 283 |
} |
| 284 |
|
| 285 |
# vim: expandtab shiftwidth=4: |