| 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 of glade traversal from rns |
| 18 |
# Originally to report github issue #184 |
| 19 |
|
| 20 |
use 5.010001; |
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
|
| 24 |
use Test::More tests => 3; |
| 25 |
use English qw( -no_match_vars ); |
| 26 |
use lib 'inc'; |
| 27 |
use Marpa::R2::Test; |
| 28 |
use Marpa::R2; |
| 29 |
use Data::Dumper; |
| 30 |
|
| 31 |
my $g = Marpa::R2::Scanless::G->new( |
| 32 |
{ source => \(<<'END_OF_SOURCE'), |
| 33 |
|
| 34 |
:default ::= action => [ name, value] |
| 35 |
lexeme default = action => [ name, value ] latm => 1 |
| 36 |
|
| 37 |
Expr ::= |
| 38 |
Number |
| 39 |
| Expr '**' Expr |
| 40 |
| Expr '-' Expr |
| 41 |
|
| 42 |
Number ~ [\d]+ |
| 43 |
|
| 44 |
:discard ~ whitespace |
| 45 |
whitespace ~ [\s]+ |
| 46 |
|
| 47 |
END_OF_SOURCE |
| 48 |
} |
| 49 |
); |
| 50 |
|
| 51 |
my $input = <<EOI; |
| 52 |
2**7-3**10 |
| 53 |
EOI |
| 54 |
|
| 55 |
my $r = Marpa::R2::Scanless::R->new( { grammar => $g } ); |
| 56 |
$r->read( \$input ); |
| 57 |
|
| 58 |
{ |
| 59 |
my $ambiguous_status = $r->ambiguous(); |
| 60 |
my $expected = <<'EOS'; |
| 61 |
Ambiguous symch at Glade=2, Symbol=<Expr>: |
| 62 |
The ambiguity is from line 1, column 1 to line 1, column 10 |
| 63 |
Text is: 2**7-3**10 |
| 64 |
There are 2 symches |
| 65 |
Symch 0 is a rule: Expr ::= Expr '**' Expr |
| 66 |
Symch 1 is a rule: Expr ::= Expr '-' Expr |
| 67 |
EOS |
| 68 |
Marpa::R2::Test::is($ambiguous_status, $expected, 'ambiguous_status()'); |
| 69 |
Test::More::ok( ( $r->ambiguity_metric() > 1 ), 'ambiguity_metric()'); |
| 70 |
} |
| 71 |
|
| 72 |
{ |
| 73 |
$r->series_restart(); |
| 74 |
my $asf = Marpa::R2::ASF->new( { slr => $r } ); |
| 75 |
my $full_result = $asf->traverse( {}, \&full_traverser ); |
| 76 |
my $actual = join "\n", @{$full_result}, q{}; |
| 77 |
my $expected = <<'EOS'; |
| 78 |
(Expr (Expr (Expr (2)) (**) (Expr (Expr (7)) (-) (Expr (3)))) (**) (Expr (10))) |
| 79 |
(Expr (Expr (Expr (Expr (2)) (**) (Expr (7))) (-) (Expr (3))) (**) (Expr (10))) |
| 80 |
(Expr (Expr (2)) (**) (Expr (Expr (Expr (7)) (-) (Expr (3))) (**) (Expr (10)))) |
| 81 |
(Expr (Expr (2)) (**) (Expr (Expr (7)) (-) (Expr (Expr (3)) (**) (Expr (10))))) |
| 82 |
(Expr (Expr (Expr (2)) (**) (Expr (7))) (-) (Expr (Expr (3)) (**) (Expr (10)))) |
| 83 |
EOS |
| 84 |
Marpa::R2::Test::is( $actual, $expected, 'Result of ASF traversal' ); |
| 85 |
} |
| 86 |
|
| 87 |
sub full_traverser { |
| 88 |
|
| 89 |
# This routine converts the glade into a list of elements. It is called recursively. |
| 90 |
my ( $glade, $scratch ) = @_; |
| 91 |
my $rule_id = $glade->rule_id(); |
| 92 |
my $symbol_id = $glade->symbol_id(); |
| 93 |
my $symbol_name = $g->symbol_name($symbol_id); |
| 94 |
|
| 95 |
# A token is a single choice, and we know enough to return it |
| 96 |
if ( not defined $rule_id ) { |
| 97 |
my $literal = $glade->literal(); |
| 98 |
return ["($literal)"]; |
| 99 |
} |
| 100 |
|
| 101 |
# Our result will be a list of choices |
| 102 |
my @return_value = (); |
| 103 |
|
| 104 |
CHOICE: while (1) { |
| 105 |
|
| 106 |
# The results at each position are a list of choices, so |
| 107 |
# to produce a new result list, we need to take a Cartesian |
| 108 |
# product of all the choices |
| 109 |
my $length = $glade->rh_length(); |
| 110 |
my @results = ( [] ); |
| 111 |
for my $rh_ix ( 0 .. $length - 1 ) { |
| 112 |
my @new_results = (); |
| 113 |
for my $old_result (@results) { |
| 114 |
my $child_value = $glade->rh_value($rh_ix); |
| 115 |
for my $new_value ( @{$child_value} ) { |
| 116 |
push @new_results, [ @{$old_result}, $new_value ]; |
| 117 |
} |
| 118 |
} ## end for my $old_result (@results) |
| 119 |
@results = @new_results; |
| 120 |
} ## end for my $rh_ix ( 0 .. $length - 1 ) |
| 121 |
|
| 122 |
# Special case for the start rule |
| 123 |
if ( $symbol_name eq '[:start]' ) { |
| 124 |
return [ map { join q{}, @{$_} } @results ]; |
| 125 |
} |
| 126 |
|
| 127 |
# Now we have a list of choices, as a list of lists. Each sub list |
| 128 |
# is a list of elements, which we need to join into |
| 129 |
# a single element. The result will be to collapse |
| 130 |
# one level of lists, and leave us with a list of |
| 131 |
# elements |
| 132 |
my $join_ws = q{ }; |
| 133 |
$join_ws = qq{\n } if $symbol_name eq 'S'; |
| 134 |
push @return_value, |
| 135 |
map { '(' . $symbol_name . q{ } . ( join $join_ws, @{$_} ) . ')' } |
| 136 |
@results; |
| 137 |
|
| 138 |
# Look at the next alternative in this glade, or end the |
| 139 |
# loop if there is none |
| 140 |
last CHOICE if not defined $glade->next(); |
| 141 |
|
| 142 |
} ## end CHOICE: while (1) |
| 143 |
|
| 144 |
# Return the list of elements for this glade |
| 145 |
return \@return_value; |
| 146 |
} ## end sub full_traverser |
| 147 |
|
| 148 |
# vim: expandtab shiftwidth=4: |