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 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
|
#!perl
# Copyright 2014 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/.
# Tests which require only grammar, input, and an output with no
# semantics -- usually just an AST
use 5.010;
use strict;
use warnings;
use Test::More tests => 32;
use English qw( -no_match_vars );
use lib 'inc';
use Marpa::R2::Test;
use Marpa::R2;
use Data::Dumper;
my @tests_data = ();
our $DEBUG = 0;
# In crediting test, JDD = Jean-Damien Durand
if (1) {
my $glenn_grammar = Marpa::R2::Scanless::G->new(
{ source => \(<<'END_OF_SOURCE'),
:default ::= action => ::array
Start ::= Child DoubleColon Token
DoubleColon ~ '::'
Child ~ 'child'
Token ~
word
| word ':' word
word ~ [\w]+
END_OF_SOURCE
}
);
my $input = 'child::book';
push @tests_data,
[
$glenn_grammar,
'child::book',
[ 'child', q{::}, 'book' ],
'Parse OK',
'Nate Glenn bug regression'
];
} ## end if (0)
# Marpa::R2::Display
# name: Case-insensitive characters examples
# start-after-line: END_OF_SOURCE
# end-before-line: '^END_OF_SOURCE$'
if (1) {
my $ic_grammar = Marpa::R2::Scanless::G->new(
{ source => \(<<'END_OF_SOURCE'),
:default ::= action => ::array
Start ::= Child DoubleColon Token
DoubleColon ~ '::'
Child ~ 'cHILd':i
Token ~
word
| word ':' word
word ~ [\w]:ic +
END_OF_SOURCE
}
);
# Marpa::R2::Display::End
push @tests_data,
[
$ic_grammar,
'ChilD::BooK',
[ 'ChilD', q{::}, 'BooK' ],
'Parse OK',
'Case insensitivity test'
];
} ## end if (0)
if (1) {
my $durand_grammar1 = Marpa::R2::Scanless::G->new(
{ source => \(<<'END_OF_SOURCE'),
:default ::= action => ::array
start symbol is test
test ::= TEST
:lexeme ~ TEST
TEST ~ '## Allowed in the input' NEWLINE
WS ~ [ \t]
WS_any ~ WS*
POUND ~ '#'
_NEWLINE ~ [\n]
NOT_NEWLINE_any ~ [^\n]*
NEWLINE ~ _NEWLINE
COMMENT ~ WS_any POUND NOT_NEWLINE_any _NEWLINE
:discard ~ COMMENT
BLANKLINE ~ WS_any _NEWLINE
:discard ~ BLANKLINE
END_OF_SOURCE
}
);
push @tests_data, [
$durand_grammar1, <<INPUT,
## Allowed in the input
# Another comment
INPUT
["## Allowed in the input\n"],
'Parse OK',
'JDD test of discard versus accepted'
];
} ## end if (0)
if (1) {
my $durand_grammar2 = Marpa::R2::Scanless::G->new(
{ source => \(<<'END_OF_SOURCE'),
:default ::= action => ::array
test ::= 'test input' NEWLINE
WS ~ [ \t]
WS_any ~ WS*
POUND ~ '#'
_NEWLINE ~ [\n]
NOT_NEWLINE_any ~ [^\n]*
NEWLINE ~ _NEWLINE
COMMENT ~ WS_any POUND NOT_NEWLINE_any _NEWLINE
:discard ~ COMMENT
BLANKLINE ~ WS_any _NEWLINE
:discard ~ BLANKLINE
END_OF_SOURCE
}
);
push @tests_data, [
$durand_grammar2, <<INPUT,
# Comment followed by a newline
# Another comment
test input
INPUT
[ 'test input', "\n" ],
'Parse OK',
'Regression test of bug found by JDD'
];
}
# ===============
if (1) {
my $durand_grammar3 = Marpa::R2::Scanless::G->new(
{ source => \(<<'END_OF_SOURCE'),
:default ::= action => ::array
Script ::= '=' '/' 'dumb'
_WhiteSpace ~ ' '
_LineTerminator ~ [\n]
_SingleLineComment ~ '//' _SingleLineCommentCharsopt
_SingleLineCommentChars ~ _SingleLineCommentChar _SingleLineCommentCharsopt
_SingleLineCommentCharsopt ~ _SingleLineCommentChars
_SingleLineCommentCharsopt ~
_SingleLineCommentChar ~ [^\n]
_S ~
_WhiteSpace
| _LineTerminator
| _SingleLineComment
S_MANY ~ _S+
:discard ~ S_MANY
END_OF_SOURCE
}
);
push @tests_data, [
$durand_grammar3, <<INPUT,
= / dumb
INPUT
[qw(= / dumb)],
'Parse OK',
'Regression test of perl_pos bug found by JDD'
];
}
# Test of forgiving token from Peter Stuifzand
if (1) {
# Marpa::R2::Display
# name: forgiving adverb example
# start-after-line: END_OF_SOURCE
# end-before-line: '^END_OF_SOURCE$'
my $source = <<'END_OF_SOURCE';
:default ::= action => ::array
product ::= sku (nl) name (nl) price price price (nl)
sku ~ sku_0 '.' sku_0
sku_0 ~ [\d]+
price ~ price_0 ',' price_0
price_0 ~ [\d]+
nl ~ [\n]
sp ~ [ ]+
:discard ~ sp
:lexeme ~ <name> forgiving => 1
name ~ [^\n]+
END_OF_SOURCE
# Marpa::R2::Display::END
my $input = <<'INPUT';
130.12312
Descriptive line
1,10 1,10 1,30
INPUT
my $slg = Marpa::R2::Scanless::G->new( { source => \$source } );
push @tests_data,
[
$slg, $input,
[ '130.12312', 'Descriptive line', '1,10', '1,10', '1,30' ],
'Parse OK', 'Test of forgiving token from Peter Stuifzand'
];
}
# Test of LATM token from Ruslan Zakirov
if (1) {
# Marpa::R2::Display
# name: latm adverb example
# start-after-line: END_OF_SOURCE
# end-before-line: '^END_OF_SOURCE$'
my $source = <<'END_OF_SOURCE';
:default ::= action => ::array
:start ::= content
content ::= name ':' value
name ~ [A-Za-z0-9-]+
value ~ [A-Za-z0-9:-]+
:lexeme ~ value latm => 1
END_OF_SOURCE
# Marpa::R2::Display
my $input = 'UID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1';
my $expected_output =
[ 'UID', ':', 'urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1' ];
my $slg = Marpa::R2::Scanless::G->new( { source => \$source } );
push @tests_data,
[
$slg, $input, $expected_output,
'Parse OK', 'Test of LATM token from Ruslan Zakirov'
];
}
# Test of LATM token from Ruslan Zakirov
# This time using the lexeme default statement
if (1) {
my $source = <<'END_OF_SOURCE';
lexeme default = latm => 1
:default ::= action => ::array
:start ::= content
content ::= name ':' value
name ~ [A-Za-z0-9-]+
value ~ [A-Za-z0-9:-]+
END_OF_SOURCE
my $input = 'UID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1';
my $expected_output =
[ 'UID', ':', 'urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1' ];
my $slg = Marpa::R2::Scanless::G->new( { source => \$source } );
push @tests_data,
[
$slg, $input, $expected_output,
'Parse OK', 'Test of LATM token using lexeme default statement'
];
}
# Test of rank adverb
if (1) {
# Marpa::R2::Display
# name: rank adverb example
# start-after-line: END_OF_SOURCE
# end-before-line: '^END_OF_SOURCE$'
my $source = <<'END_OF_SOURCE';
lexeme default = latm => 1
:default ::= action => [name,values]
:start ::= externals
externals ::= external* action => [values]
external ::= special action => ::first
| unspecial action => ::first
unspecial ::= ('I' 'am' 'special') words ('--' 'NOT!' ';') rank => 1
special ::= words (';') rank => -1
words ::= word* action => [values]
:discard ~ whitespace
whitespace ~ [\s]+
word ~ [\w!-]+
END_OF_SOURCE
my $input = <<'END_OF_INPUT';
I am special so very special -- NOT!;
I am special and nothing is going to change that;
END_OF_INPUT
# Marpa::R2::Display
my $expected_output = [
[ 'unspecial', [qw(so very special)] ],
[ 'special',
[qw(I am special and nothing is going to change that)],
]
];
my $slg = Marpa::R2::Scanless::G->new( { source => \$source } );
push @tests_data,
[
$slg, $input, $expected_output,
'Parse OK', 'Test of rank adverb for display'
];
}
# Test of rule array item descriptor for action adverb
# todo: test by converting rule and lhs ID's to names
# based on $slg->symbol_is_lexeme(symbol_id) -- to be written
if (1) {
my $source = <<'END_OF_SOURCE';
:default ::= action => [lhs, rule, values]
lexeme default = action => [lhs, rule, value]
start ::= number1 number2
number1 ::= <forty two>
number2 ::= <forty three>
<forty two> ~ '42'
<forty three> ~ '43'
END_OF_SOURCE
my $input = '4243';
my $expected_output =
[ 1, 0, [ 2, 1, [ 4, undef, '42' ] ], [ 3, 2, [ 5, undef, '43' ] ] ];
my $slg = Marpa::R2::Scanless::G->new( { source => \$source } );
push @tests_data,
[
$slg, $input, $expected_output,
'Parse OK', 'Test of rule array item descriptor for action adverb'
];
}
# Test of 'symbol', 'name' array item descriptors
if (1) {
# Marpa::R2::Display
# name: symbol, name array descriptor example
# start-after-line: END_OF_SOURCE
# end-before-line: '^END_OF_SOURCE$'
my $source = <<'END_OF_SOURCE';
:default ::= action => [symbol, name, values]
lexeme default = action => [symbol, name, value]
start ::= number1 number2 name => top
number1 ::= <forty two> name => 'number 1'
number2 ::= <forty three> name => 'number 2'
<forty two> ~ '42'
<forty three> ~ '43'
END_OF_SOURCE
# Marpa::R2::Display::End
my $input = '4243';
my $expected_output = [
'start',
'top',
[ 'number1', 'number 1', [ 'forty two', 'forty two', '42' ] ],
[ 'number2', 'number 2', [ 'forty three', 'forty three', '43' ] ]
];
my $slg = Marpa::R2::Scanless::G->new( { source => \$source } );
push @tests_data,
[
$slg, $input, $expected_output,
'Parse OK', 'Test of rule array item descriptor for action adverb'
];
}
### Test of 'inaccessible is ok'
if (1) {
# Marpa::R2::Display
# name: inaccessible is ok statement
# start-after-line: END_OF_SOURCE
# end-before-line: '^END_OF_SOURCE$'
my $source = <<'END_OF_SOURCE';
inaccessible is ok by default
:default ::= action => [values]
start ::= stuff*
stuff ::= a | b
a ::= x action => ::first
b ::= x action => ::first
c ::= x action => ::first
x ::= 'x'
END_OF_SOURCE
# Marpa::R2::Display::End
my $input = 'xx';
my $expected_output = [
[ [ 'x' ] ],
[ [ 'x' ] ]
];
my $slg = Marpa::R2::Scanless::G->new( { source => \$source } );
push @tests_data,
[
$slg, $input, $expected_output,
'Parse OK', qq{Test of "Inaccessible is ok"}
];
}
if (1) {
my $source = <<'END_OF_SOURCE';
inaccessible is ok by default
:default ::= action => ::first
start ::= !START!
start1 ::= X
start2 ::= Y
X ~ 'X'
Y ~ 'X'
END_OF_SOURCE
my $input = 'X';
my $expected_output = 'X';
for my $this_start (qw/start1 start2/) {
my $this_source = $source;
$this_source =~ s/!START!/$this_start/;
my $slg = Marpa::R2::Scanless::G->new( { source => \$this_source } );
push @tests_data,
[
$slg, $input, $expected_output,
'Parse OK', qq{Test of changing start symbols: <$this_start>}
];
} ## end for my $this_start (qw/start1 start2/)
}
if (1) {
my $source = <<'END_OF_SOURCE';
:default ::= action => ::first
dual_start ::= start1 name => 'first start rule'
dual_start ::= start2 name => 'second start rule'
start1 ::= X
start2 ::= Y
X ~ 'X'
Y ~ 'Y'
END_OF_SOURCE
my $input = 'X';
my $expected_output = 'X';
my $slg = Marpa::R2::Scanless::G->new( { source => \$source } );
# Marpa::R2::Display
# name: $slg->start_symbol_id() example
my $start_id = $slg->start_symbol_id();
# Marpa::R2::Display::End
Test::More::is( $start_id, 0, q{Test of $slg->start_symbol_id()} );
my @rule_names = ();
# Marpa::R2::Display
# name: $slg->rule_name() example
push @rule_names, $slg->rule_name($_) for $slg->rule_ids();
# Marpa::R2::Display::End
my $rule_names = join q{:}, @rule_names;
Test::More::is(
$rule_names,
'first start rule:second start rule:start1:start2:[:start]',
q{Test of $slg->rule_name()}
);
push @tests_data,
[
$slg, $input, $expected_output,
'Parse OK', qq{Test of alternative as start rule}
];
} ## end if (0)
TEST:
for my $test_data (@tests_data) {
my ( $grammar, $test_string, $expected_value, $expected_result,
$test_name )
= @{$test_data};
my ( $actual_value, $actual_result ) =
my_parser( $grammar, $test_string );
Test::More::is(
Data::Dumper::Dumper( \$actual_value ),
Data::Dumper::Dumper( \$expected_value ),
qq{Value of $test_name}
);
Test::More::is( $actual_result, $expected_result,
qq{Result of $test_name} );
} ## end TEST: for my $test_data (@tests_data)
sub my_parser {
my ( $grammar, $string ) = @_;
my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
if ( not defined eval { $recce->read( \$string ); 1 } ) {
say $EVAL_ERROR if $DEBUG;
my $abbreviated_error = $EVAL_ERROR;
chomp $abbreviated_error;
return 'No parse', $abbreviated_error;
} ## end if ( not defined eval { $recce->read( \$string ); 1 ...})
my $value_ref = $recce->value();
if ( not defined $value_ref ) {
return 'No parse', 'Input read to end but no parse';
}
return [ return ${$value_ref}, 'Parse OK' ];
} ## end sub my_parser
# vim: expandtab shiftwidth=4:
|