Debian Codegraph @

t/rankfact.t

raw
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 ranking in the presence of both a factoring and
18 # a symbolic choice.
19
20 use 5.010001;
21
22 use strict;
23 use warnings;
24
25 use Test::More tests => 2;
26 use Data::Dumper;
27 use English qw( -no_match_vars );
28 use POSIX qw(setlocale LC_ALL);
29
30 POSIX::setlocale( LC_ALL, "C" );
31
32 use lib 'inc';
33 use Marpa::R2::Test;
34 use Marpa::R2;
35
36 my @tests = (
37 [ 'aaa', 1, 2, 'R2 R2 R1 R1' ],
38 [ 'aaa', 2, 1, 'R1 R1 R2 R2' ],
39 );
40
41 for my $test (@tests) {
42 my ( $input, $rank1, $rank2, $output ) = @{$test};
43
44 my $source = <<"END_OF_SOURCE";
45 :default ::= action => main::dwim
46
47 S ::= (L) R
48 L ::= A
49 L ::= A A
50 R ::= R1 action => main::doR1 rank => $rank1
51 R ::= R2 action => main::doR2 rank => $rank2
52 R1 ::= A
53 R1 ::= A A
54 R2 ::= A
55 R2 ::= A A
56 A ::= 'a'
57
58 END_OF_SOURCE
59
60 my $grammar = Marpa::R2::Scanless::G->new( { source => \$source } );
61
62 my $recce = Marpa::R2::Scanless::R->new(
63 {
64 grammar => $grammar,
65 ranking_method => 'rule'
66 }
67 );
68 $recce->read( \$input );
69 my @results = ();
70 VALUE: for (;;) {
71 my $value_ref = $recce->value();
72 last VALUE if not defined $value_ref;
73 my $value = pop @{$$value_ref};
74 push @results, $value;
75 }
76 my $got = join " ", @results;
77 Test::More::is( $got, $output,
78 sprintf( q{Ranking synopsis test R1=%d R2=%d: "%s"-> "%s"}, $rank1, $rank2, $input, $got ) );
79 # local $Data::Dumper::Deepcopy = 1;
80 # local $Data::Dumper::Terse = 1;
81 # say STDERR Data::Dumper::Dumper(\@results);
82 }
83
84 sub main::dwim {
85 my @result = ();
86 shift;
87 ARG: for my $v ( @_ ) {
88 next ARG if not $v;
89 my $type = Scalar::Util::reftype $v;
90 if (not $type or $type ne 'ARRAY') {
91 push @result, $v;
92 next ARG;
93 }
94 my $size = scalar @{$v};
95 next ARG if $size == 0;
96 if ($size == 1) {
97 push @result, ${$v}[0];
98 next ARG;
99 }
100 push @result, $v;
101 }
102 return [@result];
103 }
104
105 sub main::doR1 { return 'R1'; }
106 sub main::doR2 { return 'R2'; }
107
108 # vim: expandtab shiftwidth=4: