| 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 error message |
| 18 |
# or an AST, but no semantics. |
| 19 |
# |
| 20 |
# Uses include tests of parsing of the SLIF DSL itself. |
| 21 |
|
| 22 |
use 5.010001; |
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
|
| 26 |
use Test::More tests => 38; |
| 27 |
use English qw( -no_match_vars ); |
| 28 |
use lib 'inc'; |
| 29 |
use Marpa::R2::Test; |
| 30 |
use Marpa::R2; |
| 31 |
use Data::Dumper; |
| 32 |
|
| 33 |
our $DEBUG = 0; |
| 34 |
my @tests_data = (); |
| 35 |
|
| 36 |
my $zero_grammar = \(<<'END_OF_SOURCE'); |
| 37 |
:default ::= action => ::array |
| 38 |
quartet ::= a a a a |
| 39 |
a ~ 'a' |
| 40 |
END_OF_SOURCE |
| 41 |
|
| 42 |
push @tests_data, |
| 43 |
[ $zero_grammar, 'aaaa', [qw(a a a a)], 'Parse OK', |
| 44 |
'No start statement' ]; |
| 45 |
|
| 46 |
my $colon1_grammar = \(<<'END_OF_SOURCE'); |
| 47 |
:default ::= action => ::array |
| 48 |
:start ::= quartet |
| 49 |
quartet ::= a a a a |
| 50 |
a ~ 'a' |
| 51 |
END_OF_SOURCE |
| 52 |
|
| 53 |
push @tests_data, |
| 54 |
[ |
| 55 |
$colon1_grammar, 'aaaa', |
| 56 |
[qw(a a a a)], 'Parse OK', |
| 57 |
'Colon start statement first' |
| 58 |
]; |
| 59 |
|
| 60 |
my $colon2_grammar = \(<<'END_OF_SOURCE'); |
| 61 |
:default ::= action => ::array |
| 62 |
quartet ::= a a a a |
| 63 |
:start ::= quartet |
| 64 |
a ~ 'a' |
| 65 |
END_OF_SOURCE |
| 66 |
|
| 67 |
push @tests_data, |
| 68 |
[ |
| 69 |
$colon2_grammar, 'aaaa', |
| 70 |
[qw(a a a a)], 'Parse OK', |
| 71 |
'Colon start statement second' |
| 72 |
]; |
| 73 |
|
| 74 |
my $english1_grammar = \(<<'END_OF_SOURCE'); |
| 75 |
:default ::= action => ::array |
| 76 |
start symbol is quartet |
| 77 |
quartet ::= a a a a |
| 78 |
a ~ 'a' |
| 79 |
END_OF_SOURCE |
| 80 |
|
| 81 |
push @tests_data, |
| 82 |
[ |
| 83 |
$english1_grammar, 'aaaa', |
| 84 |
[qw(a a a a)], 'Parse OK', |
| 85 |
'English start statement first' |
| 86 |
]; |
| 87 |
|
| 88 |
my $english2_grammar = \(<<'END_OF_SOURCE'); |
| 89 |
:default ::= action => ::array |
| 90 |
quartet ::= a a a a |
| 91 |
start symbol is quartet |
| 92 |
a ~ 'a' |
| 93 |
END_OF_SOURCE |
| 94 |
|
| 95 |
push @tests_data, [ |
| 96 |
$english2_grammar, 'aaaa', |
| 97 |
'SLIF grammar failed', |
| 98 |
<<'END_OF_MESSAGE', |
| 99 |
Parse of BNF/Scanless source failed: |
| 100 |
Length of symbol "statement" at line 2, column 13 is ambiguous |
| 101 |
Choices start with: quartet ::= a a a a |
| 102 |
Choice 1, length=20, ends at line 2, column 32 |
| 103 |
Choice 1: quartet ::= a a a a |
| 104 |
Choice 2, length=52, ends at line 3, column 31 |
| 105 |
Choice 2: quartet ::= a a a a\n start symbol is quarte |
| 106 |
END_OF_MESSAGE |
| 107 |
'English start statement second' |
| 108 |
]; |
| 109 |
|
| 110 |
my $invalid_syntax_grammar = \(<<'END_OF_SOURCE'); |
| 111 |
quartet$ ::= a b c d e f |
| 112 |
END_OF_SOURCE |
| 113 |
|
| 114 |
push @tests_data, [ |
| 115 |
$invalid_syntax_grammar, 'n/a', |
| 116 |
'SLIF grammar failed', |
| 117 |
<<'END_OF_MESSAGE', |
| 118 |
Parse of BNF/Scanless source failed |
| 119 |
Error in SLIF parse: No lexeme found at line 1, column 12 |
| 120 |
* String before error: quartet |
| 121 |
* The error was at line 1, column 12, and at character 0x0024 '$', ... |
| 122 |
* here: $ ::= a b c d e f\n |
| 123 |
END_OF_MESSAGE |
| 124 |
'Grammar with syntax error' |
| 125 |
]; |
| 126 |
|
| 127 |
# test <>-wrapping of SLIF symbol names containing spaces |
| 128 |
|
| 129 |
my $non_unique_sequence_grammar = \(<<'END_OF_SOURCE'); |
| 130 |
<sequence of items> ::= item* proper => 1 |
| 131 |
<sequence of items> ::= <forty two> |
| 132 |
END_OF_SOURCE |
| 133 |
|
| 134 |
push @tests_data, [ |
| 135 |
$non_unique_sequence_grammar, 'n/a', |
| 136 |
'SLIF grammar failed', |
| 137 |
<<'END_OF_MESSAGE', |
| 138 |
LHS of sequence rule would not be unique: <sequence of items> -> <forty two> |
| 139 |
END_OF_MESSAGE |
| 140 |
'Grammar with non-unique LHS sequence symbols' |
| 141 |
]; |
| 142 |
|
| 143 |
##### |
| 144 |
|
| 145 |
my $explicit_grammar1 = \(<<'END_OF_SOURCE'); |
| 146 |
:default ::= action => ::array |
| 147 |
quartet ::= a a a a; |
| 148 |
start symbol is quartet |
| 149 |
a ~ 'a' |
| 150 |
END_OF_SOURCE |
| 151 |
|
| 152 |
push @tests_data, |
| 153 |
[ |
| 154 |
$explicit_grammar1, 'aaaa', |
| 155 |
[qw(a a a a)], 'Parse OK', |
| 156 |
'Explicit English start statement second' |
| 157 |
]; |
| 158 |
|
| 159 |
##### |
| 160 |
|
| 161 |
{ |
| 162 |
|
| 163 |
# Marpa::R2::Display |
| 164 |
# name: statements separted by semicolon |
| 165 |
# start-after-line: END_OF_SOURCE |
| 166 |
# end-before-line: '^END_OF_SOURCE$' |
| 167 |
|
| 168 |
my $source = \(<<'END_OF_SOURCE'); |
| 169 |
:default ::= action => ::array |
| 170 |
quartet ::= a a a a; |
| 171 |
inaccessible is warn by default |
| 172 |
a ~ 'a' |
| 173 |
END_OF_SOURCE |
| 174 |
|
| 175 |
# Marpa::R2::Display::End |
| 176 |
|
| 177 |
push @tests_data, |
| 178 |
[ |
| 179 |
$source, 'aaaa', |
| 180 |
[qw(a a a a)], 'Parse OK', |
| 181 |
'Explicit inaccessible is warn statement second, using semi-colon' |
| 182 |
]; |
| 183 |
} |
| 184 |
|
| 185 |
### |
| 186 |
|
| 187 |
{ |
| 188 |
|
| 189 |
# Marpa::R2::Display |
| 190 |
# name: statements grouped in curly braces |
| 191 |
# start-after-line: END_OF_SOURCE |
| 192 |
# end-before-line: '^END_OF_SOURCE$' |
| 193 |
|
| 194 |
my $source = \(<<'END_OF_SOURCE'); |
| 195 |
{ |
| 196 |
:default ::= action => ::array |
| 197 |
quartet ::= a a a a |
| 198 |
} |
| 199 |
inaccessible is warn by default |
| 200 |
a ~ 'a' |
| 201 |
END_OF_SOURCE |
| 202 |
|
| 203 |
# Marpa::R2::Display::End |
| 204 |
|
| 205 |
push @tests_data, |
| 206 |
[ |
| 207 |
$source, 'aaaa', |
| 208 |
[qw(a a a a)], 'Parse OK', |
| 209 |
'Explicit inaccessible is warn statement second, using grouping' |
| 210 |
]; |
| 211 |
} |
| 212 |
|
| 213 |
##### |
| 214 |
|
| 215 |
my $explicit_grammar2 = \(<<'END_OF_SOURCE'); |
| 216 |
:default ::= action => ::array |
| 217 |
octet ::= a a a a |
| 218 |
start symbol <is> octet |
| 219 |
a ~ 'a' |
| 220 |
start ~ 'a' |
| 221 |
symbol ~ 'a' |
| 222 |
is ~ 'a' |
| 223 |
octet ::= a |
| 224 |
END_OF_SOURCE |
| 225 |
|
| 226 |
push @tests_data, |
| 227 |
[ |
| 228 |
$explicit_grammar2, 'aaaaaaaa', |
| 229 |
[qw(a a a a a a a), ['a']], 'Parse OK', |
| 230 |
'Long quartet; no start statement' |
| 231 |
]; |
| 232 |
|
| 233 |
##### |
| 234 |
# test null statements |
| 235 |
|
| 236 |
my $disambig_grammar = \(<<'END_OF_SOURCE'); |
| 237 |
;:default ::= action => ::array |
| 238 |
octet ::= a a a a |
| 239 |
;a ~ 'a';;;;; |
| 240 |
END_OF_SOURCE |
| 241 |
|
| 242 |
push @tests_data, |
| 243 |
[ |
| 244 |
$disambig_grammar, 'aaaa', |
| 245 |
[qw(a a a a)], 'Parse OK', |
| 246 |
'Grammar with null statements' |
| 247 |
]; |
| 248 |
|
| 249 |
##### |
| 250 |
# test grouped statements |
| 251 |
|
| 252 |
my $grouping_grammar = \(<<'END_OF_SOURCE'); |
| 253 |
;:default ::= action => ::array |
| 254 |
{quartet ::= a b c d }; |
| 255 |
a ~ 'a' { b ~ 'b' c~'c' } { d ~ 'd'; }; |
| 256 |
{ {;} } |
| 257 |
END_OF_SOURCE |
| 258 |
|
| 259 |
push @tests_data, |
| 260 |
[ |
| 261 |
$grouping_grammar, 'abcd', |
| 262 |
[qw(a b c d)], 'Parse OK', |
| 263 |
'Grammar with grouped statements' |
| 264 |
]; |
| 265 |
|
| 266 |
##### |
| 267 |
# test null adverbs |
| 268 |
|
| 269 |
{ |
| 270 |
my $grammar = \(<<'END_OF_SOURCE'); |
| 271 |
:default ::= ,action => ::array, |
| 272 |
quartet ::= a b c d , |
| 273 |
a ~ 'a' { b ~ 'b' c~'c' } d ~ 'd', |
| 274 |
END_OF_SOURCE |
| 275 |
|
| 276 |
push @tests_data, |
| 277 |
[ |
| 278 |
$grammar, 'abcd', |
| 279 |
[qw(a b c d)], 'Parse OK', |
| 280 |
'Grammar with null adverbs' |
| 281 |
]; |
| 282 |
} |
| 283 |
|
| 284 |
##### |
| 285 |
# test null adverbs |
| 286 |
|
| 287 |
{ |
| 288 |
my $grammar = \(<<'END_OF_SOURCE'); |
| 289 |
:default ::= ,action => ::array, |
| 290 |
quartet ::= a b c d e f |
| 291 |
{ a ~ 'a' { b ~ 'b' { c~'c' {; d~'d' {e~'e'}} }} f ~ 'f' } |
| 292 |
END_OF_SOURCE |
| 293 |
|
| 294 |
push @tests_data, |
| 295 |
[ |
| 296 |
$grammar, 'abcdef', |
| 297 |
[qw(a b c d e f)], 'Parse OK', |
| 298 |
'Grammar with nested statement groups' |
| 299 |
]; |
| 300 |
} |
| 301 |
|
| 302 |
##### |
| 303 |
# test discarding of spaces in array descriptor actions |
| 304 |
|
| 305 |
{ |
| 306 |
my $grammar = \(<<'END_OF_SOURCE'); |
| 307 |
:default ::= action => [lhs, value] |
| 308 |
lexeme default = action => [ lhs, value ] |
| 309 |
s ::= a |
| 310 |
a ~ '42' |
| 311 |
END_OF_SOURCE |
| 312 |
|
| 313 |
push @tests_data, |
| 314 |
[ |
| 315 |
$grammar, '42', |
| 316 |
[ 1, [ 2, '42' ] ], 'Parse OK', |
| 317 |
'Grammar with spaces in array descriptor actions' |
| 318 |
]; |
| 319 |
} |
| 320 |
|
| 321 |
##### |
| 322 |
|
| 323 |
if (1) { |
| 324 |
my $grammar = \(<<'END_OF_SOURCE'); |
| 325 |
:default ::= action => [ lhs, value] |
| 326 |
lexeme default = action => [lhs, value ] |
| 327 |
s ::= a |
| 328 |
a ~ '42' |
| 329 |
END_OF_SOURCE |
| 330 |
|
| 331 |
push @tests_data, |
| 332 |
[ |
| 333 |
$grammar, '42', |
| 334 |
[ 1, [ 2, '42' ] ], 'Parse OK', |
| 335 |
'Grammar with spaces in array descriptor actions' |
| 336 |
]; |
| 337 |
} |
| 338 |
|
| 339 |
if (1) { |
| 340 |
my $grammar = \(<<'END_OF_SOURCE'); |
| 341 |
:default ::= action => [ name, value] |
| 342 |
lexeme default = action => [name, value ] |
| 343 |
:start ::= start |
| 344 |
start ~ 'X' |
| 345 |
:discard ~ [^[:print:]] |
| 346 |
END_OF_SOURCE |
| 347 |
|
| 348 |
push @tests_data, |
| 349 |
[ |
| 350 |
$grammar, 'X', |
| 351 |
[ qw(start X) ], 'Parse OK', |
| 352 |
'Bug found by Jean-Damien Durand' |
| 353 |
]; |
| 354 |
} |
| 355 |
|
| 356 |
if (1) { |
| 357 |
use utf8; |
| 358 |
my $grammar = \(<<'END_OF_SOURCE'); |
| 359 |
:default ::= action => [name,values] |
| 360 |
<Š> ::= <Á> <Č> |
| 361 |
<Á> ::= a+ |
| 362 |
<Č> ::= c |
| 363 |
a ~ 'a' |
| 364 |
c ~ 'c' |
| 365 |
END_OF_SOURCE |
| 366 |
|
| 367 |
push @tests_data, |
| 368 |
[ |
| 369 |
$grammar, 'aac', [ 'Š', [qw(Á a a)], [qw(Č c)] ], |
| 370 |
'Parse OK', 'Bug in Unicode found by choroba' |
| 371 |
]; |
| 372 |
} |
| 373 |
|
| 374 |
TEST: |
| 375 |
for my $test_data (@tests_data) { |
| 376 |
my ( $source, $input, $expected_value, $expected_result, $test_name ) = |
| 377 |
@{$test_data}; |
| 378 |
my ( $actual_value, $actual_result ); |
| 379 |
PROCESSING: { |
| 380 |
my $grammar; |
| 381 |
if (not defined eval { |
| 382 |
$grammar = |
| 383 |
Marpa::R2::Scanless::G->new( { source => $source } ); |
| 384 |
1; |
| 385 |
} |
| 386 |
) |
| 387 |
{ |
| 388 |
say $EVAL_ERROR if $DEBUG; |
| 389 |
my $abbreviated_error = $EVAL_ERROR; |
| 390 |
|
| 391 |
chomp $abbreviated_error; |
| 392 |
$abbreviated_error =~ s/^ Marpa[:][:]R2 \s+ exception \s+ at \s+ .* \z//xms; |
| 393 |
$actual_value = 'SLIF grammar failed'; |
| 394 |
$actual_result = $abbreviated_error; |
| 395 |
last PROCESSING; |
| 396 |
} ## end if ( not defined eval { $grammar = Marpa::R2::Scanless::G...}) |
| 397 |
my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } ); |
| 398 |
|
| 399 |
if ( not defined eval { $recce->read( \$input ); 1 } ) { |
| 400 |
say $EVAL_ERROR if $DEBUG; |
| 401 |
my $abbreviated_error = $EVAL_ERROR; |
| 402 |
chomp $abbreviated_error; |
| 403 |
$actual_value = 'No parse'; |
| 404 |
$actual_result = $abbreviated_error; |
| 405 |
last PROCESSING; |
| 406 |
} ## end if ( not defined eval { $recce->read( \$input ); 1 }) |
| 407 |
my $value_ref = $recce->value(); |
| 408 |
if ( not defined $value_ref ) { |
| 409 |
$actual_value = 'No parse'; |
| 410 |
$actual_result = 'Input read to end but no parse'; |
| 411 |
last PROCESSING; |
| 412 |
} |
| 413 |
$actual_value = ${$value_ref}; |
| 414 |
$actual_result = 'Parse OK'; |
| 415 |
last PROCESSING; |
| 416 |
} ## end PROCESSING: |
| 417 |
|
| 418 |
Marpa::R2::Test::is( |
| 419 |
Data::Dumper::Dumper( \$actual_value ), |
| 420 |
Data::Dumper::Dumper( \$expected_value ), |
| 421 |
qq{Value of $test_name} |
| 422 |
); |
| 423 |
Marpa::R2::Test::is( $actual_result, $expected_result, |
| 424 |
qq{Result of $test_name} ); |
| 425 |
} ## end for my $test_data (@tests_data) |
| 426 |
|
| 427 |
# vim: expandtab shiftwidth=4: |