Debian Codegraph @

t/sl_gia.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 grammar, input, and an output with no
18 # semantics -- usually just an AST
19
20 use 5.010001;
21 use strict;
22 use warnings;
23
24 use Test::More tests => 34;
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 our $DEBUG = 0;
34
35 # In crediting test, JDD = Jean-Damien Durand
36 if (1) {
37 my $glenn_grammar = Marpa::R2::Scanless::G->new(
38 { source => \(<<'END_OF_SOURCE'),
39 :default ::= action => ::array
40
41 Start ::= Child DoubleColon Token
42
43 DoubleColon ~ '::'
44 Child ~ 'child'
45 Token ~
46 word
47 | word ':' word
48 word ~ [\w]+
49
50 END_OF_SOURCE
51 }
52 );
53
54 my $input = 'child::book';
55
56 push @tests_data,
57 [
58 $glenn_grammar,
59 'child::book',
60 [ 'child', q{::}, 'book' ],
61 'Parse OK',
62 'Nate Glenn bug regression'
63 ];
64 } ## end if (0)
65
66 # Marpa::R2::Display
67 # name: Case-insensitive characters examples
68 # start-after-line: END_OF_SOURCE
69 # end-before-line: '^END_OF_SOURCE$'
70
71 if (1) {
72 my $ic_grammar = Marpa::R2::Scanless::G->new(
73 { source => \(<<'END_OF_SOURCE'),
74 :default ::= action => ::array
75
76 Start ::= Child DoubleColon Token
77
78 DoubleColon ~ '::'
79 Child ~ 'cHILd':i
80 Token ~
81 word
82 | word ':' word
83 word ~ [\w]:ic +
84
85 END_OF_SOURCE
86 }
87 );
88
89 # Marpa::R2::Display::End
90
91 push @tests_data,
92 [
93 $ic_grammar,
94 'ChilD::BooK',
95 [ 'ChilD', q{::}, 'BooK' ],
96 'Parse OK',
97 'Case insensitivity test'
98 ];
99 } ## end if (0)
100
101 if (1) {
102 my $durand_grammar1 = Marpa::R2::Scanless::G->new(
103 { source => \(<<'END_OF_SOURCE'),
104 :default ::= action => ::array
105 start symbol is test
106 test ::= TEST
107 :lexeme ~ TEST
108 TEST ~ '## Allowed in the input' NEWLINE
109 WS ~ [ \t]
110 WS_any ~ WS*
111 POUND ~ '#'
112 _NEWLINE ~ [\n]
113 NOT_NEWLINE_any ~ [^\n]*
114 NEWLINE ~ _NEWLINE
115 COMMENT ~ WS_any POUND NOT_NEWLINE_any _NEWLINE
116 :discard ~ COMMENT
117 BLANKLINE ~ WS_any _NEWLINE
118 :discard ~ BLANKLINE
119 END_OF_SOURCE
120 }
121 );
122
123 push @tests_data, [
124 $durand_grammar1, <<INPUT,
125 ## Allowed in the input
126
127 # Another comment
128 INPUT
129 ["## Allowed in the input\n"],
130 'Parse OK',
131 'JDD test of discard versus accepted'
132 ];
133 } ## end if (0)
134
135 # ===============
136
137 if (1) {
138 my $durand_grammar2 = Marpa::R2::Scanless::G->new(
139 { source => \(<<'END_OF_SOURCE'),
140 :default ::= action => ::array
141 test ::= 'test input' NEWLINE
142 WS ~ [ \t]
143 WS_any ~ WS*
144 POUND ~ '#'
145 _NEWLINE ~ [\n]
146 NOT_NEWLINE_any ~ [^\n]*
147 NEWLINE ~ _NEWLINE
148 COMMENT ~ WS_any POUND NOT_NEWLINE_any _NEWLINE
149 :discard ~ COMMENT
150 BLANKLINE ~ WS_any _NEWLINE
151 :discard ~ BLANKLINE
152 END_OF_SOURCE
153 }
154 );
155
156 push @tests_data, [
157 $durand_grammar2, <<INPUT,
158 # Comment followed by a newline
159
160 # Another comment
161 test input
162 INPUT
163 [ 'test input', "\n" ],
164 'Parse OK',
165 'Regression test of bug found by JDD'
166 ];
167 } ## end if (1)
168
169 # ===============
170
171 if (1) {
172 my $durand_grammar3 = Marpa::R2::Scanless::G->new(
173 { source => \(<<'END_OF_SOURCE'),
174 :default ::= action => ::array
175
176 Script ::= '=' '/' 'dumb'
177
178 _WhiteSpace ~ ' '
179 _LineTerminator ~ [\n]
180 _SingleLineComment ~ '//' _SingleLineCommentCharsopt
181 _SingleLineCommentChars ~ _SingleLineCommentChar _SingleLineCommentCharsopt
182 _SingleLineCommentCharsopt ~ _SingleLineCommentChars
183 _SingleLineCommentCharsopt ~
184 _SingleLineCommentChar ~ [^\n]
185
186 _S ~
187 _WhiteSpace
188 | _LineTerminator
189 | _SingleLineComment
190
191 S_MANY ~ _S+
192 :discard ~ S_MANY
193
194 END_OF_SOURCE
195 }
196 );
197
198 push @tests_data, [
199 $durand_grammar3, <<INPUT,
200 = / dumb
201 INPUT
202 [qw(= / dumb)],
203 'Parse OK',
204 'Regression test of perl_pos bug found by JDD'
205 ];
206 } ## end if (1)
207
208
209 # ===============
210
211 # Regression test of grammar without lexers --
212 # based on one from Jean-Damien
213 if (1) {
214 my $grammar = Marpa::R2::Scanless::G->new(
215 { source => \(<<'END_OF_SOURCE'),
216 :start ::= null
217 null ::=
218 END_OF_SOURCE
219 }
220 );
221
222 push @tests_data, [
223 $grammar, q{},
224 undef,
225 'Parse OK',
226 'Regression test of lexerless grammar, bug found by JDD'
227 ];
228
229 } ## end if (1)
230
231 # Test of forgiving token from Peter Stuifzand
232 if (1) {
233
234 # Marpa::R2::Display
235 # name: forgiving adverb example
236 # start-after-line: END_OF_SOURCE
237 # end-before-line: '^END_OF_SOURCE$'
238
239 my $source = <<'END_OF_SOURCE';
240 :default ::= action => ::array
241 product ::= sku (nl) name (nl) price price price (nl)
242
243 sku ~ sku_0 '.' sku_0
244 sku_0 ~ [\d]+
245
246 price ~ price_0 ',' price_0
247 price_0 ~ [\d]+
248 nl ~ [\n]
249
250 sp ~ [ ]+
251 :discard ~ sp
252
253 :lexeme ~ <name> forgiving => 1
254 name ~ [^\n]+
255
256 END_OF_SOURCE
257
258 # Marpa::R2::Display::END
259
260 my $input = <<'INPUT';
261 130.12312
262 Descriptive line
263 1,10 1,10 1,30
264 INPUT
265
266 my $grammar = Marpa::R2::Scanless::G->new( { source => \$source } );
267 push @tests_data,
268 [
269 $grammar, $input,
270 [ '130.12312', 'Descriptive line', '1,10', '1,10', '1,30' ],
271 'Parse OK', 'Test of forgiving token from Peter Stuifzand'
272 ];
273 }
274
275 # Test of LATM token from Ruslan Zakirov
276 if (1) {
277
278 # Marpa::R2::Display
279 # name: latm adverb example
280 # start-after-line: END_OF_SOURCE
281 # end-before-line: '^END_OF_SOURCE$'
282
283 my $source = <<'END_OF_SOURCE';
284 :default ::= action => ::array
285 :start ::= content
286 content ::= name ':' value
287 name ~ [A-Za-z0-9-]+
288 value ~ [A-Za-z0-9:-]+
289 :lexeme ~ value latm => 1
290 END_OF_SOURCE
291
292 # Marpa::R2::Display
293
294 my $input = 'UID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1';
295 my $expected_output =
296 [ 'UID', ':', 'urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1' ];
297
298 my $grammar = Marpa::R2::Scanless::G->new( { source => \$source } );
299 push @tests_data,
300 [
301 $grammar, $input, $expected_output,
302 'Parse OK', 'Test of LATM token from Ruslan Zakirov'
303 ];
304 }
305
306 # Test of LATM token from Ruslan Zakirov
307 # This time using the lexeme default statement
308 if (1) {
309
310 my $source = <<'END_OF_SOURCE';
311 lexeme default = latm => 1
312 :default ::= action => ::array
313 :start ::= content
314 content ::= name ':' value
315 name ~ [A-Za-z0-9-]+
316 value ~ [A-Za-z0-9:-]+
317 END_OF_SOURCE
318
319 my $input = 'UID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1';
320 my $expected_output =
321 [ 'UID', ':', 'urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1' ];
322
323 my $grammar = Marpa::R2::Scanless::G->new( { source => \$source } );
324 push @tests_data,
325 [
326 $grammar, $input, $expected_output,
327 'Parse OK', 'Test of LATM token using lexeme default statement'
328 ];
329 }
330
331 # Test of rank adverb
332 if (1) {
333
334 # Marpa::R2::Display
335 # name: rank adverb example
336 # start-after-line: END_OF_SOURCE
337 # end-before-line: '^END_OF_SOURCE$'
338
339 my $source = <<'END_OF_SOURCE';
340 lexeme default = latm => 1
341 :default ::= action => [name,values]
342 :start ::= externals
343 externals ::= external* action => [values]
344 external ::= special action => ::first
345 | unspecial action => ::first
346 unspecial ::= ('I' 'am' 'special') words ('--' 'NOT!' ';') rank => 1
347 special ::= words (';') rank => -1
348 words ::= word* action => [values]
349
350 :discard ~ whitespace
351 whitespace ~ [\s]+
352 word ~ [\w!-]+
353 END_OF_SOURCE
354
355 my $input = <<'END_OF_INPUT';
356 I am special so very special -- NOT!;
357 I am special and nothing is going to change that;
358 END_OF_INPUT
359
360 # Marpa::R2::Display
361
362 my $expected_output = [
363 [ 'unspecial', [qw(so very special)] ],
364 [ 'special',
365 [qw(I am special and nothing is going to change that)],
366 ]
367 ];
368
369 my $grammar = Marpa::R2::Scanless::G->new( { source => \$source } );
370 push @tests_data,
371 [
372 $grammar, $input, $expected_output,
373 'Parse OK', 'Test of rank adverb for display'
374 ];
375 }
376
377 # Test of rule array item descriptor for action adverb
378 # todo: test by converting rule and lhs ID's to names
379 # based on $grammar->symbol_is_lexeme(symbol_id) -- to be written
380 if (1) {
381 my $source = <<'END_OF_SOURCE';
382
383 :default ::= action => [lhs, rule, values]
384 lexeme default = action => [lhs, rule, value]
385 start ::= number1 number2
386 number1 ::= <forty two>
387 number2 ::= <forty three>
388 <forty two> ~ '42'
389 <forty three> ~ '43'
390 END_OF_SOURCE
391
392 my $input = '4243';
393 my $expected_output =
394 [ 1, 0, [ 2, 1, [ 4, undef, '42' ] ], [ 3, 2, [ 5, undef, '43' ] ] ];
395
396 my $grammar = Marpa::R2::Scanless::G->new( { source => \$source } );
397 push @tests_data,
398 [
399 $grammar, $input, $expected_output,
400 'Parse OK', 'Test of rule array item descriptor for action adverb'
401 ];
402 }
403
404 # Test of 'symbol', 'name' array item descriptors
405 if (1) {
406
407 # Marpa::R2::Display
408 # name: symbol, name array descriptor example
409 # start-after-line: END_OF_SOURCE
410 # end-before-line: '^END_OF_SOURCE$'
411
412 my $source = <<'END_OF_SOURCE';
413
414 :default ::= action => [symbol, name, values]
415 lexeme default = action => [symbol, name, value]
416 start ::= number1 number2 name => top
417 number1 ::= <forty two> name => 'number 1'
418 number2 ::= <forty three> name => 'number 2'
419 <forty two> ~ '42'
420 <forty three> ~ '43'
421 END_OF_SOURCE
422
423 # Marpa::R2::Display::End
424
425 my $input = '4243';
426 my $expected_output = [
427 'start',
428 'top',
429 [ 'number1', 'number 1', [ 'forty two', 'forty two', '42' ] ],
430 [ 'number2', 'number 2', [ 'forty three', 'forty three', '43' ] ]
431 ];
432
433 my $grammar = Marpa::R2::Scanless::G->new( { source => \$source } );
434 push @tests_data,
435 [
436 $grammar, $input, $expected_output,
437 'Parse OK', 'Test of rule array item descriptor for action adverb'
438 ];
439 }
440
441 ### Test of 'inaccessible is ok'
442 if (1) {
443
444 # Marpa::R2::Display
445 # name: inaccessible is ok statement
446 # start-after-line: END_OF_SOURCE
447 # end-before-line: '^END_OF_SOURCE$'
448
449 my $source = <<'END_OF_SOURCE';
450
451 inaccessible is ok by default
452
453 :default ::= action => [values]
454 start ::= stuff*
455 stuff ::= a | b
456 a ::= x action => ::first
457 b ::= x action => ::first
458 c ::= x action => ::first
459 x ::= 'x'
460 END_OF_SOURCE
461
462 # Marpa::R2::Display::End
463
464 my $input = 'xx';
465 my $expected_output = [
466 [ [ 'x' ] ],
467 [ [ 'x' ] ]
468 ];
469
470 my $grammar = Marpa::R2::Scanless::G->new( { source => \$source } );
471 push @tests_data,
472 [
473 $grammar, $input, $expected_output,
474 'Parse OK', qq{Test of "Inaccessible is ok"}
475 ];
476 }
477
478 if (1) {
479 my $source = <<'END_OF_SOURCE';
480
481 inaccessible is ok by default
482 :default ::= action => ::first
483
484 start ::= !START!
485 start1 ::= X
486 start2 ::= Y
487
488 X ~ 'X'
489 Y ~ 'X'
490
491 END_OF_SOURCE
492
493 my $input = 'X';
494 my $expected_output = 'X';
495
496 for my $this_start (qw/start1 start2/) {
497
498 my $this_source = $source;
499 $this_source =~ s/!START!/$this_start/;
500 my $grammar = Marpa::R2::Scanless::G->new( { source => \$this_source } );
501 push @tests_data,
502 [
503 $grammar, $input, $expected_output,
504 'Parse OK', qq{Test of changing start symbols: <$this_start>}
505 ];
506
507 } ## end for my $this_start (qw/start1 start2/)
508 }
509
510 if (1) {
511 my $source = <<'END_OF_SOURCE';
512
513 :default ::= action => ::first
514
515 dual_start ::= start1 name => 'first start rule'
516 dual_start ::= start2 name => 'second start rule'
517 start1 ::= X
518 start2 ::= Y
519
520 X ~ 'X'
521 Y ~ 'Y'
522
523 END_OF_SOURCE
524
525 my $input = 'X';
526 my $expected_output = 'X';
527
528 my $grammar = Marpa::R2::Scanless::G->new( { source => \$source } );
529
530 # Marpa::R2::Display
531 # name: $grammar->start_symbol_id() example
532
533 my $start_id = $grammar->start_symbol_id();
534
535 # Marpa::R2::Display::End
536
537 Test::More::is( $start_id, 0, q{Test of $grammar->start_symbol_id()} );
538
539 my @rule_names = ();
540
541 # Marpa::R2::Display
542 # name: $grammar->rule_name() example
543
544 push @rule_names, $grammar->rule_name($_) for $grammar->rule_ids();
545
546 # Marpa::R2::Display::End
547
548 my $rule_names = join q{:}, @rule_names;
549 Test::More::is(
550 $rule_names,
551 'first start rule:second start rule:start1:start2:[:start]',
552 q{Test of $grammar->rule_name()}
553 );
554
555 push @tests_data,
556 [
557 $grammar, $input, $expected_output,
558 'Parse OK', qq{Test of alternative as start rule}
559 ];
560
561 } ## end if (0)
562
563 TEST:
564 for my $test_data (@tests_data) {
565 my ( $grammar, $test_string, $expected_value, $expected_result,
566 $test_name )
567 = @{$test_data};
568 my ( $actual_value, $actual_result ) =
569 my_parser( $grammar, $test_string );
570 Test::More::is(
571 Data::Dumper::Dumper( \$actual_value ),
572 Data::Dumper::Dumper( \$expected_value ),
573 qq{Value of $test_name}
574 );
575 Test::More::is( $actual_result, $expected_result,
576 qq{Result of $test_name} );
577 } ## end TEST: for my $test_data (@tests_data)
578
579 sub my_parser {
580 my ( $grammar, $string ) = @_;
581
582 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
583
584 if ( not defined eval { $recce->read( \$string ); 1 } ) {
585 say $EVAL_ERROR if $DEBUG;
586 my $abbreviated_error = $EVAL_ERROR;
587 chomp $abbreviated_error;
588 return 'No parse', $abbreviated_error;
589 } ## end if ( not defined eval { $recce->read( \$string ); 1 ...})
590 my $value_ref = $recce->value();
591 if ( not defined $value_ref ) {
592 return 'No parse', 'Input read to end but no parse';
593 }
594 return [ return ${$value_ref}, 'Parse OK' ];
595 } ## end sub my_parser
596
597 # vim: expandtab shiftwidth=4: