Debian Codegraph @

t/minus.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 use 5.010001;
18
19 use strict;
20 use warnings;
21
22 use Test::More tests => 10;
23
24 use lib 'inc';
25 use Marpa::R2::Test;
26 use Marpa::R2;
27
28 # The inefficiency (at least some of it) is deliberate.
29 # Passing up a duples of [ string, value ] and then
30 # assembling a final string at the top would be better
31 # than assembling the string then taking it
32 # apart at each step. But I wanted to test having
33 # a start symbol that appears repeatedly on the RHS.
34
35 ## no critic (Subroutines::RequireArgUnpacking)
36
37 sub subtraction {
38 shift;
39 my ( $right_string, $right_value ) = ( $_[2] =~ /^(.*)==(.*)$/xms );
40 my ( $left_string, $left_value ) = ( $_[0] =~ /^(.*)==(.*)$/xms );
41 my $value = $left_value - $right_value;
42 return '(' . $left_string . q{-} . $right_string . ')==' . $value;
43 } ## end sub subtraction
44
45 sub postfix_decr {
46 shift;
47 my ( $string, $value ) = ( $_[0] =~ /^(.*)==(.*)$/xms );
48 return '(' . $string . q{--} . ')==' . $value--;
49 }
50
51 sub prefix_decr {
52 shift;
53 my ( $string, $value ) = ( $_[1] =~ /^(.*)==(.*)$/xms );
54 return '(' . q{--} . $string . ')==' . --$value;
55 }
56
57 sub negation {
58 shift;
59 my ( $string, $value ) = ( $_[1] =~ /^(.*)==(.*)$/xms );
60 return '(' . q{-} . $string . ')==' . -$value;
61 }
62
63 sub number {
64 shift;
65 my $value = $_[0];
66 return "$value==$value";
67 }
68
69 sub default_action {
70 shift;
71 return q{} if scalar @_ <= 0;
72 return $_[0] if scalar @_ == 1;
73 return '(' . join( q{;}, @_ ) . ')';
74 } ## end sub default_action
75
76 ## use critic
77
78 my $grammar = Marpa::R2::Grammar->new(
79 { start => 'E',
80 actions => 'main',
81 rules => [
82 { lhs => 'E',
83 rhs => [qw/E Minus E/],
84 action => 'subtraction',
85 },
86 { lhs => 'E',
87 rhs => [qw/E MinusMinus/],
88 action => 'postfix_decr',
89 },
90 { lhs => 'E',
91 rhs => [qw/MinusMinus E/],
92 action => 'prefix_decr',
93 },
94 { lhs => 'E',
95 rhs => [qw/Minus E/],
96 action => 'negation'
97 },
98 { lhs => 'E',
99 rhs => [qw/Number/],
100 action => 'number'
101 },
102 ],
103
104 # Marpa::R2::Display
105 # name: Symbol descriptor example
106
107 symbols => {
108 MinusMinus => { terminal => 1 },
109 Minus => { terminal => 1 },
110 Number => { terminal => 1 },
111 },
112
113 # Marpa::R2::Display::End
114
115 default_action => 'default_action',
116 },
117 );
118 $grammar->precompute();
119
120 my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
121
122 Marpa::R2::Test::is( $grammar->show_rules,
123 <<'END_RULES', 'Minuses Equation Rules' );
124 0: E -> E Minus E
125 1: E -> E MinusMinus
126 2: E -> MinusMinus E
127 3: E -> Minus E
128 4: E -> Number
129 END_RULES
130
131 Marpa::R2::Test::is( $grammar->show_ahms,
132 <<'END_AHMS', 'Minuses Equation AHMs' );
133 AHM 0: postdot = "E"
134 E ::= . E Minus E
135 AHM 1: postdot = "Minus"
136 E ::= E . Minus E
137 AHM 2: postdot = "E"
138 E ::= E Minus . E
139 AHM 3: completion
140 E ::= E Minus E .
141 AHM 4: postdot = "E"
142 E ::= . E MinusMinus
143 AHM 5: postdot = "MinusMinus"
144 E ::= E . MinusMinus
145 AHM 6: completion
146 E ::= E MinusMinus .
147 AHM 7: postdot = "MinusMinus"
148 E ::= . MinusMinus E
149 AHM 8: postdot = "E"
150 E ::= MinusMinus . E
151 AHM 9: completion
152 E ::= MinusMinus E .
153 AHM 10: postdot = "Minus"
154 E ::= . Minus E
155 AHM 11: postdot = "E"
156 E ::= Minus . E
157 AHM 12: completion
158 E ::= Minus E .
159 AHM 13: postdot = "Number"
160 E ::= . Number
161 AHM 14: completion
162 E ::= Number .
163 AHM 15: postdot = "E"
164 E['] ::= . E
165 AHM 16: completion
166 E['] ::= E .
167 END_AHMS
168
169 my %expected = map { ( $_ => 1 ) } (
170 #<<< no perltidy
171 '(((6--)--)-1)==5',
172 '((6--)-(--1))==6',
173 '((6--)-(-(-1)))==5',
174 '(6-(--(--1)))==7',
175 '(6-(--(-(-1))))==6',
176 '(6-(-(--(-1))))==4',
177 '(6-(-(-(--1))))==6',
178 '(6-(-(-(-(-1)))))==5',
179 #>>>
180 );
181
182 $recce->read( 'Number', '6' );
183 for ( 1 .. 4 ) {
184 $recce->alternative( 'MinusMinus', \q{--}, 2 );
185 $recce->alternative( 'Minus', \q{-} );
186 $recce->earleme_complete();
187 }
188 $recce->read( 'Minus', q{-}, );
189 $recce->read( 'Number', '1' );
190
191 # Set max_parses to 20 in case there's an infinite loop.
192 # This is for debugging, after all
193 $recce->set( { max_parses => 20 } );
194
195 while ( my $value_ref = $recce->value() ) {
196 my $value = $value_ref ? ${$value_ref} : 'No parse';
197 if ( defined $expected{$value} ) {
198 delete $expected{$value};
199 Test::More::pass("Expected Value $value");
200 }
201 else {
202 Test::More::fail("Unexpected Value $value");
203 }
204 } ## end while ( my $value_ref = $recce->value() )
205
206 1; # In case used as "do" file
207
208 # Local Variables:
209 # mode: cperl
210 # cperl-indent-level: 4
211 # fill-column: 100
212 # End:
213 # vim: expandtab shiftwidth=4: