Debian Codegraph @

t/infinite.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 # A grammar with cycles
17
18 use 5.010001;
19 use strict;
20 use warnings;
21 use English qw( -no_match_vars );
22 use Fatal qw(open close chdir);
23
24 use Test::More tests => 6;
25 use lib 'inc';
26 use Marpa::R2::Test;
27 use Marpa::R2;
28
29
30 ## no critic (Subroutines::RequireArgUnpacking)
31 sub default_action {
32 shift;
33 return undef if not scalar @_;
34 return join q{ }, grep { defined $_ } @_;
35 }
36 ## use critic
37
38 package Test_Grammar;
39
40 # Formatted by Data::Dumper, which disagrees with
41 # perltidy and perlcritic about things
42 #<<< no perltidy
43
44 $Test_Grammar::MARPA_OPTIONS_1 = [
45 { 'default_action' => 'main::default_action',
46 'rules' => [
47 { 'lhs' => 's',
48 'rhs' => ['s']
49 }
50 ],
51 'start' => 's',
52 'terminals' => ['s'],
53 }
54 ];
55
56 $Test_Grammar::MARPA_OPTIONS_2 = [
57 { 'default_action' => 'main::default_action',
58 'rules' => [
59 { 'lhs' => 's',
60 'rhs' => [ 'a' ]
61 },
62 { 'lhs' => 'a',
63 'rhs' => [ 's' ]
64 }
65 ],
66 'start' => 's',
67 'terminals' => [ 'a' ],
68 }
69 ];
70
71 $Test_Grammar::MARPA_OPTIONS_8 = [
72 { default_action => 'main::default_action',
73 rules => [
74 { 'lhs' => 'S',
75 'rhs' => [ 'A' ]
76 },
77 { 'lhs' => 'A',
78 'rhs' => [ 'B', 'T', 'U' ]
79 },
80 { 'lhs' => 'B',
81 'rhs' => [ 'V', 'C' ]
82 },
83 { 'lhs' => 'C',
84 'rhs' => [ 'W', 'D', 'X' ]
85 },
86 { 'lhs' => 'D',
87 'rhs' => [ 'E' ]
88 },
89 { 'lhs' => 'E',
90 'rhs' => [ 'S' ]
91 },
92 { 'lhs' => 'T',
93 'rhs' => []
94 },
95 { 'lhs' => 'U',
96 'rhs' => []
97 },
98 { 'lhs' => 'V',
99 'rhs' => []
100 },
101 { 'lhs' => 'W',
102 'rhs' => []
103 },
104 { 'lhs' => 'X',
105 'rhs' => []
106 },
107 { lhs=>'E', rhs=>['e'] },
108 { lhs=>'T', rhs=>['t'] },
109 { lhs=>'U', rhs=>['u'] },
110 { lhs=>'V', rhs=>['v'] },
111 { lhs=>'W', rhs=>['w'] },
112 { lhs=>'X', rhs=>['x'] }
113 ],
114 'start' => 'S',
115 'terminals' => [ 'e', 't', 'u', 'v', 'w', 'x' ],
116 }
117 ];
118
119 #>>>
120 ## use critic
121
122 package main;
123
124 my $cycle1_test = [
125 'cycle1',
126 $Test_Grammar::MARPA_OPTIONS_1,
127 [ [ [ 's', \'1' ] ] ],
128 '1',
129 <<'EOS'
130 Cycle found involving rule: 0: s -> s
131 EOS
132 ];
133
134 my $cycle2_test = [
135 'cycle2',
136 $Test_Grammar::MARPA_OPTIONS_2,
137 [ [ [ 'a', \'1' ] ] ],
138 '1',
139 <<'EOS'
140 Cycle found involving rule: 0: s -> a
141 Cycle found involving rule: 1: a -> s
142 EOS
143 ];
144
145 my @cycle8_tokens = ( [ [ 'e', \'1' ], [ 'v', \'1' ], [ 'w', \'1' ] ] );
146
147 push @cycle8_tokens, map {
148 ( [ [ 'e', \$_ ],
149 [ 't', \$_ ],
150 [ 'u', \$_ ],
151 [ 'v', \$_ ],
152 [ 'w', \$_ ],
153 [ 'x', \$_ ]
154 ],
155 )
156 } qw( 2 3 4 5 6 );
157
158 my $cycle8_test = [
159 'cycle8',
160 $Test_Grammar::MARPA_OPTIONS_8,
161 \@cycle8_tokens,
162 '1 2 3 4 5 6',
163 <<'EOS'
164 Cycle found involving rule: 0: S -> A
165 Cycle found involving rule: 1: A -> B T U
166 Cycle found involving rule: 2: B -> V C
167 Cycle found involving rule: 3: C -> W D X
168 Cycle found involving rule: 4: D -> E
169 Cycle found involving rule: 5: E -> S
170 EOS
171 ];
172
173 for my $test_data ( $cycle1_test, $cycle2_test, $cycle8_test ) {
174 my ( $test_name, $marpa_options, $input, $expected, $expected_trace ) =
175 @{$test_data};
176 my $trace = q{};
177 open my $MEMORY, '>', \$trace;
178 my $grammar = Marpa::R2::Grammar->new(
179 { infinite_action => 'warn',
180 trace_file_handle => $MEMORY,
181 },
182 @{$marpa_options},
183 );
184 $grammar->precompute();
185
186 my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
187 for my $earleme_input ( @{$input} ) {
188 for my $token ( @{$earleme_input} ) {
189 $recce->alternative(@{$token});
190 }
191 $recce->earleme_complete();
192 }
193 my $value_ref = $recce->value();
194 my $value = $value_ref ? ${$value_ref} : 'No parse';
195
196 close $MEMORY;
197
198 Marpa::R2::Test::is( $value, $expected, "$test_name result" );
199 Marpa::R2::Test::is( $trace, $expected_trace, "$test_name trace" );
200
201 } ## end for my $test_data ( $cycle1_test, $cycle2_test, $cycle8_test)
202
203 1; # In case used as "do" file
204
205 # Local Variables:
206 # mode: cperl
207 # cperl-indent-level: 4
208 # fill-column: 100
209 # End:
210 # vim: expandtab shiftwidth=4: