Debian Codegraph @

t/infinite2.t

raw
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 # A grammars with cycles
18
19 use 5.010001;
20 use strict;
21 use warnings;
22 use English qw( -no_match_vars );
23 use Fatal qw(open close chdir);
24 use Test::More tests => 3;
25 use lib 'inc';
26 use Marpa::R2::Test;
27 use Marpa::R2;
28
29 ## no critic (Subroutines::RequireArgUnpacking)
30 sub show_a { return 'A(' . $_[1] . ')' }
31 sub show_b { return 'B(' . $_[1] . ')' }
32 sub default_action { shift; return join q{ }, @_ }
33 ## use critic
34
35 package Test_Grammar;
36
37 $Test_Grammar::MARPA_OPTIONS = [
38 { 'default_action' => 'main::default_action',
39 'rules' => [
40 { 'lhs' => 's',
41 'rhs' => ['a']
42 },
43 { 'action' => 'main::show_a',
44 'lhs' => 'a',
45 'rhs' => ['b']
46 },
47 { 'lhs' => 'a',
48 'rhs' => ['a:k0']
49 },
50 { 'action' => 'main::show_b',
51 'lhs' => 'b',
52 'rhs' => ['a']
53 }
54 ],
55 'start' => 's',
56 'terminals' => ['a:k0'],
57 'infinite_action' => 'warn'
58 }
59 ];
60
61 my $trace;
62 open my $MEMORY, '>', \$trace;
63 my $grammar = Marpa::R2::Grammar->new(
64 { trace_file_handle => $MEMORY, infinite_action => 'warn' },
65 @{$Test_Grammar::MARPA_OPTIONS} );
66 $grammar->precompute();
67 close $MEMORY;
68
69 Marpa::R2::Test::is( $trace, <<'EOS', 'cycle detection' );
70 Cycle found involving rule: 1: a -> b
71 Cycle found involving rule: 3: b -> a
72 EOS
73
74 my $recce = Marpa::R2::Recognizer->new(
75 { grammar => $grammar,
76 trace_file_handle => *STDERR,
77 }
78 );
79
80 $recce->read( 'a:k0', 'a' );
81
82 my %expected_original = map { ( $_ => 1 ) } qw( A(B(a)) a );
83 my %expected = %expected_original;
84
85 while ( my $value_ref = $recce->value() ) {
86 my $value = ${$value_ref};
87 if ( defined $expected{$value} ) {
88 Test::More::pass(qq{Expected value: "$value"});
89 delete $expected{$value};
90 }
91 } ## end while ( my $value_ref = $recce->value() )
92
93 for my $missing_value ( keys %expected ) {
94 Test::More::fail(qq{Missing value: "$missing_value"});
95 }
96
97 1; # In case used as "do" file
98
99 # Local Variables:
100 # mode: cperl
101 # cperl-indent-level: 4
102 # fill-column: 100
103 # End:
104 # vim: expandtab shiftwidth=4: