Debian Codegraph @

t/sl_action2.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 # Test of the actions, focusing on the various types --
18 # CODE, ref to scalar/hash/array, etc.
19
20 use 5.010001;
21 use strict;
22 use warnings;
23
24 use Test::More tests => 2;
25
26 use English qw( -no_match_vars );
27 use Fatal qw( open close );
28 use lib 'inc';
29 use Marpa::R2::Test;
30 use Marpa::R2;
31
32 my $side_effect = 0;
33
34 no warnings 'once';
35 $My_Actions::hash_ref = {'a hash ref' => 1};
36 $My_Actions::array_ref = ['an array ref'];
37 $My_Actions::array_ref2 = ['array ref 2'];
38 $My_Actions::scalar_ref = \8675309;
39 $My_Actions::scalar = 42;
40 $My_Actions::scalar2 = 'scalar2';
41 $My_Actions::code_ref = sub { return 'code ref' };
42 $My_Actions::code_ref2 = sub { return 'code ref 2' };
43 $My_Actions::code_ref_ref = \(sub { return 'code ref ref' });
44 sub My_Actions::scalar2 { return ( 'should not see me', 'shadow of scalar 2' ) };
45 sub My_Actions::array_ref2 { return 'shadow of array_ref2' };
46 sub My_Actions::code_ref2 { return 'shadow of code_ref2' };
47 use warnings;
48 sub My_Actions::code { return 'code' };
49 sub My_Actions::new { $side_effect = 42; }
50
51 my $grammar = Marpa::R2::Scanless::G->new(
52 {
53 source => \<<'END_OF_SOURCE',
54 :default ::= action => ::array
55 :start ::= S
56 S ::= <array ref> <hash ref> <ref ref> <code ref>
57 <code ref ref> <code> <scalar>
58 <scalar2> <array ref 2> <code ref 2>
59 <array ref> ::= 'a' action => array_ref
60 <hash ref> ::= 'a' action => hash_ref
61 <ref ref> ::= 'a' action => scalar_ref
62 <code ref> ::= 'a' action => code_ref
63 <code ref ref> ::= 'a' action => code_ref_ref
64 <code> ::= 'a' action => code
65 <scalar> ::= 'a' action => scalar
66 <scalar2> ::= 'a' action => scalar2
67 <array ref 2> ::= 'a' action => array_ref2
68 <code ref 2> ::= 'a' action => code_ref2
69 END_OF_SOURCE
70 });
71
72 sub do_parse {
73 my $slr = Marpa::R2::Scanless::R->new(
74 { grammar => $grammar, semantics_package => 'My_Actions', } );
75 $slr->read( \'aaaaaaaaaa' );
76 return $slr->value();
77 } ## end sub do_parse
78
79 my $value_ref;
80 $value_ref = do_parse();
81 my $expected = \[
82 [ 'an array ref' ],
83 { 'a hash ref' => 1 },
84 \8675309,
85 $My_Actions::code_ref,
86 $My_Actions::code_ref_ref,
87 'code',
88 42,
89 'shadow of scalar 2',
90 'shadow of array_ref2',
91 'shadow of code_ref2'
92 ];
93 Test::More::is_deeply($value_ref, $expected, 'Constant actions');
94 Test::More::is($side_effect, 42, 'semantics_package constructor');
95
96 # vim: expandtab shiftwidth=4: