| 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 |
# Tests of ranking, retro-ported from Marpa::R3 |
| 18 |
|
| 19 |
use 5.010001; |
| 20 |
|
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
|
| 24 |
use Test::More tests => 96; |
| 25 |
use Data::Dumper; |
| 26 |
use English qw( -no_match_vars ); |
| 27 |
use POSIX qw(setlocale LC_ALL); |
| 28 |
|
| 29 |
POSIX::setlocale(LC_ALL, "C"); |
| 30 |
|
| 31 |
use lib 'inc'; |
| 32 |
use Marpa::R2::Test; |
| 33 |
use Marpa::R2; |
| 34 |
|
| 35 |
my @tests_data = (); |
| 36 |
|
| 37 |
our $DEBUG = 0; |
| 38 |
|
| 39 |
# Tests of rank adverb based on examples from Lukas Atkinson |
| 40 |
# Here longest is highest rank, as in his original |
| 41 |
|
| 42 |
if (1) { |
| 43 |
|
| 44 |
my $source = <<'END_OF_SOURCE'; |
| 45 |
:discard ~ ws; ws ~ [\s]+ |
| 46 |
:default ::= action => ::array |
| 47 |
|
| 48 |
Top ::= List action => main::group |
| 49 |
List ::= Item3 rank => 3 |
| 50 |
List ::= Item2 rank => 2 |
| 51 |
List ::= Item1 rank => 1 |
| 52 |
List ::= List Item3 rank => 3 |
| 53 |
List ::= List Item2 rank => 2 |
| 54 |
List ::= List Item1 rank => 1 |
| 55 |
Item3 ::= VAR '=' VAR action => main::concat |
| 56 |
Item2 ::= VAR '=' action => main::concat |
| 57 |
Item1 ::= VAR action => main::concat |
| 58 |
VAR ~ [\w]+ |
| 59 |
|
| 60 |
END_OF_SOURCE |
| 61 |
|
| 62 |
my @tests = ( |
| 63 |
[ 'a', '(a)', ], |
| 64 |
[ 'a = b', '(a=b)', ], |
| 65 |
[ 'a = b = c', '(a=)(b=c)', ], |
| 66 |
[ 'a = b = c = d', '(a=)(b=)(c=d)', ], |
| 67 |
[ 'a = b c = d', '(a=b)(c=d)' ], |
| 68 |
[ 'a = b c = d e =', '(a=b)(c=d)(e=)' ], |
| 69 |
[ 'a = b c = d e', '(a=b)(c=d)(e)' ], |
| 70 |
[ 'a = b c = d e = f', '(a=b)(c=d)(e=f)' ], |
| 71 |
); |
| 72 |
|
| 73 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 74 |
{ source => \$source } ); |
| 75 |
for my $test (@tests) { |
| 76 |
my ( $input, $output ) = @{$test}; |
| 77 |
do_test( $grammar, $input, $output, 'Parse OK', |
| 78 |
qq{Test of rank by longest: "$input"} ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
# Tests of rank adverb based on examples from Lukas Atkinson |
| 83 |
# Here *shortest* is highest rank |
| 84 |
|
| 85 |
if (1) { |
| 86 |
|
| 87 |
# Marpa::R2::Display |
| 88 |
# name: Ranking, shortest highest, version 1 |
| 89 |
# start-after-line: END_OF_SOURCE |
| 90 |
# end-before-line: '^END_OF_SOURCE$' |
| 91 |
|
| 92 |
my $source = <<'END_OF_SOURCE'; |
| 93 |
:discard ~ ws; ws ~ [\s]+ |
| 94 |
:default ::= action => ::array |
| 95 |
|
| 96 |
Top ::= List action => main::group |
| 97 |
List ::= Item3 rank => 1 |
| 98 |
List ::= Item2 rank => 2 |
| 99 |
List ::= Item1 rank => 3 |
| 100 |
List ::= List Item3 rank => 1 |
| 101 |
List ::= List Item2 rank => 2 |
| 102 |
List ::= List Item1 rank => 3 |
| 103 |
Item3 ::= VAR '=' VAR action => main::concat |
| 104 |
Item2 ::= VAR '=' action => main::concat |
| 105 |
Item1 ::= VAR action => main::concat |
| 106 |
VAR ~ [\w]+ |
| 107 |
|
| 108 |
END_OF_SOURCE |
| 109 |
|
| 110 |
|
| 111 |
# Marpa::R2::Display |
| 112 |
# name: Ranking results, shortest highest, version 1 |
| 113 |
|
| 114 |
my @tests = ( |
| 115 |
[ 'a', '(a)', ], |
| 116 |
[ 'a = b', '(a=)(b)', ], |
| 117 |
[ 'a = b = c', '(a=)(b=)(c)', ], |
| 118 |
[ 'a = b = c = d', '(a=)(b=)(c=)(d)', ], |
| 119 |
[ 'a = b c = d', '(a=)(b)(c=)(d)' ], |
| 120 |
[ 'a = b c = d e =', '(a=)(b)(c=)(d)(e=)' ], |
| 121 |
[ 'a = b c = d e', '(a=)(b)(c=)(d)(e)' ], |
| 122 |
[ 'a = b c = d e = f', '(a=)(b)(c=)(d)(e=)(f)' ], |
| 123 |
); |
| 124 |
|
| 125 |
# Marpa::R2::Display::End |
| 126 |
|
| 127 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 128 |
{ source => \$source } ); |
| 129 |
for my $test (@tests) { |
| 130 |
my ( $input, $output ) = @{$test}; |
| 131 |
do_test( $grammar, $input, $output, 'Parse OK', |
| 132 |
qq{Test of rank by shortest: "$input"}, |
| 133 |
); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
# Tests of rank adverb based on examples from Lukas Atkinson |
| 138 |
# version 2 |
| 139 |
# Here longest is highest rank, as in his original |
| 140 |
|
| 141 |
if (1) { |
| 142 |
|
| 143 |
# Marpa::R2::Display |
| 144 |
# name: Ranking, longest highest, version 2 |
| 145 |
# start-after-line: END_OF_SOURCE |
| 146 |
# end-before-line: '^END_OF_SOURCE$' |
| 147 |
|
| 148 |
my $source = <<'END_OF_SOURCE'; |
| 149 |
:discard ~ ws; ws ~ [\s]+ |
| 150 |
:default ::= action => ::array |
| 151 |
|
| 152 |
Top ::= List action => main::group |
| 153 |
List ::= Item rank => 1 |
| 154 |
List ::= List Item rank => 0 |
| 155 |
Item ::= VAR '=' VAR rank => 3 action => main::concat |
| 156 |
Item ::= VAR '=' rank => 2 action => main::concat |
| 157 |
Item ::= VAR rank => 1 action => main::concat |
| 158 |
VAR ~ [\w]+ |
| 159 |
|
| 160 |
END_OF_SOURCE |
| 161 |
|
| 162 |
# Marpa::R2::Display::End |
| 163 |
|
| 164 |
my @tests = ( |
| 165 |
[ 'a', '(a)', ], |
| 166 |
[ 'a = b', '(a=b)', ], |
| 167 |
[ 'a = b = c', '(a=)(b=c)', ], |
| 168 |
[ 'a = b = c = d', '(a=)(b=)(c=d)', ], |
| 169 |
[ 'a = b c = d', '(a=b)(c=d)' ], |
| 170 |
[ 'a = b c = d e =', '(a=b)(c=d)(e=)' ], |
| 171 |
[ 'a = b c = d e', '(a=b)(c=d)(e)' ], |
| 172 |
[ 'a = b c = d e = f', '(a=b)(c=d)(e=f)' ], |
| 173 |
); |
| 174 |
|
| 175 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 176 |
{ source => \$source } ); |
| 177 |
for my $test (@tests) { |
| 178 |
my ( $input, $output ) = @{$test}; |
| 179 |
do_test( $grammar, $input, $output, 'Parse OK', |
| 180 |
qq{Test of rank by longest (v2): "$input"} ); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
# Tests of rank adverb based on examples from Lukas Atkinson |
| 185 |
# version 2 |
| 186 |
# Here *shortest* is highest rank |
| 187 |
|
| 188 |
if (1) { |
| 189 |
|
| 190 |
# Marpa::R2::Display |
| 191 |
# name: Ranking, shortest highest, version 2 |
| 192 |
# start-after-line: END_OF_SOURCE |
| 193 |
# end-before-line: '^END_OF_SOURCE$' |
| 194 |
|
| 195 |
my $source = <<'END_OF_SOURCE'; |
| 196 |
:discard ~ ws; ws ~ [\s]+ |
| 197 |
:default ::= action => ::array |
| 198 |
|
| 199 |
Top ::= List action => main::group |
| 200 |
List ::= Item rank => 0 |
| 201 |
List ::= List Item rank => 1 |
| 202 |
Item ::= VAR '=' VAR rank => 1 action => main::concat |
| 203 |
Item ::= VAR '=' rank => 2 action => main::concat |
| 204 |
Item ::= VAR rank => 3 action => main::concat |
| 205 |
VAR ~ [\w]+ |
| 206 |
|
| 207 |
END_OF_SOURCE |
| 208 |
|
| 209 |
# Marpa::R2::Display::End |
| 210 |
|
| 211 |
my @tests = ( |
| 212 |
[ 'a', '(a)', ], |
| 213 |
[ 'a = b', '(a=)(b)', ], |
| 214 |
[ 'a = b = c', '(a=)(b=)(c)', ], |
| 215 |
[ 'a = b = c = d', '(a=)(b=)(c=)(d)', ], |
| 216 |
[ 'a = b c = d', '(a=)(b)(c=)(d)' ], |
| 217 |
[ 'a = b c = d e =', '(a=)(b)(c=)(d)(e=)' ], |
| 218 |
[ 'a = b c = d e', '(a=)(b)(c=)(d)(e)' ], |
| 219 |
[ 'a = b c = d e = f', '(a=)(b)(c=)(d)(e=)(f)' ], |
| 220 |
); |
| 221 |
|
| 222 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 223 |
{ source => \$source } ); |
| 224 |
for my $test (@tests) { |
| 225 |
my ( $input, $output ) = @{$test}; |
| 226 |
do_test( $grammar, $input, $output, 'Parse OK', |
| 227 |
qq{Test of rank by shortest (v2): "$input"}, |
| 228 |
); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
# Tests of rank adverb based on examples from Lukas Atkinson |
| 233 |
# version 3: reimplemented via BNF |
| 234 |
# Here longest is highest rank, as in his original |
| 235 |
|
| 236 |
if (1) { |
| 237 |
|
| 238 |
# Marpa::R2::Display |
| 239 |
# name: Ranking via BNF, longest highest, version 3 |
| 240 |
# start-after-line: END_OF_SOURCE |
| 241 |
# end-before-line: '^END_OF_SOURCE$' |
| 242 |
|
| 243 |
my $source = <<'END_OF_SOURCE'; |
| 244 |
:discard ~ ws; ws ~ [\s]+ |
| 245 |
:default ::= action => ::array |
| 246 |
|
| 247 |
Top ::= Max_Boundeds action => main::group |
| 248 |
Top ::= Max_Boundeds Unbounded action => main::group |
| 249 |
Top ::= Unbounded action => main::group |
| 250 |
Max_Boundeds ::= Max_Bounded+ |
| 251 |
Max_Bounded ::= Eq_Finals Var_Final3 |
| 252 |
Max_Bounded ::= Var_Final |
| 253 |
Unbounded ::= Eq_Finals |
| 254 |
Eq_Finals ::= Eq_Final+ |
| 255 |
Var_Final ::= Var_Final3 | Var_Final1 |
| 256 |
Var_Final3 ::= VAR '=' VAR action => main::concat |
| 257 |
Eq_Final ::= VAR '=' action => main::concat |
| 258 |
Var_Final1 ::= VAR action => main::concat |
| 259 |
VAR ~ [\w]+ |
| 260 |
|
| 261 |
END_OF_SOURCE |
| 262 |
|
| 263 |
# Marpa::R2::Display::End |
| 264 |
|
| 265 |
my @tests = ( |
| 266 |
[ 'a', '(a)', ], |
| 267 |
[ 'a = b', '(a=b)', ], |
| 268 |
[ 'a = b = c', '(a=)(b=c)', ], |
| 269 |
[ 'a = b = c = d', '(a=)(b=)(c=d)', ], |
| 270 |
[ 'a = b c = d', '(a=b)(c=d)' ], |
| 271 |
[ 'a = b c = d e =', '(a=b)(c=d)(e=)' ], |
| 272 |
[ 'a = b c = d e', '(a=b)(c=d)(e)' ], |
| 273 |
[ 'a = b c = d e = f', '(a=b)(c=d)(e=f)' ], |
| 274 |
); |
| 275 |
|
| 276 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 277 |
{ source => \$source } ); |
| 278 |
for my $test (@tests) { |
| 279 |
my ( $input, $output ) = @{$test}; |
| 280 |
do_test( $grammar, $input, $output, 'Parse OK', |
| 281 |
qq{Test of rank by longest (v3): "$input"} ); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
# Tests of rank adverb based on examples from Lukas Atkinson |
| 286 |
# version 3: reimplemented via BNF |
| 287 |
# Here *shortest* is highest rank |
| 288 |
|
| 289 |
if (1) { |
| 290 |
|
| 291 |
# Marpa::R2::Display |
| 292 |
# name: Ranking via BNF, shortest highest, version 3 |
| 293 |
# start-after-line: END_OF_SOURCE |
| 294 |
# end-before-line: '^END_OF_SOURCE$' |
| 295 |
|
| 296 |
my $source = <<'END_OF_SOURCE'; |
| 297 |
:discard ~ ws; ws ~ [\s]+ |
| 298 |
:default ::= action => ::array |
| 299 |
|
| 300 |
Top ::= Max_Boundeds action => main::group |
| 301 |
Top ::= Max_Boundeds Unbounded action => main::group |
| 302 |
Top ::= Unbounded action => main::group |
| 303 |
Max_Boundeds ::= Max_Bounded+ |
| 304 |
Max_Bounded ::= Eq_Finals Var_Final |
| 305 |
Max_Bounded ::= Var_Final |
| 306 |
Unbounded ::= Eq_Finals |
| 307 |
Eq_Finals ::= Eq_Final+ |
| 308 |
Eq_Final ::= VAR '=' action => main::concat |
| 309 |
Var_Final ::= VAR action => main::concat |
| 310 |
VAR ~ [\w]+ |
| 311 |
|
| 312 |
END_OF_SOURCE |
| 313 |
|
| 314 |
# Marpa::R2::Display::End |
| 315 |
|
| 316 |
my @tests = ( |
| 317 |
[ 'a', '(a)', ], |
| 318 |
[ 'a = b', '(a=)(b)', ], |
| 319 |
[ 'a = b = c', '(a=)(b=)(c)', ], |
| 320 |
[ 'a = b = c = d', '(a=)(b=)(c=)(d)', ], |
| 321 |
[ 'a = b c = d', '(a=)(b)(c=)(d)' ], |
| 322 |
[ 'a = b c = d e =', '(a=)(b)(c=)(d)(e=)' ], |
| 323 |
[ 'a = b c = d e', '(a=)(b)(c=)(d)(e)' ], |
| 324 |
[ 'a = b c = d e = f', '(a=)(b)(c=)(d)(e=)(f)' ], |
| 325 |
); |
| 326 |
|
| 327 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 328 |
{ source => \$source } ); |
| 329 |
for my $test (@tests) { |
| 330 |
my ( $input, $output ) = @{$test}; |
| 331 |
do_test( $grammar, $input, $output, 'Parse OK', |
| 332 |
qq{Test of rank by shortest (v3): "$input"}, |
| 333 |
); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
sub do_test { |
| 338 |
my ( $grammar, $test_string, $expected_value, $expected_result, |
| 339 |
$test_name ) = @_; |
| 340 |
my ( $actual_value, $actual_result ) = |
| 341 |
my_parser( $grammar, $test_string ); |
| 342 |
Test::More::is( |
| 343 |
Data::Dumper::Dumper( \$actual_value ), |
| 344 |
Data::Dumper::Dumper( \$expected_value ), |
| 345 |
qq{Value of $test_name} |
| 346 |
); |
| 347 |
Test::More::is( $actual_result, $expected_result, |
| 348 |
qq{Result of $test_name} ); |
| 349 |
} |
| 350 |
|
| 351 |
sub my_parser { |
| 352 |
my ( $grammar, $string ) = @_; |
| 353 |
|
| 354 |
my $recce = Marpa::R2::Scanless::R->new( |
| 355 |
{ grammar => $grammar, ranking_method => 'high_rule_only' } ); |
| 356 |
|
| 357 |
if ( not defined eval { $recce->read( \$string ); 1 } ) { |
| 358 |
say $EVAL_ERROR if $DEBUG; |
| 359 |
my $abbreviated_error = $EVAL_ERROR; |
| 360 |
chomp $abbreviated_error; |
| 361 |
return 'No parse', $abbreviated_error; |
| 362 |
} ## end if ( not defined eval { $recce->read( \$string ); 1 ...}) |
| 363 |
my $value_ref = $recce->value(); |
| 364 |
if ( not defined $value_ref ) { |
| 365 |
return 'No parse', 'Input read to end but no parse'; |
| 366 |
} |
| 367 |
return [ return ${$value_ref}, 'Parse OK' ]; |
| 368 |
} ## end sub my_parser |
| 369 |
|
| 370 |
sub flatten { |
| 371 |
my ($array) = @_; |
| 372 |
return [] if not defined $array; |
| 373 |
my $ref = ref $array; |
| 374 |
return [$array] if $ref ne 'ARRAY'; |
| 375 |
my @flat = (); |
| 376 |
ELEMENT: for my $element (@{$array}) { |
| 377 |
my $ref = ref $element; |
| 378 |
if ($ref ne 'ARRAY') { |
| 379 |
push @flat, $element; |
| 380 |
next ELEMENT; |
| 381 |
} |
| 382 |
my $flat_piece = flatten($element); |
| 383 |
push @flat, @{$flat_piece}; |
| 384 |
} |
| 385 |
return \@flat; |
| 386 |
} |
| 387 |
|
| 388 |
# For use as a parse action |
| 389 |
sub concat { |
| 390 |
my ($pp, @args) = @_; |
| 391 |
# say STDERR 'concat: ', Data::Dumper::Dumper(\@args); |
| 392 |
my $flat = flatten(\@args); |
| 393 |
# say STDERR 'flat: ', Data::Dumper::Dumper($flat); |
| 394 |
return join '', @{$flat}; |
| 395 |
} |
| 396 |
|
| 397 |
# For use as a parse action |
| 398 |
sub group { |
| 399 |
my ($pp, @args) = @_; |
| 400 |
# say STDERR 'group args: ', Data::Dumper::Dumper(\@args); |
| 401 |
my $flat = flatten(\@args); |
| 402 |
# say STDERR 'flat: ', Data::Dumper::Dumper($flat); |
| 403 |
return join '', map { +'(' . $_ . ')'; } grep { defined } @{$flat}; |
| 404 |
} |
| 405 |
|
| 406 |
# vim: expandtab shiftwidth=4: |