Debian Codegraph @

t/sl_exhaust.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 # Tests of scannerless parsing -- some corner cases,
18 # including exhaustion at G1 level
19
20 use 5.010001;
21 use strict;
22 use warnings;
23
24 use Test::More tests => 72;
25 use English qw( -no_match_vars );
26 use lib 'inc';
27 use Marpa::R2::Test;
28 use Marpa::R2;
29
30 my $source_template = <<'END_OF_SOURCE';
31 :default ::= action => do_list
32 :start ::= Number
33 Number ::= number # If I add '+' or '*' it will work...
34 %QUANTIFIER%
35 number ~ [\d]+
36 :discard ~ whitespace
37 whitespace ~ [\s]+
38 END_OF_SOURCE
39
40 (my $source_bare = $source_template) =~ s/ %QUANTIFIER% / /xms;
41 (my $source_plus = $source_template) =~ s/ %QUANTIFIER% / + /xms;
42 (my $source_star = $source_template) =~ s/ %QUANTIFIER% / * /xms;
43
44 my $grammar_bare = Marpa::R2::Scanless::G->new( { source => \$source_bare } );
45 my $grammar_plus = Marpa::R2::Scanless::G->new( { source => \$source_plus } );
46 my $grammar_star = Marpa::R2::Scanless::G->new( { source => \$source_star } );
47
48 package My_Actions;
49 sub do_list {
50 shift;
51 return join " ", @_;
52 }
53
54 sub show_last_expression {
55 my ($self) = @_;
56 my $recce = $self->{slr};
57 my ( $start, $end ) = $recce->last_completed_range('Number');
58 return '[none]' if not defined $start;
59 my $last_expression = $recce->range_to_string( $start, $end );
60 return $last_expression;
61 } ## end sub show_last_expression
62
63 package main;
64
65 sub my_parser {
66 my ( $grammar, $string ) = @_;
67
68 my $self = bless { grammar => $grammar }, 'My_Actions';
69
70 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
71 $self->{slr} = $recce;
72 my ( $parse_value, $parse_status, $last_expression );
73
74 my $eval_ok = eval { $recce->read( \$string ); 1; };
75 my $eval_error = $EVAL_ERROR;
76
77 # Marpa::R2::Display
78 # name: $recce->exhausted example
79
80 my $exhausted_status = $recce->exhausted();
81
82 # Marpa::R2::Display::End
83
84 if ( not $eval_ok ) {
85 chomp $eval_error;
86 $eval_error =~ s/\n.*//xms;
87 return 'No parse', $eval_error, $self->show_last_expression(),
88 $exhausted_status;
89 } ## end if ( not $eval_ok )
90
91 my $value_ref = $recce->value($self);
92
93 if ( not defined $value_ref ) {
94 return 'No parse', 'Input read to end but no parse',
95 $self->show_last_expression(),
96 $exhausted_status;
97 }
98 my $value = ${$value_ref} // '';
99 return $value, 'Parse OK', 'entire input', $exhausted_status;
100
101 } ## end sub my_parser
102
103 my %grammar_by_type = (
104 'Bare' => $grammar_bare,
105 'Plus' => $grammar_plus,
106 'Star' => $grammar_star,
107 );
108
109 my @tests_data = (
110 [ 'Bare', '', 'No parse', 'Input read to end but no parse', '[none]' ],
111 [ 'Bare', '1', '1', 'Parse OK', 'entire input', 1 ],
112 [ 'Bare', '1 2', 'No parse',
113 'Error in SLIF parse: Parse exhausted, but lexemes remain, at line 1, column 3',
114 '1', 1
115 ],
116 [ 'Plus', '', 'No parse', 'Input read to end but no parse', '[none]' ],
117 [ 'Plus', '1', '1', 'Parse OK', 'entire input' ],
118 [ 'Plus', '1 2', '1 2', 'Parse OK', 'entire input' ],
119 [ 'Star', '', '', 'Parse OK', 'entire input' ],
120 [ 'Star', '1', '1', 'Parse OK', 'entire input' ],
121 [ 'Star', '1 2', '1 2', 'Parse OK', 'entire input' ],
122 );
123
124 for my $trailer ( q{}, q{ } ) {
125 for my $test_data (@tests_data) {
126 my ( $type, $test_string, $expected_value, $expected_result,
127 $expected_last_expression, $expected_exhaustion_status )
128 = @{$test_data};
129 $test_string .= $trailer;
130 my ( $actual_value, $actual_result, $actual_last_expression, $actual_exhaustion_status ) =
131 my_parser( $grammar_by_type{$type}, $test_string );
132 Test::More::is( $actual_value, $expected_value,
133 qq{$type: Value of "$test_string"} );
134 Test::More::is( $actual_result, $expected_result,
135 qq{$type: Result of "$test_string"} );
136 Test::More::is( $actual_last_expression, $expected_last_expression,
137 qq{$type: Last expression found in "$test_string"} );
138 if ($actual_exhaustion_status) {
139 if (not $expected_exhaustion_status) {
140 Test::More::fail(qq{$type: exhausted for "$test_string", but should not be});
141 } else {
142 Test::More::pass(qq{$type: exhausted for "$test_string"});
143 }
144 } else {
145 if ($expected_exhaustion_status) {
146 Test::More::fail(qq{$type: not exhausted for "$test_string", but should be});
147 } else {
148 Test::More::pass(qq{$type: not exhausted for "$test_string"});
149 }
150 }
151 } ## end for my $test_data (@tests_data)
152 } ## end for my $trailer ( q{}, q{ } )
153
154 # vim: expandtab shiftwidth=4: