Debian Codegraph @

t/50d-ranksyn.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 use 5.010001;
18
19 use strict;
20 use warnings;
21
22 use Test::More tests => 8;
23 use Data::Dumper;
24 use English qw( -no_match_vars );
25 use POSIX qw(setlocale LC_ALL);
26
27 POSIX::setlocale( LC_ALL, "C" );
28
29 use lib 'inc';
30 use Marpa::R2::Test;
31 use Marpa::R2;
32
33 my @tests_data = ();
34 my @results = ();
35
36 # Marpa::R2::Display
37 # name: Rank document synopsis
38
39 my $source = <<'END_OF_SOURCE';
40 :discard ~ ws; ws ~ [\s]+
41 :default ::= action => ::array
42
43 Top ::= List action => main::group
44 List ::= Item3 rank => 3
45 List ::= Item2 rank => 2
46 List ::= Item1 rank => 1
47 List ::= List Item3 rank => 3
48 List ::= List Item2 rank => 2
49 List ::= List Item1 rank => 1
50 Item3 ::= VAR '=' VAR action => main::concat
51 Item2 ::= VAR '=' action => main::concat
52 Item1 ::= VAR action => main::concat
53 VAR ~ [\w]+
54
55 END_OF_SOURCE
56
57 my @tests = (
58 [ 'a', '(a)', ],
59 [ 'a = b', '(a=b)', ],
60 [ 'a = b = c', '(a=)(b=c)', ],
61 [ 'a = b = c = d', '(a=)(b=)(c=d)', ],
62 [ 'a = b c = d', '(a=b)(c=d)' ],
63 [ 'a = b c = d e =', '(a=b)(c=d)(e=)' ],
64 [ 'a = b c = d e', '(a=b)(c=d)(e)' ],
65 [ 'a = b c = d e = f', '(a=b)(c=d)(e=f)' ],
66 );
67
68 my $grammar = Marpa::R2::Scanless::G->new( { source => \$source } );
69
70 for my $test (@tests) {
71 my ( $input, $output ) = @{$test};
72 my $recce = Marpa::R2::Scanless::R->new(
73 {
74 grammar => $grammar,
75 ranking_method => 'high_rule_only'
76 }
77 );
78 $recce->read( \$input );
79 my $value_ref = $recce->value();
80 if ( not defined $value_ref ) {
81 die 'No parse';
82 }
83 push @results, ${$value_ref};
84 }
85
86 # Marpa::R2::Display::End
87
88 for my $ix ( 0 .. $#tests ) {
89 my ( $input, $output ) = @{$tests[$ix]};
90 my $result = $results[$ix];
91 Test::More::is( $result, $output,
92 sprintf( 'Ranking synopsis test #%d: "%s"', $ix, $input ) );
93 }
94
95 # Marpa::R2::Display
96 # name: rank example semantics
97
98 sub flatten {
99 my ($array) = @_;
100
101 # say STDERR 'flatten arg: ', Data::Dumper::Dumper($array);
102 my $ref = ref $array;
103 return [$array] if $ref ne 'ARRAY';
104 my @flat = ();
105 ELEMENT: for my $element ( @{$array} ) {
106 my $ref = ref $element;
107 if ( $ref ne 'ARRAY' ) {
108 push @flat, $element;
109 next ELEMENT;
110 }
111 my $flat_piece = flatten($element);
112 push @flat, @{$flat_piece};
113 }
114 return \@flat;
115 }
116
117 sub concat {
118 my ( $pp, @args ) = @_;
119
120 # say STDERR 'concat: ', Data::Dumper::Dumper(\@args);
121 my $flat = flatten( \@args );
122 return join '', @{$flat};
123 }
124
125 sub group {
126 my ( $pp, @args ) = @_;
127
128 # say STDERR 'comma_sep args: ', Data::Dumper::Dumper(\@args);
129 my $flat = flatten( \@args );
130 return join '', map { +'(' . $_ . ')'; } @{$flat};
131 }
132
133 # Marpa::R2::Display::End
134
135 # vim: expandtab shiftwidth=4: