| 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 tests |
| 18 |
|
| 19 |
# Test SLIF -- predicted, nulled and completed events with |
| 20 |
# deactivation and reactivation, initialization at DSL time, |
| 21 |
# initialization override at recce creation time |
| 22 |
|
| 23 |
use 5.010001; |
| 24 |
use strict; |
| 25 |
use warnings; |
| 26 |
|
| 27 |
use Test::More tests => 10; |
| 28 |
use English qw( -no_match_vars ); |
| 29 |
use lib 'inc'; |
| 30 |
use Marpa::R2::Test; |
| 31 |
use Marpa::R2; |
| 32 |
|
| 33 |
my $rules = <<'END_OF_GRAMMAR'; |
| 34 |
:start ::= sequence |
| 35 |
sequence ::= A B C D |
| 36 |
action => OK |
| 37 |
A ::= 'a' |
| 38 |
A ::= # empty |
| 39 |
B ::= 'b' |
| 40 |
B ::= # empty |
| 41 |
C ::= 'c' |
| 42 |
C ::= # empty |
| 43 |
D ::= 'd' |
| 44 |
D ::= # empty |
| 45 |
|
| 46 |
# Marpa::R2::Display |
| 47 |
# name: SLIF predicted event statement synopsis |
| 48 |
|
| 49 |
event '^a' = predicted A |
| 50 |
event '^b'=off = predicted B |
| 51 |
event '^c'=on = predicted C |
| 52 |
event '^d' = predicted D |
| 53 |
|
| 54 |
# Marpa::R2::Display::End |
| 55 |
|
| 56 |
# Marpa::R2::Display |
| 57 |
# name: SLIF completed event statement synopsis |
| 58 |
|
| 59 |
event 'a' = completed A |
| 60 |
event 'b'=off = completed B |
| 61 |
event 'c'=on = completed C |
| 62 |
event 'd' = completed D |
| 63 |
|
| 64 |
# Marpa::R2::Display::End |
| 65 |
|
| 66 |
# Marpa::R2::Display |
| 67 |
# name: SLIF nulled event statement synopsis |
| 68 |
|
| 69 |
event '!a' = nulled A |
| 70 |
event '!b'=off = nulled B |
| 71 |
event '!c'=on = nulled C |
| 72 |
event '!d' = nulled D |
| 73 |
|
| 74 |
# Marpa::R2::Display::End |
| 75 |
|
| 76 |
END_OF_GRAMMAR |
| 77 |
|
| 78 |
# This test the order of events |
| 79 |
# No more than one of each event type per line |
| 80 |
# so that order is non-arbitrary |
| 81 |
my $location_0_events = qq{0 !a !b !c !d ^a ^b ^c ^d\n}; |
| 82 |
my $after_0_events = <<'END_OF_EVENTS'; |
| 83 |
1 a !b !c !d ^b ^c ^d |
| 84 |
2 b !c !d ^c ^d |
| 85 |
3 c !d ^d |
| 86 |
4 d |
| 87 |
END_OF_EVENTS |
| 88 |
|
| 89 |
my $grammar = Marpa::R2::Scanless::G->new( { source => \$rules } ); |
| 90 |
|
| 91 |
my @events = map { ( '!' . $_, '^' . $_, $_ ) } qw(a b c d); |
| 92 |
|
| 93 |
# Test of all events |
| 94 |
my $events_expected = $location_0_events . $after_0_events ; |
| 95 |
$events_expected =~ s/ [!^]? b \s //xmsg; # Eliminate b events |
| 96 |
do_test( "all events", $grammar, q{abcd}, $events_expected ); |
| 97 |
|
| 98 |
$events_expected = $location_0_events . join "\n", ( 1 .. 4 ), q{}; |
| 99 |
$events_expected =~ s/ [!^]? b \s //xmsg; # Eliminate b events |
| 100 |
do_test( "all events deactivated", |
| 101 |
$grammar, q{abcd}, $events_expected, [] ); |
| 102 |
|
| 103 |
# Add events for symbol b |
| 104 |
my %event_is_active = map { ( $_, 1 ) } @events; |
| 105 |
my $extra_recce_arg = { 'event_is_active' => \%event_is_active }; |
| 106 |
$events_expected = $location_0_events . $after_0_events ; |
| 107 |
do_test( 'all events activated in $recce->new()', $grammar, q{abcd}, $events_expected, undef, |
| 108 |
$extra_recce_arg ); |
| 109 |
|
| 110 |
$events_expected = $location_0_events . join "\n", ( 1 .. 4 ), q{}; |
| 111 |
do_test( 'all events activated in $recce->new(), then deactivated', |
| 112 |
$grammar, q{abcd}, $events_expected, [], $extra_recce_arg ); |
| 113 |
|
| 114 |
$event_is_active{$_} = 0 for keys %event_is_active; |
| 115 |
|
| 116 |
$events_expected = join "\n", (0 .. 4), q{}; |
| 117 |
do_test( 'all events deactivated in $recce->new(), then deactivated again', |
| 118 |
$grammar, q{abcd}, $events_expected, [], $extra_recce_arg ); |
| 119 |
|
| 120 |
sub show_last_subtext { |
| 121 |
my ($recce) = @_; |
| 122 |
my ( $start, $end ) = $recce->last_completed_range('subtext'); |
| 123 |
return 'No expression was successfully parsed' if not defined $start; |
| 124 |
return $recce->range_to_string( $start, $end ); |
| 125 |
} |
| 126 |
|
| 127 |
sub do_test { |
| 128 |
my ( $test, $slg, $string, $expected_events, $reactivate_events, $recce_args ) = @_; |
| 129 |
$recce_args //= {}; |
| 130 |
my $recce = Marpa::R2::Scanless::R->new( |
| 131 |
{ grammar => $grammar, semantics_package => 'My_Actions' }, $recce_args ); |
| 132 |
if (defined $reactivate_events) { |
| 133 |
|
| 134 |
# Marpa::R2::Display |
| 135 |
# name: SLIF activate() method synopsis |
| 136 |
|
| 137 |
$recce->activate($_, 0) for @events; |
| 138 |
|
| 139 |
# Marpa::R2::Display::End |
| 140 |
|
| 141 |
$recce->activate($_) for @{$reactivate_events}; |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
my @actual_events = (); |
| 146 |
my $length = length $string; |
| 147 |
my $pos = $recce->read( \$string ); |
| 148 |
READ: while (1) { |
| 149 |
|
| 150 |
EVENT: |
| 151 |
for my $event ( @{ $recce->events() } ) { |
| 152 |
my ($name) = @{$event}; |
| 153 |
push @{$actual_events[$pos]}, $name; |
| 154 |
} |
| 155 |
|
| 156 |
last READ if $pos >= $length; |
| 157 |
$pos = $recce->resume($pos); |
| 158 |
} ## end READ: while (1) |
| 159 |
|
| 160 |
my $value_ref = $recce->value(); |
| 161 |
if ( not defined $value_ref ) { |
| 162 |
die "No parse\n"; |
| 163 |
} |
| 164 |
my $actual_value = ${$value_ref}; |
| 165 |
my $actual_events = q{}; |
| 166 |
for (my $i = 0; $i <= $length; $i++) { |
| 167 |
my $events = $actual_events[$i] // []; |
| 168 |
$actual_events .= join " ", $i, @{$events}; |
| 169 |
$actual_events .= "\n"; |
| 170 |
} |
| 171 |
Test::More::is( $actual_value, q{1792}, qq{Value for $test} ); |
| 172 |
Marpa::R2::Test::is( $actual_events, $expected_events, |
| 173 |
qq{Events for $test} ); |
| 174 |
} ## end sub do_test |
| 175 |
|
| 176 |
sub My_Actions::OK { return 1792 }; |
| 177 |
|
| 178 |
# vim: expandtab shiftwidth=4: |