| 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 |
# A CHAF rule which starts with nullables, and cycle. |
| 17 |
|
| 18 |
use 5.010001; |
| 19 |
use strict; |
| 20 |
use warnings; |
| 21 |
|
| 22 |
use Test::More tests => 1; |
| 23 |
|
| 24 |
use lib 'inc'; |
| 25 |
use Marpa::R2::Test; |
| 26 |
use Marpa::R2; |
| 27 |
|
| 28 |
## no critic (Subroutines::RequireArgUnpacking) |
| 29 |
|
| 30 |
our $dash = q{-}; |
| 31 |
|
| 32 |
sub rule_n { |
| 33 |
shift; |
| 34 |
return 'n(' . ( join q{;}, map { $_ // q{-} } @_ ) . ')'; |
| 35 |
} |
| 36 |
|
| 37 |
sub start_rule { |
| 38 |
shift; |
| 39 |
return 'S(' . ( join q{;}, ( map { $_ // q{-} } @_ ) ) . ')'; |
| 40 |
} |
| 41 |
|
| 42 |
sub rule_f { |
| 43 |
shift; |
| 44 |
return 'f(' . ( join q{;}, ( map { $_ // q{-} } @_ ) ) . ')'; |
| 45 |
} |
| 46 |
|
| 47 |
## use critic |
| 48 |
|
| 49 |
my $grammar = Marpa::R2::Grammar->new( |
| 50 |
{ start => 'S', |
| 51 |
infinite_action => 'quiet', |
| 52 |
rules => [ |
| 53 |
{ lhs => 'S', |
| 54 |
rhs => [qw/n n n f/], |
| 55 |
action => 'main::start_rule' |
| 56 |
}, |
| 57 |
{ lhs => 'n', rhs => ['a'], action => 'main::rule_n' }, |
| 58 |
{ lhs => 'n', rhs => [], action => 'main::dash' }, |
| 59 |
{ lhs => 'f', rhs => ['a'], action => 'main::rule_f' }, |
| 60 |
{ lhs => 'f', rhs => [], action => 'main::dash' }, |
| 61 |
{ lhs => 'f', rhs => ['S'], action => 'main::rule_f' }, |
| 62 |
], |
| 63 |
terminals => [qw(a)], |
| 64 |
} |
| 65 |
); |
| 66 |
|
| 67 |
$grammar->precompute(); |
| 68 |
|
| 69 |
my @expected = qw{ |
| 70 |
S(-;-;-;f(A)) |
| 71 |
S(-;-;-;f(S(n(A);-;-;-))) |
| 72 |
S(-;-;n(A);-) |
| 73 |
S(-;n(A);-;-) |
| 74 |
S(n(A);-;-;-) |
| 75 |
}; |
| 76 |
|
| 77 |
my $input_length = 1; |
| 78 |
my $recce = |
| 79 |
Marpa::R2::Recognizer->new( { grammar => $grammar, max_parses => 99 } ); |
| 80 |
for my $token_ix ( 1 .. $input_length ) { |
| 81 |
$recce->read( 'a', 'A' ); |
| 82 |
} |
| 83 |
my $expected = $expected[$input_length]; |
| 84 |
my @values = (); |
| 85 |
while ( my $value_ref = $recce->value() ) { |
| 86 |
push @values, ${$value_ref}; |
| 87 |
} |
| 88 |
Marpa::R2::Test::is( |
| 89 |
( join "\n", sort @values ), |
| 90 |
( join "\n", @expected ), |
| 91 |
"value for input length $input_length" |
| 92 |
); |
| 93 |
|
| 94 |
1; # In case used as "do" file |
| 95 |
|
| 96 |
# Local Variables: |
| 97 |
# mode: cperl |
| 98 |
# cperl-indent-level: 4 |
| 99 |
# fill-column: 100 |
| 100 |
# End: |
| 101 |
# vim: expandtab shiftwidth=4: |