Debian Codegraph @

t/rewrite.t

raw
1 #!/usr/bin/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 # Rewriting tests, to check the accuracy of the
18 # tracing documentation.
19
20 use 5.010001;
21 use strict;
22 use warnings;
23
24 use Fatal qw(open close);
25 use Test::More tests => 2;
26
27 use lib 'inc';
28 use Marpa::R2::Test;
29 use Marpa::R2;
30
31 my $chaf_rule = {
32 lhs => 'statement',
33 rhs => [
34 qw/optional_whitespace expression
35 optional_whitespace optional_modifier
36 optional_whitespace/
37 ]
38 };
39
40 my $separated_sequence_rule = {
41 lhs => 'statements',
42 rhs => [qw/statement/],
43 separator => 'comma',
44 min => 1
45 };
46
47 our $null_parse = 'Null parse';
48
49 my $sequence_rule = {
50 lhs => 'block',
51 rhs => [qw/statements/],
52 min => 0,
53 action => 'main::null_parse'
54 };
55
56 my $grammar = Marpa::R2::Grammar->new(
57 { start => 'block',
58 terminals => [qw(block whitespace modifier expression comma)],
59 rules => [
60 $chaf_rule,
61 $separated_sequence_rule,
62 $sequence_rule,
63 { lhs => 'optional_whitespace', rhs => [qw(whitespace)] },
64 { lhs => 'optional_whitespace', },
65 { lhs => 'optional_modifier', rhs => [qw(modifier)] },
66 { lhs => 'optional_modifier', },
67 ],
68 }
69 );
70
71 $grammar->precompute();
72
73 my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
74
75 # While we are at it, test the handling of null parses in
76 # the Single Parse Evaluator
77 $recce->end_input();
78
79 # Marpa::R2::Display::End
80
81 my $show_rules_output = $grammar->show_rules();
82
83 Marpa::R2::Test::is( $show_rules_output, <<'END_RULES', 'Rewritten Rules' );
84 0: statement -> optional_whitespace expression optional_whitespace optional_modifier optional_whitespace
85 1: statements -> statement+ /* discard_sep */
86 2: block -> statements*
87 3: optional_whitespace -> whitespace
88 4: optional_whitespace -> /* empty !used */
89 5: optional_modifier -> modifier
90 6: optional_modifier -> /* empty !used */
91 END_RULES
92
93 my $value_ref = $recce->value();
94 my $value = $value_ref ? ${$value_ref} : 'No Parse';
95
96 Marpa::R2::Test::is( $value, 'Null parse', 'Null parse value' );
97
98 1; # In case used as "do" file
99
100 # Local Variables:
101 # mode: cperl
102 # cperl-indent-level: 4
103 # fill-column: 100
104 # End:
105 # vim: expandtab shiftwidth=4: