Debian Codegraph @

t/sl_latk.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 # Based on Github issue #254 -- constructor invoked
18 # on per-parse argument, which should not happen.
19
20 # _The Computer Journal_, Vol. 45, No. 6, pp. 620-630,
21 # in its "NNF" form
22
23 use 5.010001;
24 use strict;
25 use warnings;
26
27 use English qw( -no_match_vars );
28 use Test::More tests => 12;
29 use lib 'inc';
30 use Marpa::R2::Test;
31 use Marpa::R2;
32 use Data::Dumper;
33
34 package Class_Actions;
35
36 sub new {
37 my ( $class ) = @_;
38 return bless { ctor_desc => 'class ctor' }, $class;
39 }
40
41 sub do_A {
42 my ( $self, $letter ) = @_;
43 my $ctor_desc = $self->{ctor_desc} // 'no ctor';
44 return join ';', $ctor_desc, "class method", "letter=$letter";
45 }
46
47 package Package_Actions;
48
49 sub new {
50 my ( $class ) = @_;
51 return bless { ctor_desc => 'package ctor' }, $class;
52 }
53
54 sub do_A {
55 my ( $self, $letter ) = @_;
56 my $ctor_desc = $self->{ctor_desc} // 'no ctor';
57 return join ';', $ctor_desc, "package method", "letter=$letter";
58 }
59
60 package main;
61
62 my $grammar =
63 Marpa::R2::Scanless::G->new( { source => \q(A ::= 'a' action => do_A) } );
64
65 my @tests = ();
66 for my $recce_arg_desc ( 'semantics_package', 'no semantics_package' ) {
67 PPO:
68 for my $ppo_desc ( 'no', 'unblessed', 'same blessed', 'other blessed' ) {
69 my $recce_arg = {};
70 my $ctor_desc = 'no ctor';
71 my $method_desc = undef;
72 if ( $recce_arg_desc eq 'semantics_package' ) {
73 $recce_arg = { semantics_package => 'Package_Actions' };
74 $ctor_desc = 'package ctor';
75 $method_desc = 'package method';
76 }
77 my $ppo = undef;
78 SET_PPO_PARMS: {
79 last SET_PPO_PARMS if $ppo_desc eq 'no';
80 if ( $ppo_desc eq 'unblessed' ) {
81 $ppo = { desc => $ppo_desc };
82 $ctor_desc = 'no ctor';
83 last SET_PPO_PARMS;
84 }
85 if ( $ppo_desc eq 'same blessed' ) {
86 $ppo = bless { desc => $ppo_desc }, 'Package_Actions';
87 $ctor_desc = 'no ctor';
88 $method_desc = 'package method' if not defined $method_desc;
89 last SET_PPO_PARMS;
90 } ## end if ( $ppo_desc eq 'same blessed' )
91 if ( $ppo_desc eq 'other blessed' ) {
92 $ppo = bless { desc => $ppo_desc }, 'Class_Actions';
93 $ctor_desc = 'no ctor';
94 $method_desc = 'class method' if not defined $method_desc;
95 last SET_PPO_PARMS;
96 } ## end if ( $ppo_desc eq 'other blessed' )
97 die;
98 } ## end SET_PPO_PARMS:
99 next PPO if not defined $method_desc;
100 my $value = join ';', $ctor_desc, $method_desc, 'letter=a';
101 my $desc = "$recce_arg_desc; $ppo_desc ppo";
102 push @tests, [ $recce_arg, $ppo, $value, 'Parse OK', $desc ];
103 } ## end PPO: for my $ppo_desc ( 'no', 'unblessed', 'same blessed',...)
104 } ## end for my $recce_arg_desc ( 'semantics_package', 'no semantics_package')
105
106 TEST:
107 for my $test_data (@tests) {
108 my ( $recce_arg, $ppo, $expected_value, $expected_result,
109 $test_name )
110 = @{$test_data};
111 my ( $actual_value, $actual_result ) =
112 my_parser( $grammar, $recce_arg, $ppo );
113 Test::More::is(
114 Data::Dumper::Dumper( \$actual_value ),
115 Data::Dumper::Dumper( \$expected_value ),
116 qq{Value of $test_name}
117 );
118 Test::More::is( $actual_result, $expected_result,
119 qq{Result of $test_name} );
120 } ## end TEST: for my $test_data (@tests_data)
121
122 sub my_parser {
123 my ( $grammar, $recce_arg, $ppo ) = @_;
124
125 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar }, $recce_arg );
126
127 if ( not defined eval { $recce->read( \'a' ); 1 } ) {
128 # say $EVAL_ERROR
129 my $abbreviated_error = $EVAL_ERROR;
130 chomp $abbreviated_error;
131 return 'No parse', $abbreviated_error;
132 } ## end if ( not defined eval { $recce->read( \$string ); 1 ...})
133 my $value_ref;
134 if (not defined eval { $value_ref = $recce->value($ppo); 1 } ) {
135 # say $EVAL_ERROR
136 my $abbreviated_error = $EVAL_ERROR;
137 chomp $abbreviated_error;
138 return 'value() failure', $abbreviated_error;
139 }
140 if ( not defined $value_ref ) {
141 return 'No parse', 'Input read to end but no parse';
142 }
143 return ${$value_ref}, 'Parse OK';
144 } ## end sub my_parser
145
146 # vim: expandtab shiftwidth=4: