| 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 |
use 5.010001; |
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
use English qw( -no_match_vars ); |
| 21 |
|
| 22 |
use Test::More tests => 6; |
| 23 |
use lib 'inc'; |
| 24 |
use Marpa::R2::Test; |
| 25 |
use Marpa::R2; |
| 26 |
|
| 27 |
package Test_Grammar; |
| 28 |
|
| 29 |
# This grammar is from Data::Dumper, |
| 30 |
# which disagrees with Perl::Critic about proper |
| 31 |
# use of quotes and with perltidy about |
| 32 |
# formatting |
| 33 |
|
| 34 |
#<<< no perltidy |
| 35 |
##no critic (ValuesAndExpressions::ProhibitNoisyQuotes) |
| 36 |
|
| 37 |
$Test_Grammar::MARPA_OPTIONS = [ |
| 38 |
{ |
| 39 |
default_empty_action => '::undef', |
| 40 |
'rules' => [ |
| 41 |
{ |
| 42 |
'action' => 'comment', |
| 43 |
'lhs' => 'comment:optional', |
| 44 |
'rhs' => [ |
| 45 |
'comment' |
| 46 |
] |
| 47 |
}, |
| 48 |
{ |
| 49 |
'lhs' => 'comment:optional', |
| 50 |
'rhs' => [] |
| 51 |
}, |
| 52 |
{ |
| 53 |
'action' => 'show_perl_line', |
| 54 |
'lhs' => 'perl-line', |
| 55 |
'rhs' => [ |
| 56 |
'perl-statements', |
| 57 |
'comment:optional' |
| 58 |
] |
| 59 |
}, |
| 60 |
{ |
| 61 |
'action' => 'show_statement_sequence', |
| 62 |
'lhs' => 'perl-statements', |
| 63 |
'min' => 1, |
| 64 |
'rhs' => [ |
| 65 |
'perl-statement' |
| 66 |
], |
| 67 |
'separator' => 'semicolon' |
| 68 |
}, |
| 69 |
{ |
| 70 |
'action' => 'show_division', |
| 71 |
'lhs' => 'perl-statement', |
| 72 |
'rhs' => [ |
| 73 |
'division' |
| 74 |
] |
| 75 |
}, |
| 76 |
{ |
| 77 |
'action' => 'show_function_call', |
| 78 |
'lhs' => 'perl-statement', |
| 79 |
'rhs' => [ |
| 80 |
'function-call' |
| 81 |
] |
| 82 |
}, |
| 83 |
{ |
| 84 |
'action' => 'show_die', |
| 85 |
'lhs' => 'perl-statement', |
| 86 |
'rhs' => [ |
| 87 |
'die:k0', |
| 88 |
'string-literal' |
| 89 |
] |
| 90 |
}, |
| 91 |
{ |
| 92 |
'lhs' => 'division', |
| 93 |
'rhs' => [ |
| 94 |
'expr', |
| 95 |
'division-sign', |
| 96 |
'expr' |
| 97 |
] |
| 98 |
}, |
| 99 |
{ |
| 100 |
'lhs' => 'expr', |
| 101 |
'rhs' => [ |
| 102 |
'function-call' |
| 103 |
] |
| 104 |
}, |
| 105 |
{ |
| 106 |
'lhs' => 'expr', |
| 107 |
'rhs' => [ |
| 108 |
'number' |
| 109 |
] |
| 110 |
}, |
| 111 |
{ |
| 112 |
'action' => 'show_unary', |
| 113 |
'lhs' => 'function-call', |
| 114 |
'rhs' => [ |
| 115 |
'unary-function-name', |
| 116 |
'argument' |
| 117 |
] |
| 118 |
}, |
| 119 |
{ |
| 120 |
'action' => 'show_nullary', |
| 121 |
'lhs' => 'function-call', |
| 122 |
'rhs' => [ |
| 123 |
'nullary-function-name' |
| 124 |
] |
| 125 |
}, |
| 126 |
{ |
| 127 |
'lhs' => 'argument', |
| 128 |
'rhs' => [ |
| 129 |
'pattern-match' |
| 130 |
] |
| 131 |
} |
| 132 |
], |
| 133 |
'start' => 'perl-line', |
| 134 |
'terminals' => [ |
| 135 |
'die:k0', |
| 136 |
'unary-function-name', |
| 137 |
'nullary-function-name', |
| 138 |
'number', |
| 139 |
'semicolon', |
| 140 |
'division-sign', |
| 141 |
'pattern-match', |
| 142 |
'comment', |
| 143 |
'string-literal' |
| 144 |
], |
| 145 |
} |
| 146 |
]; |
| 147 |
|
| 148 |
my %regexes = ( |
| 149 |
'die:k0' => 'die', |
| 150 |
'unary-function-name' => '(caller|eof|sin|localtime)', |
| 151 |
'nullary-function-name' => '(caller|eof|sin|time|localtime)', |
| 152 |
'number' => '\\d+', |
| 153 |
'semicolon' => ';', |
| 154 |
'division-sign' => '[/]', |
| 155 |
'pattern-match' => '[/][^/]*/', |
| 156 |
'comment' => '[#].*', |
| 157 |
'string-literal' => '"[^"]*"', |
| 158 |
); |
| 159 |
|
| 160 |
## use critic |
| 161 |
#>>> |
| 162 |
# |
| 163 |
|
| 164 |
package main; |
| 165 |
|
| 166 |
my @test_data = ( |
| 167 |
[ 'sin', |
| 168 |
q{sin / 25 ; # / ; die "this dies!"}, |
| 169 |
[ 'sin function call, die statement', 'division, comment' ] |
| 170 |
], |
| 171 |
[ 'time', q{time / 25 ; # / ; die "this dies!"}, ['division, comment'] ] |
| 172 |
); |
| 173 |
|
| 174 |
my $g = Marpa::R2::Grammar->new( |
| 175 |
{ warnings => 1, |
| 176 |
actions => 'main', |
| 177 |
}, |
| 178 |
@{$Test_Grammar::MARPA_OPTIONS} |
| 179 |
); |
| 180 |
|
| 181 |
$g->precompute(); |
| 182 |
|
| 183 |
TEST: for my $test_data (@test_data) { |
| 184 |
|
| 185 |
my ( $test_name, $test_input, $test_results ) = @{$test_data}; |
| 186 |
|
| 187 |
my @event_tokens = keys %regexes; |
| 188 |
my $recce = Marpa::R2::Recognizer->new( |
| 189 |
{ grammar => $g, event_if_expected => \@event_tokens } ); |
| 190 |
|
| 191 |
my $input_length = length $test_input; |
| 192 |
pos $test_input = 0; |
| 193 |
my $terminals_expected_matches_events = 1; |
| 194 |
|
| 195 |
# Marpa::R2::Display |
| 196 |
# name: Recognizer terminals_expected Synopsis |
| 197 |
|
| 198 |
my $terminals_expected = $recce->terminals_expected(); |
| 199 |
|
| 200 |
# Marpa::R2::Display::End |
| 201 |
|
| 202 |
for ( my $pos = 0; $pos < $input_length; $pos++ ) { |
| 203 |
|
| 204 |
# Marpa::R2::Display |
| 205 |
# name: Recognizer events() Synopsis |
| 206 |
|
| 207 |
my @expected_symbols = |
| 208 |
map { $_->[1]; } |
| 209 |
grep { $_->[0] eq 'SYMBOL_EXPECTED' } @{ $recce->events() }; |
| 210 |
|
| 211 |
# Marpa::R2::Display::End |
| 212 |
|
| 213 |
TOKEN: for my $token ( @{$terminals_expected} ) { |
| 214 |
next TOKEN if grep { $token eq $_ } @expected_symbols; |
| 215 |
$terminals_expected_matches_events = 0; |
| 216 |
Test::More::diag( $token, ' not in events() at pos ', $pos ); |
| 217 |
} |
| 218 |
|
| 219 |
TOKEN: for my $token (@expected_symbols) { |
| 220 |
next TOKEN if grep { $token eq $_ } @{$terminals_expected}; |
| 221 |
$terminals_expected_matches_events = 0; |
| 222 |
Test::More::diag( $token, ' not in terminals_expected() at pos ', |
| 223 |
$pos ); |
| 224 |
} ## end TOKEN: for my $token (@expected_symbols) |
| 225 |
|
| 226 |
TOKEN_TYPE: for my $token ( keys %regexes ) { |
| 227 |
my $regex = $regexes{$token}; |
| 228 |
next TOKEN_TYPE |
| 229 |
if not grep { $token eq $_ } @{$terminals_expected}; |
| 230 |
pos $test_input = $pos; |
| 231 |
next TOKEN_TYPE |
| 232 |
if not $test_input =~ m{ \G \s* (?<match>$regex) }xgms; |
| 233 |
|
| 234 |
## no critic (Variables::ProhibitPunctuationVars) |
| 235 |
$recce->alternative( $token, \$+{match}, |
| 236 |
( ( pos $test_input ) - $pos ) ); |
| 237 |
|
| 238 |
} ## end TOKEN_TYPE: for my $token ( keys %regexes ) |
| 239 |
$recce->earleme_complete(); |
| 240 |
$terminals_expected = $recce->terminals_expected(); |
| 241 |
} ## end for ( my $pos = 0; $pos < $input_length; $pos++ ) |
| 242 |
$recce->end_input(); |
| 243 |
|
| 244 |
my @parses; |
| 245 |
while ( defined( my $value_ref = $recce->value() ) ) { |
| 246 |
my $value = $value_ref ? ${$value_ref} : 'No parse'; |
| 247 |
push @parses, $value; |
| 248 |
} |
| 249 |
my $expected_parse_count = scalar @{$test_results}; |
| 250 |
my $parse_count = scalar @parses; |
| 251 |
Marpa::R2::Test::is( $parse_count, $expected_parse_count, |
| 252 |
"$test_name: Parse count" ); |
| 253 |
|
| 254 |
my $expected = join "\n", sort @{$test_results}; |
| 255 |
my $actual = join "\n", sort @parses; |
| 256 |
Marpa::R2::Test::is( $actual, $expected, "$test_name: Parse match" ); |
| 257 |
|
| 258 |
Test::More::ok( $terminals_expected_matches_events, |
| 259 |
'Output of terminals_expected() matched events()' ); |
| 260 |
|
| 261 |
} ## end TEST: for my $test_data (@test_data) |
| 262 |
|
| 263 |
sub show_perl_line { |
| 264 |
shift; |
| 265 |
return join ', ', grep {defined} @_; |
| 266 |
} |
| 267 |
|
| 268 |
sub comment { return 'comment' } |
| 269 |
sub show_statement_sequence { shift; return join q{, }, @_ } |
| 270 |
sub show_division { return 'division' } |
| 271 |
sub show_function_call { return $_[1] } |
| 272 |
sub show_die { return 'die statement' } |
| 273 |
sub show_unary { return $_[1] . ' function call' } |
| 274 |
sub show_nullary { return $_[1] . ' function call' } |
| 275 |
|
| 276 |
1; # In case used as "do" file |