Debian Codegraph @

t/thin_deprec.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 # Testing using deprecated methods of
18 # the thin interface
19
20 use 5.010001;
21 use strict;
22 use warnings;
23
24 use Test::More tests => 13;
25
26 use lib 'inc';
27 use Marpa::R2::Test;
28 use English qw( -no_match_vars );
29 use Fatal qw( close open );
30 use Marpa::R2;
31
32 my $grammar = Marpa::R2::Thin::G->new( { if => 1 } );
33 $grammar->force_valued();
34 my $symbol_S = $grammar->symbol_new();
35 my $symbol_E = $grammar->symbol_new();
36 $grammar->start_symbol_set($symbol_S);
37 my $symbol_op = $grammar->symbol_new();
38 my $symbol_number = $grammar->symbol_new();
39 my $start_rule_id = $grammar->rule_new( $symbol_S, [$symbol_E] );
40 my $op_rule_id =
41 $grammar->rule_new( $symbol_E, [ $symbol_E, $symbol_op, $symbol_E ] );
42 my $number_rule_id = $grammar->rule_new( $symbol_E, [$symbol_number] );
43
44 $grammar->precompute();
45
46 my $recce = Marpa::R2::Thin::R->new($grammar);
47 $recce->start_input();
48
49 # The numbers from 1 to 3 are themselves --
50 # that is, they index their own token value.
51 # Important: zero cannot be itself!
52
53 my @token_values = ( 0 .. 3 );
54 my $zero = -1 + push @token_values, 0;
55 my $minus_token_value = -1 + push @token_values, q{-};
56 my $plus_token_value = -1 + push @token_values, q{+};
57 my $multiply_token_value = -1 + push @token_values, q{*};
58
59 $recce->alternative( $symbol_number, 2, 1 );
60 $recce->earleme_complete();
61 $recce->alternative( $symbol_op, $minus_token_value, 1 );
62 $recce->earleme_complete();
63 $recce->alternative( $symbol_number, $zero, 1 );
64 $recce->earleme_complete();
65 $recce->alternative( $symbol_op, $multiply_token_value, 1 );
66 $recce->earleme_complete();
67 $recce->alternative( $symbol_number, 3, 1 );
68 $recce->earleme_complete();
69 $recce->alternative( $symbol_op, $plus_token_value, 1 );
70 $recce->earleme_complete();
71 $recce->alternative( $symbol_number, 1, 1 );
72 $recce->earleme_complete();
73
74 my $latest_earley_set_ID = $recce->latest_earley_set();
75 my $bocage = Marpa::R2::Thin::B->new( $recce, $latest_earley_set_ID );
76 my $order = Marpa::R2::Thin::O->new($bocage);
77 my $tree = Marpa::R2::Thin::T->new($order);
78 my @actual_values = ();
79 while ( $tree->next() ) {
80 my $valuator = Marpa::R2::Thin::V->new($tree);
81 my @stack = ();
82 STEP: while (1) {
83 my ( $type, @step_data ) = $valuator->step();
84 last STEP if not defined $type;
85 if ( $type eq 'MARPA_STEP_TOKEN' ) {
86 my ( undef, $token_value_ix, $arg_n ) = @step_data;
87 $stack[$arg_n] = $token_values[$token_value_ix];
88 next STEP;
89 }
90 if ( $type eq 'MARPA_STEP_RULE' ) {
91 my ( $rule_id, $arg_0, $arg_n ) = @step_data;
92 if ( $rule_id == $start_rule_id ) {
93 my ( $string, $value ) = @{ $stack[$arg_n] };
94 $stack[$arg_0] = "$string == $value";
95 next STEP;
96 }
97 if ( $rule_id == $number_rule_id ) {
98 my $number = $stack[$arg_0];
99 $stack[$arg_0] = [ $number, $number ];
100 next STEP;
101 }
102 if ( $rule_id == $op_rule_id ) {
103 my $op = $stack[ $arg_0 + 1 ];
104 my ( $right_string, $right_value ) = @{ $stack[$arg_n] };
105 my ( $left_string, $left_value ) = @{ $stack[$arg_0] };
106 my $value;
107 my $text = '(' . $left_string . $op . $right_string . ')';
108 if ( $op eq q{+} ) {
109 $stack[$arg_0] = [ $text, $left_value + $right_value ];
110 next STEP;
111 }
112 if ( $op eq q{-} ) {
113 $stack[$arg_0] = [ $text, $left_value - $right_value ];
114 next STEP;
115 }
116 if ( $op eq q{*} ) {
117 $stack[$arg_0] = [ $text, $left_value * $right_value ];
118 next STEP;
119 }
120 die "Unknown op: $op";
121 } ## end if ( $rule_id == $op_rule_id )
122 die "Unknown rule $rule_id";
123 } ## end if ( $type eq 'MARPA_STEP_RULE' )
124 die "Unexpected step type: $type";
125 } ## end STEP: while (1)
126 push @actual_values, $stack[0];
127 } ## end while ( $tree->next() )
128
129 my %expected_value = (
130 '(2-(0*(3+1))) == 2' => 1,
131 '(((2-0)*3)+1) == 7' => 1,
132 '((2-(0*3))+1) == 3' => 1,
133 '((2-0)*(3+1)) == 8' => 1,
134 '(2-((0*3)+1)) == 1' => 1,
135 );
136
137 my $i = 0;
138 for my $actual_value (@actual_values) {
139 if ( defined $expected_value{$actual_value} ) {
140 delete $expected_value{$actual_value};
141 Test::More::pass("Expected Value $i: $actual_value");
142 }
143 else {
144 Test::More::fail("Unexpected Value $i: $actual_value");
145 }
146 $i++;
147 } ## end for my $actual_value (@actual_values)
148
149 # For the error methods, start clean,
150 # with a new, trivial grammar
151 $grammar = $recce = $bocage = $order = $tree = undef;
152 $grammar = Marpa::R2::Thin::G->new( { if => 1 } );
153 $grammar->force_valued();
154
155 my ( $error_code, $error_description ) = $grammar->error();
156 my @error_names = Marpa::R2::Thin::error_names();
157 my $error_name = $error_names[$error_code];
158
159 Test::More::is( $error_code, 0, 'Grammar error code' );
160 Test::More::is( $error_name, 'MARPA_ERR_NONE', 'Grammar error name' );
161 Test::More::is( $error_description, 'No error', 'Grammar error description' );
162
163 $symbol_S = $grammar->symbol_new();
164 my $symbol_a = $grammar->symbol_new();
165 my $symbol_sep = $grammar->symbol_new();
166 $grammar->start_symbol_set($symbol_S);
167
168 my $sequence_rule_id = $grammar->sequence_new(
169 $symbol_S,
170 $symbol_a,
171 { separator => $symbol_sep,
172 proper => 0,
173 min => 1
174 }
175 );
176
177 $grammar->precompute();
178 my @events;
179 my $event_ix = $grammar->event_count();
180 while ( $event_ix-- ) {
181
182 my ( $event_type, $value ) = $grammar->event( $event_ix++ );
183
184 }
185
186 $recce = Marpa::R2::Thin::R->new($grammar);
187
188 $recce->ruby_slippers_set(1);
189
190 $recce->start_input();
191 $recce->alternative( $symbol_a, 1, 1 );
192 $recce->earleme_complete();
193
194 my @terminals = $recce->terminals_expected();
195
196 Test::More::is( ( scalar @terminals ), 1, 'count of terminals expected' );
197 Test::More::is( $terminals[0], $symbol_sep, 'expected terminal' );
198
199 my $report;
200
201 my $ordinal = $recce->latest_earley_set();
202 $recce->progress_report_start($ordinal);
203 ITEM: while (1) {
204 my ( $rule_id, $dot_position, $origin ) = $recce->progress_item();
205 last ITEM if not defined $rule_id;
206 push @{$report}, [ $rule_id, $dot_position, $origin ];
207 }
208 $recce->progress_report_finish();
209
210 Test::More::is( ( join q{ }, map { @{$_} } @{$report} ),
211 '0 -1 0', 'progress report' );
212
213 $recce->alternative( $symbol_sep, 1, 1 );
214 $recce->earleme_complete();
215 $recce->alternative( $symbol_a, 1, 1 );
216 $recce->earleme_complete();
217 $recce->alternative( $symbol_sep, 1, 1 );
218 $recce->earleme_complete();
219 $recce->alternative( $symbol_a, 1, 1 );
220 $recce->earleme_complete();
221 $latest_earley_set_ID = $recce->latest_earley_set();
222 $bocage = Marpa::R2::Thin::B->new( $recce, $latest_earley_set_ID );
223 $order = Marpa::R2::Thin::O->new($bocage);
224 $tree = Marpa::R2::Thin::T->new($order);
225 $tree->next();
226 my $valuator = Marpa::R2::Thin::V->new($tree);
227 my $locations_report = q{};
228 STEP: for ( ;; ) {
229 my ( $type, @step_data ) = $valuator->step();
230 last STEP if not defined $type;
231
232 $type = $valuator->step_type();
233 my ( $start, $end ) = $valuator->location();
234 if ( $type eq 'MARPA_STEP_RULE' ) {
235 my ($rule_id) = @step_data;
236 $locations_report .= "Rule $rule_id is from $start to $end\n";
237 }
238 if ( $type eq 'MARPA_STEP_TOKEN' ) {
239 my ($token_id) = @step_data;
240 $locations_report .= "Token $token_id is from $start to $end\n";
241 }
242 if ( $type eq 'MARPA_STEP_NULLING_SYMBOL' ) {
243 my ($symbol_id) = @step_data;
244 $locations_report
245 .= "Nulling symbol $symbol_id is from $start to $end\n";
246 }
247
248 } ## end STEP: for ( ;; )
249
250 Test::More::is( $locations_report, <<'EXPECTED', 'Step locations' );
251 Token 1 is from 0 to 1
252 Token 2 is from 1 to 2
253 Token 1 is from 2 to 3
254 Token 2 is from 3 to 4
255 Token 1 is from 4 to 5
256 Rule 0 is from 0 to 5
257 EXPECTED
258
259 {
260 my $symbol_count = 0;
261 my @unvalued_symbols = ();
262 for (
263 my $symbol_id = 0;
264 $symbol_id <= $grammar->highest_symbol_id();
265 $symbol_id++
266 )
267 {
268 $grammar->throw_set(0);
269 my $result = $grammar->symbol_is_start($symbol_id);
270 $grammar->throw_set(1);
271 next SYMBOL if $result == -1; # well-formed but non-existent
272 if ($result < 0) {
273 my ( $error_code, $error_description ) = $grammar->error();
274 die "symbol_is_start($symbol_id) failed ($error_code) $error_description";
275 }
276 $symbol_count++;
277 push @unvalued_symbols, $symbol_id
278 if !$grammar->symbol_is_valued($symbol_id);
279 } ## end for ( my $symbol_id = 0; $symbol_id <= $grammar->...)
280 my $unvalued_desc =
281 ( scalar @unvalued_symbols )
282 ? ( join q{ }, @unvalued_symbols )
283 : 'none';
284 Test::More::ok( ( $unvalued_desc eq 'none' ),
285 "Unvalued symbols: $unvalued_desc of $symbol_count" );
286 }
287
288 # vim: expandtab shiftwidth=4: