Debian Codegraph @

t/sl_completed.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 -- completion events
18
19 use 5.010001;
20 use strict;
21 use warnings;
22
23 use Test::More tests => 10;
24 use English qw( -no_match_vars );
25 use lib 'inc';
26 use Marpa::R2::Test;
27 use Marpa::R2;
28
29 my $rules = <<'END_OF_GRAMMAR';
30 :start ::= text
31 text ::= <text segment>* action => OK
32 <text segment> ::= subtext
33 <text segment> ::= <word>
34 subtext ::= '(' text ')'
35
36 event subtext = completed <subtext>
37
38 word ~ [\w]+
39 :discard ~ whitespace
40 whitespace ~ [\s]+
41 END_OF_GRAMMAR
42
43 my $grammar = Marpa::R2::Scanless::G->new( { source => \$rules } );
44
45
46 do_test($grammar, q{42 ( hi 42 hi ) 7 11}, [ '( hi 42 hi )' ]);
47 do_test($grammar, q{42 ( hi) 42 (hi ) 7 11}, [ '( hi)', '(hi )' ] );
48 do_test($grammar, q{(hi 42 hi)}, ['(hi 42 hi)']);
49 do_test($grammar, q{1(2(3(4)))}, [ qw{ (4) (3(4)) (2(3(4))) } ]);
50 do_test($grammar, q{(((1)2)3)4}, [ qw{(1) ((1)2) (((1)2)3)} ]);
51
52 sub show_last_subtext {
53 my ($recce) = @_;
54 my ( $start, $end ) = $recce->last_completed_range('subtext');
55 return 'No expression was successfully parsed' if not defined $start;
56 return $recce->range_to_string( $start, $end );
57 }
58
59 sub do_test {
60 my ( $grammar, $string, $expected_events ) = @_;
61 my @actual_events;
62 my $recce = Marpa::R2::Scanless::R->new(
63 { grammar => $grammar, semantics_package => 'My_Actions' } );
64 my $length = length $string;
65 my $pos = $recce->read( \$string );
66 READ: while (1) {
67
68 # Marpa::R2::Display
69 # name: SLR event() method synopsis
70
71 EVENT: for (
72 my $event_ix = 0;
73 my $event = $recce->event($event_ix);
74 $event_ix++
75 )
76 {
77 my ($name) = @{$event};
78 if ( $name eq 'subtext' ) {
79 push @actual_events, show_last_subtext($recce);
80 next EVENT;
81 }
82 } ## end for ( my $event_ix = 0; my $event = $recce->event($event_ix...))
83
84 # Marpa::R2::Display::End
85
86 last READ if $pos >= $length;
87 $pos = $recce->resume($pos);
88 } ## end READ: while (1)
89 my $value_ref = $recce->value();
90 if ( not defined $value_ref ) {
91 die "No parse\n";
92 }
93 my $actual_value = ${$value_ref};
94 Test::More::is( $actual_value, q{1792}, qq{Value for "$string"} );
95 Test::More::is_deeply( \@actual_events, $expected_events, qq{Events for "$string"} );
96 } ## end sub do_test
97
98 sub My_Actions::OK { return 1792 };
99
100 # vim: expandtab shiftwidth=4: