| 1 |
#!/usr/bin/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 |
# Tests of the SLIF's discard events |
| 18 |
|
| 19 |
use 5.010001; |
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
use Test::More tests => 342; |
| 23 |
use English qw( -no_match_vars ); |
| 24 |
use Scalar::Util; |
| 25 |
|
| 26 |
use lib 'inc'; |
| 27 |
use Marpa::R2::Test; |
| 28 |
|
| 29 |
## no critic (ErrorHandling::RequireCarping); |
| 30 |
|
| 31 |
use Marpa::R2; |
| 32 |
|
| 33 |
my @settings = ( '=on', '=off', q{} ); |
| 34 |
for my $grammar_setting (@settings) { |
| 35 |
|
| 36 |
my $null_dsl = <<'END_OF_SOURCE'; |
| 37 |
:default ::= action => [g1start,g1len,name,values] |
| 38 |
discard default = event => :symbol=off |
| 39 |
lexeme default = action => [ g1start, g1len, start, length, value ] |
| 40 |
latm => 1 |
| 41 |
|
| 42 |
Script ::= |
| 43 |
:discard ~ whitespace event => ws=on |
| 44 |
whitespace ~ [\s] |
| 45 |
END_OF_SOURCE |
| 46 |
|
| 47 |
$null_dsl =~ s/ =on $ /$grammar_setting/xms; |
| 48 |
|
| 49 |
my $null_grammar = Marpa::R2::Scanless::G->new( |
| 50 |
{ bless_package => 'My_Nodes', |
| 51 |
source => \$null_dsl, |
| 52 |
} |
| 53 |
); |
| 54 |
|
| 55 |
for my $recce_setting (@settings) { |
| 56 |
|
| 57 |
my $event_is_on = 1; |
| 58 |
my $recce_arg = {}; |
| 59 |
if ( $recce_setting eq '=on' ) { |
| 60 |
$recce_arg = { event_is_active => { ws => 1 } }; |
| 61 |
} |
| 62 |
elsif ( $recce_setting eq '=off' ) { |
| 63 |
$recce_arg = { event_is_active => { ws => 0 } }; |
| 64 |
$event_is_on = 0; |
| 65 |
} |
| 66 |
else { |
| 67 |
$event_is_on = 0 if $grammar_setting eq '=off'; |
| 68 |
} |
| 69 |
|
| 70 |
for my $input ( q{}, ' ', ' ' ) { |
| 71 |
|
| 72 |
my $recce = |
| 73 |
Marpa::R2::Scanless::R->new( { grammar => $null_grammar }, |
| 74 |
$recce_arg ); |
| 75 |
|
| 76 |
my $length = length $input; |
| 77 |
my $pos = $recce->read( \$input ); |
| 78 |
|
| 79 |
my $p_events = gather_events( $recce, $pos, $length ); |
| 80 |
my $actual_events = join q{ }, |
| 81 |
map { $_->[0], $_->[-1] } @{$p_events}; |
| 82 |
|
| 83 |
my $expected_events = q{}; |
| 84 |
if ($event_is_on) { |
| 85 |
$expected_events = join q{ }, ( ('ws 0') x $length ); |
| 86 |
} |
| 87 |
|
| 88 |
my $test_name = "Test of $length discarded spaces"; |
| 89 |
$test_name .= ' g' . $grammar_setting if $grammar_setting; |
| 90 |
$test_name .= ' r' . $recce_setting if $recce_setting; |
| 91 |
Test::More::is( $actual_events, $expected_events, $test_name ); |
| 92 |
|
| 93 |
my $value_ref = $recce->value(); |
| 94 |
die "No parse was found\n" if not defined $value_ref; |
| 95 |
|
| 96 |
} ## end for my $input ( q{}, ' ', ' ' ) |
| 97 |
|
| 98 |
# Discards with a non-trivial grammar |
| 99 |
|
| 100 |
my $non_trivial_dsl = <<'END_OF_SOURCE'; |
| 101 |
:default ::= action => [g1start,g1len,name,values] |
| 102 |
discard default = event => :symbol=off |
| 103 |
lexeme default = action => [ g1start, g1len, start, length, value ] |
| 104 |
latm => 1 |
| 105 |
|
| 106 |
text ::= a b |
| 107 |
a ~ 'a' |
| 108 |
b ~ 'b' |
| 109 |
:discard ~ whitespace event => ws=on |
| 110 |
whitespace ~ [\s] |
| 111 |
END_OF_SOURCE |
| 112 |
$non_trivial_dsl =~ s/ =on $ /$grammar_setting/xms; |
| 113 |
|
| 114 |
my $non_trivial_grammar = Marpa::R2::Scanless::G->new( |
| 115 |
{ bless_package => 'My_Nodes', |
| 116 |
source => \$non_trivial_dsl |
| 117 |
} |
| 118 |
); |
| 119 |
|
| 120 |
for my $pattern ( 0 .. 7 ) { |
| 121 |
|
| 122 |
# use binary numbers to generate all possible |
| 123 |
# space patterns |
| 124 |
my @spaces = split //xms, sprintf "%03b", $pattern; |
| 125 |
my @chars = qw{a b}; |
| 126 |
my @input = (); |
| 127 |
for my $i ( 0 .. 1 ) { |
| 128 |
push @input, ' ' if $spaces[$i]; |
| 129 |
push @input, $chars[$i]; |
| 130 |
} |
| 131 |
push @input, ' ' if $spaces[-1]; |
| 132 |
|
| 133 |
my @expected = (); |
| 134 |
for my $i ( 0 .. $#spaces ) { |
| 135 |
push @expected, "ws $i" if $spaces[$i]; |
| 136 |
} |
| 137 |
|
| 138 |
# say join q{}, '^', @input, '$'; |
| 139 |
my $input = join q{}, @input; |
| 140 |
|
| 141 |
my $recce = Marpa::R2::Scanless::R->new( |
| 142 |
{ grammar => $non_trivial_grammar }, $recce_arg ); |
| 143 |
|
| 144 |
my $length = length $input; |
| 145 |
my $pos = $recce->read( \$input ); |
| 146 |
|
| 147 |
my $p_events = gather_events( $recce, $pos, $length ); |
| 148 |
my $actual_events = join q{ }, |
| 149 |
map { $_->[0], $_->[-1] } @{$p_events}; |
| 150 |
my $expected_events = q{}; |
| 151 |
if ($event_is_on) { |
| 152 |
$expected_events = join q{ }, @expected; |
| 153 |
} |
| 154 |
my $test_name = qq{Test of non-trivial parse, input="$input"}; |
| 155 |
$test_name .= ' g' . $grammar_setting if $grammar_setting; |
| 156 |
$test_name .= ' r' . $recce_setting if $recce_setting; |
| 157 |
Test::More::is( $actual_events, $expected_events, $test_name ); |
| 158 |
|
| 159 |
my $value_ref = $recce->value(); |
| 160 |
die "No parse was found\n" if not defined $value_ref; |
| 161 |
|
| 162 |
} ## end for my $pattern ( 0 .. 7 ) |
| 163 |
} ## end for my $recce_setting ( '=on', '=off', q{} ) |
| 164 |
} ## end for my $grammar_setting ( '=on', '=off', q{} ) |
| 165 |
|
| 166 |
# Test of 2 types of events |
| 167 |
|
| 168 |
for my $ws_g_setting (@settings) { |
| 169 |
for my $bracketed_g_setting (@settings) { |
| 170 |
|
| 171 |
my $dsl2 = <<'END_OF_SOURCE'; |
| 172 |
:default ::= action => [g1start,g1len,name,values] |
| 173 |
discard default = event => :symbol=off |
| 174 |
lexeme default = action => [ g1start, g1len, start, length, value ] |
| 175 |
latm => 1 |
| 176 |
|
| 177 |
Script ::= |
| 178 |
:discard ~ whitespace event => ws=on |
| 179 |
whitespace ~ [\s] |
| 180 |
:discard ~ bracketed event => bracketed=on |
| 181 |
bracketed ~ '(' <no close bracket> ')' |
| 182 |
<no close bracket> ~ [^)]* |
| 183 |
END_OF_SOURCE |
| 184 |
|
| 185 |
$dsl2 =~ s/ ws=on $ /ws$ws_g_setting/xms; |
| 186 |
$dsl2 =~ s/ bracketed=on $ /bracketed$bracketed_g_setting/xms; |
| 187 |
|
| 188 |
my $grammar2 = Marpa::R2::Scanless::G->new( |
| 189 |
{ bless_package => 'My_Nodes', source => \$dsl2, } ); |
| 190 |
|
| 191 |
for my $ws_r_setting (@settings) { |
| 192 |
for my $bracketed_r_setting (@settings) { |
| 193 |
|
| 194 |
my %event_is_on = ( bracketed => 1, ws => 1 ); |
| 195 |
my %event_is_active_value = (); |
| 196 |
if ( $ws_r_setting eq '=on' ) { |
| 197 |
$event_is_active_value{ws} = 1; |
| 198 |
} |
| 199 |
elsif ( $ws_r_setting eq '=off' ) { |
| 200 |
$event_is_active_value{ws} = 0; |
| 201 |
$event_is_on{ws} = 0; |
| 202 |
} |
| 203 |
else { |
| 204 |
$event_is_on{ws} = 0 if $ws_g_setting eq '=off'; |
| 205 |
} |
| 206 |
if ( $bracketed_r_setting eq '=on' ) { |
| 207 |
$event_is_active_value{bracketed} = 1; |
| 208 |
} |
| 209 |
elsif ( $bracketed_r_setting eq '=off' ) { |
| 210 |
$event_is_active_value{bracketed} = 0; |
| 211 |
$event_is_on{bracketed} = 0; |
| 212 |
} |
| 213 |
else { |
| 214 |
$event_is_on{bracketed} = 0 |
| 215 |
if $bracketed_g_setting eq '=off'; |
| 216 |
} |
| 217 |
my @extra_recce_args = |
| 218 |
( { event_is_active => \%event_is_active_value } ) |
| 219 |
if scalar %event_is_active_value; |
| 220 |
|
| 221 |
for my $input ( q{ (x) }, q{(x) }, q{ (x)} ) { |
| 222 |
my $recce = |
| 223 |
Marpa::R2::Scanless::R->new( { grammar => $grammar2 }, |
| 224 |
@extra_recce_args ); |
| 225 |
|
| 226 |
my $length = length $input; |
| 227 |
my $pos = $recce->read( \$input ); |
| 228 |
|
| 229 |
my $p_events = gather_events( $recce, $pos, $length ); |
| 230 |
my $actual_events = join q{ }, |
| 231 |
map { $_->[0], $_->[-1] } @{$p_events}; |
| 232 |
my $expected_events = $input; |
| 233 |
if ( $event_is_on{bracketed} ) { |
| 234 |
$expected_events =~ s/[(] [x]+ [)]/bracketed 0/xms; |
| 235 |
} |
| 236 |
else { |
| 237 |
# Place an 'x' marker so we don't confuse initial |
| 238 |
# and terminal spaces, during the coming substitutions |
| 239 |
$expected_events =~ s/[(] [x]+ [)]/x/xms; |
| 240 |
} |
| 241 |
if ( $event_is_on{ws} ) { |
| 242 |
$expected_events =~ s/\A \s /ws 0 /xms; |
| 243 |
$expected_events =~ s/\s \z/ ws 0/xms; |
| 244 |
} |
| 245 |
else { |
| 246 |
$expected_events =~ s/\A \s //xms; |
| 247 |
$expected_events =~ s/\s \z//xms; |
| 248 |
} |
| 249 |
|
| 250 |
# Clean up extra spaces and the 'x' markers |
| 251 |
$expected_events =~ s/x/ /gxms; |
| 252 |
$expected_events =~ s/\s+/ /gxms; |
| 253 |
$expected_events =~ s/\s \z//gxms; |
| 254 |
$expected_events =~ s/\A \s//gxms; |
| 255 |
|
| 256 |
my $test_name = |
| 257 |
qq{Test of two discard types, input="$input"}; |
| 258 |
$test_name .= ' ws:g' . $ws_g_setting if $ws_g_setting; |
| 259 |
$test_name .= ' ws:r' . $ws_r_setting if $ws_r_setting; |
| 260 |
$test_name .= ' bracketed:g' . $bracketed_g_setting |
| 261 |
if $bracketed_g_setting; |
| 262 |
$test_name .= ' bracketed:r' . $bracketed_r_setting |
| 263 |
if $bracketed_r_setting; |
| 264 |
|
| 265 |
Test::More::is( $actual_events, $expected_events, |
| 266 |
$test_name ); |
| 267 |
|
| 268 |
my $value_ref = $recce->value(); |
| 269 |
die "No parse was found\n" if not defined $value_ref; |
| 270 |
|
| 271 |
} ## end for my $input ( q{ (x) }, q{(x) }, q{ (x)} ) |
| 272 |
} ## end for my $bracketed_r_setting (@settings) |
| 273 |
} ## end for my $ws_r_setting (@settings) |
| 274 |
} ## end for my $bracketed_g_setting (@settings) |
| 275 |
} ## end for my $ws_g_setting (@settings) |
| 276 |
|
| 277 |
sub gather_events { |
| 278 |
my ($recce, $pos, $length) = @_; |
| 279 |
my @actual_events; |
| 280 |
READ: while (1) { |
| 281 |
|
| 282 |
EVENT: |
| 283 |
for my $event ( @{ $recce->events() } ) { |
| 284 |
my ( $name, @other_stuff ) = @{$event}; |
| 285 |
# say STDERR 'Event received!!! -- ', Data::Dumper::Dumper($event); |
| 286 |
push @actual_events, $event; |
| 287 |
} |
| 288 |
|
| 289 |
last READ if $pos >= $length; |
| 290 |
$pos = $recce->resume($pos); |
| 291 |
} ## end READ: while (1) |
| 292 |
return \@actual_events; |
| 293 |
} ## end sub gather_event |
| 294 |
# vim: expandtab shiftwidth=4: |