Debian Codegraph @

t/sl_gif1.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 # Tests which require only a GIF combination-- a grammar (G),
18 # input (I), and an (F) ASF output, with no semantics
19
20 use 5.010001;
21 use strict;
22 use warnings;
23
24 use Test::More tests => 22;
25 use English qw( -no_match_vars );
26 use lib 'inc';
27 use Marpa::R2::Test;
28 use Marpa::R2;
29 use Data::Dumper;
30
31 my @tests_data = ();
32
33 my $aaaa_grammar = Marpa::R2::Scanless::G->new(
34 { source => \(<<'END_OF_SOURCE'),
35 :start ::= quartet
36 quartet ::= a a a a
37 a ~ 'a'
38 END_OF_SOURCE
39 }
40 );
41
42 push @tests_data, [
43 $aaaa_grammar, 'aaaa',
44 <<'END_OF_ASF',
45 GL2 Rule 0: quartet ::= a a a a
46 GL3 Symbol a: "a"
47 GL4 Symbol a: "a"
48 GL5 Symbol a: "a"
49 GL6 Symbol a: "a"
50 END_OF_ASF
51 'ASF OK',
52 'Basic "a a a a" grammar'
53 ]
54 if 1;
55
56 my $abcd_grammar = Marpa::R2::Scanless::G->new(
57 { source => \(<<'END_OF_SOURCE'),
58 :start ::= quartet
59 quartet ::= a b c d
60 a ~ 'a'
61 b ~ 'b'
62 c ~ 'c'
63 d ~ 'd'
64 END_OF_SOURCE
65 }
66 );
67
68 # Marpa::R2::Display
69 # name: ASF symch dump example grammar
70 # start-after-line: END_OF_SOURCE
71 # end-before-line: '^END_OF_SOURCE$'
72
73 my $venus_grammar = Marpa::R2::Scanless::G->new(
74 { source => \(<<'END_OF_SOURCE'),
75 :start ::= planet
76 planet ::= hesperus
77 planet ::= phosphorus
78 hesperus ::= venus
79 phosphorus ::= venus
80 venus ~ 'venus'
81 END_OF_SOURCE
82 }
83 );
84
85 # Marpa::R2::Display::End
86
87 # Marpa::R2::Display
88 # name: ASF symch dump example output
89 # start-after-line: END_OF_OUTPUT
90 # end-before-line: '^END_OF_OUTPUT$'
91
92 push @tests_data, [
93 $venus_grammar, 'venus',
94 <<'END_OF_OUTPUT',
95 Symbol #0 planet has 2 symches
96 Symch #0.0
97 GL2 Rule 0: planet ::= hesperus
98 GL3 Rule 2: hesperus ::= venus
99 GL4 Symbol venus: "venus"
100 Symch #0.1
101 GL2 Rule 1: planet ::= phosphorus
102 GL5 Rule 3: phosphorus ::= venus
103 GL6 Symbol venus: "venus"
104 END_OF_OUTPUT
105 'ASF OK',
106 '"Hesperus is Phosphorus"" grammar'
107 ]
108 if 1;
109
110 # Marpa::R2::Display::End
111
112 push @tests_data, [
113 $abcd_grammar, 'abcd',
114 <<'END_OF_ASF',
115 GL2 Rule 0: quartet ::= a b c d
116 GL3 Symbol a: "a"
117 GL4 Symbol b: "b"
118 GL5 Symbol c: "c"
119 GL6 Symbol d: "d"
120 END_OF_ASF
121 'ASF OK',
122 'Basic "a b c d" grammar'
123 ]
124 if 1;
125
126 # Marpa::R2::Display
127 # name: ASF factoring dump example grammar
128 # start-after-line: END_OF_SOURCE
129 # end-before-line: '^END_OF_SOURCE$'
130
131 my $bb_grammar = Marpa::R2::Scanless::G->new(
132 { source => \(<<'END_OF_SOURCE'),
133 :start ::= top
134 top ::= b b
135 b ::= a a
136 b ::= a
137 a ~ 'a'
138 END_OF_SOURCE
139 }
140 );
141
142 # Marpa::R2::Display::End
143
144 # Marpa::R2::Display
145 # name: ASF factoring dump example output
146 # start-after-line: END_OF_OUTPUT
147 # end-before-line: '^END_OF_OUTPUT$'
148
149 push @tests_data, [
150 $bb_grammar, 'aaa',
151 <<'END_OF_OUTPUT',
152 GL2 Rule 0: top ::= b b
153 Factoring #0
154 GL3 Rule 2: b ::= a
155 GL4 Symbol a: "a"
156 GL5 Rule 1: b ::= a a
157 GL6 Symbol a: "a"
158 GL7 Symbol a: "a"
159 Factoring #1
160 GL8 Rule 1: b ::= a a
161 GL9 Symbol a: "a"
162 GL10 Symbol a: "a"
163 GL11 Rule 2: b ::= a
164 GL12 Symbol a: "a"
165 END_OF_OUTPUT
166 'ASF OK',
167 '"b b" grammar'
168 ]
169 if 1;
170
171 # Marpa::R2::Display::End
172
173 my $seq_grammar = Marpa::R2::Scanless::G->new(
174 { source => \(<<'END_OF_SOURCE'),
175 :start ::= sequence
176 sequence ::= item+
177 item ::= pair | singleton
178 singleton ::= 'a'
179 pair ::= item item
180 END_OF_SOURCE
181 }
182 );
183
184 push @tests_data, [
185 $seq_grammar, 'aa',
186 <<'END_OF_ASF',
187 GL2 Rule 0: sequence ::= item +
188 Factoring #0
189 GL3 Rule 1: item ::= pair
190 GL4 Rule 4: pair ::= item item
191 GL5 Rule 2: item ::= singleton
192 GL6 Rule 3: singleton ::= 'a'
193 GL7 Symbol 'a': "a"
194 GL8 Rule 2: item ::= singleton
195 GL9 Rule 3: singleton ::= 'a'
196 GL10 Symbol 'a': "a"
197 Factoring #1
198 GL5 already displayed
199 GL8 already displayed
200 END_OF_ASF
201 'ASF OK',
202 'Sequence grammar for "aa"'
203 ]
204 if 1;
205
206 push @tests_data, [
207 $seq_grammar, 'aaa',
208 <<'END_OF_ASF',
209 GL2 Rule 0: sequence ::= item +
210 Factoring #0
211 GL3 Rule 1: item ::= pair
212 GL4 Rule 4: pair ::= item item
213 Factoring #0.0
214 GL5 Rule 1: item ::= pair
215 GL6 Rule 4: pair ::= item item
216 GL7 Rule 2: item ::= singleton
217 GL8 Rule 3: singleton ::= 'a'
218 GL9 Symbol 'a': "a"
219 GL10 Rule 2: item ::= singleton
220 GL11 Rule 3: singleton ::= 'a'
221 GL12 Symbol 'a': "a"
222 GL13 Rule 2: item ::= singleton
223 GL14 Rule 3: singleton ::= 'a'
224 GL15 Symbol 'a': "a"
225 Factoring #0.1
226 GL7 already displayed
227 GL16 Rule 1: item ::= pair
228 GL17 Rule 4: pair ::= item item
229 GL10 already displayed
230 GL13 already displayed
231 Factoring #1
232 GL5 already displayed
233 GL13 already displayed
234 Factoring #2
235 GL7 already displayed
236 GL10 already displayed
237 GL13 already displayed
238 Factoring #3
239 GL7 already displayed
240 GL16 already displayed
241 END_OF_ASF
242 'ASF OK',
243 'Sequence grammar for "aaa"'
244 ]
245 if 1;
246
247 my $venus_seq_grammar = Marpa::R2::Scanless::G->new(
248 { source => \(<<'END_OF_SOURCE'),
249 :start ::= sequence
250 sequence ::= item+
251 item ::= pair | Hesperus | Phosphorus
252 Hesperus ::= 'a'
253 Phosphorus ::= 'a'
254 pair ::= item item
255 END_OF_SOURCE
256 }
257 );
258
259 push @tests_data, [
260 $venus_seq_grammar, 'aa',
261 <<'END_OF_ASF',
262 GL2 Rule 0: sequence ::= item +
263 Factoring #0
264 GL3 Rule 1: item ::= pair
265 GL4 Rule 6: pair ::= item item
266 Symbol #0 item has 2 symches
267 Symch #0.0.0
268 GL5 Rule 2: item ::= Hesperus
269 GL6 Rule 4: Hesperus ::= 'a'
270 GL7 Symbol 'a': "a"
271 Symch #0.0.1
272 GL5 Rule 3: item ::= Phosphorus
273 GL8 Rule 5: Phosphorus ::= 'a'
274 GL9 Symbol 'a': "a"
275 Symbol #1 item has 2 symches
276 Symch #0.1.0
277 GL10 Rule 2: item ::= Hesperus
278 GL11 Rule 4: Hesperus ::= 'a'
279 GL12 Symbol 'a': "a"
280 Symch #0.1.1
281 GL10 Rule 3: item ::= Phosphorus
282 GL13 Rule 5: Phosphorus ::= 'a'
283 GL14 Symbol 'a': "a"
284 Factoring #1
285 GL5 already displayed
286 GL10 already displayed
287 END_OF_ASF
288 'ASF OK',
289 'Sequence grammar for "aa"'
290 ]
291 if 1;
292
293 my $nulls_grammar = Marpa::R2::Scanless::G->new(
294 { source => \(<<'END_OF_SOURCE'),
295 :start ::= top
296 top ::= a a a a
297 a ::= 'a'
298 a ::=
299 END_OF_SOURCE
300 }
301 );
302
303 push @tests_data, [
304 $nulls_grammar, 'aaaa',
305 <<'END_OF_ASF',
306 GL2 Rule 0: top ::= a a a a
307 GL3 Rule 1: a ::= 'a'
308 GL4 Symbol 'a': "a"
309 GL5 Rule 1: a ::= 'a'
310 GL6 Symbol 'a': "a"
311 GL7 Rule 1: a ::= 'a'
312 GL8 Symbol 'a': "a"
313 GL9 Rule 1: a ::= 'a'
314 GL10 Symbol 'a': "a"
315 END_OF_ASF
316 'ASF OK',
317 'Nulls grammar for "aaaa"'
318 ]
319 if 1;
320
321 push @tests_data, [
322 $nulls_grammar, 'aaa',
323 <<'END_OF_ASF',
324 GL2 Rule 0: top ::= a a a a
325 Factoring #0
326 GL3 Symbol a: ""
327 GL4 Rule 1: a ::= 'a'
328 GL5 Symbol 'a': "a"
329 GL6 Rule 1: a ::= 'a'
330 GL7 Symbol 'a': "a"
331 GL8 Rule 1: a ::= 'a'
332 GL9 Symbol 'a': "a"
333 Factoring #1
334 GL4 already displayed
335 GL10 Symbol a: ""
336 GL6 already displayed
337 GL8 already displayed
338 Factoring #2
339 GL4 already displayed
340 GL6 already displayed
341 GL8 already displayed
342 GL11 Symbol a: ""
343 Factoring #3
344 GL4 already displayed
345 GL6 already displayed
346 GL12 Symbol a: ""
347 GL8 already displayed
348 END_OF_ASF
349 'ASF OK',
350 'Nulls grammar for "aaa"'
351 ]
352 if 1;
353
354 push @tests_data, [
355 $nulls_grammar, 'aa',
356 <<'END_OF_ASF',
357 GL2 Rule 0: top ::= a a a a
358 Factoring #0
359 GL3 Symbol a: ""
360 GL4 Symbol a: ""
361 GL5 Rule 1: a ::= 'a'
362 GL6 Symbol 'a': "a"
363 GL7 Rule 1: a ::= 'a'
364 GL8 Symbol 'a': "a"
365 Factoring #1
366 GL3 already displayed
367 GL5 already displayed
368 GL7 already displayed
369 GL9 Symbol a: ""
370 Factoring #2
371 GL3 already displayed
372 GL5 already displayed
373 GL10 Symbol a: ""
374 GL7 already displayed
375 Factoring #3
376 GL5 already displayed
377 GL11 Symbol a: ""
378 GL7 already displayed
379 GL9 already displayed
380 Factoring #4
381 GL5 already displayed
382 GL11 already displayed
383 GL10 already displayed
384 GL7 already displayed
385 Factoring #5
386 GL5 already displayed
387 GL7 already displayed
388 GL12 Symbol a: ""
389 GL13 Symbol a: ""
390 END_OF_ASF
391 'ASF OK',
392 'Nulls grammar for "aa"'
393 ]
394 if 1;
395
396 push @tests_data, [
397 $nulls_grammar, 'a',
398 <<'END_OF_ASF',
399 GL2 Rule 0: top ::= a a a a
400 Factoring #0
401 GL3 Rule 1: a ::= 'a'
402 GL4 Symbol 'a': "a"
403 GL5 Symbol a: ""
404 GL6 Symbol a: ""
405 GL7 Symbol a: ""
406 Factoring #1
407 GL8 Symbol a: ""
408 GL3 already displayed
409 GL9 Symbol a: ""
410 GL10 Symbol a: ""
411 Factoring #2
412 GL8 already displayed
413 GL11 Symbol a: ""
414 GL3 already displayed
415 GL12 Symbol a: ""
416 Factoring #3
417 GL8 already displayed
418 GL11 already displayed
419 GL13 Symbol a: ""
420 GL3 already displayed
421 END_OF_ASF
422 'ASF OK',
423 'Nulls grammar for "a"'
424 ]
425 if 1;
426
427 TEST:
428 for my $test_data (@tests_data) {
429 my ( $grammar, $test_string, $expected_value, $expected_result,
430 $test_name )
431 = @{$test_data};
432
433 my ( $actual_value, $actual_result ) =
434 my_parser( $grammar, $test_string );
435
436 Marpa::R2::Test::is(
437 Data::Dumper::Dumper( \$actual_value ),
438 Data::Dumper::Dumper( \$expected_value ),
439 qq{Value of $test_name}
440 );
441
442 Test::More::is( $actual_result, $expected_result,
443 qq{Result of $test_name} );
444 } ## end TEST: for my $test_data (@tests_data)
445
446 sub my_parser {
447 my ( $grammar, $string ) = @_;
448
449 my $slr = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
450
451 if ( not defined eval { $slr->read( \$string ); 1 } ) {
452 my $abbreviated_error = $EVAL_ERROR;
453 chomp $abbreviated_error;
454 return 'No parse', $abbreviated_error;
455 } ## end if ( not defined eval { $slr->read( \$string ); 1 } )
456 my $asf = Marpa::R2::ASF->new( { slr => $slr } );
457 if ( not defined $asf ) {
458 return 'No ASF', 'Input read to end but no ASF';
459 }
460
461 my $asf_desc = $asf->dump();
462 return $asf_desc, 'ASF OK';
463
464 } ## end sub my_parser
465
466 # vim: expandtab shiftwidth=4: