| 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 |
# The low-level ASF synopses and related tests |
| 18 |
|
| 19 |
use 5.010001; |
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
|
| 23 |
use Test::More tests => 1; |
| 24 |
use English qw( -no_match_vars ); |
| 25 |
use lib 'inc'; |
| 26 |
use Marpa::R2::Test; |
| 27 |
use Marpa::R2; |
| 28 |
use Data::Dumper; |
| 29 |
use Scalar::Util; |
| 30 |
|
| 31 |
# Marpa::R2::Display |
| 32 |
# name: ASF low-level calls synopsis, code part 1 |
| 33 |
|
| 34 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 35 |
{ source => \(<<'END_OF_SOURCE'), |
| 36 |
:start ::= pair |
| 37 |
pair ::= duple | item item |
| 38 |
duple ::= item item |
| 39 |
item ::= Hesperus | Phosphorus |
| 40 |
Hesperus ::= 'a' |
| 41 |
Phosphorus ::= 'a' |
| 42 |
END_OF_SOURCE |
| 43 |
} |
| 44 |
); |
| 45 |
|
| 46 |
my $slr = Marpa::R2::Scanless::R->new( { grammar => $grammar } ); |
| 47 |
$slr->read( \'aa' ); |
| 48 |
my $asf = Marpa::R2::ASF->new( { slr => $slr } ); |
| 49 |
die 'No ASF' if not defined $asf; |
| 50 |
my $output_as_array = asf_to_basic_tree($asf); |
| 51 |
my $actual_output = array_display($output_as_array); |
| 52 |
|
| 53 |
# Marpa::R2::Display::End |
| 54 |
|
| 55 |
# Marpa::R2::Display |
| 56 |
# name: ASF low-level calls synopsis, output |
| 57 |
# start-after-line: EXPECTED_OUTPUT |
| 58 |
# end-before-line: '^EXPECTED_OUTPUT$' |
| 59 |
|
| 60 |
my $expected_output = <<'EXPECTED_OUTPUT'; |
| 61 |
Glade 2 has 2 symches |
| 62 |
Glade 2, Symch 0, pair ::= duple |
| 63 |
Glade 6, duple ::= item item |
| 64 |
Glade 8 has 2 symches |
| 65 |
Glade 8, Symch 0, item ::= Hesperus |
| 66 |
Glade 13, Hesperus ::= 'a' |
| 67 |
Glade 15, Symbol 'a': "a" |
| 68 |
Glade 8, Symch 1, item ::= Phosphorus |
| 69 |
Glade 1, Phosphorus ::= 'a' |
| 70 |
Glade 17, Symbol 'a': "a" |
| 71 |
Glade 7 has 2 symches |
| 72 |
Glade 7, Symch 0, item ::= Hesperus |
| 73 |
Glade 22, Hesperus ::= 'a' |
| 74 |
Glade 24, Symbol 'a': "a" |
| 75 |
Glade 7, Symch 1, item ::= Phosphorus |
| 76 |
Glade 9, Phosphorus ::= 'a' |
| 77 |
Glade 26, Symbol 'a': "a" |
| 78 |
Glade 2, Symch 1, pair ::= item item |
| 79 |
Glade 8 revisited |
| 80 |
Glade 7 revisited |
| 81 |
EXPECTED_OUTPUT |
| 82 |
|
| 83 |
# Marpa::R2::Display::End |
| 84 |
|
| 85 |
Marpa::R2::Test::is( $actual_output, $expected_output, |
| 86 |
'Output for basic ASF synopsis' ); |
| 87 |
|
| 88 |
# Marpa::R2::Display |
| 89 |
# name: ASF low-level calls synopsis, code part 2 |
| 90 |
|
| 91 |
sub asf_to_basic_tree { |
| 92 |
my ( $asf, $glade ) = @_; |
| 93 |
my $peak = $asf->peak(); |
| 94 |
return glade_to_basic_tree( $asf, $peak, [] ); |
| 95 |
} ## end sub asf_to_basic_tree |
| 96 |
|
| 97 |
sub glade_to_basic_tree { |
| 98 |
my ( $asf, $glade, $seen ) = @_; |
| 99 |
return bless ["Glade $glade revisited"], 'My_Revisit' |
| 100 |
if $seen->[$glade]; |
| 101 |
$seen->[$glade] = 1; |
| 102 |
my $grammar = $asf->grammar(); |
| 103 |
my @symches = (); |
| 104 |
my $symch_count = $asf->glade_symch_count($glade); |
| 105 |
SYMCH: for ( my $symch_ix = 0; $symch_ix < $symch_count; $symch_ix++ ) { |
| 106 |
my $rule_id = $asf->symch_rule_id( $glade, $symch_ix ); |
| 107 |
if ( $rule_id < 0 ) { |
| 108 |
my $literal = $asf->glade_literal($glade); |
| 109 |
my $symbol_id = $asf->glade_symbol_id($glade); |
| 110 |
my $display_form = $grammar->symbol_display_form($symbol_id); |
| 111 |
push @symches, |
| 112 |
bless [qq{Glade $glade, Symbol $display_form: "$literal"}], |
| 113 |
'My_Token'; |
| 114 |
next SYMCH; |
| 115 |
} ## end if ( $rule_id < 0 ) |
| 116 |
|
| 117 |
# ignore any truncation of the factorings |
| 118 |
my $factoring_count = |
| 119 |
$asf->symch_factoring_count( $glade, $symch_ix ); |
| 120 |
my @symch_description = ("Glade $glade"); |
| 121 |
push @symch_description, "Symch $symch_ix" if $symch_count > 1; |
| 122 |
push @symch_description, $grammar->rule_show($rule_id); |
| 123 |
my $symch_description = join q{, }, @symch_description; |
| 124 |
|
| 125 |
my @factorings = ($symch_description); |
| 126 |
for ( |
| 127 |
my $factor_ix = 0; |
| 128 |
$factor_ix < $factoring_count; |
| 129 |
$factor_ix++ |
| 130 |
) |
| 131 |
{ |
| 132 |
my $downglades = |
| 133 |
$asf->factoring_downglades( $glade, $symch_ix, |
| 134 |
$factor_ix ); |
| 135 |
push @factorings, |
| 136 |
bless [ map { glade_to_basic_tree( $asf, $_, $seen ) } |
| 137 |
@{$downglades} ], 'My_Rule'; |
| 138 |
} ## end for ( my $factor_ix = 0; $factor_ix < $factoring_count...) |
| 139 |
if ( $factoring_count > 1 ) { |
| 140 |
push @symches, |
| 141 |
bless [ |
| 142 |
"Glade $glade, symch $symch_ix has $factoring_count factorings", |
| 143 |
@factorings |
| 144 |
], |
| 145 |
'My_Factorings'; |
| 146 |
next SYMCH; |
| 147 |
} ## end if ( $factoring_count > 1 ) |
| 148 |
push @symches, bless [ @factorings[ 0, 1 ] ], 'My_Factorings'; |
| 149 |
} ## end SYMCH: for ( my $symch_ix = 0; $symch_ix < $symch_count; ...) |
| 150 |
return bless [ "Glade $glade has $symch_count symches", @symches ], |
| 151 |
'My_Symches' |
| 152 |
if $symch_count > 1; |
| 153 |
return $symches[0]; |
| 154 |
} ## end sub glade_to_basic_tree |
| 155 |
|
| 156 |
# Marpa::R2::Display::End |
| 157 |
|
| 158 |
# Marpa::R2::Display |
| 159 |
# name: ASF low-level calls synopsis, code part 3 |
| 160 |
|
| 161 |
sub array_display { |
| 162 |
my ($array) = @_; |
| 163 |
my ( undef, @lines ) = @{ array_lines_display($array) }; |
| 164 |
my $text = q{}; |
| 165 |
for my $line (@lines) { |
| 166 |
my ( $indent, $body ) = @{$line}; |
| 167 |
$indent -= 6; |
| 168 |
$text .= ( q{ } x $indent ) . $body . "\n"; |
| 169 |
} |
| 170 |
return $text; |
| 171 |
} ## end sub array_display |
| 172 |
|
| 173 |
sub array_lines_display { |
| 174 |
my ($array) = @_; |
| 175 |
my $reftype = Scalar::Util::reftype($array) // '!undef!'; |
| 176 |
return [ [ 0, $array ] ] if $reftype ne 'ARRAY'; |
| 177 |
my @lines = (); |
| 178 |
ELEMENT: for my $element ( @{$array} ) { |
| 179 |
for my $line ( @{ array_lines_display($element) } ) { |
| 180 |
my ( $indent, $body ) = @{$line}; |
| 181 |
push @lines, [ $indent + 2, $body ]; |
| 182 |
} |
| 183 |
} ## end ELEMENT: for my $element ( @{$array} ) |
| 184 |
return \@lines; |
| 185 |
} ## end sub array_lines_display |
| 186 |
|
| 187 |
# Marpa::R2::Display::End |
| 188 |
|
| 189 |
# vim: expandtab shiftwidth=4: |