Debian Codegraph @

t/rankord.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 order of ranking -- should be depth-first left-to-right
18
19 use 5.010001;
20
21 use strict;
22 use warnings;
23
24 use Test::More tests => 2;
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 $input = 'aaaa';
36
37 my $source = <<"END_OF_SOURCE";
38 :default ::= action => main::dwim
39
40 S ::= A A action => main::flatten
41 A ::= A1 rank => 2
42 A ::= A2 rank => 1
43 A1 ::= B B action => main::dwimB
44 A2 ::= B B action => main::dwimB
45 B ::= B1 rank => 2
46 B ::= B2 rank => 1
47 B1 ::= ('a') action => main::dwimB
48 B2 ::= ('a') action => main::dwimB
49
50 END_OF_SOURCE
51
52 my $grammar = Marpa::R2::Scanless::G->new( { source => \$source } );
53
54 my $recce = Marpa::R2::Scanless::R->new(
55 {
56 grammar => $grammar,
57 ranking_method => 'rule'
58 }
59 );
60 $recce->read( \$input );
61 my @rawGot = ();
62 VALUE: for ( ; ; ) {
63 my $value_ref = $recce->value();
64 last VALUE if not defined $value_ref;
65 push @rawGot, $$value_ref;
66 # local $Data::Dumper::Deepcopy = 1;
67 # local $Data::Dumper::Terse = 1;
68 # say STDERR Data::Dumper::Dumper($value_ref);
69 }
70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1 B1 B2 A2
123 B2 B2 A1 B1 B2 A2
124 B1 B1 A2 B1 B2 A2
125 B2 B1 A2 B1 B2 A2
126 B1 B2 A2 B1 B2 A2
127 B2 B2 A2 B1 B2 A2
128 B1 B1 A1 B2 B2 A2
129 B2 B1 A1 B2 B2 A2
130 B1 B2 A1 B2 B2 A2
131 B2 B2 A1 B2 B2 A2
132 B1 B1 A2 B2 B2 A2
133 B2 B1 A2 B2 B2 A2
134 B1 B2 A2 B2 B2 A2
135 B2 B2 A2 B2 B2 A2
136 EOS
137
138 my @got = ();
139 my @revGot = ();
140 for my $gotArr (@rawGot) {
141 my $gotLine = join " ", @{$gotArr};
142 my $revGotLine = join " ", reverse @{$gotArr};
143 push @got, $gotLine;
144 push @revGot, $revGotLine;
145 }
146 my $got = join "\n", @got, '';
147 my @sortedGot = sort { $a cmp $b } @revGot;
148 Test::More::is( $got, $output, 'Ranking order vs expected');
149 Test::More::is_deeply( \@revGot, \@sortedGot, 'Ranking order vs sort');
150
151 sub main::dwim {
152 my @result = ();
153 shift;
154 ARG: for my $v (@_) {
155 next ARG if not $v;
156 my $type = Scalar::Util::reftype $v;
157 if ( not $type or $type ne 'ARRAY' ) {
158 push @result, $v;
159 next ARG;
160 }
161 my $size = scalar @{$v};
162 next ARG if $size == 0;
163 if ( $size == 1 ) {
164 push @result, ${$v}[0];
165 next ARG;
166 }
167 push @result, $v;
168 }
169 return [@result];
170 }
171
172 sub main::dwimB {
173 my @result = ();
174 shift;
175 ARG: for my $v (@_) {
176 next ARG if not $v;
177 my $type = Scalar::Util::reftype $v;
178 if ( not $type or $type ne 'ARRAY' ) {
179 push @result, $v;
180 next ARG;
181 }
182 my $size = scalar @{$v};
183 next ARG if $size == 0;
184 if ( $size == 1 ) {
185 push @result, ${$v}[0];
186 next ARG;
187 }
188 push @result, $v;
189 }
190 my $rule_id = $Marpa::R2::Context::rule;
191 my $grammar = $Marpa::R2::Context::grammar;
192 my ($lhs) = $grammar->rule($rule_id);
193 return [ @result, $lhs ];
194 }
195
196 sub flatten {
197 my ( $parseValue, @values ) = @_;
198 my $arrRef = flattenArrayRef( \@values );
199 return $arrRef;
200 }
201
202 sub flattenArrayRef {
203 my ($array) = @_;
204 return [] if not defined $array;
205 my $ref = ref $array;
206 return [$array] if $ref ne 'ARRAY';
207 my @flat = ();
208 ELEMENT: for my $element ( @{$array} ) {
209 my $ref = ref $element;
210 if ( $ref ne 'ARRAY' ) {
211 push @flat, $element;
212 next ELEMENT;
213 }
214 my $flat_piece = flattenArrayRef($element);
215 push @flat, @{$flat_piece};
216 }
217 return \@flat;
218 }
219
220 # vim: expandtab shiftwidth=4: