| 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 null ranking |
| 18 |
|
| 19 |
use 5.010001; |
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
|
| 23 |
use Test::More tests => 10; |
| 24 |
use lib 'inc'; |
| 25 |
use Marpa::R2::Test; |
| 26 |
use Marpa::R2; |
| 27 |
|
| 28 |
## no critic (Subroutines::RequireArgUnpacking) |
| 29 |
|
| 30 |
sub default_action { |
| 31 |
shift; |
| 32 |
my $v_count = scalar @_; |
| 33 |
return q{} if $v_count <= 0; |
| 34 |
return $_[0] if $v_count == 1; |
| 35 |
return '(' . join( q{;}, @_ ) . ')'; |
| 36 |
} ## end sub default_action |
| 37 |
|
| 38 |
## use critic |
| 39 |
|
| 40 |
# Marpa::R2::Display |
| 41 |
# name: null-ranking adverb example |
| 42 |
|
| 43 |
my $high_dsl = <<'END_OF_DSL'; |
| 44 |
:default ::= action => main::default_action |
| 45 |
:start ::= S |
| 46 |
A ::= 'a' |
| 47 |
A ::= empty |
| 48 |
empty ::= |
| 49 |
S ::= A A A A null-ranking => high |
| 50 |
END_OF_DSL |
| 51 |
|
| 52 |
# Marpa::R2::Display::End |
| 53 |
|
| 54 |
my $low_dsl = $high_dsl; |
| 55 |
$low_dsl =~ s/\s+ [=][>] \s+ high \Z/ => low/xms; |
| 56 |
my %dsl = ( high => \$high_dsl, low => \$low_dsl ); |
| 57 |
|
| 58 |
my @maximal = ( q{}, qw[(a;;;) (a;a;;) (a;a;a;) (a;a;a;a)] ); |
| 59 |
my @minimal = ( q{}, qw[(;;;a) (;;a;a) (;a;a;a) (a;a;a;a)] ); |
| 60 |
|
| 61 |
for my $maximal ( 0, 1 ) { |
| 62 |
my $dsl = $dsl{ $maximal ? 'low' : 'high' }; |
| 63 |
my $grammar = Marpa::R2::Scanless::G->new( { source => $dsl } ); |
| 64 |
my $recce = Marpa::R2::Scanless::R->new( |
| 65 |
{ grammar => $grammar, |
| 66 |
ranking_method => 'high_rule_only' |
| 67 |
} |
| 68 |
); |
| 69 |
|
| 70 |
my $input_length = 4; |
| 71 |
my $input = 'a' x $input_length; |
| 72 |
$recce->read( \$input ); |
| 73 |
|
| 74 |
for my $i ( 0 .. $input_length ) { |
| 75 |
my $expected = $maximal ? \@maximal : \@minimal; |
| 76 |
my $name = $maximal ? 'maximal' : 'minimal'; |
| 77 |
|
| 78 |
# Marpa::R2::Display |
| 79 |
# name: SLIF recognizer series_restart() synopsis |
| 80 |
|
| 81 |
$recce->series_restart( { end => $i } ); |
| 82 |
|
| 83 |
# Marpa::R2::Display::End |
| 84 |
|
| 85 |
# Marpa::R2::Display |
| 86 |
# name: SLIF recognizer set() synopsis |
| 87 |
|
| 88 |
$recce->set( { max_parses => 42 } ); |
| 89 |
|
| 90 |
# Marpa::R2::Display::End |
| 91 |
|
| 92 |
my $result = $recce->value(); |
| 93 |
die "No parse" if not defined $result; |
| 94 |
Test::More::is( ${$result}, $expected->[$i], |
| 95 |
"$name parse, length=$i" ); |
| 96 |
|
| 97 |
} ## end for my $i ( 0 .. $input_length ) |
| 98 |
} ## end for my $maximal ( 0, 1 ) |
| 99 |
|
| 100 |
1; # In case used as "do" file |
| 101 |
|
| 102 |
# Local Variables: |
| 103 |
# mode: cperl |
| 104 |
# cperl-indent-level: 4 |
| 105 |
# fill-column: 100 |
| 106 |
# End: |
| 107 |
# vim: expandtab shiftwidth=4: |