Debian Codegraph @

t/sl_leo.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 # The example from p. 166 of Leo's paper,
17 # augmented to test Leo prediction items.
18 #
19
20 use 5.010001;
21 use strict;
22 use warnings;
23
24 use Test::More tests => 2;
25
26 use lib 'inc';
27 use Marpa::R2::Test;
28 use Marpa::R2;
29
30 ## no critic (Subroutines::RequireArgUnpacking)
31
32 sub main::default_action {
33 shift;
34 return ( join q{}, grep {defined} @_ );
35 }
36
37 ## use critic
38
39 my $grammar = Marpa::R2::Scanless::G->new(
40 {
41 default_action => 'main::default_action',
42 source => \(<<'END_OF_DSL'),
43 :start ::= S
44 S ::= 'a' A
45 A ::= B
46 B ::= C
47 C ::= S
48 S ::=
49 event A = completed <A>
50 event C = completed <C>
51 event S = completed <S>
52 event 'A[]' = nulled <A>
53 event 'C[]' = nulled <C>
54 event 'S[]' = nulled <S>
55 END_OF_DSL
56 }
57 );
58
59 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
60 my $input = 'aaa';
61 my $event_history = q{};
62 my $pos = $recce->read( \$input );
63 READ: while (1) {
64 my @event_names;
65 for ( my $ix = 0; my $event = $recce->event($ix); $ix++ ) {
66 push @event_names, @{$event};
67 }
68 $event_history .= join q{ }, $pos, sort @event_names;
69 $event_history .= "\n";
70 last READ if $pos >= length $input;
71 $pos = $recce->resume();
72 } ## end READ: while (1)
73 my $value_ref = $recce->value();
74 my $value = $value_ref ? ${$value_ref} : 'No parse';
75 Marpa::R2::Test::is( $value, 'aaa', 'Leo SLIF parse' );
76 Marpa::R2::Test::is( $event_history, <<'END_OF_TEXT', 'Event history' );
77 0 S[]
78 1 A[] C[] S S[]
79 2 A A[] C C[] S S[]
80 3 A A[] C C[] S S[]
81 END_OF_TEXT
82
83 # vim: expandtab shiftwidth=4: