| 1 |
# Copyright 2022 Jeffrey Kegler |
| 2 |
# This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 3 |
# redistribute it and/or modify it under the terms of the GNU Lesser |
| 4 |
# General Public License as published by the Free Software Foundation, |
| 5 |
# either version 3 of the License, or (at your option) any later version. |
| 6 |
# |
| 7 |
# Marpa::R2 is distributed in the hope that it will be useful, |
| 8 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 10 |
# Lesser General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU Lesser |
| 13 |
# General Public License along with Marpa::R2. If not, see |
| 14 |
# http://www.gnu.org/licenses/. |
| 15 |
|
| 16 |
package Marpa::R2::ASF; |
| 17 |
|
| 18 |
use 5.010001; |
| 19 |
use strict; |
| 20 |
use warnings; |
| 21 |
no warnings qw(recursion); |
| 22 |
|
| 23 |
use vars qw($VERSION $STRING_VERSION); |
| 24 |
$VERSION = '14.000000'; |
| 25 |
$STRING_VERSION = $VERSION; |
| 26 |
## no critic(BuiltinFunctions::ProhibitStringyEval) |
| 27 |
$VERSION = eval $VERSION; |
| 28 |
## use critic |
| 29 |
|
| 30 |
# The code in this file, for now, breaks "the rules". It makes use |
| 31 |
# of internal methods not documented as part of Libmarpa. |
| 32 |
# It is intended to create documented Libmarpa methods to underlie |
| 33 |
# this interface, and rewrite it to use them |
| 34 |
|
| 35 |
package Marpa::R2::Internal::ASF; |
| 36 |
|
| 37 |
# This is more complicated that it needs to be for the current implementation. |
| 38 |
# It allows for LHS terminals (implemented in Libmarpa but not allowed by the SLIF). |
| 39 |
# It also assumes that every or-node which can be constructed from preceding or-nodes |
| 40 |
# and the input will be present. This is currently the case, but in the future |
| 41 |
# rules and/or symbols may have extra-syntactic conditions attached making this |
| 42 |
# assumption false. |
| 43 |
|
| 44 |
# Terms: |
| 45 |
|
| 46 |
# NID (Node ID): Encoded ID of either an or-node or an and-node. |
| 47 |
# |
| 48 |
# Extensions: |
| 49 |
# Set "powers": A set of power 0 is an "atom" -- a single NID. |
| 50 |
# A set of power 1 is a set of NID's -- a nidset. |
| 51 |
# A set of power 2 is a set of sets of NID's, also called a powerset. |
| 52 |
# A set of power 3 is a set of powersets, etc. |
| 53 |
# |
| 54 |
# The whole ID of NID is the external rule id of an or-node, or -1 |
| 55 |
# if the NID is for a token and-node. |
| 56 |
# |
| 57 |
# Intensions: |
| 58 |
# A Symch is a nidset, where all the NID's share the same "whole ID" |
| 59 |
# and the same span. NID's in a symch may differ in their internal rule, |
| 60 |
# or have different causes. If the symch contains and-node NID's they |
| 61 |
# will all have the same symbol. |
| 62 |
# |
| 63 |
# A choicepoint is a powerset -- a set of symches all of which share |
| 64 |
# the same set of predecessors. (This set of predecessors is a power 3 set of |
| 65 |
# choicepoints.) All symches in a choicepoint also share the same span, |
| 66 |
# and the same symch-symbol. A symch's symbol is the LHS of the rule, |
| 67 |
# or the symbol of the token in the token and-nodes. |
| 68 |
|
| 69 |
sub intset_id { |
| 70 |
my ( $asf, @ids ) = @_; |
| 71 |
my $key = join q{ }, sort { $a <=> $b } @ids; |
| 72 |
my $intset_by_key = $asf->[Marpa::R2::Internal::ASF::INTSET_BY_KEY]; |
| 73 |
my $intset_id = $intset_by_key->{$key}; |
| 74 |
return $intset_id if defined $intset_id; |
| 75 |
$intset_id = $asf->[Marpa::R2::Internal::ASF::NEXT_INTSET_ID]++; |
| 76 |
$intset_by_key->{$key} = $intset_id; |
| 77 |
return $intset_id; |
| 78 |
} ## end sub intset_id |
| 79 |
|
| 80 |
sub Marpa::R2::Nidset::obtain { |
| 81 |
my ( $class, $asf, @nids ) = @_; |
| 82 |
my $id = intset_id( $asf, @nids ); |
| 83 |
my $nidset_by_id = $asf->[Marpa::R2::Internal::ASF::NIDSET_BY_ID]; |
| 84 |
my $nidset = $nidset_by_id->[$id]; |
| 85 |
return $nidset if defined $nidset; |
| 86 |
$nidset = bless [], $class; |
| 87 |
$nidset->[Marpa::R2::Internal::Nidset::ID] = $id; |
| 88 |
$nidset->[Marpa::R2::Internal::Nidset::NIDS] = |
| 89 |
[ sort { $a <=> $b } @nids ]; |
| 90 |
$nidset_by_id->[$id] = $nidset; |
| 91 |
return $nidset; |
| 92 |
} ## end sub Marpa::R2::Nidset::obtain |
| 93 |
|
| 94 |
sub Marpa::R2::Nidset::nids { |
| 95 |
my ($nidset) = @_; |
| 96 |
return $nidset->[Marpa::R2::Internal::Nidset::NIDS]; |
| 97 |
} |
| 98 |
|
| 99 |
sub Marpa::R2::Nidset::nid { |
| 100 |
my ( $nidset, $ix ) = @_; |
| 101 |
return $nidset->[Marpa::R2::Internal::Nidset::NIDS]->[$ix]; |
| 102 |
} |
| 103 |
|
| 104 |
sub Marpa::R2::Nidset::count { |
| 105 |
my ($nidset) = @_; |
| 106 |
return scalar @{ $nidset->[Marpa::R2::Internal::Nidset::NIDS] }; |
| 107 |
} |
| 108 |
|
| 109 |
sub Marpa::R2::Nidset::id { |
| 110 |
my ($nidset) = @_; |
| 111 |
return $nidset->[Marpa::R2::Internal::Nidset::ID]; |
| 112 |
} |
| 113 |
|
| 114 |
sub Marpa::R2::Nidset::show { |
| 115 |
my ($nidset) = @_; |
| 116 |
my $id = $nidset->id(); |
| 117 |
my $nids = $nidset->nids(); |
| 118 |
return "Nidset #$id: " . join q{ }, @{$nids}; |
| 119 |
} ## end sub Marpa::R2::Nidset::show |
| 120 |
|
| 121 |
sub Marpa::R2::Powerset::obtain { |
| 122 |
my ( $class, $asf, @nidset_ids ) = @_; |
| 123 |
my $id = intset_id( $asf, @nidset_ids ); |
| 124 |
my $powerset_by_id = $asf->[Marpa::R2::Internal::ASF::POWERSET_BY_ID]; |
| 125 |
my $powerset = $powerset_by_id->[$id]; |
| 126 |
return $powerset if defined $powerset; |
| 127 |
$powerset = bless [], $class; |
| 128 |
$powerset->[Marpa::R2::Internal::Powerset::ID] = $id; |
| 129 |
$powerset->[Marpa::R2::Internal::Powerset::NIDSET_IDS] = |
| 130 |
[ sort { $a <=> $b } @nidset_ids ]; |
| 131 |
$powerset_by_id->[$id] = $powerset; |
| 132 |
return $powerset; |
| 133 |
} ## end sub Marpa::R2::Powerset::obtain |
| 134 |
|
| 135 |
sub Marpa::R2::Powerset::nidset_ids { |
| 136 |
my ($powerset) = @_; |
| 137 |
return $powerset->[Marpa::R2::Internal::Powerset::NIDSET_IDS]; |
| 138 |
} |
| 139 |
|
| 140 |
sub Marpa::R2::Powerset::count { |
| 141 |
my ($powerset) = @_; |
| 142 |
return scalar @{ $powerset->[Marpa::R2::Internal::Powerset::NIDSET_IDS] }; |
| 143 |
} |
| 144 |
|
| 145 |
sub Marpa::R2::Powerset::nidset_id { |
| 146 |
my ( $powerset, $ix ) = @_; |
| 147 |
my $nidset_ids = $powerset->[Marpa::R2::Internal::Powerset::NIDSET_IDS]; |
| 148 |
return if $ix > $#{$nidset_ids}; |
| 149 |
return $powerset->[Marpa::R2::Internal::Powerset::NIDSET_IDS]->[$ix]; |
| 150 |
} ## end sub Marpa::R2::Powerset::nidset_id |
| 151 |
|
| 152 |
sub Marpa::R2::Powerset::nidset { |
| 153 |
my ( $powerset, $asf, $ix ) = @_; |
| 154 |
my $nidset_ids = $powerset->[Marpa::R2::Internal::Powerset::NIDSET_IDS]; |
| 155 |
return if $ix > $#{$nidset_ids}; |
| 156 |
my $nidset_id = $powerset->[Marpa::R2::Internal::Powerset::NIDSET_IDS]->[$ix]; |
| 157 |
my $nidset_by_id = $asf->[Marpa::R2::Internal::ASF::NIDSET_BY_ID]; |
| 158 |
return $nidset_by_id->[$nidset_id]; |
| 159 |
} ## end sub Marpa::R2::Powerset::nidset_id |
| 160 |
|
| 161 |
sub Marpa::R2::Powerset::id { |
| 162 |
my ($powerset) = @_; |
| 163 |
return $powerset->[Marpa::R2::Internal::Powerset::ID]; |
| 164 |
} |
| 165 |
|
| 166 |
sub Marpa::R2::Powerset::show { |
| 167 |
my ($powerset) = @_; |
| 168 |
my $id = $powerset->id(); |
| 169 |
my $nidset_ids = $powerset->nidset_ids(); |
| 170 |
return "Powerset #$id: " . join q{ }, @{$nidset_ids}; |
| 171 |
} ## end sub Marpa::R2::Powerset::show |
| 172 |
|
| 173 |
sub set_last_choice { |
| 174 |
my ( $asf, $nook ) = @_; |
| 175 |
my $or_nodes = $asf->[Marpa::R2::Internal::ASF::OR_NODES]; |
| 176 |
my $or_node_id = $nook->[Marpa::R2::Internal::Nook::OR_NODE]; |
| 177 |
my $and_nodes = $or_nodes->[$or_node_id]; |
| 178 |
my $choice = $nook->[Marpa::R2::Internal::Nook::FIRST_CHOICE]; |
| 179 |
return if $choice > $#{$and_nodes}; |
| 180 |
if ( nook_has_semantic_cause( $asf, $nook ) ) { |
| 181 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 182 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 183 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 184 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 185 |
my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 186 |
my $and_node_id = $and_nodes->[$choice]; |
| 187 |
my $current_predecessor = |
| 188 |
$bocage->_marpa_b_and_node_predecessor($and_node_id) // -1; |
| 189 |
AND_NODE: while (1) { |
| 190 |
$choice++; |
| 191 |
$and_node_id = $and_nodes->[$choice]; |
| 192 |
last AND_NODE if not defined $and_node_id; |
| 193 |
my $next_predecessor = |
| 194 |
$bocage->_marpa_b_and_node_predecessor($and_node_id) // -1; |
| 195 |
last AND_NODE if $current_predecessor != $next_predecessor; |
| 196 |
} ## end AND_NODE: while (1) |
| 197 |
$choice--; |
| 198 |
} ## end if ( nook_has_semantic_cause( $asf, $nook ) ) |
| 199 |
$nook->[Marpa::R2::Internal::Nook::LAST_CHOICE] = $choice; |
| 200 |
return $choice; |
| 201 |
} ## end sub set_last_choice |
| 202 |
|
| 203 |
sub nook_new { |
| 204 |
my ( $asf, $or_node_id, $parent_or_node_id ) = @_; |
| 205 |
my $nook = []; |
| 206 |
$nook->[Marpa::R2::Internal::Nook::OR_NODE] = $or_node_id; |
| 207 |
$nook->[Marpa::R2::Internal::Nook::PARENT] = $parent_or_node_id // -1; |
| 208 |
$nook->[Marpa::R2::Internal::Nook::FIRST_CHOICE] = 0; |
| 209 |
set_last_choice( $asf, $nook ); |
| 210 |
return $nook; |
| 211 |
} ## end sub nook_new |
| 212 |
|
| 213 |
sub nook_increment { |
| 214 |
my ( $asf, $nook ) = @_; |
| 215 |
$nook->[Marpa::R2::Internal::Nook::LAST_CHOICE] //= 0; |
| 216 |
$nook->[Marpa::R2::Internal::Nook::FIRST_CHOICE] = |
| 217 |
$nook->[Marpa::R2::Internal::Nook::LAST_CHOICE] + 1; |
| 218 |
return if not defined set_last_choice( $asf, $nook ); |
| 219 |
return 1; |
| 220 |
} ## end sub nook_increment |
| 221 |
|
| 222 |
sub nook_has_semantic_cause { |
| 223 |
my ( $asf, $nook ) = @_; |
| 224 |
my $or_node = $nook->[Marpa::R2::Internal::Nook::OR_NODE]; |
| 225 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 226 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 227 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 228 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 229 |
my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 230 |
|
| 231 |
my $irl_id = $bocage->_marpa_b_or_node_irl($or_node); |
| 232 |
my $predot_position = $bocage->_marpa_b_or_node_position($or_node) - 1; |
| 233 |
my $predot_isyid = |
| 234 |
$grammar_c->_marpa_g_irl_rhs( $irl_id, $predot_position ); |
| 235 |
return $grammar_c->_marpa_g_nsy_is_semantic($predot_isyid); |
| 236 |
} ## end sub nook_has_semantic_cause |
| 237 |
|
| 238 |
# No check for conflicting usage -- value(), asf(), etc. |
| 239 |
# at this point |
| 240 |
sub Marpa::R2::ASF::peak { |
| 241 |
my ($asf) = @_; |
| 242 |
my $or_nodes = $asf->[Marpa::R2::Internal::ASF::OR_NODES]; |
| 243 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 244 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 245 |
|
| 246 |
my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 247 |
die 'No Bocage' if not $bocage; |
| 248 |
my $augment_or_node_id = $bocage->_marpa_b_top_or_node(); |
| 249 |
my $augment_and_node_id = $or_nodes->[$augment_or_node_id]->[0]; |
| 250 |
my $start_or_node_id = |
| 251 |
$bocage->_marpa_b_and_node_cause($augment_and_node_id); |
| 252 |
|
| 253 |
my $base_nidset = Marpa::R2::Nidset->obtain( $asf, $start_or_node_id ); |
| 254 |
my $glade_id = $base_nidset->id(); |
| 255 |
|
| 256 |
# Cannot "obtain" the glade if it is not registered |
| 257 |
$asf->[Marpa::R2::Internal::ASF::GLADES]->[$glade_id] |
| 258 |
->[Marpa::R2::Internal::Glade::REGISTERED] = 1; |
| 259 |
glade_obtain( $asf, $glade_id ); |
| 260 |
return $glade_id; |
| 261 |
} ## end sub Marpa::R2::ASF::peak |
| 262 |
|
| 263 |
our $NID_LEAF_BASE = -43; |
| 264 |
|
| 265 |
# Range from -1 to -42 reserved for special values |
| 266 |
sub and_node_to_nid { return -$_[0] + $NID_LEAF_BASE; } |
| 267 |
sub nid_to_and_node { return -$_[0] + $NID_LEAF_BASE; } |
| 268 |
|
| 269 |
sub normalize_asf_blessing { |
| 270 |
my ($name) = @_; |
| 271 |
$name =~ s/\A \s * //xms; |
| 272 |
$name =~ s/ \s * \z//xms; |
| 273 |
$name =~ s/ \s+ / /gxms; |
| 274 |
$name =~ s/ \W /_/gxms; |
| 275 |
return $name; |
| 276 |
} ## end sub normalize_asf_blessing |
| 277 |
|
| 278 |
sub Marpa::R2::Internal::ASF::blessings_set { |
| 279 |
my ( $asf, $default_blessing ) = @_; |
| 280 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 281 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 282 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 283 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 284 |
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES]; |
| 285 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 286 |
|
| 287 |
my $default_token_blessing_package = |
| 288 |
$asf->[ Marpa::R2::Internal::ASF::DEFAULT_TOKEN_BLESSING_PACKAGE ]; |
| 289 |
my $default_rule_blessing_package = |
| 290 |
$asf->[Marpa::R2::Internal::ASF::DEFAULT_RULE_BLESSING_PACKAGE]; |
| 291 |
|
| 292 |
my @rule_blessing = (); |
| 293 |
my $highest_rule_id = $grammar_c->highest_rule_id(); |
| 294 |
RULE: for ( my $rule_id = 0; $rule_id <= $highest_rule_id; $rule_id++ ) { |
| 295 |
my $blessing; |
| 296 |
my $rule = $rules->[$rule_id]; |
| 297 |
$blessing = $rule->[Marpa::R2::Internal::Rule::BLESSING] |
| 298 |
if defined $rule; |
| 299 |
if ( defined $blessing and q{::} ne substr $blessing, 0, 2 ) { |
| 300 |
$rule_blessing[$rule_id] = $blessing; |
| 301 |
next RULE; |
| 302 |
} |
| 303 |
my $lhs_id = $grammar_c->rule_lhs($rule_id); |
| 304 |
my $name = $grammar->symbol_name($lhs_id); |
| 305 |
$rule_blessing[$rule_id] = join q{::}, $default_rule_blessing_package, |
| 306 |
normalize_asf_blessing($name); |
| 307 |
} ## end RULE: for ( my $rule_id = 0; $rule_id <= $highest_rule_id; ...) |
| 308 |
|
| 309 |
my @symbol_blessing = (); |
| 310 |
my $highest_symbol_id = $grammar_c->highest_symbol_id(); |
| 311 |
SYMBOL: |
| 312 |
for ( my $symbol_id = 0; $symbol_id <= $highest_symbol_id; $symbol_id++ ) |
| 313 |
{ |
| 314 |
my $blessing; |
| 315 |
my $symbol = $symbols->[$symbol_id]; |
| 316 |
$blessing = $symbol->[Marpa::R2::Internal::Symbol::BLESSING] |
| 317 |
if defined $symbol; |
| 318 |
if ( defined $blessing and q{::} ne substr $blessing, 0, 2 ) { |
| 319 |
$symbol_blessing[$symbol_id] = $blessing; |
| 320 |
next SYMBOL; |
| 321 |
} |
| 322 |
my $name = $grammar->symbol_name($symbol_id); |
| 323 |
$symbol_blessing[$symbol_id] = join q{::}, |
| 324 |
$default_token_blessing_package, |
| 325 |
normalize_asf_blessing($name); |
| 326 |
} ## end SYMBOL: for ( my $symbol_id = 0; $symbol_id <= $highest_symbol_id...) |
| 327 |
$asf->[Marpa::R2::Internal::ASF::RULE_BLESSINGS] = \@rule_blessing; |
| 328 |
$asf->[Marpa::R2::Internal::ASF::SYMBOL_BLESSINGS] = \@symbol_blessing; |
| 329 |
return $asf; |
| 330 |
} ## end sub Marpa::R2::Internal::ASF::blessings_set |
| 331 |
|
| 332 |
# Returns undef if no parse |
| 333 |
sub Marpa::R2::ASF::new { |
| 334 |
my ( $class, @arg_hashes ) = @_; |
| 335 |
my $asf = bless [], $class; |
| 336 |
|
| 337 |
my $slr; |
| 338 |
|
| 339 |
for my $arg_hash (@arg_hashes) { |
| 340 |
ARG: for my $arg ( keys %{$arg_hash} ) { |
| 341 |
if ( $arg eq 'slr' ) { |
| 342 |
$asf->[Marpa::R2::Internal::ASF::SLR] = $slr = |
| 343 |
$arg_hash->{$arg}; |
| 344 |
next ARG; |
| 345 |
} |
| 346 |
if ( $arg eq 'factoring_max' ) { |
| 347 |
$asf->[Marpa::R2::Internal::ASF::FACTORING_MAX] = |
| 348 |
$arg_hash->{$arg}; |
| 349 |
next ARG; |
| 350 |
} |
| 351 |
Marpa::R2::exception( |
| 352 |
qq{Unknown named arg to $asf->new(): "$arg"}); |
| 353 |
} ## end ARG: for my $arg ( keys %{$arg_hash} ) |
| 354 |
} ## end for my $arg_hash (@arg_hashes) |
| 355 |
|
| 356 |
Marpa::R2::exception( |
| 357 |
q{The "slr" named argument must be specified with the Marpa::R2::ASF::new method} |
| 358 |
) if not defined $slr; |
| 359 |
$asf->[Marpa::R2::Internal::ASF::SLR] = $slr; |
| 360 |
$asf->[Marpa::R2::Internal::ASF::FACTORING_MAX] //= 42; |
| 361 |
|
| 362 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 363 |
|
| 364 |
if ( defined $recce->[Marpa::R2::Internal::Recognizer::TREE_MODE] ) { |
| 365 |
|
| 366 |
# If we already in ASF mode, or are in valuation mode, we cannot create an ASF |
| 367 |
Marpa::R2::exception( |
| 368 |
"An attempt was made to create an ASF for a SLIF recognizer already in use\n", |
| 369 |
" The recognizer must be reset first\n", |
| 370 |
' The current SLIF recognizer mode is "', |
| 371 |
$recce->[Marpa::R2::Internal::Recognizer::TREE_MODE], |
| 372 |
qq{"\n} |
| 373 |
); |
| 374 |
} ## end if ( defined $recce->[Marpa::R2::Internal::Recognizer::TREE_MODE...]) |
| 375 |
$recce->[Marpa::R2::Internal::Recognizer::TREE_MODE] = 'forest'; |
| 376 |
|
| 377 |
( $asf->[Marpa::R2::Internal::ASF::RULE_RESOLUTIONS], |
| 378 |
$asf->[Marpa::R2::Internal::ASF::LEXEME_RESOLUTIONS] |
| 379 |
) = Marpa::R2::Internal::Value::resolve_recce( $recce, $slr ); |
| 380 |
|
| 381 |
$asf->[Marpa::R2::Internal::ASF::SYMCH_BLESSING_PACKAGE] = 'My_Symch'; |
| 382 |
$asf->[Marpa::R2::Internal::ASF::FACTORING_BLESSING_PACKAGE] = |
| 383 |
'My_Factoring'; |
| 384 |
$asf->[Marpa::R2::Internal::ASF::PROBLEM_BLESSING_PACKAGE] = 'My_Problem'; |
| 385 |
$asf->[Marpa::R2::Internal::ASF::DEFAULT_RULE_BLESSING_PACKAGE] = |
| 386 |
'My_Rule'; |
| 387 |
$asf->[Marpa::R2::Internal::ASF::DEFAULT_TOKEN_BLESSING_PACKAGE] = |
| 388 |
'My_Token'; |
| 389 |
|
| 390 |
$asf->[Marpa::R2::Internal::ASF::NEXT_INTSET_ID] = 0; |
| 391 |
$asf->[Marpa::R2::Internal::ASF::INTSET_BY_KEY] = {}; |
| 392 |
|
| 393 |
$asf->[Marpa::R2::Internal::ASF::NIDSET_BY_ID] = []; |
| 394 |
$asf->[Marpa::R2::Internal::ASF::POWERSET_BY_ID] = []; |
| 395 |
|
| 396 |
$asf->[Marpa::R2::Internal::ASF::GLADES] = []; |
| 397 |
|
| 398 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 399 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 400 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 401 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 402 |
my $recce_c = $recce->[Marpa::R2::Internal::Recognizer::C]; |
| 403 |
|
| 404 |
my $ordering = $recce->ordering_get(); |
| 405 |
Marpa::R2::exception( "Parse failed\n") if not $ordering; |
| 406 |
|
| 407 |
Marpa::R2::exception( |
| 408 |
"An attempt was make to create an ASF for a null parse\n", |
| 409 |
" A null parse is a successful parse of a zero-length string\n", |
| 410 |
" ASF's are not defined for null parses\n" |
| 411 |
) if $ordering->is_null(); |
| 412 |
|
| 413 |
my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 414 |
|
| 415 |
my $or_nodes = $asf->[Marpa::R2::Internal::ASF::OR_NODES] = []; |
| 416 |
use sort 'stable'; |
| 417 |
OR_NODE: for ( my $or_node_id = 0;; $or_node_id++ ) { |
| 418 |
my @and_node_ids = |
| 419 |
$ordering->_marpa_o_or_node_and_node_ids($or_node_id); |
| 420 |
last OR_NODE if not scalar @and_node_ids; |
| 421 |
my @sorted_and_node_ids = map { $_->[-1] } sort { $a <=> $b } map { |
| 422 |
[ ( $bocage->_marpa_b_and_node_predecessor($_) // -1 ), $_ ] |
| 423 |
} @and_node_ids; |
| 424 |
$or_nodes->[$or_node_id] = \@and_node_ids; |
| 425 |
} ## end OR_NODE: for ( my $or_node_id = 0;; $or_node_id++ ) |
| 426 |
|
| 427 |
blessings_set($asf); |
| 428 |
return $asf; |
| 429 |
|
| 430 |
} ## end sub Marpa::R2::ASF::new |
| 431 |
|
| 432 |
sub Marpa::R2::ASF::glade_is_visited { |
| 433 |
my ( $asf, $glade_id ) = @_; |
| 434 |
my $glade = $asf->[Marpa::R2::Internal::ASF::GLADES]->[$glade_id]; |
| 435 |
return if not $glade; |
| 436 |
return $glade->[Marpa::R2::Internal::Glade::VISITED]; |
| 437 |
} ## end sub Marpa::R2::ASF::glade_is_visited |
| 438 |
|
| 439 |
sub Marpa::R2::ASF::glade_visited_clear { |
| 440 |
my ( $asf, $glade_id ) = @_; |
| 441 |
my $glade_list = |
| 442 |
defined $glade_id |
| 443 |
? [ $asf->[Marpa::R2::Internal::ASF::GLADES]->[$glade_id] ] |
| 444 |
: $asf->[Marpa::R2::Internal::ASF::GLADES]; |
| 445 |
$_->[Marpa::R2::Internal::Glade::VISITED] = undef |
| 446 |
for grep {defined} @{$glade_list}; |
| 447 |
return; |
| 448 |
} ## end sub Marpa::R2::ASF::glade_visited_clear |
| 449 |
|
| 450 |
sub nid_sort_ix { |
| 451 |
my ( $asf, $nid ) = @_; |
| 452 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 453 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 454 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 455 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 456 |
my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 457 |
if ( $nid >= 0 ) { |
| 458 |
my $irl_id = $bocage->_marpa_b_or_node_irl($nid); |
| 459 |
return $grammar_c->_marpa_g_source_xrl($irl_id); |
| 460 |
} |
| 461 |
my $and_node_id = nid_to_and_node($nid); |
| 462 |
my $token_nsy_id = $bocage->_marpa_b_and_node_symbol($and_node_id); |
| 463 |
my $token_id = $grammar_c->_marpa_g_source_xsy($token_nsy_id); |
| 464 |
|
| 465 |
# -2 is reserved for 'end of data' |
| 466 |
return -$token_id - 3; |
| 467 |
} ## end sub nid_sort_ix |
| 468 |
|
| 469 |
sub Marpa::R2::ASF::grammar { |
| 470 |
my ($asf) = @_; |
| 471 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 472 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 473 |
return $slg; |
| 474 |
} ## end sub Marpa::R2::ASF::grammar |
| 475 |
|
| 476 |
sub nid_rule_id { |
| 477 |
my ( $asf, $nid ) = @_; |
| 478 |
return if $nid < 0; |
| 479 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 480 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 481 |
my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 482 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 483 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 484 |
my $irl_id = $bocage->_marpa_b_or_node_irl($nid); |
| 485 |
my $xrl_id = $grammar_c->_marpa_g_source_xrl($irl_id); |
| 486 |
return $xrl_id; |
| 487 |
} |
| 488 |
|
| 489 |
sub or_node_es_span { |
| 490 |
my ( $asf, $choicepoint ) = @_; |
| 491 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 492 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 493 |
my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 494 |
my $origin_es = $bocage->_marpa_b_or_node_origin($choicepoint); |
| 495 |
my $current_es = $bocage->_marpa_b_or_node_set($choicepoint); |
| 496 |
return $origin_es, $current_es - $origin_es; |
| 497 |
} ## end sub or_node_es_span |
| 498 |
|
| 499 |
sub token_es_span { |
| 500 |
my ( $asf, $and_node_id ) = @_; |
| 501 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 502 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 503 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 504 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 505 |
my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 506 |
my $predecessor_id = $bocage->_marpa_b_and_node_predecessor($and_node_id); |
| 507 |
my $parent_or_node_id = $bocage->_marpa_b_and_node_parent($and_node_id); |
| 508 |
|
| 509 |
if ( defined $predecessor_id ) { |
| 510 |
my $origin_es = $bocage->_marpa_b_or_node_set($predecessor_id); |
| 511 |
my $current_es = $bocage->_marpa_b_or_node_set($parent_or_node_id); |
| 512 |
return ( $origin_es, $current_es - $origin_es ); |
| 513 |
} |
| 514 |
return or_node_es_span( $asf, $parent_or_node_id ); |
| 515 |
} ## end sub token_es_span |
| 516 |
|
| 517 |
sub nid_literal { |
| 518 |
my ( $asf, $nid ) = @_; |
| 519 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 520 |
if ( $nid <= $NID_LEAF_BASE ) { |
| 521 |
my $and_node_id = nid_to_and_node($nid); |
| 522 |
my ( $start, $length ) = token_es_span( $asf, $and_node_id ); |
| 523 |
return q{} if $length == 0; |
| 524 |
return $slr->substring( $start, $length ); |
| 525 |
} ## end if ( $nid <= $NID_LEAF_BASE ) |
| 526 |
if ( $nid >= 0 ) { |
| 527 |
return $slr->substring( or_node_es_span( $asf, $nid ) ); |
| 528 |
} |
| 529 |
Marpa::R2::exception("No literal for node ID: $nid"); |
| 530 |
} |
| 531 |
|
| 532 |
sub nid_span { |
| 533 |
my ( $asf, $nid ) = @_; |
| 534 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 535 |
if ( $nid <= $NID_LEAF_BASE ) { |
| 536 |
my $and_node_id = nid_to_and_node($nid); |
| 537 |
my ( $start, $length ) = token_es_span( $asf, $and_node_id ); |
| 538 |
return ($start, 0) if $length == 0; |
| 539 |
return $slr->es_to_input_span( $start, $length ); |
| 540 |
} ## end if ( $nid <= $NID_LEAF_BASE ) |
| 541 |
if ( $nid >= 0 ) { |
| 542 |
return $slr->es_to_input_span( or_node_es_span( $asf, $nid ) ); |
| 543 |
} |
| 544 |
Marpa::R2::exception("No literal for node ID: $nid"); |
| 545 |
} |
| 546 |
|
| 547 |
sub nid_token_id { |
| 548 |
my ( $asf, $nid ) = @_; |
| 549 |
return if $nid > $NID_LEAF_BASE; |
| 550 |
my $and_node_id = nid_to_and_node($nid); |
| 551 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 552 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 553 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 554 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 555 |
my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 556 |
my $token_nsy_id = $bocage->_marpa_b_and_node_symbol($and_node_id); |
| 557 |
my $token_id = $grammar_c->_marpa_g_source_xsy($token_nsy_id); |
| 558 |
return $token_id; |
| 559 |
} |
| 560 |
|
| 561 |
sub nid_symbol_id { |
| 562 |
my ( $asf, $nid ) = @_; |
| 563 |
my $token_id = nid_token_id($asf, $nid); |
| 564 |
return $token_id if defined $token_id; |
| 565 |
Marpa::R2::exception("No symbol ID for node ID: $nid") if $nid < 0; |
| 566 |
|
| 567 |
# Not a token, so return the LHS of the rule |
| 568 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 569 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 570 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 571 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 572 |
my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 573 |
my $irl_id = $bocage->_marpa_b_or_node_irl($nid); |
| 574 |
my $xrl_id = $grammar_c->_marpa_g_source_xrl($irl_id); |
| 575 |
my $lhs_id = $grammar_c->rule_lhs($xrl_id); |
| 576 |
return $lhs_id; |
| 577 |
} |
| 578 |
|
| 579 |
sub nid_symbol_name { |
| 580 |
my ( $asf, $nid ) = @_; |
| 581 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 582 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 583 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 584 |
my $symbol_id = nid_symbol_id($asf, $nid); |
| 585 |
return $grammar->symbol_name($symbol_id); |
| 586 |
} |
| 587 |
|
| 588 |
sub nid_token_name { |
| 589 |
my ( $asf, $nid ) = @_; |
| 590 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 591 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 592 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 593 |
my $token_id = nid_token_id($asf, $nid); |
| 594 |
return if not defined $token_id; |
| 595 |
return $grammar->symbol_name($token_id); |
| 596 |
} |
| 597 |
|
| 598 |
# Memoization is heavily used -- it needs to be to keep the worst cases from |
| 599 |
# going exponential. The need to memoize is the reason for the very heavy use of |
| 600 |
# hashes. For example, quite often an HOH (hash of hashes) is used where |
| 601 |
# an HoL (hash of lists) would usually be preferred. But the HOL would leave me |
| 602 |
# with the problem of having duplicates, which if followed up upon, would make |
| 603 |
# the algorithm go exponential. |
| 604 |
|
| 605 |
# For the "seen" hashes, the intent, in C, is to use a bit vector. Since typically |
| 606 |
# choicepoints will only use a tiny fraction of the or- and and-node space, I'll create |
| 607 |
# a per-choicepoint index in the bit vector for each or- and and-node. The index will |
| 608 |
# per-ASF, and to avoid the overhead of clearing it, it will track, or each node, the |
| 609 |
# current CP indexing it. It is assumed that the indexes need only remain valid within |
| 610 |
# the method call that constructs the CPI (choicepoint iterator). |
| 611 |
|
| 612 |
sub first_factoring { |
| 613 |
my ($choicepoint, $nid_of_choicepoint) = @_; |
| 614 |
|
| 615 |
# Current NID of current SYMCH |
| 616 |
# The caller should ensure that we are never called unless the current |
| 617 |
# NID is for a rule. |
| 618 |
Marpa::R2::exception( |
| 619 |
"Internal error: first_factoring() called for negative NID: $nid_of_choicepoint" |
| 620 |
) if $nid_of_choicepoint < 0; |
| 621 |
|
| 622 |
# Due to skipping, even the top or-node can have no valid choices |
| 623 |
my $asf = $choicepoint->[Marpa::R2::Internal::Choicepoint::ASF]; |
| 624 |
my $or_nodes = $asf->[Marpa::R2::Internal::ASF::OR_NODES]; |
| 625 |
if ( not scalar @{ $or_nodes->[$nid_of_choicepoint] } ) { |
| 626 |
$choicepoint->[Marpa::R2::Internal::Choicepoint::FACTORING_STACK] = |
| 627 |
undef; |
| 628 |
return; |
| 629 |
} |
| 630 |
|
| 631 |
$choicepoint->[Marpa::R2::Internal::Choicepoint::OR_NODE_IN_USE] |
| 632 |
->{$nid_of_choicepoint} = 1; |
| 633 |
my $nook = nook_new( $asf, $nid_of_choicepoint ); |
| 634 |
$choicepoint->[Marpa::R2::Internal::Choicepoint::FACTORING_STACK] = |
| 635 |
[$nook]; |
| 636 |
|
| 637 |
# Iterate as long as we cannot finish this stack |
| 638 |
while ( not factoring_finish($choicepoint, $nid_of_choicepoint) ) { |
| 639 |
return if not factoring_iterate($choicepoint); |
| 640 |
} |
| 641 |
return 1; |
| 642 |
|
| 643 |
} |
| 644 |
|
| 645 |
sub next_factoring { |
| 646 |
my ($choicepoint, $nid_of_choicepoint) = @_; |
| 647 |
my $factoring_stack = |
| 648 |
$choicepoint->[Marpa::R2::Internal::Choicepoint::FACTORING_STACK]; |
| 649 |
Marpa::R2::exception( |
| 650 |
'Attempt to iterate factoring of uninitialized checkpoint') |
| 651 |
if not $factoring_stack; |
| 652 |
|
| 653 |
while ( factoring_iterate($choicepoint) ) { |
| 654 |
return 1 if factoring_finish($choicepoint, $nid_of_choicepoint); |
| 655 |
} |
| 656 |
|
| 657 |
# Found nothing to iterate |
| 658 |
return; |
| 659 |
} |
| 660 |
|
| 661 |
sub factoring_iterate { |
| 662 |
my ($choicepoint) = @_; |
| 663 |
my $asf = $choicepoint->[Marpa::R2::Internal::Choicepoint::ASF]; |
| 664 |
my $factoring_stack = |
| 665 |
$choicepoint->[Marpa::R2::Internal::Choicepoint::FACTORING_STACK]; |
| 666 |
FIND_NODE_TO_ITERATE: while (1) { |
| 667 |
if ( not scalar @{$factoring_stack} ) { |
| 668 |
$choicepoint->[Marpa::R2::Internal::Choicepoint::FACTORING_STACK] |
| 669 |
= undef; |
| 670 |
return; |
| 671 |
} |
| 672 |
my $top_nook = $factoring_stack->[-1]; |
| 673 |
if ( nook_increment( $asf, $top_nook ) ) { |
| 674 |
last FIND_NODE_TO_ITERATE; # in C, a "break" will do this |
| 675 |
} |
| 676 |
|
| 677 |
# Could not iterate |
| 678 |
# "Dirty" the corresponding bits in the parent and pop this nook |
| 679 |
my $stack_ix_of_parent_nook = |
| 680 |
$top_nook->[Marpa::R2::Internal::Nook::PARENT]; |
| 681 |
if ( $stack_ix_of_parent_nook >= 0 ) { |
| 682 |
my $parent_nook = $factoring_stack->[$stack_ix_of_parent_nook]; |
| 683 |
$parent_nook->[Marpa::R2::Internal::Nook::CAUSE_IS_EXPANDED] = 0 |
| 684 |
if $top_nook->[Marpa::R2::Internal::Nook::IS_CAUSE]; |
| 685 |
$parent_nook->[Marpa::R2::Internal::Nook::PREDECESSOR_IS_EXPANDED] |
| 686 |
= 0 |
| 687 |
if $top_nook->[Marpa::R2::Internal::Nook::IS_PREDECESSOR]; |
| 688 |
} ## end if ( $stack_ix_of_parent_nook >= 0 ) |
| 689 |
|
| 690 |
my $top_or_node = $top_nook->[Marpa::R2::Internal::Nook::OR_NODE]; |
| 691 |
$choicepoint->[Marpa::R2::Internal::Choicepoint::OR_NODE_IN_USE] |
| 692 |
->{$top_or_node} = undef; |
| 693 |
pop @{$factoring_stack}; |
| 694 |
} ## end FIND_NODE_TO_ITERATE: while (1) |
| 695 |
return 1; |
| 696 |
} ## end sub factoring_iterate |
| 697 |
|
| 698 |
sub factoring_finish { |
| 699 |
my ($choicepoint, $nid_of_choicepoint) = @_; |
| 700 |
my $asf = $choicepoint->[Marpa::R2::Internal::Choicepoint::ASF]; |
| 701 |
my $or_nodes = $asf->[Marpa::R2::Internal::ASF::OR_NODES]; |
| 702 |
my $factoring_stack = |
| 703 |
$choicepoint->[Marpa::R2::Internal::Choicepoint::FACTORING_STACK]; |
| 704 |
|
| 705 |
my $nidset_by_id = $asf->[Marpa::R2::Internal::ASF::NIDSET_BY_ID]; |
| 706 |
my $powerset_by_id = $asf->[Marpa::R2::Internal::ASF::POWERSET_BY_ID]; |
| 707 |
|
| 708 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 709 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 710 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 711 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 712 |
my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 713 |
|
| 714 |
my @worklist = ( 0 .. $#{$factoring_stack} ); |
| 715 |
|
| 716 |
DO_WORKLIST: while ( scalar @worklist ) { |
| 717 |
my $stack_ix_of_work_nook = $worklist[-1]; |
| 718 |
my $work_nook = $factoring_stack->[$stack_ix_of_work_nook]; |
| 719 |
my $work_or_node = $work_nook->[Marpa::R2::Internal::Nook::OR_NODE]; |
| 720 |
my $working_choice = |
| 721 |
$work_nook->[Marpa::R2::Internal::Nook::FIRST_CHOICE]; |
| 722 |
my $work_and_node_id = $or_nodes->[$work_or_node]->[$working_choice]; |
| 723 |
my $child_or_node; |
| 724 |
my $child_is_cause; |
| 725 |
my $child_is_predecessor; |
| 726 |
FIND_CHILD_OR_NODE: { |
| 727 |
|
| 728 |
if ( !$work_nook->[Marpa::R2::Internal::Nook::CAUSE_IS_EXPANDED] ) |
| 729 |
{ |
| 730 |
if ( not nook_has_semantic_cause( $asf, $work_nook ) ) { |
| 731 |
$child_or_node = |
| 732 |
$bocage->_marpa_b_and_node_cause($work_and_node_id); |
| 733 |
$child_is_cause = 1; |
| 734 |
last FIND_CHILD_OR_NODE; |
| 735 |
} ## end if ( not nook_has_semantic_cause( $asf, $work_nook )) |
| 736 |
} ## end if ( !$work_nook->[...]) |
| 737 |
$work_nook->[Marpa::R2::Internal::Nook::CAUSE_IS_EXPANDED] = 1; |
| 738 |
if ( !$work_nook |
| 739 |
->[Marpa::R2::Internal::Nook::PREDECESSOR_IS_EXPANDED] ) |
| 740 |
{ |
| 741 |
$child_or_node = |
| 742 |
$bocage->_marpa_b_and_node_predecessor($work_and_node_id); |
| 743 |
if ( defined $child_or_node ) { |
| 744 |
$child_is_predecessor = 1; |
| 745 |
last FIND_CHILD_OR_NODE; |
| 746 |
} |
| 747 |
} ## end if ( !$work_nook->[...]) |
| 748 |
$work_nook->[Marpa::R2::Internal::Nook::PREDECESSOR_IS_EXPANDED] = |
| 749 |
1; |
| 750 |
pop @worklist; |
| 751 |
next DO_WORKLIST; |
| 752 |
} ## end FIND_CHILD_OR_NODE: |
| 753 |
|
| 754 |
return 0 |
| 755 |
if |
| 756 |
$choicepoint->[Marpa::R2::Internal::Choicepoint::OR_NODE_IN_USE] |
| 757 |
->{$child_or_node}; |
| 758 |
|
| 759 |
return 0 |
| 760 |
if not scalar @{ $or_nodes->[$work_or_node] }; |
| 761 |
|
| 762 |
my $new_nook = |
| 763 |
nook_new( $asf, $child_or_node, $stack_ix_of_work_nook ); |
| 764 |
if ($child_is_cause) { |
| 765 |
$new_nook->[Marpa::R2::Internal::Nook::IS_CAUSE] = 1; |
| 766 |
$work_nook->[Marpa::R2::Internal::Nook::CAUSE_IS_EXPANDED] = 1; |
| 767 |
} |
| 768 |
if ($child_is_predecessor) { |
| 769 |
$new_nook->[Marpa::R2::Internal::Nook::IS_PREDECESSOR] = 1; |
| 770 |
$work_nook->[Marpa::R2::Internal::Nook::PREDECESSOR_IS_EXPANDED] = |
| 771 |
1; |
| 772 |
} |
| 773 |
push @{$factoring_stack}, $new_nook; |
| 774 |
push @worklist, $#{$factoring_stack}; |
| 775 |
|
| 776 |
} ## end DO_WORKLIST: while ( scalar @worklist ) |
| 777 |
|
| 778 |
return 1; |
| 779 |
|
| 780 |
} ## end sub factoring_finish |
| 781 |
|
| 782 |
sub and_nodes_to_cause_nids { |
| 783 |
my ( $asf, @and_node_ids ) = @_; |
| 784 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 785 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 786 |
my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 787 |
my %causes = (); |
| 788 |
for my $and_node_id (@and_node_ids) { |
| 789 |
my $cause_nid = $bocage->_marpa_b_and_node_cause($and_node_id) |
| 790 |
// and_node_to_nid($and_node_id); |
| 791 |
$causes{$cause_nid} = 1; |
| 792 |
} |
| 793 |
return [ keys %causes ]; |
| 794 |
} ## end sub and_nodes_to_cause_nids |
| 795 |
|
| 796 |
sub glade_id_factors { |
| 797 |
my ($choicepoint) = @_; |
| 798 |
my $asf = $choicepoint->[Marpa::R2::Internal::Choicepoint::ASF]; |
| 799 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 800 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 801 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 802 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 803 |
my $bocage = $recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 804 |
my $or_nodes = $asf->[Marpa::R2::Internal::ASF::OR_NODES]; |
| 805 |
|
| 806 |
my @result; |
| 807 |
my $factoring_stack = |
| 808 |
$choicepoint->[Marpa::R2::Internal::Choicepoint::FACTORING_STACK]; |
| 809 |
return if not $factoring_stack; |
| 810 |
FACTOR: |
| 811 |
for ( |
| 812 |
my $factor_ix = 0; |
| 813 |
$factor_ix <= $#{$factoring_stack}; |
| 814 |
$factor_ix++ |
| 815 |
) |
| 816 |
{ |
| 817 |
my $nook = $factoring_stack->[$factor_ix]; |
| 818 |
next FACTOR if not nook_has_semantic_cause( $asf, $nook ); |
| 819 |
my $or_node = $nook->[Marpa::R2::Internal::Nook::OR_NODE]; |
| 820 |
my $and_nodes = $or_nodes->[$or_node]; |
| 821 |
my $cause_nids = and_nodes_to_cause_nids( |
| 822 |
$asf, |
| 823 |
map { $and_nodes->[$_] } ( |
| 824 |
$nook->[Marpa::R2::Internal::Nook::FIRST_CHOICE] |
| 825 |
.. $nook->[Marpa::R2::Internal::Nook::LAST_CHOICE] |
| 826 |
) |
| 827 |
); |
| 828 |
my $base_nidset = Marpa::R2::Nidset->obtain( $asf, @{$cause_nids} ); |
| 829 |
my $glade_id = $base_nidset->id(); |
| 830 |
|
| 831 |
$asf->[Marpa::R2::Internal::ASF::GLADES]->[$glade_id] |
| 832 |
->[Marpa::R2::Internal::Glade::REGISTERED] = 1; |
| 833 |
push @result, $glade_id; |
| 834 |
} ## end FACTOR: for ( my $factor_ix = 0; $factor_ix <= $#{...}) |
| 835 |
return \@result; |
| 836 |
} ## end sub glade_id_factors |
| 837 |
|
| 838 |
sub glade_obtain { |
| 839 |
my ( $asf, $glade_id ) = @_; |
| 840 |
|
| 841 |
my $factoring_max = $asf->[Marpa::R2::Internal::ASF::FACTORING_MAX]; |
| 842 |
|
| 843 |
my $glades = $asf->[Marpa::R2::Internal::ASF::GLADES]; |
| 844 |
my $glade = $glades->[$glade_id]; |
| 845 |
if ( not defined $glade |
| 846 |
or not $glade->[Marpa::R2::Internal::Glade::REGISTERED] ) |
| 847 |
{ |
| 848 |
say Data::Dumper::Dumper($glade); |
| 849 |
Marpa::R2::exception( |
| 850 |
"Attempt to use an invalid glade, one whose ID is $glade_id"); |
| 851 |
} ## end if ( not defined $glade or not $glade->[...]) |
| 852 |
|
| 853 |
# Return the glade if it is already set up |
| 854 |
return $glade if $glade->[Marpa::R2::Internal::Glade::SYMCHES]; |
| 855 |
|
| 856 |
my $base_nidset = |
| 857 |
$asf->[Marpa::R2::Internal::ASF::NIDSET_BY_ID]->[$glade_id]; |
| 858 |
my $choicepoint; |
| 859 |
my $choicepoint_powerset; |
| 860 |
{ |
| 861 |
my @source_data = (); |
| 862 |
for my $source_nid ( @{ $base_nidset->nids() } ) { |
| 863 |
my $sort_ix = nid_sort_ix( $asf, $source_nid ); |
| 864 |
push @source_data, [ $sort_ix, $source_nid ]; |
| 865 |
} |
| 866 |
my @sorted_source_data = sort { $a->[0] <=> $b->[0] } @source_data; |
| 867 |
my $nid_ix = 0; |
| 868 |
my ( $sort_ix_of_this_nid, $this_nid ) = |
| 869 |
@{ $sorted_source_data[ $nid_ix++ ] }; |
| 870 |
my @nids_with_current_sort_ix = (); |
| 871 |
my $current_sort_ix = $sort_ix_of_this_nid; |
| 872 |
my @symch_ids = (); |
| 873 |
NID: while (1) { |
| 874 |
|
| 875 |
if ( $sort_ix_of_this_nid != $current_sort_ix ) { |
| 876 |
|
| 877 |
# Currently only whole id break logic |
| 878 |
my $nidset_for_sort_ix = Marpa::R2::Nidset->obtain( $asf, |
| 879 |
@nids_with_current_sort_ix ); |
| 880 |
push @symch_ids, $nidset_for_sort_ix->id(); |
| 881 |
@nids_with_current_sort_ix = (); |
| 882 |
$current_sort_ix = $sort_ix_of_this_nid; |
| 883 |
} ## end if ( $sort_ix_of_this_nid != $current_sort_ix ) |
| 884 |
last NID if not defined $this_nid; |
| 885 |
push @nids_with_current_sort_ix, $this_nid; |
| 886 |
my $sorted_entry = $sorted_source_data[ $nid_ix++ ]; |
| 887 |
if ( defined $sorted_entry ) { |
| 888 |
( $sort_ix_of_this_nid, $this_nid ) = @{$sorted_entry}; |
| 889 |
next NID; |
| 890 |
} |
| 891 |
$this_nid = undef; |
| 892 |
$sort_ix_of_this_nid = -2; |
| 893 |
} ## end NID: while (1) |
| 894 |
$choicepoint_powerset = Marpa::R2::Powerset->obtain( $asf, @symch_ids ); |
| 895 |
$choicepoint->[Marpa::R2::Internal::Choicepoint::ASF] = $asf; |
| 896 |
$choicepoint->[Marpa::R2::Internal::Choicepoint::FACTORING_STACK] = |
| 897 |
undef; |
| 898 |
} |
| 899 |
|
| 900 |
# Check if choicepoint already seen? |
| 901 |
my @symches = (); |
| 902 |
my $symch_count = $choicepoint_powerset->count(); |
| 903 |
SYMCH: for ( my $symch_ix = 0; $symch_ix < $symch_count; $symch_ix++ ) { |
| 904 |
$choicepoint->[Marpa::R2::Internal::Choicepoint::FACTORING_STACK] = |
| 905 |
undef; |
| 906 |
my $symch_nidset = $choicepoint_powerset->nidset($asf, $symch_ix); |
| 907 |
my $choicepoint_nid = $symch_nidset->nid(0); |
| 908 |
my $symch_rule_id = nid_rule_id($asf, $choicepoint_nid) // -1; |
| 909 |
|
| 910 |
# Initial undef indicates no factorings omitted |
| 911 |
my @factorings = ( $symch_rule_id, undef ); |
| 912 |
|
| 913 |
# For a token |
| 914 |
# There will not be multiple factorings or nids, |
| 915 |
# it is assumed, for a token |
| 916 |
if ( $symch_rule_id < 0 ) { |
| 917 |
my $base_nidset = Marpa::R2::Nidset->obtain( $asf, $choicepoint_nid ); |
| 918 |
my $glade_id = $base_nidset->id(); |
| 919 |
|
| 920 |
$asf->[Marpa::R2::Internal::ASF::GLADES]->[$glade_id] |
| 921 |
->[Marpa::R2::Internal::Glade::REGISTERED] = 1; |
| 922 |
push @factorings, [$glade_id]; |
| 923 |
push @symches, \@factorings; |
| 924 |
next SYMCH; |
| 925 |
} ## end if ( $symch_rule_id < 0 ) |
| 926 |
|
| 927 |
my $symch = $choicepoint_powerset->nidset($asf, $symch_ix); |
| 928 |
my $nid_count = $symch->count(); |
| 929 |
my $factorings_omitted; |
| 930 |
FACTORINGS_LOOP: |
| 931 |
for ( my $nid_ix = 0; $nid_ix < $nid_count; $nid_ix++ ) { |
| 932 |
$choicepoint_nid = $symch_nidset->nid($nid_ix); |
| 933 |
first_factoring($choicepoint, $choicepoint_nid); |
| 934 |
my $factoring = glade_id_factors($choicepoint); |
| 935 |
|
| 936 |
FACTOR: while ( defined $factoring ) { |
| 937 |
if ( scalar @factorings > $factoring_max ) { |
| 938 |
|
| 939 |
# update factorings omitted flag |
| 940 |
$factorings[1] = 1; |
| 941 |
last FACTORINGS_LOOP; |
| 942 |
} |
| 943 |
my @factoring = (); |
| 944 |
for ( |
| 945 |
my $item_ix = $#{$factoring}; |
| 946 |
$item_ix >= 0; |
| 947 |
$item_ix-- |
| 948 |
) |
| 949 |
{ |
| 950 |
push @factoring, $factoring->[$item_ix]; |
| 951 |
} ## end for ( my $item_ix = $#{$factoring}; $item_ix >= 0; ...) |
| 952 |
push @factorings, \@factoring; |
| 953 |
next_factoring($choicepoint, $choicepoint_nid); |
| 954 |
$factoring = glade_id_factors($choicepoint); |
| 955 |
} ## end FACTOR: while ( defined $factoring ) |
| 956 |
} ## end FACTORINGS_LOOP: for ( my $nid_ix = 0; $nid_ix < $nid_count; $nid_ix...) |
| 957 |
push @symches, \@factorings; |
| 958 |
} ## end SYMCH: for ( my $symch_ix = 0; $symch_ix < $symch_count; ...) |
| 959 |
|
| 960 |
$glade->[Marpa::R2::Internal::Glade::SYMCHES] = \@symches; |
| 961 |
|
| 962 |
$glade->[Marpa::R2::Internal::Glade::ID] = $glade_id; |
| 963 |
$asf->[Marpa::R2::Internal::ASF::GLADES]->[$glade_id] = $glade; |
| 964 |
return $glade; |
| 965 |
} ## end sub glade_obtain |
| 966 |
|
| 967 |
sub Marpa::R2::ASF::glade_symch_count { |
| 968 |
my ( $asf, $glade_id ) = @_; |
| 969 |
my $glade = glade_obtain( $asf, $glade_id ); |
| 970 |
Marpa::R2::exception("No glade found for glade ID $glade_id)") if not defined $glade; |
| 971 |
return scalar @{ $glade->[Marpa::R2::Internal::Glade::SYMCHES] }; |
| 972 |
} |
| 973 |
|
| 974 |
sub Marpa::R2::ASF::glade_literal { |
| 975 |
my ( $asf, $glade_id ) = @_; |
| 976 |
my $nidset_by_id = $asf->[Marpa::R2::Internal::ASF::NIDSET_BY_ID]; |
| 977 |
my $nidset = $nidset_by_id->[$glade_id]; |
| 978 |
Marpa::R2::exception("No glade found for glade ID $glade_id)") if not defined $nidset; |
| 979 |
my $nid0 = $nidset->nid(0); |
| 980 |
return nid_literal($asf, $nid0); |
| 981 |
} ## end sub Marpa::R2::ASF::glade_literal |
| 982 |
|
| 983 |
sub Marpa::R2::ASF::glade_span { |
| 984 |
my ( $asf, $glade_id ) = @_; |
| 985 |
my $nidset_by_id = $asf->[Marpa::R2::Internal::ASF::NIDSET_BY_ID]; |
| 986 |
my $nidset = $nidset_by_id->[$glade_id]; |
| 987 |
Marpa::R2::exception("No glade found for glade ID $glade_id)") if not defined $nidset; |
| 988 |
my $nid0 = $nidset->nid(0); |
| 989 |
return nid_span($asf, $nid0); |
| 990 |
} |
| 991 |
|
| 992 |
sub Marpa::R2::ASF::glade_symbol_id { |
| 993 |
my ( $asf, $glade_id ) = @_; |
| 994 |
my $nidset_by_id = $asf->[Marpa::R2::Internal::ASF::NIDSET_BY_ID]; |
| 995 |
my $nidset = $nidset_by_id->[$glade_id]; |
| 996 |
Marpa::R2::exception("No glade found for glade ID $glade_id)") if not defined $nidset; |
| 997 |
my $nid0 = $nidset->nid(0); |
| 998 |
return nid_symbol_id($asf, $nid0); |
| 999 |
} |
| 1000 |
|
| 1001 |
sub Marpa::R2::ASF::symch_rule_id { |
| 1002 |
my ( $asf, $glade_id, $symch_ix ) = @_; |
| 1003 |
my $glade = glade_obtain( $asf, $glade_id ); |
| 1004 |
my $symches = $glade->[Marpa::R2::Internal::Glade::SYMCHES]; |
| 1005 |
return if $symch_ix > $#{$symches}; |
| 1006 |
my ($rule_id) = @{ $symches->[$symch_ix] }; |
| 1007 |
return $rule_id; |
| 1008 |
} ## end sub Marpa::R2::ASF::symch_rule_id |
| 1009 |
|
| 1010 |
sub Marpa::R2::ASF::symch_factoring_count { |
| 1011 |
my ( $asf, $glade_id, $symch_ix ) = @_; |
| 1012 |
my $glade = glade_obtain( $asf, $glade_id ); |
| 1013 |
Marpa::R2::exception("No glade found for glade ID $glade_id)") if not defined $glade; |
| 1014 |
my $symches = $glade->[Marpa::R2::Internal::Glade::SYMCHES]; |
| 1015 |
return if $symch_ix > $#{$symches}; |
| 1016 |
return $#{ $symches->[$symch_ix] } - 1; # length minus 2 |
| 1017 |
} ## end sub Marpa::R2::ASF::symch_factoring_count |
| 1018 |
|
| 1019 |
sub Marpa::R2::ASF::factoring_downglades { |
| 1020 |
my ( $asf, $glade_id, $symch_ix, $factoring_ix ) = @_; |
| 1021 |
my $glade = glade_obtain( $asf, $glade_id ); |
| 1022 |
Marpa::R2::exception("No glade found for glade ID $glade_id)") if not defined $glade; |
| 1023 |
my $symches = $glade->[Marpa::R2::Internal::Glade::SYMCHES]; |
| 1024 |
Marpa::R2::exception("No symch #$symch_ix exists for glade ID $glade_id") |
| 1025 |
if $symch_ix > $#{$symches}; |
| 1026 |
my $symch = $symches->[$symch_ix]; |
| 1027 |
my ( $rule_id, undef, @factorings ) = @{$symch}; |
| 1028 |
Marpa::R2::exception("No downglades for glade ID $glade_id, symch #$symch_ix: it is a token symch") |
| 1029 |
if $rule_id < 0; |
| 1030 |
return if $factoring_ix >= scalar @factorings; |
| 1031 |
my $factoring = $factorings[$factoring_ix]; |
| 1032 |
return $factoring; |
| 1033 |
} |
| 1034 |
|
| 1035 |
sub Marpa::R2::ASF::factoring_symbol_count { |
| 1036 |
my ( $asf, $glade_id, $symch_ix, $factoring_ix ) = @_; |
| 1037 |
my $factoring = $asf->factoring_downglades($glade_id, $symch_ix, $factoring_ix); |
| 1038 |
return if not defined $factoring; |
| 1039 |
return scalar @{$factoring}; |
| 1040 |
} ## end sub Marpa::R2::ASF::factoring_symbol_count |
| 1041 |
|
| 1042 |
sub Marpa::R2::ASF::factor_downglade { |
| 1043 |
my ( $asf, $glade_id, $symch_ix, $factoring_ix, $symbol_ix ) = @_; |
| 1044 |
my $factoring = $asf->factoring_downglades($glade_id, $symch_ix, $factoring_ix); |
| 1045 |
return if not defined $factoring; |
| 1046 |
return $factoring->[$symbol_ix]; |
| 1047 |
} ## end sub Marpa::R2::ASF::factor_downglade |
| 1048 |
|
| 1049 |
sub Marpa::R2::Internal::ASF::ambiguities { |
| 1050 |
my ($asf) = @_; |
| 1051 |
my $peak = $asf->peak(); |
| 1052 |
return Marpa::R2::Internal::ASF::glade_ambiguities( $asf, $peak, [] ); |
| 1053 |
} |
| 1054 |
|
| 1055 |
sub Marpa::R2::Internal::ASF::glade_ambiguities { |
| 1056 |
my ( $asf, $glade, $seen ) = @_; |
| 1057 |
return [] if $seen->[$glade]; # empty on revisit |
| 1058 |
$seen->[$glade] = 1; |
| 1059 |
my $grammar = $asf->grammar(); |
| 1060 |
my $symch_count = $asf->glade_symch_count($glade); |
| 1061 |
if ( $symch_count > 1 ) { |
| 1062 |
my $literal = $asf->glade_literal($glade); |
| 1063 |
my $symbol_id = $asf->glade_symbol_id($glade); |
| 1064 |
my $display_form = $grammar->symbol_display_form($symbol_id); |
| 1065 |
return [ [ 'symch', $glade, ] ]; |
| 1066 |
} ## end if ( $symch_count > 1 ) |
| 1067 |
my $rule_id = $asf->symch_rule_id( $glade, 0 ); |
| 1068 |
return [] if $rule_id < 0; # no ambiguities if a token |
| 1069 |
|
| 1070 |
# ignore any truncation of the factorings |
| 1071 |
|
| 1072 |
my $factoring_count = $asf->symch_factoring_count( $glade, 0 ); |
| 1073 |
if ( $factoring_count <= 1 ) { |
| 1074 |
my $downglades = $asf->factoring_downglades( $glade, 0, 0 ); |
| 1075 |
my @problems = |
| 1076 |
map { @{ glade_ambiguities( $asf, $_, $seen ) } } @{$downglades}; |
| 1077 |
return \@problems; |
| 1078 |
} ## end if ( $factoring_count <= 1 ) |
| 1079 |
my @results = (); |
| 1080 |
|
| 1081 |
my $downglades = $asf->factoring_downglades( $glade, 0, 0 ); |
| 1082 |
my $min_factors = $#{$downglades} + 1; |
| 1083 |
my ( $upglade_start, $upglade_length ) = $asf->glade_span($glade); |
| 1084 |
my $sync_location = $upglade_start + $upglade_length; |
| 1085 |
|
| 1086 |
my @factors_by_factoring = ($downglades); |
| 1087 |
for ( |
| 1088 |
my $factoring_ix = 1; |
| 1089 |
$factoring_ix < $factoring_count; |
| 1090 |
$factoring_ix++ |
| 1091 |
) |
| 1092 |
{ |
| 1093 |
my $downglades = |
| 1094 |
$asf->factoring_downglades( $glade, 0, $factoring_ix ); |
| 1095 |
my $factor_count = $#{$downglades} + 1; |
| 1096 |
$min_factors = |
| 1097 |
$min_factors > $factor_count ? $factor_count : $min_factors; |
| 1098 |
|
| 1099 |
# Determine a first potential |
| 1100 |
# "sync location of the factors" from |
| 1101 |
# the earliest start of the first downglade of any factoring. |
| 1102 |
# Currently this will be the start of the parent glade, but this |
| 1103 |
# method will be safe against any future hacks. |
| 1104 |
my ($this_sync_location) = $asf->glade_span( $downglades->[0] ); |
| 1105 |
$sync_location = |
| 1106 |
List::Util::min( $this_sync_location, $sync_location ); |
| 1107 |
|
| 1108 |
push @factors_by_factoring, $downglades; |
| 1109 |
} ## end for ( my $factoring_ix = 1; $factoring_ix < $factoring_count...) |
| 1110 |
|
| 1111 |
my @factor_ix = (0) x $factoring_count; |
| 1112 |
SYNC_PASS: while (1) { |
| 1113 |
|
| 1114 |
# Assume synced and unambiguous until we see otherwise. |
| 1115 |
my $is_synced = 1; |
| 1116 |
|
| 1117 |
# First find a synch'ed set of factors, if we can |
| 1118 |
FACTORING: |
| 1119 |
for ( |
| 1120 |
my $factoring_ix = 0; |
| 1121 |
$factoring_ix < $factoring_count; |
| 1122 |
$factoring_ix++ |
| 1123 |
) |
| 1124 |
{ |
| 1125 |
my $this_factor_ix = $factor_ix[$factoring_ix]; |
| 1126 |
my $this_downglade = |
| 1127 |
$factors_by_factoring[$factoring_ix][$this_factor_ix]; |
| 1128 |
my ($this_start) = $asf->glade_span($this_downglade); |
| 1129 |
|
| 1130 |
# To keep time complexity down we limit the number of times we deal |
| 1131 |
# with a factoring at a sync location to 3, worst case -- a pass which |
| 1132 |
# identifies it as a potential sync location, a pass which |
| 1133 |
# (if possible) brings all the factors to that location, and a |
| 1134 |
# pass which leaves all factor IX's where they are, and determines |
| 1135 |
# we have found a sync location. This makes out time O(f*n), where |
| 1136 |
# f is the factoring count and n is the mininum number of factors. |
| 1137 |
|
| 1138 |
while ( $this_start < $sync_location ) { |
| 1139 |
$factor_ix[$factoring_ix]++; |
| 1140 |
last SYNC_PASS if $factor_ix[$factoring_ix] >= $min_factors; |
| 1141 |
$this_start = $asf->glade_span($this_downglade); |
| 1142 |
} ## end if ( $this_start < $sync_location ) |
| 1143 |
if ( $this_start > $sync_location ) { |
| 1144 |
$is_synced = 0; |
| 1145 |
$sync_location = $this_start; |
| 1146 |
} |
| 1147 |
} ## end FACTORING: for ( my $factoring_ix = 0; $factoring_ix < ...) |
| 1148 |
|
| 1149 |
next SYNC_PASS if not $is_synced; |
| 1150 |
|
| 1151 |
# If here, every factor starts at the sync location |
| 1152 |
|
| 1153 |
SYNCED_RESULT: { |
| 1154 |
|
| 1155 |
my $ambiguous_factors; |
| 1156 |
my $first_factor_ix = $factor_ix[0]; |
| 1157 |
my $first_downglade = $factors_by_factoring[0][$first_factor_ix]; |
| 1158 |
|
| 1159 |
FACTORING: |
| 1160 |
for ( |
| 1161 |
my $factoring_ix = 1; |
| 1162 |
$factoring_ix < $factoring_count; |
| 1163 |
$factoring_ix++ |
| 1164 |
) |
| 1165 |
{ |
| 1166 |
my $this_factor_ix = $factor_ix[$factoring_ix]; |
| 1167 |
my $this_downglade = |
| 1168 |
$factors_by_factoring[$factoring_ix][$this_factor_ix]; |
| 1169 |
if ( $this_downglade != $first_downglade ) { |
| 1170 |
$ambiguous_factors = [ |
| 1171 |
$first_factor_ix, $factoring_ix, |
| 1172 |
$this_factor_ix |
| 1173 |
]; |
| 1174 |
last FACTORING; |
| 1175 |
} ## end if ( $this_downglade != $first_downglade ) |
| 1176 |
|
| 1177 |
} ## end FACTORING: for ( my $factoring_ix = 1; $factoring_ix < ...) |
| 1178 |
|
| 1179 |
# If here, all the the downglades are identical |
| 1180 |
if ( not defined $ambiguous_factors ) { |
| 1181 |
push @results, |
| 1182 |
@{ glade_ambiguities( $asf, $first_downglade, $seen ) }; |
| 1183 |
last SYNCED_RESULT; |
| 1184 |
} |
| 1185 |
|
| 1186 |
# First factoring IX is always zero |
| 1187 |
push @results, |
| 1188 |
[ 'factoring', $glade, 0, @{$ambiguous_factors} ]; |
| 1189 |
} ## end SYNCED_RESULT: |
| 1190 |
|
| 1191 |
$factor_ix[$_]++ for 0 .. $factoring_count; |
| 1192 |
last SYNC_PASS if List::Util::max(@factor_ix) >= $min_factors; |
| 1193 |
|
| 1194 |
} ## end SYNC_PASS: while (1) |
| 1195 |
|
| 1196 |
return \@results; |
| 1197 |
|
| 1198 |
} ## end sub Marpa::R2::Internal::ASF::glade_ambiguities |
| 1199 |
|
| 1200 |
# A generic display routine for ambiguities -- complex application will |
| 1201 |
# want to replace this, using it perhaps as a fallback. |
| 1202 |
sub Marpa::R2::Internal::ASF::ambiguities_show { |
| 1203 |
my ( $asf, $ambiguities ) = @_; |
| 1204 |
my $grammar = $asf->grammar(); |
| 1205 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 1206 |
my $p_input = $slr->[Marpa::R2::Internal::Scanless::R::P_INPUT_STRING]; |
| 1207 |
my $result = q{}; |
| 1208 |
AMBIGUITY: for my $ambiguity ( @{$ambiguities} ) { |
| 1209 |
my $type = $ambiguity->[0]; |
| 1210 |
if ( $type eq 'symch' ) { |
| 1211 |
|
| 1212 |
# Not tested !!!! |
| 1213 |
my ( undef, $glade ) = @{$ambiguity}; |
| 1214 |
my $symbol_display_form = |
| 1215 |
$grammar->symbol_display_form( |
| 1216 |
$asf->glade_symbol_id($glade) ); |
| 1217 |
my ( $start, $length ) = $asf->glade_span($glade); |
| 1218 |
my ( $start_line, $start_column ) = $slr->line_column($start); |
| 1219 |
my ( $end_line, $end_column ) = |
| 1220 |
$slr->line_column( $start + $length - 1 ); |
| 1221 |
my $display_length = List::Util::min( $length, 60 ); |
| 1222 |
$result |
| 1223 |
.= qq{Ambiguous symch at Glade=$glade, Symbol=<$symbol_display_form>:\n}; |
| 1224 |
$result |
| 1225 |
.= qq{ The ambiguity is from line $start_line, column $start_column } |
| 1226 |
. qq{to line $end_line, column $end_column\n}; |
| 1227 |
my $literal_label = |
| 1228 |
$display_length == $length ? 'Text is: ' : 'Text begins: '; |
| 1229 |
$result |
| 1230 |
.= q{ } |
| 1231 |
. $literal_label |
| 1232 |
. Marpa::R2::Internal::Scanless::input_escape( $p_input, |
| 1233 |
$start, $display_length ) |
| 1234 |
. qq{\n}; |
| 1235 |
|
| 1236 |
my $symch_count = $asf->glade_symch_count($glade); |
| 1237 |
my $display_symch_count = List::Util::min( 5, $symch_count ); |
| 1238 |
$result .= |
| 1239 |
$symch_count == $display_symch_count |
| 1240 |
? " There are $symch_count symches\n" |
| 1241 |
: " There are $symch_count symches -- showing only the first $display_symch_count\n"; |
| 1242 |
SYMCH_IX: for my $symch_ix ( 0 .. $display_symch_count - 1 ) { |
| 1243 |
my $rule_id = $asf->symch_rule_id( $glade, $symch_ix ); |
| 1244 |
if ( $rule_id < 0 ) { |
| 1245 |
$result .= " Symch $symch_ix is a token\n"; |
| 1246 |
next SYMCH_IX; |
| 1247 |
} |
| 1248 |
$result .= " Symch $symch_ix is a rule: " |
| 1249 |
. $grammar->rule_show($rule_id) . "\n"; |
| 1250 |
} ## end SYMCH_IX: for my $symch_ix ( 0 .. $display_symch_count - 1 ) |
| 1251 |
|
| 1252 |
next AMBIGUITY; |
| 1253 |
} ## end if ( $type eq 'symch' ) |
| 1254 |
if ( $type eq 'factoring' ) { |
| 1255 |
my $factoring_ix1 = 0; |
| 1256 |
my ( undef, $glade, $symch_ix, $factor_ix1, $factoring_ix2, |
| 1257 |
$factor_ix2 ) |
| 1258 |
= @{$ambiguity}; |
| 1259 |
my $first_downglades = |
| 1260 |
$asf->factoring_downglades( $glade, $symch_ix, 0 ); |
| 1261 |
my $first_downglade = $first_downglades->[$factor_ix1]; |
| 1262 |
{ |
| 1263 |
my $these_downglades = |
| 1264 |
$asf->factoring_downglades( $glade, $symch_ix, |
| 1265 |
$factoring_ix2 ); |
| 1266 |
my $this_downglade = $these_downglades->[$factor_ix2]; |
| 1267 |
my $symbol_display_form = |
| 1268 |
$grammar->symbol_display_form( |
| 1269 |
$asf->glade_symbol_id($first_downglade) ); |
| 1270 |
my ( $start, $first_length ) = |
| 1271 |
$asf->glade_span($first_downglade); |
| 1272 |
my ( undef, $this_length ) = |
| 1273 |
$asf->glade_span($this_downglade); |
| 1274 |
my ( $start_line, $start_column ) = $slr->line_column($start); |
| 1275 |
my $display_length = |
| 1276 |
List::Util::min( $first_length, $this_length, 60 ); |
| 1277 |
$result |
| 1278 |
.= qq{Length of symbol "$symbol_display_form" at line $start_line, column $start_column is ambiguous\n}; |
| 1279 |
|
| 1280 |
if ( $display_length > 0 ) { |
| 1281 |
$result .= qq{ Choices start with: } |
| 1282 |
. Marpa::R2::Internal::Scanless::input_escape( |
| 1283 |
$p_input, $start, $display_length ) |
| 1284 |
. qq{\n}; |
| 1285 |
} ## end if ( $display_length > 0 ) |
| 1286 |
|
| 1287 |
my @display_downglade = ( $first_downglade, $this_downglade ); |
| 1288 |
DISPLAY_GLADE: |
| 1289 |
for ( |
| 1290 |
my $glade_ix = 0; |
| 1291 |
$glade_ix <= $#display_downglade; |
| 1292 |
$glade_ix++ |
| 1293 |
) |
| 1294 |
{ |
| 1295 |
# Choices may be zero length |
| 1296 |
my $choice_number = $glade_ix + 1; |
| 1297 |
my $glade_id = $display_downglade[$glade_ix]; |
| 1298 |
my ( undef, $length ) = $asf->glade_span($glade_id); |
| 1299 |
if ( $length <= 0 ) { |
| 1300 |
$result |
| 1301 |
.= qq{ Choice $choice_number is zero length\n}; |
| 1302 |
next DISPLAY_GLADE; |
| 1303 |
} |
| 1304 |
my ( $end_line, $end_column ) = |
| 1305 |
$slr->line_column( $start + $length - 1 ); |
| 1306 |
$result |
| 1307 |
.= qq{ Choice $choice_number, length=$length, ends at line $end_line, column $end_column\n}; |
| 1308 |
if ( $length > 60 ) { |
| 1309 |
$result .= qq{ Choice $choice_number ending: } |
| 1310 |
. Marpa::R2::Internal::Scanless::reversed_input_escape( |
| 1311 |
$p_input, $start + $length, 60 ) |
| 1312 |
. qq{\n}; |
| 1313 |
next DISPLAY_GLADE; |
| 1314 |
} ## end if ( $length > 60 ) |
| 1315 |
$result .= qq{ Choice $choice_number: } |
| 1316 |
. Marpa::R2::Internal::Scanless::input_escape( |
| 1317 |
$p_input, $start, $length ) |
| 1318 |
. qq{\n}; |
| 1319 |
|
| 1320 |
} ## end DISPLAY_GLADE: for ( my $glade_ix = 0; $glade_ix <= ...) |
| 1321 |
next AMBIGUITY; |
| 1322 |
} ## end FACTORING: for ( my $factoring_ix = 1; $factoring_ix < ...) |
| 1323 |
next AMBIGUITY; |
| 1324 |
} ## end if ( $type eq 'factoring' ) |
| 1325 |
$result |
| 1326 |
.= qq{Ambiguities of type "$type" not implemented:\n} |
| 1327 |
. Data::Dumper::dumper($ambiguity); |
| 1328 |
next AMBIGUITY; |
| 1329 |
|
| 1330 |
} ## end AMBIGUITY: for my $ambiguity ( @{$ambiguities} ) |
| 1331 |
return $result; |
| 1332 |
} ## end sub Marpa::R2::Internal::ASF::ambiguities_show |
| 1333 |
|
| 1334 |
# The higher level calls |
| 1335 |
|
| 1336 |
sub Marpa::R2::ASF::traverse { |
| 1337 |
my ( $asf, $per_traverse_object, $method ) = @_; |
| 1338 |
if ( ref $method ne 'CODE' ) { |
| 1339 |
Marpa::R2::exception( |
| 1340 |
'Argument to $asf->traverse() must be an anonymous subroutine'); |
| 1341 |
} |
| 1342 |
if ( not ref $per_traverse_object ) { |
| 1343 |
Marpa::R2::exception( |
| 1344 |
'Argument to $asf->traverse() must be a reference'); |
| 1345 |
} |
| 1346 |
my $peak = $asf->peak(); |
| 1347 |
my $peak_glade = glade_obtain( $asf, $peak ); |
| 1348 |
my $traverser = bless [], "Marpa::R2::Internal::ASF::Traverse"; |
| 1349 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::ASF] = $asf; |
| 1350 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::CODE] = $method; |
| 1351 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::PER_TRAVERSE_OBJECT] = $per_traverse_object; |
| 1352 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::VALUES] = []; |
| 1353 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::GLADE] = $peak_glade; |
| 1354 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::SYMCH_IX] = 0; |
| 1355 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::FACTORING_IX] = 0; |
| 1356 |
return $method->( $traverser, $per_traverse_object ); |
| 1357 |
} ## end sub Marpa::R2::ASF::traverse |
| 1358 |
|
| 1359 |
sub Marpa::R2::Internal::ASF::Traverse::all_choices { |
| 1360 |
my ( $traverser ) = @_; |
| 1361 |
|
| 1362 |
my @values = Marpa::R2::Internal::ASF::Traverse::rh_values( $traverser ); |
| 1363 |
my @results = ( [] ); |
| 1364 |
for my $rh_ix ( 0 .. @values - 1 ) { |
| 1365 |
my @new_results = (); |
| 1366 |
for my $old_result (@results) { |
| 1367 |
my $child_value = $values[$rh_ix]; |
| 1368 |
$child_value = [ $child_value ] unless ref $child_value eq 'ARRAY'; |
| 1369 |
for my $new_value ( @{ $child_value } ) { |
| 1370 |
push @new_results, [ @{$old_result}, $new_value ]; |
| 1371 |
} |
| 1372 |
} |
| 1373 |
@results = @new_results; |
| 1374 |
} ## end for my $rh_ix ( 0 .. $length - 1 ) |
| 1375 |
|
| 1376 |
return @results; |
| 1377 |
} |
| 1378 |
|
| 1379 |
|
| 1380 |
sub Marpa::R2::Internal::ASF::Traverse::literal { |
| 1381 |
my ( $traverser ) = @_; |
| 1382 |
my $asf = $traverser->[Marpa::R2::Internal::ASF::Traverse::ASF]; |
| 1383 |
my $glade = $traverser->[Marpa::R2::Internal::ASF::Traverse::GLADE]; |
| 1384 |
my $glade_id = $glade->[Marpa::R2::Internal::Glade::ID]; |
| 1385 |
return $asf->glade_literal($glade_id); |
| 1386 |
} |
| 1387 |
|
| 1388 |
sub Marpa::R2::Internal::ASF::Traverse::span { |
| 1389 |
my ( $traverser ) = @_; |
| 1390 |
my $asf = $traverser->[Marpa::R2::Internal::ASF::Traverse::ASF]; |
| 1391 |
my $glade = $traverser->[Marpa::R2::Internal::ASF::Traverse::GLADE]; |
| 1392 |
my $glade_id = $glade->[Marpa::R2::Internal::Glade::ID]; |
| 1393 |
return $asf->glade_span($glade_id); |
| 1394 |
} |
| 1395 |
|
| 1396 |
sub Marpa::R2::Internal::ASF::Traverse::symbol_id { |
| 1397 |
my ( $traverser ) = @_; |
| 1398 |
my $asf = $traverser->[Marpa::R2::Internal::ASF::Traverse::ASF]; |
| 1399 |
my $glade = $traverser->[Marpa::R2::Internal::ASF::Traverse::GLADE]; |
| 1400 |
my $glade_id = $glade->[Marpa::R2::Internal::Glade::ID]; |
| 1401 |
return $asf->glade_symbol_id($glade_id); |
| 1402 |
} |
| 1403 |
|
| 1404 |
sub Marpa::R2::Internal::ASF::Traverse::rule_id { |
| 1405 |
my ( $traverser ) = @_; |
| 1406 |
my $glade = $traverser->[Marpa::R2::Internal::ASF::Traverse::GLADE]; |
| 1407 |
my $symch_ix = |
| 1408 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::SYMCH_IX]; |
| 1409 |
my $symch = $glade->[Marpa::R2::Internal::Glade::SYMCHES]->[$symch_ix]; |
| 1410 |
my ( $rule_id ) = @{$symch}; |
| 1411 |
return if $rule_id < 0; |
| 1412 |
return $rule_id; |
| 1413 |
} ## end sub Marpa::R2::Internal::ASF::Traverse::rule_id |
| 1414 |
|
| 1415 |
sub Marpa::R2::Internal::ASF::Traverse::rh_length { |
| 1416 |
my ( $traverser ) = @_; |
| 1417 |
my $glade = $traverser->[Marpa::R2::Internal::ASF::Traverse::GLADE]; |
| 1418 |
my $symch_ix = |
| 1419 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::SYMCH_IX]; |
| 1420 |
my $symch = $glade->[Marpa::R2::Internal::Glade::SYMCHES]->[$symch_ix]; |
| 1421 |
my ( $rule_id, undef, @factorings ) = @{$symch}; |
| 1422 |
Marpa::R2::exception( |
| 1423 |
'$glade->rh_length($rh_ix) called for a token -- that is not allowed') |
| 1424 |
if $rule_id < 0; |
| 1425 |
my $factoring_ix = |
| 1426 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::FACTORING_IX]; |
| 1427 |
my $factoring = $factorings[$factoring_ix]; |
| 1428 |
return scalar @{$factoring}; |
| 1429 |
} ## end sub Marpa::R2::Internal::ASF::Traverse::rh_length |
| 1430 |
|
| 1431 |
sub Marpa::R2::Internal::ASF::Traverse::rh_value { |
| 1432 |
my ( $traverser, $rh_ix ) = @_; |
| 1433 |
my $glade = $traverser->[Marpa::R2::Internal::ASF::Traverse::GLADE]; |
| 1434 |
my $symch_ix = |
| 1435 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::SYMCH_IX]; |
| 1436 |
my $symch = $glade->[Marpa::R2::Internal::Glade::SYMCHES]->[$symch_ix]; |
| 1437 |
my ( $rule_id, undef, @factorings ) = @{$symch}; |
| 1438 |
Marpa::R2::exception( |
| 1439 |
'$glade->rh_value($rh_ix) called for a token -- that is not allowed') |
| 1440 |
if $rule_id < 0; |
| 1441 |
my $factoring_ix = |
| 1442 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::FACTORING_IX]; |
| 1443 |
my $factoring = $factorings[$factoring_ix]; |
| 1444 |
return if $rh_ix > $#{$factoring}; |
| 1445 |
my $downglade_id = $factoring->[$rh_ix]; |
| 1446 |
my $memoized_value = $traverser->[Marpa::R2::Internal::ASF::Traverse::VALUES]->[$downglade_id]; |
| 1447 |
return $memoized_value if defined $memoized_value; |
| 1448 |
my $asf = $traverser->[Marpa::R2::Internal::ASF::Traverse::ASF]; |
| 1449 |
my $downglade = glade_obtain( $asf, $downglade_id ); |
| 1450 |
my $blessing = ref $traverser; |
| 1451 |
|
| 1452 |
# A shallow clone |
| 1453 |
my $child_traverser = bless [ @{$traverser} ], $blessing; |
| 1454 |
$child_traverser->[Marpa::R2::Internal::ASF::Traverse::GLADE] = |
| 1455 |
$downglade; |
| 1456 |
$child_traverser->[Marpa::R2::Internal::ASF::Traverse::SYMCH_IX] = 0; |
| 1457 |
$child_traverser->[Marpa::R2::Internal::ASF::Traverse::FACTORING_IX] = 0; |
| 1458 |
my $code = $traverser->[Marpa::R2::Internal::ASF::Traverse::CODE]; |
| 1459 |
my $value = $code->( |
| 1460 |
$child_traverser, |
| 1461 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::PER_TRAVERSE_OBJECT] |
| 1462 |
); |
| 1463 |
Marpa::R2::exception( |
| 1464 |
'The ASF traversing method returned undef -- that is not allowed') |
| 1465 |
if not defined $value; |
| 1466 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::VALUES]->[$downglade_id] |
| 1467 |
= $value; |
| 1468 |
return $value; |
| 1469 |
} ## end sub Marpa::R2::Internal::ASF::Traverse::rh_value |
| 1470 |
|
| 1471 |
sub Marpa::R2::Internal::ASF::Traverse::rh_values { |
| 1472 |
my ( $traverser ) = @_; |
| 1473 |
return map { Marpa::R2::Internal::ASF::Traverse::rh_value( $traverser, $_ ) } |
| 1474 |
0 .. Marpa::R2::Internal::ASF::Traverse::rh_length( $traverser ) - 1; |
| 1475 |
} |
| 1476 |
|
| 1477 |
sub Marpa::R2::Internal::ASF::Traverse::next_factoring { |
| 1478 |
my ($traverser) = @_; |
| 1479 |
my $glade = $traverser->[Marpa::R2::Internal::ASF::Traverse::GLADE]; |
| 1480 |
my $glade_id = $glade->[Marpa::R2::Internal::Glade::ID]; |
| 1481 |
my $asf = $traverser->[Marpa::R2::Internal::ASF::Traverse::ASF]; |
| 1482 |
my $symch_ix = $traverser->[Marpa::R2::Internal::ASF::Traverse::SYMCH_IX]; |
| 1483 |
my $last_factoring = |
| 1484 |
$asf->symch_factoring_count( $glade_id, $symch_ix ) - 1; |
| 1485 |
my $factoring_ix = |
| 1486 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::FACTORING_IX]; |
| 1487 |
return if $factoring_ix >= $last_factoring; |
| 1488 |
$factoring_ix++; |
| 1489 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::FACTORING_IX] = |
| 1490 |
$factoring_ix; |
| 1491 |
return $factoring_ix; |
| 1492 |
} ## end sub Marpa::R2::Internal::ASF::Traverse::next_factoring |
| 1493 |
|
| 1494 |
sub Marpa::R2::Internal::ASF::Traverse::next_symch { |
| 1495 |
my ($traverser) = @_; |
| 1496 |
my $glade = $traverser->[Marpa::R2::Internal::ASF::Traverse::GLADE]; |
| 1497 |
my $glade_id = $glade->[Marpa::R2::Internal::Glade::ID]; |
| 1498 |
my $asf = $traverser->[Marpa::R2::Internal::ASF::Traverse::ASF]; |
| 1499 |
my $symch_ix = $traverser->[Marpa::R2::Internal::ASF::Traverse::SYMCH_IX]; |
| 1500 |
my $last_symch = $asf->glade_symch_count( $glade_id ) - 1; |
| 1501 |
return if $symch_ix >= $last_symch; |
| 1502 |
$symch_ix++; |
| 1503 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::SYMCH_IX] = $symch_ix; |
| 1504 |
$traverser->[Marpa::R2::Internal::ASF::Traverse::FACTORING_IX] = 0; |
| 1505 |
return $symch_ix; |
| 1506 |
} ## end sub Marpa::R2::Internal::ASF::Traverse::next_symch |
| 1507 |
|
| 1508 |
sub Marpa::R2::Internal::ASF::Traverse::next { |
| 1509 |
my ($traverser) = @_; |
| 1510 |
return $traverser->next_factoring() // $traverser->next_symch(); |
| 1511 |
} |
| 1512 |
|
| 1513 |
# GLADE_SEEN is a local -- this is to silence warnings |
| 1514 |
our %GLADE_SEEN; |
| 1515 |
|
| 1516 |
sub form_choice { |
| 1517 |
my ( $parent_choice, $sub_choice ) = @_; |
| 1518 |
return $sub_choice if not defined $parent_choice; |
| 1519 |
return join q{.}, $parent_choice, $sub_choice; |
| 1520 |
} |
| 1521 |
|
| 1522 |
sub Marpa::R2::ASF::dump_glade { |
| 1523 |
my ( $asf, $glade_id, $parent_choice, $item_ix ) = @_; |
| 1524 |
if ( $GLADE_SEEN{$glade_id} ) { |
| 1525 |
return [ [0, $glade_id, "already displayed"] ]; |
| 1526 |
} |
| 1527 |
$GLADE_SEEN{$glade_id} = 1; |
| 1528 |
|
| 1529 |
my $grammar = $asf->grammar(); |
| 1530 |
my @lines = (); |
| 1531 |
my $symch_indent = 0; |
| 1532 |
|
| 1533 |
my $symch_count = $asf->glade_symch_count($glade_id); |
| 1534 |
my $symch_choice = $parent_choice; |
| 1535 |
if ( $symch_count > 1 ) { |
| 1536 |
$item_ix //= 0; |
| 1537 |
push @lines, |
| 1538 |
[ 0, undef, "Symbol #$item_ix " |
| 1539 |
. $grammar->symbol_display_form($asf->glade_symbol_id($glade_id)) |
| 1540 |
. " has $symch_count symches" ]; |
| 1541 |
$symch_indent += 2; |
| 1542 |
$symch_choice = form_choice( $parent_choice, $item_ix ); |
| 1543 |
} ## end if ( $symch_count > 1 ) |
| 1544 |
for ( my $symch_ix = 0; $symch_ix < $symch_count; $symch_ix++ ) { |
| 1545 |
my $current_choice = |
| 1546 |
$symch_count > 1 |
| 1547 |
? form_choice( $symch_choice, $symch_ix ) |
| 1548 |
: $symch_choice; |
| 1549 |
my $indent = $symch_indent; |
| 1550 |
if ( $symch_count > 1 ) { |
| 1551 |
push @lines, [ $symch_indent , undef, "Symch #$current_choice" ]; |
| 1552 |
} |
| 1553 |
my $rule_id = $asf->symch_rule_id( $glade_id, $symch_ix ); |
| 1554 |
if ( $rule_id >= 0 ) { |
| 1555 |
push @lines, |
| 1556 |
[ |
| 1557 |
$symch_indent, $glade_id, |
| 1558 |
"Rule $rule_id: " . $grammar->rule_show($rule_id) |
| 1559 |
]; |
| 1560 |
for my $line ( |
| 1561 |
@{ dump_factorings( |
| 1562 |
$asf, $glade_id, $symch_ix, $current_choice |
| 1563 |
) } |
| 1564 |
) |
| 1565 |
{ |
| 1566 |
my ( $line_indent, @rest_of_line ) = @{$line}; |
| 1567 |
push @lines, [ $line_indent + $symch_indent + 2, @rest_of_line ]; |
| 1568 |
} ## end for my $line ( dump_factorings( $asf, $glade_id, ...)) |
| 1569 |
} ## end if ( $rule_id >= 0 ) |
| 1570 |
else { |
| 1571 |
my $line = dump_terminal( $asf, $glade_id, $current_choice ); |
| 1572 |
my ( $line_indent, @rest_of_line ) = @{$line}; |
| 1573 |
push @lines, [ $line_indent + $symch_indent, @rest_of_line ]; |
| 1574 |
} ## end else [ if ( $rule_id >= 0 ) ] |
| 1575 |
} ## end for ( my $symch_ix = 0; $symch_ix < $symch_count; $symch_ix...) |
| 1576 |
return \@lines; |
| 1577 |
} |
| 1578 |
|
| 1579 |
# Show all the factorings of a SYMCH |
| 1580 |
sub dump_factorings { |
| 1581 |
my ( $asf, $glade_id, $symch_ix, $parent_choice ) = @_; |
| 1582 |
|
| 1583 |
my @lines; |
| 1584 |
my $factoring_count = $asf->symch_factoring_count( $glade_id, $symch_ix ); |
| 1585 |
for ( |
| 1586 |
my $factoring_ix = 0; |
| 1587 |
$factoring_ix < $factoring_count; |
| 1588 |
$factoring_ix++ |
| 1589 |
) |
| 1590 |
{ |
| 1591 |
my $indent = 0; |
| 1592 |
my $current_choice = $parent_choice; |
| 1593 |
if ( $factoring_count > 1 ) { |
| 1594 |
$indent = 2; |
| 1595 |
$current_choice = form_choice( $parent_choice, $factoring_ix ); |
| 1596 |
push @lines, [ 0, undef, "Factoring #$current_choice" ]; |
| 1597 |
} |
| 1598 |
my $symbol_count = |
| 1599 |
$asf->factoring_symbol_count( $glade_id, $symch_ix, |
| 1600 |
$factoring_ix ); |
| 1601 |
SYMBOL: for my $symbol_ix ( 0 .. $symbol_count - 1 ) { |
| 1602 |
my $downglade = |
| 1603 |
$asf->factor_downglade( $glade_id, $symch_ix, $factoring_ix, |
| 1604 |
$symbol_ix ); |
| 1605 |
for my $line ( |
| 1606 |
@{ $asf->dump_glade( $downglade, $current_choice, |
| 1607 |
$symbol_ix ) |
| 1608 |
} |
| 1609 |
) |
| 1610 |
{ |
| 1611 |
my ( $line_indent, @rest_of_line ) = @{$line}; |
| 1612 |
push @lines, [ $line_indent + $indent, @rest_of_line ]; |
| 1613 |
|
| 1614 |
} ## end for my $line ( @{ $asf->dump_glade( $downglade, ...)}) |
| 1615 |
} ## end SYMBOL: for my $symbol_ix ( 0 .. $symbol_count - 1 ) |
| 1616 |
} ## end for ( my $factoring_ix = 0; $factoring_ix < $factoring_count...) |
| 1617 |
return \@lines; |
| 1618 |
} ## end sub dump_factorings |
| 1619 |
|
| 1620 |
sub dump_terminal { |
| 1621 |
my ( $asf, $glade_id, $symch_ix, $parent_choice ) = @_; |
| 1622 |
|
| 1623 |
# There can only be one symbol in a terminal and therefore only one factoring |
| 1624 |
my $current_choice = $parent_choice; |
| 1625 |
my $literal = $asf->glade_literal($glade_id); |
| 1626 |
my $symbol_id = $asf->glade_symbol_id($glade_id); |
| 1627 |
my $grammar = $asf->grammar(); |
| 1628 |
my $display_form = $grammar->symbol_display_form($symbol_id); |
| 1629 |
return [0, $glade_id, qq{Symbol $display_form: "$literal"}]; |
| 1630 |
} ## end sub dump_terminal |
| 1631 |
|
| 1632 |
sub Marpa::R2::ASF::dump { |
| 1633 |
my ($asf) = @_; |
| 1634 |
my $peak = $asf->peak(); |
| 1635 |
local %GLADE_SEEN = (); ## no critic (Variables::ProhibitLocalVars) |
| 1636 |
my $lines = $asf->dump_glade( $peak ); |
| 1637 |
my $next_sequenced_id = 1; # one-based |
| 1638 |
my %sequenced_id = (); |
| 1639 |
$sequenced_id{$_} //= $next_sequenced_id++ for grep { defined } map { $_->[1] } @{$lines}; |
| 1640 |
my $text = q{}; |
| 1641 |
for my $line ( @{$lines}[ 1 .. $#$lines ] ) { |
| 1642 |
my ( $line_indent, $glade_id, $body ) = @{$line}; |
| 1643 |
$line_indent -= 2; |
| 1644 |
$text .= q{ } x $line_indent; |
| 1645 |
$text .= 'GL' . $sequenced_id{$glade_id} . q{ } if defined $glade_id; |
| 1646 |
$text .= "$body\n"; |
| 1647 |
} |
| 1648 |
return $text; |
| 1649 |
} ## end sub show |
| 1650 |
|
| 1651 |
sub Marpa::R2::ASF::show_nidsets { |
| 1652 |
my ($asf) = @_; |
| 1653 |
my $text = q{}; |
| 1654 |
my $nidsets = $asf->[Marpa::R2::Internal::ASF::NIDSET_BY_ID]; |
| 1655 |
for my $nidset ( grep {defined} @{$nidsets} ) { |
| 1656 |
$text .= $nidset->show() . "\n"; |
| 1657 |
} |
| 1658 |
return $text; |
| 1659 |
} ## end sub Marpa::R2::ASF::show_nidsets |
| 1660 |
|
| 1661 |
sub Marpa::R2::ASF::show_powersets { |
| 1662 |
my ($asf) = @_; |
| 1663 |
my $text = q{}; |
| 1664 |
my $powersets = $asf->[Marpa::R2::Internal::ASF::POWERSET_BY_ID]; |
| 1665 |
for my $powerset ( grep {defined} @{$powersets} ) { |
| 1666 |
$text .= $powerset->show() . "\n"; |
| 1667 |
} |
| 1668 |
return $text; |
| 1669 |
} ## end sub Marpa::R2::ASF::show_powersets |
| 1670 |
|
| 1671 |
sub dump_nook { |
| 1672 |
my ( $asf, $nook ) = @_; |
| 1673 |
my $slr = $asf->[Marpa::R2::Internal::ASF::SLR]; |
| 1674 |
my $or_nodes = $asf->[Marpa::R2::Internal::ASF::OR_NODES]; |
| 1675 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1676 |
my $or_node_id = $nook->[Marpa::R2::Internal::Nook::OR_NODE]; |
| 1677 |
my $and_node_count = scalar @{ $or_nodes->[$or_node_id] }; |
| 1678 |
my $text = 'Nook '; |
| 1679 |
my @text = (); |
| 1680 |
push @text, $nook->[Marpa::R2::Internal::Nook::IS_CAUSE] ? q{C} : q{-}; |
| 1681 |
push @text, |
| 1682 |
$nook->[Marpa::R2::Internal::Nook::IS_PREDECESSOR] ? q{P} : q{-}; |
| 1683 |
push @text, |
| 1684 |
$nook->[Marpa::R2::Internal::Nook::CAUSE_IS_EXPANDED] ? q{C+} : q{--}; |
| 1685 |
push @text, |
| 1686 |
$nook->[Marpa::R2::Internal::Nook::PREDECESSOR_IS_EXPANDED] |
| 1687 |
? q{P+} |
| 1688 |
: q{--}; |
| 1689 |
$text .= join q{ }, @text; |
| 1690 |
$text |
| 1691 |
.= ' @' |
| 1692 |
. $nook->[Marpa::R2::Internal::Nook::FIRST_CHOICE] . q{-} |
| 1693 |
. $nook->[Marpa::R2::Internal::Nook::LAST_CHOICE] |
| 1694 |
. qq{ of $and_node_count: }; |
| 1695 |
$text .= $recce->verbose_or_node($or_node_id); |
| 1696 |
return $text; |
| 1697 |
} ## end sub dump_nook |
| 1698 |
|
| 1699 |
# For debugging |
| 1700 |
sub dump_factoring_stack { |
| 1701 |
my ( $asf, $stack ) = @_; |
| 1702 |
my $text = q{}; |
| 1703 |
for ( my $stack_ix = 0; $stack_ix <= $#{$stack}; $stack_ix++ ) { |
| 1704 |
|
| 1705 |
# Nook already has newline at end |
| 1706 |
$text .= "$stack_ix: " . dump_nook( $asf, $stack->[$stack_ix] ); |
| 1707 |
} |
| 1708 |
return $text . "\n"; |
| 1709 |
} ## end sub dump_factoring_stack |
| 1710 |
|
| 1711 |
1; |
| 1712 |
|
| 1713 |
# vim: expandtab shiftwidth=4: |