| 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 -- named lexeme events |
| 18 |
# deactivation and reactivation |
| 19 |
|
| 20 |
use 5.010001; |
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
|
| 24 |
use Test::More tests => 12; |
| 25 |
use English qw( -no_match_vars ); |
| 26 |
use lib 'inc'; |
| 27 |
use Marpa::R2::Test; |
| 28 |
use Marpa::R2; |
| 29 |
|
| 30 |
my $base_rules = <<'END_OF_GRAMMAR'; |
| 31 |
:start ::= sequence |
| 32 |
sequence ::= char* action => OK |
| 33 |
char ::= a | b | c | d |
| 34 |
a ~ 'a' |
| 35 |
b ~ 'b' |
| 36 |
c ~ 'c' |
| 37 |
d ~ 'd' |
| 38 |
|
| 39 |
# Marpa::R2::Display |
| 40 |
# name: SLIF named lexeme event synopsis |
| 41 |
|
| 42 |
:lexeme ~ <a> pause => before event => 'before a' |
| 43 |
:lexeme ~ <b> pause => after event => 'after b'=on |
| 44 |
:lexeme ~ <c> pause => before event => 'before c'=off |
| 45 |
:lexeme ~ <d> pause => after event => 'after d' |
| 46 |
|
| 47 |
# Marpa::R2::Display::End |
| 48 |
|
| 49 |
END_OF_GRAMMAR |
| 50 |
|
| 51 |
my $rules = $base_rules; |
| 52 |
$rules =~ s/=off$//gxms; |
| 53 |
|
| 54 |
# This test the order of events |
| 55 |
# No more than one of each event type per line |
| 56 |
# so that order is non-arbitrary |
| 57 |
my $events_expected = <<'END_OF_EVENTS'; |
| 58 |
END_OF_EVENTS |
| 59 |
|
| 60 |
my $grammar = Marpa::R2::Scanless::G->new( { source => \$rules } ); |
| 61 |
|
| 62 |
my %base_expected_events; |
| 63 |
$base_expected_events{'all'} = <<'END_OF_EVENTS'; |
| 64 |
0 before a |
| 65 |
1 before a |
| 66 |
3 after b |
| 67 |
4 after b |
| 68 |
5 after b |
| 69 |
5 before c |
| 70 |
6 before c |
| 71 |
7 before c |
| 72 |
9 after d |
| 73 |
9 before a |
| 74 |
10 before a |
| 75 |
11 before a |
| 76 |
13 after b |
| 77 |
13 before c |
| 78 |
14 before c |
| 79 |
16 after d |
| 80 |
17 after d |
| 81 |
18 after d |
| 82 |
19 after d |
| 83 |
19 before a |
| 84 |
21 after b |
| 85 |
21 before c |
| 86 |
23 after d |
| 87 |
END_OF_EVENTS |
| 88 |
$base_expected_events{'once'} = <<'END_OF_EVENTS'; |
| 89 |
0 before a |
| 90 |
3 after b |
| 91 |
5 before c |
| 92 |
9 after d |
| 93 |
END_OF_EVENTS |
| 94 |
$base_expected_events{'seq'} = <<'END_OF_EVENTS'; |
| 95 |
0 before a |
| 96 |
3 after b |
| 97 |
5 before c |
| 98 |
9 after d |
| 99 |
9 before a |
| 100 |
13 after b |
| 101 |
13 before c |
| 102 |
16 after d |
| 103 |
19 before a |
| 104 |
21 after b |
| 105 |
21 before c |
| 106 |
23 after d |
| 107 |
END_OF_EVENTS |
| 108 |
|
| 109 |
my %expected_events = %base_expected_events; |
| 110 |
|
| 111 |
sub do_test { |
| 112 |
my ($slr, $test) = @_; |
| 113 |
state $string = q{aabbbcccdaaabccddddabcd}; |
| 114 |
state $length = length $string; |
| 115 |
my $pos = $slr->read( \$string ); |
| 116 |
my $actual_events = q{}; |
| 117 |
my $deactivated_event_name; |
| 118 |
READ: while (1) { |
| 119 |
my @actual_events = (); |
| 120 |
my $event_name; |
| 121 |
EVENT: |
| 122 |
for my $event ( @{ $slr->events() } ) { |
| 123 |
my ($event_name) = @{$event}; |
| 124 |
die "event name is undef" if not defined $event_name; |
| 125 |
die "Unexpected event: $event_name" |
| 126 |
if not $event_name =~ m/\A (before|after) \s [abcd] \z/xms; |
| 127 |
ACTIVATION_LOGIC: { |
| 128 |
last ACTIVATION_LOGIC if $test eq 'all'; |
| 129 |
if ( $test eq 'once' ) { |
| 130 |
$slr->activate( $event_name, 0 ); |
| 131 |
} |
| 132 |
if ( $test eq 'seq' ) { |
| 133 |
$slr->activate( $deactivated_event_name, 1 ) |
| 134 |
if defined $deactivated_event_name; |
| 135 |
$slr->activate( $event_name, 0 ); |
| 136 |
$deactivated_event_name = $event_name; |
| 137 |
} ## end if ( $test eq 'seq' ) |
| 138 |
} ## end ACTIVATION_LOGIC: |
| 139 |
push @actual_events, $event_name; |
| 140 |
} ## end for my $event ( @{ $slr->events() } ) |
| 141 |
if (@actual_events) { |
| 142 |
$actual_events .= join q{ }, $pos, @actual_events; |
| 143 |
$actual_events .= "\n"; |
| 144 |
my ( $start_of_lexeme, $length_of_lexeme ) = $slr->pause_span(); |
| 145 |
$pos = $start_of_lexeme + $length_of_lexeme; |
| 146 |
} elsif (my $pause_lexeme = $slr->pause_lexeme()) { |
| 147 |
$actual_events .= "unnamed\n"; |
| 148 |
my ( $start_of_lexeme, $length_of_lexeme ) = $slr->pause_span(); |
| 149 |
$pos = $start_of_lexeme + $length_of_lexeme; |
| 150 |
} |
| 151 |
last READ if $pos >= $length; |
| 152 |
$pos = $slr->resume($pos); |
| 153 |
} ## end READ: while (1) |
| 154 |
my $value_ref = $slr->value(); |
| 155 |
if ( not defined $value_ref ) { |
| 156 |
die "No parse\n"; |
| 157 |
} |
| 158 |
my $actual_value = ${$value_ref}; |
| 159 |
Test::More::is( $actual_value, q{1792}, qq{Value for test "$test"} ); |
| 160 |
my $expected_events = q{}; |
| 161 |
Marpa::R2::Test::is( $actual_events, $expected_events{$test}, |
| 162 |
qq{Events for test "$test"} ); |
| 163 |
} ## end sub do_test |
| 164 |
|
| 165 |
my $slr = Marpa::R2::Scanless::R->new( { grammar => $grammar, semantics_package => 'My_Actions' } ); |
| 166 |
do_test($slr, 'all'); |
| 167 |
$slr = Marpa::R2::Scanless::R->new( { grammar => $grammar, semantics_package => 'My_Actions' } ); |
| 168 |
do_test($slr, 'once'); |
| 169 |
$slr = Marpa::R2::Scanless::R->new( { grammar => $grammar, semantics_package => 'My_Actions' } ); |
| 170 |
do_test($slr, 'seq'); |
| 171 |
|
| 172 |
# Once again, but with unnamed events |
| 173 |
$expected_events{'all'} =~ s/^ [^\n]+ $/unnamed/gxms; |
| 174 |
$rules =~ s/^ ([:] lexeme \s [^\n]* ) \s+ event \s* [=][>] [^\n]* $/$1/gxms; |
| 175 |
$grammar = Marpa::R2::Scanless::G->new( { source => \$rules } ); |
| 176 |
$slr = Marpa::R2::Scanless::R->new( { grammar => $grammar, semantics_package => 'My_Actions' } ); |
| 177 |
do_test($slr, 'all'); |
| 178 |
|
| 179 |
# Yet another time, with initializers |
| 180 |
%expected_events = %base_expected_events; |
| 181 |
$expected_events{'all'} =~ s/^\d+ \s before \s c \n//gxms; |
| 182 |
$rules = $base_rules; |
| 183 |
$grammar = Marpa::R2::Scanless::G->new( { source => \$rules } ); |
| 184 |
$slr = Marpa::R2::Scanless::R->new( { grammar => $grammar, semantics_package => 'My_Actions' } ); |
| 185 |
do_test($slr, 'all'); |
| 186 |
|
| 187 |
# Yet another time, with initializers |
| 188 |
%expected_events = %base_expected_events; |
| 189 |
$expected_events{'all'} =~ s/^\d+ \s after \s b \n//gxms; |
| 190 |
$rules = $base_rules; |
| 191 |
$grammar = Marpa::R2::Scanless::G->new( { source => \$rules } ); |
| 192 |
|
| 193 |
# Marpa::R2::Display |
| 194 |
# name: SLIF recce event_is_active named arg example |
| 195 |
|
| 196 |
$slr = Marpa::R2::Scanless::R->new( |
| 197 |
{ grammar => $grammar, |
| 198 |
semantics_package => 'My_Actions', |
| 199 |
event_is_active => { 'before c' => 1, 'after b' => 0 } |
| 200 |
} |
| 201 |
); |
| 202 |
|
| 203 |
# Marpa::R2::Display::End |
| 204 |
|
| 205 |
do_test($slr, 'all'); |
| 206 |
|
| 207 |
sub My_Actions::OK { return 1792 } |
| 208 |
|
| 209 |
# vim: expandtab shiftwidth=4: |