Debian Codegraph @

t/sl_durand.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 # Regression tests for several bugs found by Jean-Damien
18
19 use 5.010001;
20 use strict;
21 use warnings;
22
23 use Test::More tests => 10;
24
25 use lib 'inc';
26 use Marpa::R2::Test;
27
28 ## no critic (ErrorHandling::RequireCarping);
29
30 use Marpa::R2;
31
32 my $dsl;
33 my $grammar;
34 my $recce;
35 my $input;
36 my $length;
37 my $expected_output;
38 my $actual_output;
39 my $pos = 0;
40
41 # This first problem was with ambiguous SLIF parses when
42 # used together with values from an external scanner
43
44 $dsl = <<'END_OF_SOURCE';
45 :default ::= action => ::first
46 :start ::= Expression
47 Expression ::= Number
48 | Expression Add Expression action => do_add
49 | Expression Multiply Expression action => do_multiply
50 Add ~ '+'
51 Multiply ~ '*'
52 Number ~ digits
53 digits ~ [\d]+
54 :discard ~ whitespace
55 whitespace ~ [\s]+
56 END_OF_SOURCE
57
58 $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } );
59 $recce = Marpa::R2::Scanless::R->new(
60 { grammar => $grammar, semantics_package => 'My_Actions' } );
61 $input = '2*1+3*4+5';
62 $pos = 0;
63 $recce->read( \$input, 0, 0 );
64 for my $input_token (qw(2 * 1 + 3 * 4 + 5)) {
65 my $token_type =
66 $input_token eq '+' ? 'Add'
67 : $input_token eq '*' ? 'Multiply'
68 : 'Number';
69 my $return_value = $recce->lexeme_read( $token_type, $pos, 1, $input_token );
70 $pos++;
71 Test::More::is( $return_value, $pos, "Return value of lexeme_read() is $pos" );
72 } ## end for my $input_token (qw(2 * 1 + 3 * 4 + 5))
73
74 my @values = ();
75 while ( my $value_ref = $recce->value() ) {
76 push @values, ${$value_ref};
77 }
78
79 $expected_output = '19 19 25 29 31 36 36 37 37 42 45 56 72 72';
80 $actual_output = join " ", sort @values;
81 Test::More::is( $actual_output, $expected_output, 'Values for Durand test' );
82
83 sub My_Actions::do_add {
84 my ( undef, $t1, undef, $t2 ) = @_;
85 return $t1 + $t2;
86 }
87
88 sub My_Actions::do_multiply {
89 my ( undef, $t1, undef, $t2 ) = @_;
90 return $t1 * $t2;
91 }
92 1; # In case used as "do" file
93
94 # Local Variables:
95 # mode: cperl
96 # cperl-indent-level: 4
97 # fill-column: 100
98 # End:
99 # vim: expandtab shiftwidth=4: