| 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 negative ranks, SLIF ranks and |
| 18 |
# external SLIF scanning |
| 19 |
|
| 20 |
# This uses an ambiguous grammar to implement a binary |
| 21 |
# counter. A very expensive way to do it, but a |
| 22 |
# good test of the ranking logic. |
| 23 |
|
| 24 |
use 5.010001; |
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
use Test::More tests => 16; |
| 29 |
use English qw( -no_match_vars ); |
| 30 |
use lib 'inc'; |
| 31 |
use Marpa::R2::Test; |
| 32 |
use Marpa::R2; |
| 33 |
|
| 34 |
sub My_Actions::zero { return '0' } |
| 35 |
sub My_Actions::one { return '1' } |
| 36 |
|
| 37 |
sub My_Actions::start_rule_action { |
| 38 |
shift; |
| 39 |
return join q{}, @_; |
| 40 |
} |
| 41 |
|
| 42 |
## use critic |
| 43 |
|
| 44 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 45 |
{ |
| 46 |
source => \(<<'END_OF_GRAMMAR'), |
| 47 |
:start ::= S |
| 48 |
S ::= digit digit digit digit action => start_rule_action |
| 49 |
digit ::= |
| 50 |
zero rank => 1 action => zero |
| 51 |
| one rank => -1 action => one |
| 52 |
zero ~ 't' |
| 53 |
one ~ 't' |
| 54 |
END_OF_GRAMMAR |
| 55 |
} |
| 56 |
); |
| 57 |
|
| 58 |
my @counting_up = |
| 59 |
qw{ 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 }; |
| 60 |
|
| 61 |
my $recce = Marpa::R2::Scanless::R->new( |
| 62 |
{ grammar => $grammar, |
| 63 |
semantics_package => 'My_Actions', |
| 64 |
ranking_method => 'rule' |
| 65 |
} |
| 66 |
); |
| 67 |
|
| 68 |
$recce->read(\'tttt'); |
| 69 |
|
| 70 |
my $i = 0; |
| 71 |
while ( my $result = $recce->value() ) { |
| 72 |
my $got = ${$result}; |
| 73 |
my $expected = reverse $counting_up[$i]; |
| 74 |
Test::More::is( $got, $expected, "counting up $i" ); |
| 75 |
$i++; |
| 76 |
} ## end while ( my $result = $recce->value() ) |
| 77 |
|
| 78 |
1; # In case used as "do" file |
| 79 |
|
| 80 |
# vim: expandtab shiftwidth=4: |