Debian Codegraph @

t/sl_syn.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 # Synopsis for scannerless parsing, main POD page
18
19 use 5.010001;
20 use strict;
21 use warnings;
22
23 use Test::More tests => 21;
24 use English qw( -no_match_vars );
25 use lib 'inc';
26 use Marpa::R2::Test;
27 use Marpa::R2;
28
29 my $grammar = Marpa::R2::Scanless::G->new(
30 {
31
32 # Marpa::R2::Display
33 # name: Scanless concept example
34
35 source => \(<<'END_OF_SOURCE'),
36 :start ::= <number sequence>
37 <number sequence> ::= <number>+ action => add_sequence
38 number ~ digit+
39 digit ~ [0-9]
40 :discard ~ whitespace
41 whitespace ~ [\s]+
42 END_OF_SOURCE
43
44 # Marpa::R2::Display::End
45
46 }
47 );
48
49 package My_Actions;
50 sub add_sequence {
51 my ($self, @numbers) = @_;
52 return List::Util::sum @numbers, 0;
53 }
54
55 sub show_sequence_so_far {
56 my ($self) = @_;
57 my $recce = $self->{recce};
58 my ( $start, $end ) = $recce->last_completed_range('number sequence');
59 return if not defined $start;
60 my $sequence_so_far = $recce->range_to_string( $start, $end );
61 return $sequence_so_far;
62 } ## end sub show_sequence_so_far
63
64 package main;
65
66 sub my_parser {
67 my ( $grammar, $string ) = @_;
68
69 my $parse_arg = bless { grammar => $grammar }, 'My_Actions';
70
71 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
72 $parse_arg->{recce} = $recce;
73 my ( $parse_value, $parse_status, $sequence_so_far );
74
75 if ( not defined eval { $recce->read( \$string ); 1 } ) {
76 return 'No parse', $EVAL_ERROR, $parse_arg->show_sequence_so_far();
77 }
78 my $value_ref = $recce->value( $parse_arg);
79 if ( not defined $value_ref ) {
80 return 'No parse', 'Input read to end but no parse',
81 $parse_arg->show_sequence_so_far();
82 }
83 return [ return ${$value_ref}, 'Parse OK', ];
84 } ## end sub my_parser
85
86 my @tests_data = (
87 [ ' 1 2 3 1 2 4', 13, qr/\AParse \s+ OK\z/xms ],
88 [ ' 8675311', 8675311, qr/\AParse \s+ OK\z/xms ],
89 [ '867 5311', 6178, qr/\AParse \s+ OK\z/xms ],
90 [ ' 8 6 7 5 3 1 1', 31, qr/\AParse \s+ OK\z/xms ],
91 [ '1234', 1234, qr/\AParse \s+ OK\z/xms ],
92 [ '2 x 1234', 'No parse',
93 qr/ No \s+ lexeme \s+ found \s+ at \s /xms,
94 2
95 ],
96 [ '', 'No parse',
97 qr/\A Input \s+ read \s+ to \s+ end \s+ but \s+ no \s+ parse \z/xms,
98 ],
99 );
100
101 TEST:
102 for my $test_data (@tests_data) {
103 my ($test_string, $expected_value,
104 $expected_result, $expected_sequence_so_far
105 ) = @{$test_data};
106 $expected_sequence_so_far //= 'none';
107 my ($actual_value,
108 $actual_result, $actual_sequence_so_far
109 ) = my_parser( $grammar, $test_string );
110 $actual_sequence_so_far //= 'none';
111 Test::More::is( $actual_value, $expected_value, qq{Value of "$test_string"} );
112 Test::More::like( $actual_result, $expected_result, qq{Result of "$test_string"} );
113 Test::More::is( $actual_sequence_so_far, $expected_sequence_so_far, qq{Sequence so far from "$test_string"} );
114 } ## end TEST: for my $test_string (@test_strings)
115 # vim: expandtab shiftwidth=4: