Debian Codegraph @

t/sl_prefix.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 # Test of scannerless parsing -- prefix addition
18
19 use 5.010001;
20 use strict;
21 use warnings;
22
23 use Test::More tests => 30;
24 use English qw( -no_match_vars );
25 use lib 'inc';
26 use Marpa::R2::Test;
27 use Marpa::R2;
28
29 my $prefix_grammar = Marpa::R2::Scanless::G->new(
30 {
31 source => \(<<'END_OF_RULES'),
32 :default ::= action => do_arg0
33 :start ::= Script
34 Script ::= Calculation* action => do_list
35 Calculation ::= Expression | ('say') Expression
36 Expression ::=
37 Number
38 | ('+') Expression Expression action => do_add
39 Number ~ [\d] +
40 :discard ~ whitespace
41 whitespace ~ [\s]+
42 # allow comments
43 :discard ~ <hash comment>
44 <hash comment> ~ <terminated hash comment> | <unterminated
45 final hash comment>
46 <terminated hash comment> ~ '#' <hash comment body> <vertical space char>
47 <unterminated final hash comment> ~ '#' <hash comment body>
48 <hash comment body> ~ <hash comment char>*
49 <vertical space char> ~ [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
50 <hash comment char> ~ [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
51 END_OF_RULES
52 }
53 );
54
55 sub My_Actions::do_list {
56 my ( $self, @results ) = @_;
57 return +( scalar @results ) . ' results: ' . join q{ }, @results;
58 }
59
60 sub My_Actions::do_add { shift; return $_[0] + $_[1] }
61 sub My_Actions::do_arg0 { shift; return shift; }
62
63 sub My_Actions::show_last_expression {
64 my ($self) = @_;
65 my $recce = $self->{recce};
66 my ( $start, $end ) = $recce->last_completed_range('Expression');
67 return if not defined $start;
68 my $last_expression = $recce->range_to_string( $start, $end );
69 return $last_expression;
70 } ## end sub My_Actions::show_last_expression
71
72 sub my_parser {
73 my ( $grammar, $string ) = @_;
74
75 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
76 my $self = bless { grammar => $grammar, recce => $recce }, 'My_Actions';
77 my ( $parse_value, $parse_status, $last_expression );
78
79 if ( not defined eval { $recce->read( \$string ); 1 } ) {
80 my $abbreviated_error = $EVAL_ERROR;
81 chomp $abbreviated_error;
82 $abbreviated_error =~ s/\n.*//xms;
83 return 'No parse', $abbreviated_error, $self->show_last_expression();
84 } ## end if ( not defined eval { $recce->read( \$string ); 1 ...})
85 my $value_ref = $recce->value($self);
86 if ( not defined $value_ref ) {
87 return 'No parse', 'Input read to end but no parse',
88 $self->show_last_expression();
89 }
90 return [ return ${$value_ref}, 'Parse OK', 'entire input' ];
91 } ## end sub my_parser
92
93 my @tests_data = (
94 [ '+++ 1 2 3 + + 1 2 4', '1 results: 13', 'Parse OK', 'entire input' ],
95 [ 'say + 1 2', '1 results: 3', 'Parse OK', 'entire input' ],
96 [ '+ 1 say 2', 'No parse', 'Error in SLIF parse: No lexemes accepted at line 1, column 5', '1' ],
97 [ '+ 1 2 3 + + 1 2 4', '3 results: 3 3 7', 'Parse OK', 'entire input' ],
98 [ '+++', 'No parse', 'Input read to end but no parse', 'none' ],
99 [ '++1 2++', 'No parse', 'Input read to end but no parse', '+1 2' ],
100 [ '++1 2++3 4++', 'No parse', 'Input read to end but no parse', '+3 4' ],
101 [ '1 + 2 +3 4 + 5 + 6 + 7', 'No parse', 'Input read to end but no parse', '7' ],
102 [ '+12', 'No parse', 'Input read to end but no parse', '12' ],
103 [ '+1234', 'No parse', 'Input read to end but no parse', '1234' ],
104 );
105
106 TEST:
107 for my $test_data (@tests_data) {
108 my ($test_string, $expected_value,
109 $expected_result, $expected_last_expression
110 ) = @{$test_data};
111 my ($actual_value,
112 $actual_result, $actual_last_expression
113 ) = my_parser( $prefix_grammar, $test_string );
114 $actual_last_expression //= 'none';
115 Test::More::is( $actual_value, $expected_value, qq{Value of "$test_string"} );
116 Test::More::is( $actual_result, $expected_result, qq{Result of "$test_string"} );
117 Test::More::is( $actual_last_expression, $expected_last_expression, qq{Last expression found in "$test_string"} );
118 } ## end TEST: for my $test_string (@test_strings)
119
120 # vim: expandtab shiftwidth=4: