Debian Codegraph @

t/sl_null_example.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 # An ambiguous equation
17
18 use 5.010001;
19 use strict;
20 use warnings;
21
22 use Test::More tests => 1;
23
24 use lib 'inc';
25 use Marpa::R2::Test;
26 use English qw( -no_match_vars );
27 use Fatal qw(open close);
28 use Marpa::R2;
29
30 ## no critic (InputOutput::RequireBriefOpen)
31 open my $original_stdout, q{>&STDOUT};
32 ## use critic
33
34 sub save_stdout {
35 my $save;
36 my $save_ref = \$save;
37 close STDOUT;
38 open STDOUT, q{>}, $save_ref;
39 return $save_ref;
40 } ## end sub save_stdout
41
42 sub restore_stdout {
43 close STDOUT;
44 open STDOUT, q{>&}, $original_stdout;
45 return 1;
46 }
47
48 # Marpa::R2::Display
49 # name: SLIF null value example
50
51 sub do_L {
52 shift;
53 return 'L(' . ( join q{;}, map { $_ // '[ERROR!]' } @_ ) . ')';
54 }
55
56 sub do_R {
57 return 'R(): I will never be called';
58 }
59
60 sub do_S {
61 shift;
62 return 'S(' . ( join q{;}, map { $_ // '[ERROR!]' } @_ ) . ')';
63 }
64
65 sub do_X { return 'X(' . $_[1] . ')'; }
66 sub do_Y { return 'Y(' . $_[1] . ')'; }
67
68 ## no critic (Variables::ProhibitPackageVars)
69 our $null_A = 'null A';
70 our $null_B = 'null B';
71 our $null_L = 'null L';
72 our $null_R = 'null R';
73 our $null_X = 'null X';
74 our $null_Y = 'null Y';
75 ## use critic
76
77 my $slg = Marpa::R2::Scanless::G->new(
78 { source => \<<'END_OF_DSL',
79 :start ::= S
80 S ::= L R action => do_S
81 L ::= A B X action => do_L
82 L ::= action => null_L
83 R ::= A B Y action => do_R
84 R ::= action => null_R
85 A ::= action => null_A
86 B ::= action => null_B
87 X ::= action => null_X
88 X ::= 'x' action => do_X
89 Y ::= action => null_Y
90 Y ::= 'y' action => do_Y
91 END_OF_DSL
92 }
93 );
94
95 my $slr = Marpa::R2::Scanless::R->new(
96 { grammar => $slg,
97 semantics_package => 'main',
98 }
99 );
100
101 $slr->read( \'x' );
102
103 # Marpa::R2::Display::End
104
105 ## use critic
106
107 # Marpa::R2::Display
108 # name: SLIF null value example output
109 # start-after-line: END_OF_OUTPUT
110 # end-before-line: '^END_OF_OUTPUT$'
111
112 chomp( my $expected = <<'END_OF_OUTPUT');
113 S(L(null A;null B;X(x));null R)
114 END_OF_OUTPUT
115
116 # Marpa::R2::Display::End
117
118 my $value = $slr->value();
119 Marpa::R2::Test::is( ${$value}, $expected, 'Null example' );
120
121 # Local Variables:
122 # mode: cperl
123 # cperl-indent-level: 4
124 # fill-column: 100
125 # End:
126 # vim: expandtab shiftwidth=4: