| 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::Scanless::R; |
| 17 |
|
| 18 |
use 5.010001; |
| 19 |
use strict; |
| 20 |
use warnings; |
| 21 |
|
| 22 |
use vars qw($VERSION $STRING_VERSION); |
| 23 |
$VERSION = '14.000000'; |
| 24 |
$STRING_VERSION = $VERSION; |
| 25 |
## no critic(BuiltinFunctions::ProhibitStringyEval) |
| 26 |
$VERSION = eval $VERSION; |
| 27 |
## use critic |
| 28 |
|
| 29 |
package Marpa::R2::Internal::Scanless::R; |
| 30 |
|
| 31 |
use Scalar::Util 'blessed'; |
| 32 |
use English qw( -no_match_vars ); |
| 33 |
|
| 34 |
our $PACKAGE = 'Marpa::R2::Scanless::R'; |
| 35 |
our $TRACE_FILE_HANDLE; |
| 36 |
|
| 37 |
sub Marpa::R2::Scanless::R::last_completed_range { |
| 38 |
my ( $self, $symbol_name ) = @_; |
| 39 |
my ( $start, $length ) = $self->last_completed($symbol_name); |
| 40 |
return if not defined $start; |
| 41 |
my $end = $start + $length; |
| 42 |
return ( $start, $end ); |
| 43 |
} ## end sub Marpa::R2::Scanless::R::last_completed_range |
| 44 |
|
| 45 |
# Given a scanless |
| 46 |
# recognizer and a symbol, |
| 47 |
# return the start earley set |
| 48 |
# and length |
| 49 |
# of the last such symbol completed, |
| 50 |
# undef if there was none. |
| 51 |
sub Marpa::R2::Scanless::R::last_completed { |
| 52 |
my ( $slr, $symbol_name ) = @_; |
| 53 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 54 |
my $thick_g1_grammar = |
| 55 |
$slg->[Marpa::R2::Internal::Scanless::G::THICK_G1_GRAMMAR]; |
| 56 |
my $thick_g1_recce = |
| 57 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 58 |
my $thin_g1_recce = $thick_g1_recce->thin(); |
| 59 |
my $sought_rules = |
| 60 |
$slg->[Marpa::R2::Internal::Scanless::G::CACHE_RULEIDS_BY_LHS_NAME] |
| 61 |
->{$symbol_name}; |
| 62 |
if ( not defined $sought_rules ) { |
| 63 |
my $g1_tracer = $thick_g1_grammar->tracer(); |
| 64 |
my $thin_g1_grammar = $thick_g1_grammar->thin(); |
| 65 |
my $symbol_id = $g1_tracer->symbol_by_name($symbol_name); |
| 66 |
Marpa::R2::exception("Bad symbol in last_completed(): $symbol_name") |
| 67 |
if not defined $symbol_id; |
| 68 |
$sought_rules = |
| 69 |
$slg->[Marpa::R2::Internal::Scanless::G::CACHE_RULEIDS_BY_LHS_NAME] |
| 70 |
->{$symbol_name} = |
| 71 |
[ grep { $thin_g1_grammar->rule_lhs($_) == $symbol_id; } |
| 72 |
0 .. $thin_g1_grammar->highest_rule_id() ]; |
| 73 |
Marpa::R2::exception( |
| 74 |
"Looking for completion of non-existent rule lhs: $symbol_name") |
| 75 |
if not scalar @{$sought_rules}; |
| 76 |
} ## end if ( not defined $sought_rules ) |
| 77 |
my $latest_earley_set = $thin_g1_recce->latest_earley_set(); |
| 78 |
my $earley_set = $latest_earley_set; |
| 79 |
|
| 80 |
# Initialize to one past the end, so we can tell if there were no hits |
| 81 |
my $first_origin = $latest_earley_set + 1; |
| 82 |
EARLEY_SET: while ( $earley_set >= 0 ) { |
| 83 |
$thin_g1_recce->progress_report_start($earley_set); |
| 84 |
ITEM: while (1) { |
| 85 |
my ( $rule_id, $dot_position, $origin ) = |
| 86 |
$thin_g1_recce->progress_item(); |
| 87 |
last ITEM if not defined $rule_id; |
| 88 |
next ITEM if $dot_position != -1; |
| 89 |
next ITEM if not scalar grep { $_ == $rule_id } @{$sought_rules}; |
| 90 |
next ITEM if $origin >= $first_origin; |
| 91 |
$first_origin = $origin; |
| 92 |
} ## end ITEM: while (1) |
| 93 |
$thin_g1_recce->progress_report_finish(); |
| 94 |
last EARLEY_SET if $first_origin <= $latest_earley_set; |
| 95 |
$earley_set--; |
| 96 |
} ## end EARLEY_SET: while ( $earley_set >= 0 ) |
| 97 |
return if $earley_set < 0; |
| 98 |
return ( $first_origin, ( $earley_set - $first_origin ) ); |
| 99 |
} ## end sub Marpa::R2::Scanless::R::last_completed |
| 100 |
|
| 101 |
# Returns most input stream span for symbol. |
| 102 |
# If more than one ends at the same location, |
| 103 |
# returns the longest. |
| 104 |
# Returns under if there is no such span. |
| 105 |
# Other failure is thrown. |
| 106 |
sub Marpa::R2::Scanless::R::last_completed_span { |
| 107 |
my ( $slr, $symbol_name ) = @_; |
| 108 |
my ($g1_origin, $g1_span) = $slr->last_completed( $symbol_name ); |
| 109 |
return if not defined $g1_origin; |
| 110 |
my ($start_input_location) = $slr->g1_location_to_span($g1_origin + 1); |
| 111 |
my @end_span = $slr->g1_location_to_span($g1_origin + $g1_span); |
| 112 |
return ($start_input_location, ($end_span[0]+$end_span[1])-$start_input_location); |
| 113 |
} |
| 114 |
|
| 115 |
# In terms of earley sets. |
| 116 |
# Kept for backward compatibiity |
| 117 |
sub Marpa::R2::Scanless::R::range_to_string { |
| 118 |
my ( $self, $start_earley_set, $end_earley_set ) = @_; |
| 119 |
return $self->substring( $start_earley_set, |
| 120 |
$end_earley_set - $start_earley_set ); |
| 121 |
} |
| 122 |
|
| 123 |
# Not documented. Should I? |
| 124 |
sub Marpa::R2::Scanless::R::es_to_input_span { |
| 125 |
my ( $slr, $start_earley_set, $length_in_parse_locations ) = @_; |
| 126 |
return |
| 127 |
if not defined $start_earley_set |
| 128 |
or not defined $length_in_parse_locations; |
| 129 |
my $thick_g1_recce = |
| 130 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 131 |
my $thin_g1_recce = $thick_g1_recce->thin(); |
| 132 |
my $latest_earley_set = $thin_g1_recce->latest_earley_set(); |
| 133 |
|
| 134 |
my $earley_set_for_first_position = $start_earley_set + 1; |
| 135 |
my $earley_set_for_last_position = |
| 136 |
$start_earley_set + $length_in_parse_locations; |
| 137 |
|
| 138 |
die 'Error in $slr->substring(', |
| 139 |
"$start_earley_set, $length_in_parse_locations", '): ', |
| 140 |
"start ($start_earley_set) is at or after latest_earley_set ($latest_earley_set)" |
| 141 |
if $earley_set_for_first_position > $latest_earley_set; |
| 142 |
die 'Error in $slr->substring(', |
| 143 |
"$start_earley_set, $length_in_parse_locations", '): ', |
| 144 |
"end ( $start_earley_set + $length_in_parse_locations ) is after latest_earley_set ($latest_earley_set)" |
| 145 |
if $earley_set_for_last_position > $latest_earley_set; |
| 146 |
|
| 147 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 148 |
my ($first_start_position) = |
| 149 |
$thin_slr->span($earley_set_for_first_position); |
| 150 |
my ( $last_start_position, $last_length ) = |
| 151 |
$thin_slr->span($earley_set_for_last_position); |
| 152 |
my $length_in_characters = |
| 153 |
( $last_start_position + $last_length ) - $first_start_position; |
| 154 |
|
| 155 |
# Negative lengths are quite possible if the application has jumped around in |
| 156 |
# the input. |
| 157 |
$length_in_characters = 0 if $length_in_characters <= 0; |
| 158 |
return ( $first_start_position, $length_in_characters ); |
| 159 |
|
| 160 |
} ## end sub Marpa::R2::Scanless::R::es_to_input_span |
| 161 |
|
| 162 |
# Substring in terms of earley sets. |
| 163 |
# Necessary for the use of show_progress() |
| 164 |
# Given a scanless recognizer and |
| 165 |
# and two earley sets, return the input string |
| 166 |
sub Marpa::R2::Scanless::R::substring { |
| 167 |
my ( $slr, $start_earley_set, $length_in_parse_locations ) = @_; |
| 168 |
my ( $first_start_position, $length_in_characters ) = |
| 169 |
$slr->es_to_input_span( $start_earley_set, |
| 170 |
$length_in_parse_locations ); |
| 171 |
my $p_input = $slr->[Marpa::R2::Internal::Scanless::R::P_INPUT_STRING]; |
| 172 |
return substr ${$p_input}, $first_start_position, $length_in_characters; |
| 173 |
} ## end sub Marpa::R2::Scanless::R::substring |
| 174 |
|
| 175 |
sub Marpa::R2::Scanless::R::g1_location_to_span { |
| 176 |
my ( $self, $g1_location ) = @_; |
| 177 |
my $thin_self = $self->[Marpa::R2::Internal::Scanless::R::C]; |
| 178 |
return $thin_self->span($g1_location); |
| 179 |
} |
| 180 |
|
| 181 |
# Substring in terms of locations in the input stream |
| 182 |
# This is the one users will be most interested in. |
| 183 |
sub Marpa::R2::Scanless::R::literal { |
| 184 |
my ( $slr, $start_pos, $length ) = @_; |
| 185 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 186 |
return $thin_slr->substring( $start_pos, $length ); |
| 187 |
} ## end sub Marpa::R2::Scanless::R::literal |
| 188 |
|
| 189 |
sub Marpa::R2::Internal::Scanless::meta_recce { |
| 190 |
my ($hash_args) = @_; |
| 191 |
state $meta_grammar = Marpa::R2::Internal::Scanless::meta_grammar(); |
| 192 |
$hash_args->{grammar} = $meta_grammar; |
| 193 |
my $self = Marpa::R2::Scanless::R->new($hash_args); |
| 194 |
return $self; |
| 195 |
} ## end sub Marpa::R2::Internal::Scanless::meta_recce |
| 196 |
|
| 197 |
# For error messages, make it convenient to use an SLR |
| 198 |
sub Marpa::R2::Scanless::R::rule_show { |
| 199 |
my ( $slr, $rule_id ) = @_; |
| 200 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 201 |
return $slg->rule_show($rule_id); |
| 202 |
} |
| 203 |
|
| 204 |
sub Marpa::R2::Scanless::R::new { |
| 205 |
my ( $class, @args ) = @_; |
| 206 |
|
| 207 |
my $slr = []; |
| 208 |
bless $slr, $class; |
| 209 |
|
| 210 |
# Set SLIF (not NAIF) recognizer args to default |
| 211 |
$slr->[Marpa::R2::Internal::Scanless::R::EXHAUSTION_ACTION] = 'fatal'; |
| 212 |
$slr->[Marpa::R2::Internal::Scanless::R::REJECTION_ACTION] = 'fatal'; |
| 213 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_LEXERS] = 0; |
| 214 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_TERMINALS] = 0; |
| 215 |
|
| 216 |
my ($g1_recce_args, $flat_args) = |
| 217 |
Marpa::R2::Internal::Scanless::R::set( $slr, "new", @args ); |
| 218 |
my $too_many_earley_items = $g1_recce_args->{too_many_earley_items}; |
| 219 |
|
| 220 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 221 |
|
| 222 |
Marpa::R2::exception( |
| 223 |
qq{Marpa::R2::Scanless::R::new() called without a "grammar" argument} |
| 224 |
) if not defined $slg; |
| 225 |
|
| 226 |
my $slg_class = 'Marpa::R2::Scanless::G'; |
| 227 |
if ( not blessed $slg or not $slg->isa($slg_class) ) { |
| 228 |
my $ref_type = ref $slg; |
| 229 |
my $desc = $ref_type ? "a ref to $ref_type" : 'not a ref'; |
| 230 |
Marpa::R2::exception( |
| 231 |
qq{'grammar' named argument to new() is $desc\n}, |
| 232 |
" It should be a ref to $slg_class\n" ); |
| 233 |
} ## end if ( not blessed $slg or not $slg->isa($slg_class) ) |
| 234 |
|
| 235 |
my $thick_g1_grammar = |
| 236 |
$slg->[Marpa::R2::Internal::Scanless::G::THICK_G1_GRAMMAR]; |
| 237 |
|
| 238 |
my $trace_file_handle = $g1_recce_args->{trace_file_handle}; |
| 239 |
$trace_file_handle //= $thick_g1_grammar->[Marpa::R2::Internal::Grammar::TRACE_FILE_HANDLE] ; |
| 240 |
|
| 241 |
my $thick_g1_recce = |
| 242 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE] = bless [], |
| 243 |
'Marpa::R2::Recognizer'; |
| 244 |
|
| 245 |
local $Marpa::R2::Internal::TRACE_FH = |
| 246 |
$thick_g1_recce->[Marpa::R2::Internal::Recognizer::TRACE_FILE_HANDLE] = $trace_file_handle; |
| 247 |
|
| 248 |
$thick_g1_recce->[Marpa::R2::Internal::Recognizer::GRAMMAR] = $thick_g1_grammar; |
| 249 |
|
| 250 |
my $grammar_c = $thick_g1_grammar->[Marpa::R2::Internal::Grammar::C]; |
| 251 |
|
| 252 |
my $recce_c = $thick_g1_recce->[Marpa::R2::Internal::Recognizer::C] = |
| 253 |
Marpa::R2::Thin::R->new($grammar_c); |
| 254 |
if ( not defined $recce_c ) { |
| 255 |
Marpa::R2::exception( $grammar_c->error() ); |
| 256 |
} |
| 257 |
|
| 258 |
$recce_c->ruby_slippers_set(1); |
| 259 |
|
| 260 |
if ( defined $thick_g1_grammar->[Marpa::R2::Internal::Grammar::ACTION_OBJECT] |
| 261 |
or defined $thick_g1_grammar->[Marpa::R2::Internal::Grammar::ACTIONS] |
| 262 |
or not defined $thick_g1_grammar->[Marpa::R2::Internal::Grammar::INTERNAL] ) |
| 263 |
{ |
| 264 |
$thick_g1_recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE_SOURCE] = |
| 265 |
'legacy'; |
| 266 |
} ## end if ( defined $grammar->[...]) |
| 267 |
|
| 268 |
if ( defined( my $value = $g1_recce_args->{'leo'} ) ) { |
| 269 |
my $boolean = $value ? 1 : 0; |
| 270 |
$thick_g1_recce->use_leo_set($boolean); |
| 271 |
delete $g1_recce_args->{leo}; |
| 272 |
} |
| 273 |
|
| 274 |
$thick_g1_recce->[Marpa::R2::Internal::Recognizer::WARNINGS] = 1; |
| 275 |
$thick_g1_recce->[Marpa::R2::Internal::Recognizer::RANKING_METHOD] = 'none'; |
| 276 |
$thick_g1_recce->[Marpa::R2::Internal::Recognizer::MAX_PARSES] = 0; |
| 277 |
$thick_g1_recce->[Marpa::R2::Internal::Recognizer::TRACE_TERMINALS] = 0; |
| 278 |
|
| 279 |
# Position 0 is not used because 0 indicates an unvalued token. |
| 280 |
# Position 1 is reserved for undef. |
| 281 |
# Position 2 is reserved for literal tokens (used in SLIF). |
| 282 |
$thick_g1_recce->[Marpa::R2::Internal::Recognizer::TOKEN_VALUES] = [undef, undef, undef]; |
| 283 |
|
| 284 |
$thick_g1_recce->reset_evaluation(); |
| 285 |
|
| 286 |
my $thin_slr = |
| 287 |
Marpa::R2::Thin::SLR->new( $slg->[Marpa::R2::Internal::Scanless::G::C], |
| 288 |
$thick_g1_recce->thin() ); |
| 289 |
$thin_slr->earley_item_warning_threshold_set($too_many_earley_items) |
| 290 |
if defined $too_many_earley_items; |
| 291 |
$slr->[Marpa::R2::Internal::Scanless::R::C] = $thin_slr; |
| 292 |
$slr->[Marpa::R2::Internal::Scanless::R::EVENTS] = []; |
| 293 |
|
| 294 |
my $symbol_ids_by_event_name_and_type = |
| 295 |
$slg->[ |
| 296 |
Marpa::R2::Internal::Scanless::G::SYMBOL_IDS_BY_EVENT_NAME_AND_TYPE]; |
| 297 |
|
| 298 |
my $event_is_active_arg = $flat_args->{event_is_active} // {}; |
| 299 |
if (ref $event_is_active_arg ne 'HASH') { |
| 300 |
Marpa::R2::exception( 'event_is_active named argument must be ref to hash' ); |
| 301 |
} |
| 302 |
|
| 303 |
# Completion/nulled/prediction events are always initialized by |
| 304 |
# Libmarpa to 'on'. So here we need to override that if and only |
| 305 |
# if we in fact want to initialize them to 'off'. |
| 306 |
|
| 307 |
# Events are already initialized as described by |
| 308 |
# the DSL. Here we override that with the recce arg, if |
| 309 |
# necessary. |
| 310 |
|
| 311 |
EVENT: for my $event_name ( keys %{$event_is_active_arg} ) { |
| 312 |
|
| 313 |
my $is_active = $event_is_active_arg->{$event_name}; |
| 314 |
|
| 315 |
my $symbol_ids = |
| 316 |
$symbol_ids_by_event_name_and_type->{$event_name}->{lexeme}; |
| 317 |
$thin_slr->lexeme_event_activate( $_, $is_active ) |
| 318 |
for @{$symbol_ids}; |
| 319 |
my $lexer_rule_ids = |
| 320 |
$symbol_ids_by_event_name_and_type->{$event_name}->{discard}; |
| 321 |
$thin_slr->discard_event_activate( $_, $is_active ) |
| 322 |
for @{$lexer_rule_ids}; |
| 323 |
|
| 324 |
$symbol_ids = |
| 325 |
$symbol_ids_by_event_name_and_type->{$event_name}->{completion} |
| 326 |
// []; |
| 327 |
$recce_c->completion_symbol_activate( $_, $is_active ) |
| 328 |
for @{$symbol_ids}; |
| 329 |
$symbol_ids = |
| 330 |
$symbol_ids_by_event_name_and_type->{$event_name}->{nulled} // []; |
| 331 |
$recce_c->nulled_symbol_activate( $_, $is_active ) for @{$symbol_ids}; |
| 332 |
$symbol_ids = |
| 333 |
$symbol_ids_by_event_name_and_type->{$event_name}->{prediction} |
| 334 |
// []; |
| 335 |
$recce_c->prediction_symbol_activate( $_, $is_active ) |
| 336 |
for @{$symbol_ids}; |
| 337 |
} ## end EVENT: for my $event_name ( keys %{$event_is_active_arg} ) |
| 338 |
|
| 339 |
if ( not $recce_c->start_input() ) { |
| 340 |
my $error = $grammar_c->error(); |
| 341 |
Marpa::R2::exception( 'Recognizer start of input failed: ', $error ); |
| 342 |
} |
| 343 |
|
| 344 |
$thick_g1_recce->set($g1_recce_args); |
| 345 |
|
| 346 |
if ( $thick_g1_recce->[Marpa::R2::Internal::Recognizer::TRACE_TERMINALS] > 1 ) { |
| 347 |
my @terminals_expected = @{ $thick_g1_recce->terminals_expected() }; |
| 348 |
for my $terminal ( sort @terminals_expected ) { |
| 349 |
say {$Marpa::R2::Internal::TRACE_FH} |
| 350 |
qq{Expecting "$terminal" at earleme 0} |
| 351 |
or Marpa::R2::exception("Cannot print: $ERRNO"); |
| 352 |
} |
| 353 |
} ## end if ( $thick_g1_recce->[Marpa::R2::Internal::Recognizer::TRACE_TERMINALS...]) |
| 354 |
|
| 355 |
Marpa::R2::Internal::Scanless::convert_libmarpa_events($slr); |
| 356 |
|
| 357 |
return $slr; |
| 358 |
} ## end sub Marpa::R2::Scanless::R::new |
| 359 |
|
| 360 |
sub Marpa::R2::Scanless::R::set { |
| 361 |
my ( $slr, @args ) = @_; |
| 362 |
my $naif_recce_args = |
| 363 |
Marpa::R2::Internal::Scanless::R::set( $slr, "set", @args ); |
| 364 |
my $naif_recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 365 |
$naif_recce->set($naif_recce_args); |
| 366 |
return $slr; |
| 367 |
} ## end sub Marpa::R2::Scanless::R::set |
| 368 |
|
| 369 |
# The context flag indicates whether this set is called directly by the user |
| 370 |
# or is for series reset or the constructor. "Context" flags of this kind |
| 371 |
# are much decried practice, and for good reason, but in this case |
| 372 |
# I think it is justified. |
| 373 |
# This logic really needs to be all in one place, and so a flag |
| 374 |
# to trigger the minor differences needed by the various calling |
| 375 |
# contexts is a small price to pay. |
| 376 |
sub Marpa::R2::Internal::Scanless::R::set { |
| 377 |
|
| 378 |
my ( $slr, $method, @hash_ref_args ) = @_; |
| 379 |
|
| 380 |
# These NAIF recce args are allowed in all contexts |
| 381 |
state $common_naif_recce_args = { |
| 382 |
map { ( $_, 1 ); } |
| 383 |
qw(end max_parses semantics_package too_many_earley_items |
| 384 |
trace_actions trace_file_handle trace_terminals trace_values) |
| 385 |
}; |
| 386 |
state $common_slif_recce_args = |
| 387 |
{ map { ( $_, 1 ); } qw(trace_lexers rejection exhaustion) }; |
| 388 |
state $set_method_args = { |
| 389 |
map { ( $_, 1 ); } ( |
| 390 |
keys %{$common_slif_recce_args}, |
| 391 |
keys %{$common_naif_recce_args} |
| 392 |
) |
| 393 |
}; |
| 394 |
state $new_method_args = { |
| 395 |
map { ( $_, 1 ); } qw(grammar ranking_method event_is_active), |
| 396 |
keys %{$set_method_args} |
| 397 |
}; |
| 398 |
state $series_restart_method_args = { |
| 399 |
map { ( $_, 1 ); } ( |
| 400 |
keys %{$common_slif_recce_args}, |
| 401 |
keys %{$common_naif_recce_args} |
| 402 |
) |
| 403 |
}; |
| 404 |
|
| 405 |
for my $args (@hash_ref_args) { |
| 406 |
my $ref_type = ref $args; |
| 407 |
if ( not $ref_type ) { |
| 408 |
Marpa::R2::exception( q{$slr->} |
| 409 |
. $method |
| 410 |
. qq{() expects args as ref to HASH; got non-reference instead} |
| 411 |
); |
| 412 |
} ## end if ( not $ref_type ) |
| 413 |
if ( $ref_type ne 'HASH' ) { |
| 414 |
Marpa::R2::exception( q{$slr->} |
| 415 |
. $method |
| 416 |
. qq{() expects args as ref to HASH, got ref to $ref_type instead} |
| 417 |
); |
| 418 |
} ## end if ( $ref_type ne 'HASH' ) |
| 419 |
} ## end for my $args (@hash_ref_args) |
| 420 |
|
| 421 |
my %flat_args = (); |
| 422 |
for my $hash_ref (@hash_ref_args) { |
| 423 |
ARG: for my $arg_name ( keys %{$hash_ref} ) { |
| 424 |
$flat_args{$arg_name} = $hash_ref->{$arg_name}; |
| 425 |
} |
| 426 |
} |
| 427 |
my $ok_args = $set_method_args; |
| 428 |
$ok_args = $new_method_args if $method eq 'new'; |
| 429 |
$ok_args = $series_restart_method_args if $method eq 'series_restart'; |
| 430 |
my @bad_args = grep { not $ok_args->{$_} } keys %flat_args; |
| 431 |
if ( scalar @bad_args ) { |
| 432 |
Marpa::R2::exception( |
| 433 |
q{Bad named argument(s) to $slr->} |
| 434 |
. $method |
| 435 |
. q{() method: } |
| 436 |
. join q{ }, |
| 437 |
@bad_args |
| 438 |
); |
| 439 |
} ## end if ( scalar @bad_args ) |
| 440 |
|
| 441 |
# Special SLIF (not NAIF) recce arg processing goes here |
| 442 |
if ( exists $flat_args{'exhaustion'} ) { |
| 443 |
|
| 444 |
state $exhaustion_actions = { map { ( $_, 0 ) } qw(fatal event) }; |
| 445 |
my $value = $flat_args{'exhaustion'} // 'undefined'; |
| 446 |
Marpa::R2::exception( |
| 447 |
qq{'exhaustion' named arg value is $value (should be one of }, |
| 448 |
( join q{, }, |
| 449 |
map { q{'} . $_ . q{'} } keys %{$exhaustion_actions} |
| 450 |
), |
| 451 |
')' |
| 452 |
) if not exists $exhaustion_actions->{$value}; |
| 453 |
$slr->[Marpa::R2::Internal::Scanless::R::EXHAUSTION_ACTION] = $value; |
| 454 |
|
| 455 |
} ## end if ( exists $flat_args{'exhaustion'} ) |
| 456 |
|
| 457 |
# Special SLIF (not NAIF) recce arg processing goes here |
| 458 |
if ( exists $flat_args{'rejection'} ) { |
| 459 |
|
| 460 |
state $rejection_actions = { map { ( $_, 0 ) } qw(fatal event) }; |
| 461 |
my $value = $flat_args{'rejection'} // 'undefined'; |
| 462 |
Marpa::R2::exception( |
| 463 |
qq{'rejection' named arg value is $value (should be one of }, |
| 464 |
( join q{, }, |
| 465 |
map { q{'} . $_ . q{'} } keys %{$rejection_actions} |
| 466 |
), |
| 467 |
')' |
| 468 |
) if not exists $rejection_actions->{$value}; |
| 469 |
$slr->[Marpa::R2::Internal::Scanless::R::REJECTION_ACTION] = $value; |
| 470 |
|
| 471 |
} ## end if ( exists $flat_args{'rejection'} ) |
| 472 |
|
| 473 |
# A bit hack-ish, but some named args are copies straight to an member of |
| 474 |
# the Scanless::R class, so this maps named args to the index of the array |
| 475 |
# that holds the members. |
| 476 |
state $copy_arg_to_index = { |
| 477 |
trace_file_handle => |
| 478 |
Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE, |
| 479 |
trace_lexers => Marpa::R2::Internal::Scanless::R::TRACE_LEXERS, |
| 480 |
trace_terminals => Marpa::R2::Internal::Scanless::R::TRACE_TERMINALS, |
| 481 |
grammar => Marpa::R2::Internal::Scanless::R::GRAMMAR, |
| 482 |
}; |
| 483 |
|
| 484 |
ARG: for my $arg_name ( keys %flat_args ) { |
| 485 |
my $index = $copy_arg_to_index->{$arg_name}; |
| 486 |
next ARG if not defined $index; |
| 487 |
my $value = $flat_args{$arg_name}; |
| 488 |
$slr->[$index] = $value; |
| 489 |
} ## end ARG: for my $arg_name ( keys %flat_args ) |
| 490 |
|
| 491 |
# Normalize trace levels to numbers |
| 492 |
for my $trace_level_arg ( |
| 493 |
Marpa::R2::Internal::Scanless::R::TRACE_TERMINALS, |
| 494 |
Marpa::R2::Internal::Scanless::R::TRACE_LEXERS |
| 495 |
) |
| 496 |
{ |
| 497 |
$slr->[$trace_level_arg] = 0 |
| 498 |
if |
| 499 |
not Scalar::Util::looks_like_number( $slr->[$trace_level_arg] ); |
| 500 |
} ## end for my $trace_level_arg ( ...) |
| 501 |
|
| 502 |
# Trace file handle can never be undefined |
| 503 |
if (not defined $slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE] ) |
| 504 |
{ |
| 505 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 506 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE] = |
| 507 |
$slg->[Marpa::R2::Internal::Scanless::G::TRACE_FILE_HANDLE]; |
| 508 |
} ## end if ( not defined $slr->[...]) |
| 509 |
|
| 510 |
# These NAIF recce args, when applicable, are simply copies of the the |
| 511 |
# SLIF args of the same name |
| 512 |
state $copyable_naif_recce_args = { |
| 513 |
map { ( $_, 1 ); } |
| 514 |
qw(end max_parses semantics_package too_many_earley_items ranking_method |
| 515 |
trace_actions trace_file_handle trace_terminals trace_values) |
| 516 |
}; |
| 517 |
|
| 518 |
# Prune flat args of all those named args which are NOT to be copied |
| 519 |
# into the NAIF recce args |
| 520 |
my %g1_recce_args = (); |
| 521 |
for my $arg_name ( grep { $copyable_naif_recce_args->{$_} } |
| 522 |
keys %flat_args ) |
| 523 |
{ |
| 524 |
$g1_recce_args{$arg_name} = $flat_args{$arg_name}; |
| 525 |
} |
| 526 |
|
| 527 |
return \%g1_recce_args, \%flat_args; |
| 528 |
|
| 529 |
} ## end sub Marpa::R2::Internal::Scanless::R::set |
| 530 |
|
| 531 |
sub Marpa::R2::Scanless::R::thin { |
| 532 |
return $_[0]->[Marpa::R2::Internal::Scanless::R::C]; |
| 533 |
} |
| 534 |
|
| 535 |
sub Marpa::R2::Scanless::R::trace { |
| 536 |
my ( $self, $level ) = @_; |
| 537 |
my $thin_slr = $self->[Marpa::R2::Internal::Scanless::R::C]; |
| 538 |
$level //= 1; |
| 539 |
return $thin_slr->trace($level); |
| 540 |
} ## end sub Marpa::R2::Scanless::R::trace |
| 541 |
|
| 542 |
sub Marpa::R2::Scanless::R::error { |
| 543 |
my ($self) = @_; |
| 544 |
return $self->[Marpa::R2::Internal::Scanless::R::READ_STRING_ERROR]; |
| 545 |
} |
| 546 |
|
| 547 |
sub Marpa::R2::Scanless::R::read { |
| 548 |
my ( $self, $p_string, $start_pos, $length ) = @_; |
| 549 |
|
| 550 |
$start_pos //= 0; |
| 551 |
$length //= -1; |
| 552 |
Marpa::R2::exception( |
| 553 |
"Multiple read()'s tried on a scannerless recognizer\n", |
| 554 |
' Currently the string cannot be changed once set' |
| 555 |
) if defined $self->[Marpa::R2::Internal::Scanless::R::P_INPUT_STRING]; |
| 556 |
|
| 557 |
if ( ( my $ref_type = ref $p_string ) ne 'SCALAR' ) { |
| 558 |
my $desc = $ref_type ? "a ref to $ref_type" : 'not a ref'; |
| 559 |
Marpa::R2::exception( |
| 560 |
qq{Arg to Marpa::R2::Scanless::R::read() is $desc\n}, |
| 561 |
' It should be a ref to scalar' ); |
| 562 |
} ## end if ( ( my $ref_type = ref $p_string ) ne 'SCALAR' ) |
| 563 |
|
| 564 |
if ( not defined ${$p_string} ) { |
| 565 |
Marpa::R2::exception( |
| 566 |
qq{Arg to Marpa::R2::Scanless::R::read() is a ref to an undef\n}, |
| 567 |
' It should be a ref to a defined scalar' ); |
| 568 |
} ## end if ( ( my $ref_type = ref $p_string ) ne 'SCALAR' ) |
| 569 |
|
| 570 |
$self->[Marpa::R2::Internal::Scanless::R::P_INPUT_STRING] = $p_string; |
| 571 |
|
| 572 |
my $thin_slr = $self->[Marpa::R2::Internal::Scanless::R::C]; |
| 573 |
my $trace_terminals = $self->[Marpa::R2::Internal::Scanless::R::TRACE_TERMINALS]; |
| 574 |
my $trace_lexers = $self->[Marpa::R2::Internal::Scanless::R::TRACE_LEXERS]; |
| 575 |
$thin_slr->trace_terminals($trace_terminals) if $trace_terminals; |
| 576 |
$thin_slr->trace_lexers($trace_lexers) if $trace_lexers; |
| 577 |
|
| 578 |
$thin_slr->string_set($p_string); |
| 579 |
|
| 580 |
return 0 if @{ $self->[Marpa::R2::Internal::Scanless::R::EVENTS] }; |
| 581 |
|
| 582 |
return $self->resume( $start_pos, $length ); |
| 583 |
|
| 584 |
} ## end sub Marpa::R2::Scanless::R::read |
| 585 |
|
| 586 |
my $libmarpa_trace_event_handlers = { |
| 587 |
|
| 588 |
'g1 accepted lexeme' => sub { |
| 589 |
my ( $slr, $event ) = @_; |
| 590 |
my ( undef, undef, $lexeme_start_pos, $lexeme_end_pos, $g1_lexeme) |
| 591 |
= @{$event}; |
| 592 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 593 |
my $raw_token_value = |
| 594 |
$thin_slr->substring( $lexeme_start_pos, |
| 595 |
$lexeme_end_pos - $lexeme_start_pos ); |
| 596 |
my $trace_file_handle = |
| 597 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 598 |
my $thick_g1_recce = |
| 599 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 600 |
my $thick_g1_grammar = $thick_g1_recce->grammar(); |
| 601 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 602 |
say {$trace_file_handle} qq{Accepted lexeme }, |
| 603 |
input_range_describe( $slr, $lexeme_start_pos, |
| 604 |
$lexeme_end_pos - 1 ), |
| 605 |
q{ e}, $slr->current_g1_location(), |
| 606 |
q{: }, |
| 607 |
$thick_g1_grammar->symbol_in_display_form($g1_lexeme), |
| 608 |
qq{; value="$raw_token_value"} |
| 609 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 610 |
}, |
| 611 |
'rejected lexeme' => sub { |
| 612 |
my ( $slr, $event ) = @_; |
| 613 |
# Necessary to check, because this one can be returned when not tracing |
| 614 |
return if not $slr->[Marpa::R2::Internal::Scanless::R::TRACE_TERMINALS]; |
| 615 |
my ( undef, undef, $lexeme_start_pos, $lexeme_end_pos, $g1_lexeme) |
| 616 |
= @{$event}; |
| 617 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 618 |
my $raw_token_value = |
| 619 |
$thin_slr->substring( $lexeme_start_pos, |
| 620 |
$lexeme_end_pos - $lexeme_start_pos ); |
| 621 |
my $trace_file_handle = |
| 622 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 623 |
my $thick_g1_recce = |
| 624 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 625 |
my $thick_g1_grammar = $thick_g1_recce->grammar(); |
| 626 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 627 |
say {$trace_file_handle} qq{Rejected lexeme }, |
| 628 |
input_range_describe( $slr, $lexeme_start_pos, |
| 629 |
$lexeme_end_pos - 1 ), |
| 630 |
q{: }, |
| 631 |
$thick_g1_grammar->symbol_in_display_form($g1_lexeme), |
| 632 |
qq{; value="$raw_token_value"} |
| 633 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 634 |
}, |
| 635 |
'expected lexeme' => sub { |
| 636 |
my ( $slr, $event ) = @_; |
| 637 |
# Necessary to check, because this one can be returned when not tracing |
| 638 |
return if not $slr->[Marpa::R2::Internal::Scanless::R::TRACE_TERMINALS]; |
| 639 |
my ( undef, undef, $position, $g1_lexeme, $assertion_id) |
| 640 |
= @{$event}; |
| 641 |
my ( $line, $column ) = $slr->line_column($position); |
| 642 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 643 |
my $trace_file_handle = |
| 644 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 645 |
my $thick_g1_recce = |
| 646 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 647 |
my $thick_g1_grammar = $thick_g1_recce->grammar(); |
| 648 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 649 |
say {$trace_file_handle} qq{Expected lexeme }, |
| 650 |
$thick_g1_grammar->symbol_in_display_form($g1_lexeme), |
| 651 |
" at line $line, column $column; assertion ID = $assertion_id" |
| 652 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 653 |
}, |
| 654 |
'outprioritized lexeme' => sub { |
| 655 |
my ( $slr, $event ) = @_; |
| 656 |
my ( undef, undef, $lexeme_start_pos, $lexeme_end_pos, $g1_lexeme, |
| 657 |
$lexeme_priority, $required_priority ) |
| 658 |
= @{$event}; |
| 659 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 660 |
my $raw_token_value = |
| 661 |
$thin_slr->substring( $lexeme_start_pos, |
| 662 |
$lexeme_end_pos - $lexeme_start_pos ); |
| 663 |
my $trace_file_handle = |
| 664 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 665 |
my $thick_g1_recce = |
| 666 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 667 |
my $thick_g1_grammar = $thick_g1_recce->grammar(); |
| 668 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 669 |
say {$trace_file_handle} |
| 670 |
qq{Outprioritized lexeme }, |
| 671 |
input_range_describe( $slr, $lexeme_start_pos, |
| 672 |
$lexeme_end_pos - 1 ), |
| 673 |
q{: }, |
| 674 |
$thick_g1_grammar->symbol_in_display_form($g1_lexeme), |
| 675 |
qq{; value="$raw_token_value"; }, |
| 676 |
qq{priority was $lexeme_priority, but $required_priority was required} |
| 677 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 678 |
}, |
| 679 |
'g1 duplicate lexeme' => sub { |
| 680 |
my ( $slr, $event ) = @_; |
| 681 |
my ( undef, undef, $lexeme_start_pos, $lexeme_end_pos, $g1_lexeme ) = |
| 682 |
@{$event}; |
| 683 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 684 |
my $raw_token_value = |
| 685 |
$thin_slr->substring( $lexeme_start_pos, |
| 686 |
$lexeme_end_pos - $lexeme_start_pos ); |
| 687 |
my $trace_file_handle = |
| 688 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 689 |
my $thick_g1_recce = |
| 690 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 691 |
my $thick_g1_grammar = $thick_g1_recce->grammar(); |
| 692 |
say {$trace_file_handle} |
| 693 |
'Rejected as duplicate lexeme ', |
| 694 |
input_range_describe( $slr, $lexeme_start_pos, |
| 695 |
$lexeme_end_pos - 1 ), |
| 696 |
q{: }, |
| 697 |
$thick_g1_grammar->symbol_in_display_form($g1_lexeme), |
| 698 |
qq{; value="$raw_token_value"} |
| 699 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 700 |
}, |
| 701 |
'g1 attempting lexeme' => sub { |
| 702 |
my ( $slr, $event ) = @_; |
| 703 |
my ( undef, undef, $lexeme_start_pos, $lexeme_end_pos, $g1_lexeme ) = |
| 704 |
@{$event}; |
| 705 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 706 |
my $raw_token_value = |
| 707 |
$thin_slr->substring( $lexeme_start_pos, |
| 708 |
$lexeme_end_pos - $lexeme_start_pos ); |
| 709 |
my $trace_file_handle = |
| 710 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 711 |
my $thick_g1_recce = |
| 712 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 713 |
my $thick_g1_grammar = $thick_g1_recce->grammar(); |
| 714 |
say {$trace_file_handle} |
| 715 |
'Attempting to read lexeme ', |
| 716 |
input_range_describe( $slr, $lexeme_start_pos, |
| 717 |
$lexeme_end_pos - 1 ), |
| 718 |
q{ e}, $slr->current_g1_location(), |
| 719 |
q{: }, |
| 720 |
$thick_g1_grammar->symbol_in_display_form($g1_lexeme), |
| 721 |
qq{; value="$raw_token_value"} |
| 722 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 723 |
}, |
| 724 |
'lexer reading codepoint' => sub { |
| 725 |
my ( $slr, $event ) = @_; |
| 726 |
my ( undef, undef, $codepoint, $position, ) = @{$event}; |
| 727 |
my $char = chr $codepoint; |
| 728 |
my @char_desc = (); |
| 729 |
push @char_desc, qq{"$char"} |
| 730 |
if $char =~ /[\p{IsGraph}]/xms; |
| 731 |
push @char_desc, ( sprintf '0x%04x', $codepoint ); |
| 732 |
my $char_desc = join q{ }, @char_desc; |
| 733 |
my ( $line, $column ) = $slr->line_column($position); |
| 734 |
my $trace_file_handle = |
| 735 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 736 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 737 |
say {$trace_file_handle} |
| 738 |
qq{Reading codepoint $char_desc at line $line, column $column} |
| 739 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 740 |
}, |
| 741 |
'lexer accepted codepoint' => sub { |
| 742 |
my ( $slr, $event ) = @_; |
| 743 |
my ( undef, undef, $codepoint, $position, $token_id ) = |
| 744 |
@{$event}; |
| 745 |
my $char = chr $codepoint; |
| 746 |
my @char_desc = (); |
| 747 |
push @char_desc, qq{"$char"} |
| 748 |
if $char =~ /[\p{IsGraph}]/xms; |
| 749 |
push @char_desc, ( sprintf '0x%04x', $codepoint ); |
| 750 |
my $char_desc = join q{ }, @char_desc; |
| 751 |
my $grammar = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 752 |
my $thick_lex_grammar = |
| 753 |
$grammar->[Marpa::R2::Internal::Scanless::G::THICK_LEX_GRAMMARS] |
| 754 |
->[0]; |
| 755 |
my $symbol_in_display_form = |
| 756 |
$thick_lex_grammar->symbol_in_display_form($token_id), |
| 757 |
my ( $line, $column ) = $slr->line_column($position); |
| 758 |
my $trace_file_handle = |
| 759 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 760 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 761 |
say {$trace_file_handle} |
| 762 |
qq{Codepoint $char_desc accepted as $symbol_in_display_form at line $line, column $column} |
| 763 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 764 |
}, |
| 765 |
'lexer rejected codepoint' => sub { |
| 766 |
my ( $slr, $event ) = @_; |
| 767 |
my ( undef, undef, $codepoint, $position, $token_id ) = |
| 768 |
@{$event}; |
| 769 |
my $char = chr $codepoint; |
| 770 |
my @char_desc = (); |
| 771 |
push @char_desc, qq{"$char"} |
| 772 |
if $char =~ /[\p{IsGraph}]/xms; |
| 773 |
push @char_desc, ( sprintf '0x%04x', $codepoint ); |
| 774 |
my $char_desc = join q{ }, @char_desc; |
| 775 |
my $grammar = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 776 |
my $thick_lex_grammar = |
| 777 |
$grammar->[Marpa::R2::Internal::Scanless::G::THICK_LEX_GRAMMARS] |
| 778 |
->[0]; |
| 779 |
my $symbol_in_display_form = |
| 780 |
$thick_lex_grammar->symbol_in_display_form($token_id), |
| 781 |
my ( $line, $column ) = $slr->line_column($position); |
| 782 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 783 |
my $trace_file_handle = |
| 784 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 785 |
say {$trace_file_handle} |
| 786 |
qq{Codepoint $char_desc rejected as $symbol_in_display_form at line $line, column $column} |
| 787 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 788 |
}, |
| 789 |
'lexer restarted recognizer' => sub { |
| 790 |
my ( $slr, $event ) = @_; |
| 791 |
my ( undef, undef, $position ) = @{$event}; |
| 792 |
my ( $line, $column ) = $slr->line_column($position); |
| 793 |
my $trace_file_handle = |
| 794 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 795 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 796 |
say {$trace_file_handle} |
| 797 |
qq{Restarted recognizer at line $line, column $column} |
| 798 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 799 |
}, |
| 800 |
'discarded lexeme' => sub { |
| 801 |
my ( $slr, $event ) = @_; |
| 802 |
my ( undef, undef, $lex_rule_id, $start, $end ) = |
| 803 |
@{$event}; |
| 804 |
my $grammar = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 805 |
my $thick_lex_grammar = |
| 806 |
$grammar->[Marpa::R2::Internal::Scanless::G::THICK_LEX_GRAMMARS] |
| 807 |
->[0]; |
| 808 |
my $grammar_c = $thick_lex_grammar->[Marpa::R2::Internal::Grammar::C]; |
| 809 |
my $rule_length = $grammar_c->rule_length($lex_rule_id); |
| 810 |
my @rhs_ids = |
| 811 |
map { $grammar_c->rule_rhs( $lex_rule_id, $_ ) } |
| 812 |
( 0 .. $rule_length - 1 ); |
| 813 |
my @rhs = |
| 814 |
map { $thick_lex_grammar->symbol_in_display_form($_) } @rhs_ids; |
| 815 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 816 |
my $trace_file_handle = |
| 817 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 818 |
say {$trace_file_handle} qq{Discarded lexeme }, |
| 819 |
input_range_describe( $slr, $start, $end - 1 ), q{: }, join q{ }, |
| 820 |
@rhs |
| 821 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 822 |
}, |
| 823 |
'g1 pausing before lexeme' => sub { |
| 824 |
my ( $slr, $event ) = @_; |
| 825 |
my ( undef, undef, $start, $end, $lexeme_id ) = @{$event}; |
| 826 |
my $thick_g1_recce = |
| 827 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 828 |
my $thick_g1_grammar = $thick_g1_recce->grammar(); |
| 829 |
my $lexeme_name = |
| 830 |
$thick_g1_grammar->symbol_in_display_form($lexeme_id); |
| 831 |
my $trace_file_handle = |
| 832 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 833 |
say {$trace_file_handle} 'Paused before lexeme ', |
| 834 |
input_range_describe( $slr, $start, $end - 1 ), ": $lexeme_name" |
| 835 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 836 |
}, |
| 837 |
'g1 pausing after lexeme' => sub { |
| 838 |
my ( $slr, $event ) = @_; |
| 839 |
my ( undef, undef, $start, $end, $lexeme_id ) = @{$event}; |
| 840 |
my $thick_g1_recce = |
| 841 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 842 |
my $thick_g1_grammar = $thick_g1_recce->grammar(); |
| 843 |
my $lexeme_name = |
| 844 |
$thick_g1_grammar->symbol_in_display_form($lexeme_id); |
| 845 |
my $trace_file_handle = |
| 846 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 847 |
say {$trace_file_handle} 'Paused after lexeme ', |
| 848 |
input_range_describe( $slr, $start, $end - 1 ), ": $lexeme_name" |
| 849 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 850 |
}, |
| 851 |
'ignored lexeme' => sub { |
| 852 |
my ( $slr, $event ) = @_; |
| 853 |
my ( undef, undef, $g1_symbol_id, $start, $end ) = @{$event}; |
| 854 |
my $thick_g1_recce = |
| 855 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 856 |
my $thick_g1_grammar = $thick_g1_recce->grammar(); |
| 857 |
my $lexeme_name = |
| 858 |
$thick_g1_grammar->symbol_in_display_form($g1_symbol_id); |
| 859 |
my $trace_file_handle = |
| 860 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 861 |
say {$trace_file_handle} 'Ignored lexeme ', |
| 862 |
input_range_describe( $slr, $start, $end - 1 ), ": $lexeme_name" |
| 863 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 864 |
}, |
| 865 |
}; |
| 866 |
|
| 867 |
my $libmarpa_event_handlers = { |
| 868 |
q{'trace} => sub { |
| 869 |
my ( $slr, $event ) = @_; |
| 870 |
my $handler = $libmarpa_trace_event_handlers->{ $event->[1] }; |
| 871 |
if ( defined $handler ) { |
| 872 |
$handler->( $slr, $event ); |
| 873 |
} |
| 874 |
else { |
| 875 |
my $trace_file_handle = |
| 876 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 877 |
say {$trace_file_handle} join q{ }, qw(Trace event:), @{$event}[1 .. $#{$event}] |
| 878 |
or Marpa::R2::exception("Could not say(): $ERRNO"); |
| 879 |
} ## end else [ if ( defined $handler ) ] |
| 880 |
return 0; |
| 881 |
}, |
| 882 |
|
| 883 |
'symbol completed' => sub { |
| 884 |
my ( $slr, $event ) = @_; |
| 885 |
my ( undef, $completed_symbol_id ) = @{$event}; |
| 886 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 887 |
my $completion_event_by_id = |
| 888 |
$slg->[Marpa::R2::Internal::Scanless::G::COMPLETION_EVENT_BY_ID]; |
| 889 |
push @{ $slr->[Marpa::R2::Internal::Scanless::R::EVENTS] }, |
| 890 |
[ $completion_event_by_id->[$completed_symbol_id] ]; |
| 891 |
return 1; |
| 892 |
}, |
| 893 |
|
| 894 |
'symbol nulled' => sub { |
| 895 |
my ( $slr, $event ) = @_; |
| 896 |
my ( undef, $nulled_symbol_id ) = @{$event}; |
| 897 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 898 |
my $nulled_event_by_id = |
| 899 |
$slg->[Marpa::R2::Internal::Scanless::G::NULLED_EVENT_BY_ID]; |
| 900 |
push @{ $slr->[Marpa::R2::Internal::Scanless::R::EVENTS] }, |
| 901 |
[ $nulled_event_by_id->[$nulled_symbol_id] ]; |
| 902 |
return 1; |
| 903 |
}, |
| 904 |
|
| 905 |
'symbol predicted' => sub { |
| 906 |
my ( $slr, $event ) = @_; |
| 907 |
my ( undef, $predicted_symbol_id ) = @{$event}; |
| 908 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 909 |
my $prediction_event_by_id = |
| 910 |
$slg->[Marpa::R2::Internal::Scanless::G::PREDICTION_EVENT_BY_ID]; |
| 911 |
push @{ $slr->[Marpa::R2::Internal::Scanless::R::EVENTS] }, |
| 912 |
[ $prediction_event_by_id->[$predicted_symbol_id] ]; |
| 913 |
return 1; |
| 914 |
}, |
| 915 |
|
| 916 |
# 'after lexeme' is same -- copied over below |
| 917 |
'before lexeme' => sub { |
| 918 |
my ( $slr, $event ) = @_; |
| 919 |
my ( undef, $lexeme_id ) = @{$event}; |
| 920 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 921 |
my $lexeme_event = |
| 922 |
$slg->[Marpa::R2::Internal::Scanless::G::LEXEME_EVENT_BY_ID] |
| 923 |
->[$lexeme_id]; |
| 924 |
push @{ $slr->[Marpa::R2::Internal::Scanless::R::EVENTS] }, |
| 925 |
[$lexeme_event] |
| 926 |
if defined $lexeme_event; |
| 927 |
return 1; |
| 928 |
}, |
| 929 |
|
| 930 |
'discarded lexeme' => sub { |
| 931 |
my ( $slr, $event ) = @_; |
| 932 |
my ( undef, $rule_id, @other_data) = @{$event}; |
| 933 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 934 |
my $lexeme_event = |
| 935 |
$slg->[Marpa::R2::Internal::Scanless::G::DISCARD_EVENT_BY_LEXER_RULE] |
| 936 |
->[$rule_id]; |
| 937 |
push @{ $slr->[Marpa::R2::Internal::Scanless::R::EVENTS] }, |
| 938 |
[$lexeme_event, @other_data] |
| 939 |
if defined $lexeme_event; |
| 940 |
return 1; |
| 941 |
}, |
| 942 |
|
| 943 |
'unknown g1 event' => sub { |
| 944 |
my ( $slr, $event ) = @_; |
| 945 |
Marpa::R2::exception( ( join q{ }, 'Unknown event:', @{$event} ) ); |
| 946 |
return 0; |
| 947 |
}, |
| 948 |
|
| 949 |
'no acceptable input' => sub { |
| 950 |
## Do nothing at this point |
| 951 |
return 0; |
| 952 |
}, |
| 953 |
}; |
| 954 |
|
| 955 |
$libmarpa_event_handlers->{'after lexeme'} = $libmarpa_event_handlers->{'before lexeme'}; |
| 956 |
|
| 957 |
# Return 1 if internal scanning should pause |
| 958 |
sub Marpa::R2::Internal::Scanless::convert_libmarpa_events { |
| 959 |
my ($slr) = @_; |
| 960 |
my $pause = 0; |
| 961 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 962 |
EVENT: for my $event ( $thin_slr->events() ) { |
| 963 |
my ($event_type) = @{$event}; |
| 964 |
my $handler = $libmarpa_event_handlers->{$event_type}; |
| 965 |
Marpa::R2::exception( ( join q{ }, 'Unknown event:', @{$event} ) ) |
| 966 |
if not defined $handler; |
| 967 |
$pause = 1 if $handler->( $slr, $event ); |
| 968 |
} ## end EVENT: for my $event ( $thin_slr->events() ) |
| 969 |
return $pause; |
| 970 |
} ## end sub Marpa::R2::Internal::Scanless::convert_libmarpa_events |
| 971 |
|
| 972 |
sub Marpa::R2::Scanless::R::resume { |
| 973 |
my ( $slr, $start_pos, $length ) = @_; |
| 974 |
Marpa::R2::exception( |
| 975 |
"Attempt to resume an SLIF recce which has no string set\n", |
| 976 |
' The string should be set first using read()' |
| 977 |
) |
| 978 |
if not defined $slr->[Marpa::R2::Internal::Scanless::R::P_INPUT_STRING]; |
| 979 |
|
| 980 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 981 |
my $trace_terminals = |
| 982 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_TERMINALS]; |
| 983 |
my $trace_lexers = $slr->[Marpa::R2::Internal::Scanless::R::TRACE_LEXERS]; |
| 984 |
|
| 985 |
$thin_slr->pos_set( ($start_pos // $thin_slr->pos()), ($length // -1)); |
| 986 |
$slr->[Marpa::R2::Internal::Scanless::R::EVENTS] = []; |
| 987 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 988 |
my $thin_slg = $slg->[Marpa::R2::Internal::Scanless::G::C]; |
| 989 |
|
| 990 |
OUTER_READ: while (1) { |
| 991 |
|
| 992 |
my $problem_code = $thin_slr->read(); |
| 993 |
|
| 994 |
last OUTER_READ if not $problem_code; |
| 995 |
my $pause = |
| 996 |
Marpa::R2::Internal::Scanless::convert_libmarpa_events($slr); |
| 997 |
|
| 998 |
if ( $trace_lexers > 2 ) { |
| 999 |
my $stream_pos = $thin_slr->pos(); |
| 1000 |
my $trace_file_handle = |
| 1001 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 1002 |
my $thick_lex_grammar = |
| 1003 |
$slg->[Marpa::R2::Internal::Scanless::G::THICK_LEX_GRAMMARS]->[0]; |
| 1004 |
my $lex_tracer = $thick_lex_grammar->tracer(); |
| 1005 |
my ( $line, $column ) = $slr->line_column($stream_pos); |
| 1006 |
print {$trace_file_handle} |
| 1007 |
qq{\n=== Progress report at line $line, column $column\n}, |
| 1008 |
$lex_tracer->lexer_progress_report($slr), |
| 1009 |
qq{=== End of progress report at line $line, column $column\n}, |
| 1010 |
or Marpa::R2::exception("Cannot print(): $ERRNO"); |
| 1011 |
} ## end if ( $trace_lexers > 2 ) |
| 1012 |
|
| 1013 |
last OUTER_READ if $pause; |
| 1014 |
next OUTER_READ if $problem_code eq 'event'; |
| 1015 |
next OUTER_READ if $problem_code eq 'trace'; |
| 1016 |
|
| 1017 |
# The event on exhaustion only occurs if needed to provide a reason |
| 1018 |
# to return -- if an exhausted reader would return anyway, there is |
| 1019 |
# no exhaustion event. For a reliable way to detect exhaustion, |
| 1020 |
# use the $slr->exhausted() method. |
| 1021 |
# The name of the exhausted event begins with a single quote, so |
| 1022 |
# that it will not conflict with any user-defined event name. |
| 1023 |
|
| 1024 |
if ( $problem_code eq 'R1 exhausted before end' |
| 1025 |
and $slr->[Marpa::R2::Internal::Scanless::R::EXHAUSTION_ACTION] |
| 1026 |
eq 'event' ) |
| 1027 |
{ |
| 1028 |
push @{ $slr->[Marpa::R2::Internal::Scanless::R::EVENTS] }, |
| 1029 |
[q{'exhausted}]; |
| 1030 |
last OUTER_READ; |
| 1031 |
} ## end if ( $problem_code eq 'R1 exhausted before end' and ...) |
| 1032 |
|
| 1033 |
if ( $problem_code eq 'no lexeme' |
| 1034 |
and $slr->[Marpa::R2::Internal::Scanless::R::REJECTION_ACTION] |
| 1035 |
eq 'event' ) |
| 1036 |
{ |
| 1037 |
push @{ $slr->[Marpa::R2::Internal::Scanless::R::EVENTS] }, |
| 1038 |
[q{'rejected}]; |
| 1039 |
last OUTER_READ; |
| 1040 |
} |
| 1041 |
|
| 1042 |
if ( $problem_code eq 'invalid char' ) { |
| 1043 |
my $codepoint = $thin_slr->codepoint(); |
| 1044 |
Marpa::R2::exception( |
| 1045 |
qq{Failed at unacceptable character }, |
| 1046 |
character_describe( chr $codepoint ) ); |
| 1047 |
} ## end if ( $problem_code eq 'invalid char' ) |
| 1048 |
|
| 1049 |
if ( $problem_code eq 'unregistered char' ) { |
| 1050 |
|
| 1051 |
state $op_alternative = Marpa::R2::Thin::op('alternative'); |
| 1052 |
state $op_invalid_char = Marpa::R2::Thin::op('invalid_char'); |
| 1053 |
state $op_earleme_complete = |
| 1054 |
Marpa::R2::Thin::op('earleme_complete'); |
| 1055 |
|
| 1056 |
# Recover by registering character, if we can |
| 1057 |
my $codepoint = $thin_slr->codepoint(); |
| 1058 |
my $character = |
| 1059 |
substr ${$slr->[Marpa::R2::Internal::Scanless::R::P_INPUT_STRING]}, |
| 1060 |
$thin_slr->pos(), 1; |
| 1061 |
my $character_class_table = |
| 1062 |
$slg->[Marpa::R2::Internal::Scanless::G::CHARACTER_CLASS_TABLES] |
| 1063 |
->[0]; |
| 1064 |
my @ops; |
| 1065 |
for my $entry ( @{$character_class_table} ) { |
| 1066 |
|
| 1067 |
my ( $symbol_id, $re ) = @{$entry}; |
| 1068 |
if ( $character =~ $re ) { |
| 1069 |
|
| 1070 |
if ( $trace_terminals >= 2 ) { |
| 1071 |
my $thick_lex_grammar = |
| 1072 |
$slg->[ |
| 1073 |
Marpa::R2::Internal::Scanless::G::THICK_LEX_GRAMMARS] |
| 1074 |
->[0]; |
| 1075 |
my $trace_file_handle = $slr->[ |
| 1076 |
Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 1077 |
my $char_desc = sprintf 'U+%04x', $codepoint; |
| 1078 |
if ( $character =~ m/[[:graph:]]+/ ) { |
| 1079 |
$char_desc .= qq{ '$character'}; |
| 1080 |
} |
| 1081 |
say {$trace_file_handle} |
| 1082 |
qq{Registering character $char_desc as symbol $symbol_id: }, |
| 1083 |
$thick_lex_grammar->symbol_in_display_form( |
| 1084 |
$symbol_id) |
| 1085 |
or |
| 1086 |
Marpa::R2::exception("Could not say(): $ERRNO"); |
| 1087 |
} ## end if ( $trace_terminals >= 2 ) |
| 1088 |
push @ops, $op_alternative, $symbol_id, 1, 1; |
| 1089 |
} ## end if ( $character =~ $re ) |
| 1090 |
} ## end for my $entry ( @{$character_class_table} ) |
| 1091 |
|
| 1092 |
if ( not @ops ) { |
| 1093 |
$thin_slr->char_register( $codepoint, $op_invalid_char ); |
| 1094 |
next OUTER_READ; |
| 1095 |
} |
| 1096 |
$thin_slr->char_register( $codepoint, @ops, |
| 1097 |
$op_earleme_complete ); |
| 1098 |
next OUTER_READ; |
| 1099 |
} ## end if ( $problem_code eq 'unregistered char' ) |
| 1100 |
|
| 1101 |
return $slr->read_problem($problem_code); |
| 1102 |
|
| 1103 |
} ## end OUTER_READ: while (1) |
| 1104 |
|
| 1105 |
return $thin_slr->pos(); |
| 1106 |
} ## end sub Marpa::R2::Scanless::R::resume |
| 1107 |
|
| 1108 |
sub Marpa::R2::Scanless::R::event { |
| 1109 |
my ( $self, $event_ix ) = @_; |
| 1110 |
return $self->[Marpa::R2::Internal::Scanless::R::EVENTS]->[$event_ix]; |
| 1111 |
} |
| 1112 |
|
| 1113 |
sub Marpa::R2::Scanless::R::events { |
| 1114 |
my ($self) = @_; |
| 1115 |
return $self->[Marpa::R2::Internal::Scanless::R::EVENTS]; |
| 1116 |
} |
| 1117 |
|
| 1118 |
## From here, recovery is a matter for the caller, |
| 1119 |
## if it is possible at all |
| 1120 |
sub Marpa::R2::Scanless::R::read_problem { |
| 1121 |
my ( $slr, $problem_code ) = @_; |
| 1122 |
|
| 1123 |
die 'No problem_code in slr->read_problem()' if not $problem_code; |
| 1124 |
|
| 1125 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 1126 |
my $grammar = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 1127 |
|
| 1128 |
my $thick_lex_grammar = |
| 1129 |
$grammar->[Marpa::R2::Internal::Scanless::G::THICK_LEX_GRAMMARS]->[0]; |
| 1130 |
my $lex_tracer = $thick_lex_grammar->tracer(); |
| 1131 |
|
| 1132 |
my $trace_file_handle = |
| 1133 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 1134 |
|
| 1135 |
my $thick_g1_recce = |
| 1136 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1137 |
my $thin_g1_recce = $thick_g1_recce->thin(); |
| 1138 |
my $thick_g1_grammar = $thick_g1_recce->grammar(); |
| 1139 |
my $g1_tracer = $thick_g1_grammar->tracer(); |
| 1140 |
|
| 1141 |
my $pos = $thin_slr->pos(); |
| 1142 |
my $problem_pos = $pos; |
| 1143 |
my $p_string = $slr->[Marpa::R2::Internal::Scanless::R::P_INPUT_STRING]; |
| 1144 |
my $length_of_string = length ${$p_string}; |
| 1145 |
|
| 1146 |
my $problem; |
| 1147 |
my $stream_status = 0; |
| 1148 |
my $g1_status = 0; |
| 1149 |
CODE_TO_PROBLEM: { |
| 1150 |
if ( $problem_code eq 'R1 exhausted before end' ) { |
| 1151 |
my ($lexeme_start) = $thin_slr->lexeme_span(); |
| 1152 |
my ( $line, $column ) = $slr->line_column($lexeme_start); |
| 1153 |
$problem = |
| 1154 |
"Parse exhausted, but lexemes remain, at line $line, column $column\n"; |
| 1155 |
last CODE_TO_PROBLEM; |
| 1156 |
} |
| 1157 |
if ( $problem_code eq 'SLIF loop' ) { |
| 1158 |
my ($lexeme_start) = $thin_slr->lexeme_span(); |
| 1159 |
my ( $line, $column ) = $slr->line_column($lexeme_start); |
| 1160 |
$problem = "SLIF loops at line $line, column $column"; |
| 1161 |
last CODE_TO_PROBLEM; |
| 1162 |
} |
| 1163 |
if ( $problem_code eq 'no lexeme' ) { |
| 1164 |
$problem_pos = $thin_slr->problem_pos(); |
| 1165 |
my ( $line, $column ) = $slr->line_column($problem_pos); |
| 1166 |
my $lexer_name; |
| 1167 |
my @details = (); |
| 1168 |
my %rejections = (); |
| 1169 |
my @events = $thin_slr->events(); |
| 1170 |
if ( scalar @events > 100 ) { |
| 1171 |
my $omitted = scalar @events - 100; |
| 1172 |
push @details, |
| 1173 |
" [there were $omitted events -- only the first 100 were examined]"; |
| 1174 |
$#events = 99; |
| 1175 |
} ## end if ( scalar @events > 100 ) |
| 1176 |
EVENT: for my $event (@events) { |
| 1177 |
my ( $event_type, $trace_event_type, $lexeme_start_pos, |
| 1178 |
$lexeme_end_pos, $g1_lexeme ) |
| 1179 |
= @{$event}; |
| 1180 |
next EVENT |
| 1181 |
if $event_type ne q{'trace} |
| 1182 |
or $trace_event_type ne 'rejected lexeme'; |
| 1183 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 1184 |
my $raw_token_value = |
| 1185 |
$thin_slr->substring( $lexeme_start_pos, |
| 1186 |
$lexeme_end_pos - $lexeme_start_pos ); |
| 1187 |
my $trace_file_handle = |
| 1188 |
$slr |
| 1189 |
->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 1190 |
my $thick_g1_recce = |
| 1191 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1192 |
my $thick_g1_grammar = $thick_g1_recce->grammar(); |
| 1193 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 1194 |
|
| 1195 |
# Different internal symbols may have the same external "display form", |
| 1196 |
# which in naive reporting logic would result in many identical messages, |
| 1197 |
# confusing the user. This logic makes sure that identical rejection |
| 1198 |
# reports are not repeated, even when they have different causes |
| 1199 |
# internally. |
| 1200 |
|
| 1201 |
$rejections{ |
| 1202 |
$thick_g1_grammar->symbol_in_display_form( |
| 1203 |
$g1_lexeme) |
| 1204 |
. qq{; value="$raw_token_value"; length = } |
| 1205 |
. ( $lexeme_end_pos - $lexeme_start_pos ) } = 1; |
| 1206 |
} ## end EVENT: for my $event (@events) |
| 1207 |
my @problem = (); |
| 1208 |
my @rejections = keys %rejections; |
| 1209 |
if ( scalar @rejections ) { |
| 1210 |
my $rejection_count = scalar @rejections; |
| 1211 |
push @problem, |
| 1212 |
"No lexemes accepted at line $line, column $column"; |
| 1213 |
REJECTION: for my $i ( 0 .. 5 ) { |
| 1214 |
my $rejection = $rejections[$i]; |
| 1215 |
last REJECTION if not defined $rejection; |
| 1216 |
push @problem, qq{ Rejected lexeme #$i: $rejection}; |
| 1217 |
} |
| 1218 |
if ( $rejection_count > 5 ) { |
| 1219 |
push @problem, |
| 1220 |
" [there were $rejection_count rejection messages -- only the first 5 are shown]"; |
| 1221 |
} |
| 1222 |
push @problem, @details; |
| 1223 |
} ## end if ( scalar @rejections ) |
| 1224 |
else { |
| 1225 |
push @problem, |
| 1226 |
"No lexeme found at line $line, column $column"; |
| 1227 |
} |
| 1228 |
$problem = join "\n", @problem; |
| 1229 |
last CODE_TO_PROBLEM; |
| 1230 |
} ## end if ( $problem_code eq 'no lexeme' ) |
| 1231 |
$problem = 'Unrecognized problem code: ' . $problem_code; |
| 1232 |
} ## end CODE_TO_PROBLEM: |
| 1233 |
|
| 1234 |
my $desc; |
| 1235 |
DESC: { |
| 1236 |
if ( defined $problem ) { |
| 1237 |
$desc .= "$problem"; |
| 1238 |
} |
| 1239 |
if ( $stream_status == -1 ) { |
| 1240 |
$desc = 'Lexer: Character rejected'; |
| 1241 |
last DESC; |
| 1242 |
} |
| 1243 |
if ( $stream_status == -2 ) { |
| 1244 |
$desc = 'Lexer: Unregistered character'; |
| 1245 |
last DESC; |
| 1246 |
} |
| 1247 |
|
| 1248 |
# -5 indicates success, in which case we should never have called this subroutine. |
| 1249 |
if ( $stream_status == -3 || $stream_status == -5 ) { |
| 1250 |
$desc = 'Unexpected return value from lexer: Parse exhausted'; |
| 1251 |
last DESC; |
| 1252 |
} |
| 1253 |
if ($g1_status) { |
| 1254 |
my $true_event_count = $thin_slr->g1()->event_count(); |
| 1255 |
EVENT: |
| 1256 |
for ( |
| 1257 |
my $event_ix = 0; |
| 1258 |
$event_ix < $true_event_count; |
| 1259 |
$event_ix++ |
| 1260 |
) |
| 1261 |
{ |
| 1262 |
my ( $event_type, $value ) = |
| 1263 |
$thin_slr->g1()->event($event_ix); |
| 1264 |
if ( $event_type eq 'MARPA_EVENT_EARLEY_ITEM_THRESHOLD' ) { |
| 1265 |
$desc = join "\n", $desc, |
| 1266 |
"G1 grammar: Earley item count ($value) exceeds warning threshold\n"; |
| 1267 |
next EVENT; |
| 1268 |
} |
| 1269 |
if ( $event_type eq 'MARPA_EVENT_SYMBOL_EXPECTED' ) { |
| 1270 |
$desc = join "\n", $desc, |
| 1271 |
"Unexpected G1 grammar event: $event_type " |
| 1272 |
. $g1_tracer->symbol_name($value); |
| 1273 |
next EVENT; |
| 1274 |
} ## end if ( $event_type eq 'MARPA_EVENT_SYMBOL_EXPECTED' ) |
| 1275 |
if ( $event_type eq 'MARPA_EVENT_EXHAUSTED' ) { |
| 1276 |
$desc = join "\n", $desc, 'Parse exhausted'; |
| 1277 |
next EVENT; |
| 1278 |
} |
| 1279 |
Marpa::R2::exception( $desc, "\n", |
| 1280 |
qq{Unknown event: "$event_type"; event value = $value\n} |
| 1281 |
); |
| 1282 |
} ## end EVENT: for ( my $event_ix = 0; $event_ix < ...) |
| 1283 |
last DESC; |
| 1284 |
} ## end if ($g1_status) |
| 1285 |
if ( $g1_status < 0 ) { |
| 1286 |
$desc = 'G1 error: ' . $thin_slr->g1()->error(); |
| 1287 |
chomp $desc; |
| 1288 |
last DESC; |
| 1289 |
} |
| 1290 |
} ## end DESC: |
| 1291 |
my $read_string_error; |
| 1292 |
if ( $problem_pos < $length_of_string) { |
| 1293 |
my $char = substr ${$p_string}, $problem_pos, 1; |
| 1294 |
my $char_desc = character_describe($char); |
| 1295 |
my ( $line, $column ) = $thin_slr->line_column($problem_pos); |
| 1296 |
my $prefix = |
| 1297 |
$problem_pos >= 50 |
| 1298 |
? ( substr ${$p_string}, $problem_pos - 50, 50 ) |
| 1299 |
: ( substr ${$p_string}, 0, $problem_pos ); |
| 1300 |
|
| 1301 |
$read_string_error = |
| 1302 |
"Error in SLIF parse: $desc\n" |
| 1303 |
. '* String before error: ' |
| 1304 |
. Marpa::R2::escape_string( $prefix, -50 ) . "\n" |
| 1305 |
. "* The error was at line $line, column $column, and at character $char_desc, ...\n" |
| 1306 |
. '* here: ' |
| 1307 |
. Marpa::R2::escape_string( ( substr ${$p_string}, $problem_pos, 50 ), |
| 1308 |
50 ) |
| 1309 |
. "\n"; |
| 1310 |
} ## end elsif ( $problem_pos < $length_of_string ) |
| 1311 |
else { |
| 1312 |
$read_string_error = |
| 1313 |
"Error in SLIF parse: $desc\n" |
| 1314 |
. "* Error was at end of input\n" |
| 1315 |
. '* String before error: ' |
| 1316 |
. Marpa::R2::escape_string( ${$p_string}, -50 ) . "\n"; |
| 1317 |
} ## end else [ if ($g1_status) ] |
| 1318 |
|
| 1319 |
if ( $slr->[Marpa::R2::Internal::Scanless::R::TRACE_LEXERS] ) { |
| 1320 |
my $stream_pos = $thin_slr->pos(); |
| 1321 |
my $trace_file_handle = |
| 1322 |
$slr->[Marpa::R2::Internal::Scanless::R::TRACE_FILE_HANDLE]; |
| 1323 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 1324 |
my $thick_lex_grammar = |
| 1325 |
$grammar->[Marpa::R2::Internal::Scanless::G::THICK_LEX_GRAMMARS]->[0]; |
| 1326 |
my $lex_tracer = $thick_lex_grammar->tracer(); |
| 1327 |
my ( $line, $column ) = $slr->line_column($stream_pos); |
| 1328 |
$read_string_error .= |
| 1329 |
qq{\n=== Progress report for lexer at line $line, column $column\n} . |
| 1330 |
$lex_tracer->lexer_progress_report($slr); |
| 1331 |
} |
| 1332 |
|
| 1333 |
$slr->[Marpa::R2::Internal::Scanless::R::READ_STRING_ERROR] = |
| 1334 |
$read_string_error; |
| 1335 |
Marpa::R2::exception($read_string_error); |
| 1336 |
|
| 1337 |
# Never reached |
| 1338 |
# Fall through to return undef |
| 1339 |
return; |
| 1340 |
|
| 1341 |
} ## end sub Marpa::R2::Scanless::R::read_problem |
| 1342 |
|
| 1343 |
sub character_describe { |
| 1344 |
my ($char) = @_; |
| 1345 |
my $text = sprintf '0x%04x', ord $char; |
| 1346 |
$text .= q{ } |
| 1347 |
. ( |
| 1348 |
$char =~ m/[[:graph:]]/xms |
| 1349 |
? qq{'$char'} |
| 1350 |
: '(non-graphic character)' |
| 1351 |
); |
| 1352 |
return $text; |
| 1353 |
} ## end sub character_describe |
| 1354 |
|
| 1355 |
my @escape_by_ord = (); |
| 1356 |
$escape_by_ord[ ord q{\\} ] = q{\\\\}; |
| 1357 |
$escape_by_ord[ ord eval qq{"$_"} ] = $_ |
| 1358 |
for "\\t", "\\r", "\\f", "\\b", "\\a", "\\e"; |
| 1359 |
$escape_by_ord[0xa] = '\\n'; |
| 1360 |
$escape_by_ord[$_] //= chr $_ for 32 .. 126; |
| 1361 |
$escape_by_ord[$_] //= sprintf( "\\x%02x", $_ ) for 0 .. 255; |
| 1362 |
|
| 1363 |
# This and the sister routine for "forward strings" |
| 1364 |
# should replace the other string "escaping" subroutine |
| 1365 |
# in the NAIF |
| 1366 |
sub Marpa::R2::Internal::Scanless::reversed_input_escape { |
| 1367 |
my ( $p_input, $base_pos, $length ) = @_; |
| 1368 |
my @escaped_chars = (); |
| 1369 |
my $pos = $base_pos - 1 ; |
| 1370 |
|
| 1371 |
my $trailing_spaces = 0; |
| 1372 |
CHAR: while ( $pos > 0 ) { |
| 1373 |
last CHAR if substr ${$p_input}, $pos, 1 ne q{ }; |
| 1374 |
$trailing_spaces++; |
| 1375 |
$pos--; |
| 1376 |
} |
| 1377 |
my $length_so_far = $trailing_spaces * 2; |
| 1378 |
|
| 1379 |
CHAR: while ( $pos >= 0 ) { |
| 1380 |
my $char = substr ${$p_input}, $pos, 1; |
| 1381 |
my $ord = ord $char; |
| 1382 |
my $escaped_char = $escape_by_ord[$ord] |
| 1383 |
// sprintf( "\\x{%04x}", $ord ); |
| 1384 |
my $char_length = length $escaped_char; |
| 1385 |
$length_so_far += $char_length; |
| 1386 |
last CHAR if $length_so_far > $length; |
| 1387 |
push @escaped_chars, $escaped_char; |
| 1388 |
$pos--; |
| 1389 |
} ## end CHAR: while ( $pos > 0 and $pos < $end_of_input ) |
| 1390 |
@escaped_chars = reverse @escaped_chars; |
| 1391 |
push @escaped_chars, '\\s' for 1 .. $trailing_spaces; |
| 1392 |
return join q{}, @escaped_chars; |
| 1393 |
} ## end sub Marpa::R2::Internal::Scanless::input_escape |
| 1394 |
|
| 1395 |
sub Marpa::R2::Internal::Scanless::input_escape { |
| 1396 |
my ( $p_input, $base_pos, $length ) = @_; |
| 1397 |
my @escaped_chars = (); |
| 1398 |
my $pos = $base_pos; |
| 1399 |
|
| 1400 |
my $length_so_far = 0; |
| 1401 |
|
| 1402 |
my $end_of_input = length ${$p_input}; |
| 1403 |
CHAR: while ( $pos < $end_of_input ) { |
| 1404 |
my $char = substr ${$p_input}, $pos, 1; |
| 1405 |
my $ord = ord $char; |
| 1406 |
my $escaped_char = $escape_by_ord[$ord] |
| 1407 |
// sprintf( "\\x{%04x}", $ord ); |
| 1408 |
my $char_length = length $escaped_char; |
| 1409 |
$length_so_far += $char_length; |
| 1410 |
last CHAR if $length_so_far > $length; |
| 1411 |
push @escaped_chars, $escaped_char; |
| 1412 |
$pos++; |
| 1413 |
} ## end CHAR: while ( $pos < $end_of_input ) |
| 1414 |
|
| 1415 |
my $trailing_spaces = 0; |
| 1416 |
TRAILING_SPACE: |
| 1417 |
for ( |
| 1418 |
my $first_non_space_ix = $#escaped_chars; |
| 1419 |
$first_non_space_ix >= 0; |
| 1420 |
$first_non_space_ix-- |
| 1421 |
) |
| 1422 |
{ |
| 1423 |
last TRAILING_SPACE if $escaped_chars[$first_non_space_ix] ne q{ }; |
| 1424 |
pop @escaped_chars; |
| 1425 |
$trailing_spaces++; |
| 1426 |
} ## end TRAILING_SPACE: for ( my $first_non_space_ix = $#escaped_chars; ...) |
| 1427 |
if ($trailing_spaces) { |
| 1428 |
$length_so_far -= $trailing_spaces; |
| 1429 |
TRAILING_SPACE: while ( $trailing_spaces-- > 0 ) { |
| 1430 |
$length_so_far += 2; |
| 1431 |
last TRAILING_SPACE if $length_so_far > $length; |
| 1432 |
push @escaped_chars, '\\s'; |
| 1433 |
} |
| 1434 |
} ## end if ($trailing_spaces) |
| 1435 |
return join q{}, @escaped_chars; |
| 1436 |
} ## end sub Marpa::R2::Internal::Scanless::input_escape |
| 1437 |
|
| 1438 |
sub Marpa::R2::Scanless::R::ambiguity_metric { |
| 1439 |
my ($slr) = @_; |
| 1440 |
my $thick_g1_recce = |
| 1441 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1442 |
my $ordering = $thick_g1_recce->ordering_get(); |
| 1443 |
my $metric = $ordering ? $ordering->ambiguity_metric() : 0; |
| 1444 |
my $bocage = $thick_g1_recce->[Marpa::R2::Internal::Recognizer::B_C]; |
| 1445 |
return $metric; |
| 1446 |
} ## end sub Marpa::R2::Scanless::R::ambiguity_metric |
| 1447 |
|
| 1448 |
sub Marpa::R2::Scanless::R::ambiguous { |
| 1449 |
my ($slr) = @_; |
| 1450 |
local $Marpa::R2::Context::slr = $slr; |
| 1451 |
my $ambiguity_metric = $slr->ambiguity_metric(); |
| 1452 |
return q{No parse} if $ambiguity_metric <= 0; |
| 1453 |
return q{} if $ambiguity_metric == 1; |
| 1454 |
my $asf = Marpa::R2::ASF->new( { slr => $slr } ); |
| 1455 |
die 'Could not create ASF' if not defined $asf; |
| 1456 |
my $ambiguities = Marpa::R2::Internal::ASF::ambiguities($asf); |
| 1457 |
my @ambiguities = grep {defined} @{$ambiguities}[ 0 .. 1 ]; |
| 1458 |
return Marpa::R2::Internal::ASF::ambiguities_show( $asf, \@ambiguities ); |
| 1459 |
} ## end sub Marpa::R2::Scanless::R::ambiguous |
| 1460 |
|
| 1461 |
# This is a Marpa Scanless::G method, but is included in this |
| 1462 |
# file because internally it is all about the recognizer. |
| 1463 |
sub Marpa::R2::Scanless::G::parse { |
| 1464 |
my ( $slg, $input_ref, $arg1, @more_args ) = @_; |
| 1465 |
if ( not defined $input_ref or ref $input_ref ne 'SCALAR' ) { |
| 1466 |
Marpa::R2::exception( |
| 1467 |
q{$slr->parse(): first argument must be a ref to string}); |
| 1468 |
} |
| 1469 |
my @recce_args = ( { grammar => $slg } ); |
| 1470 |
my @semantics_package_arg = (); |
| 1471 |
DO_ARG1: { |
| 1472 |
last if not defined $arg1; |
| 1473 |
my $reftype = ref $arg1; |
| 1474 |
if ( $reftype eq 'HASH' ) { |
| 1475 |
|
| 1476 |
# if second arg is ref to hash, it is the first set |
| 1477 |
# of named args for |
| 1478 |
# the recognizer |
| 1479 |
push @recce_args, $arg1; |
| 1480 |
last DO_ARG1; |
| 1481 |
} ## end if ( $reftype eq 'HASH' ) |
| 1482 |
if ( $reftype eq q{} ) { |
| 1483 |
|
| 1484 |
# if second arg is a string, it is the semantic package |
| 1485 |
push @semantics_package_arg, { semantics_package => $arg1 }; |
| 1486 |
} |
| 1487 |
if ( ref $arg1 and ref $input_ref ne 'HASH' ) { |
| 1488 |
Marpa::R2::exception( |
| 1489 |
q{$slr->parse(): second argument must be a package name or a ref to HASH} |
| 1490 |
); |
| 1491 |
} |
| 1492 |
} ## end DO_ARG1: |
| 1493 |
if ( grep { ref $_ ne 'HASH' } @more_args ) { |
| 1494 |
Marpa::R2::exception( |
| 1495 |
q{$slr->parse(): third and later arguments must be ref to HASH}); |
| 1496 |
} |
| 1497 |
my $slr = Marpa::R2::Scanless::R->new( @recce_args, @more_args, |
| 1498 |
@semantics_package_arg ); |
| 1499 |
my $input_length = ${$input_ref}; |
| 1500 |
my $length_read = $slr->read($input_ref); |
| 1501 |
if ( $length_read != length $input_length ) { |
| 1502 |
die 'read in $slr->parse() ended prematurely', "\n", |
| 1503 |
" The input length is $input_length\n", |
| 1504 |
" The length read is $length_read\n", |
| 1505 |
" The cause may be an event\n", |
| 1506 |
" The $slr->parse() method does not allow parses to trigger events"; |
| 1507 |
} ## end if ( $length_read != length $input_length ) |
| 1508 |
if ( my $ambiguous_status = $slr->ambiguous() ) { |
| 1509 |
Marpa::R2::exception( "Parse of the input is ambiguous\n", |
| 1510 |
$ambiguous_status ); |
| 1511 |
} |
| 1512 |
|
| 1513 |
my $value_ref = $slr->value(); |
| 1514 |
Marpa::R2::exception( |
| 1515 |
'$slr->parse() read the input, but there was no parse', "\n" ) |
| 1516 |
if not $value_ref; |
| 1517 |
|
| 1518 |
return $value_ref; |
| 1519 |
} ## end sub Marpa::R2::Scanless::G::parse |
| 1520 |
|
| 1521 |
sub Marpa::R2::Scanless::R::rule_closure { |
| 1522 |
|
| 1523 |
my ( $slr, $rule_id ) = @_; |
| 1524 |
|
| 1525 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1526 |
|
| 1527 |
if ( not $recce->[Marpa::R2::Internal::Recognizer::REGISTRATIONS] ) { |
| 1528 |
|
| 1529 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 1530 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1531 |
my $recce_c = $recce->[Marpa::R2::Internal::Recognizer::C]; |
| 1532 |
my $per_parse_arg = {}; |
| 1533 |
my $trace_actions = $recce->[Marpa::R2::Internal::Recognizer::TRACE_ACTIONS] // 0; |
| 1534 |
my $trace_file_handle = $recce->[Marpa::R2::Internal::Recognizer::TRACE_FILE_HANDLE]; |
| 1535 |
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES]; |
| 1536 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 1537 |
my $tracer = $grammar->[Marpa::R2::Internal::Grammar::TRACER]; |
| 1538 |
|
| 1539 |
Marpa::R2::Internal::Value::registration_init( $recce, $per_parse_arg ); |
| 1540 |
|
| 1541 |
} ## end if ( not $recce->[Marpa::R2::Internal::Recognizer::REGISTRATIONS...]) |
| 1542 |
|
| 1543 |
my $rule_closure = $recce->[Marpa::R2::Internal::Recognizer::CLOSURE_BY_RULE_ID]->[$rule_id]; |
| 1544 |
if (defined $rule_closure){ |
| 1545 |
my $ref_rule_closure = ref $rule_closure; |
| 1546 |
if ( $ref_rule_closure eq 'CODE' ){ |
| 1547 |
return $rule_closure; |
| 1548 |
} |
| 1549 |
elsif ( $ref_rule_closure eq 'SCALAR' ){ |
| 1550 |
return $rule_closure; |
| 1551 |
} |
| 1552 |
} |
| 1553 |
else{ |
| 1554 |
return |
| 1555 |
} |
| 1556 |
|
| 1557 |
} ## end sub Marpa::R2::Scanless::R::rule_closure |
| 1558 |
|
| 1559 |
sub Marpa::R2::Scanless::R::value { |
| 1560 |
my ( $slr, $per_parse_arg ) = @_; |
| 1561 |
my $thick_g1_recce = |
| 1562 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1563 |
my $thick_g1_value = $thick_g1_recce->value( $slr, $per_parse_arg ); |
| 1564 |
return $thick_g1_value; |
| 1565 |
} ## end sub Marpa::R2::Scanless::R::value |
| 1566 |
|
| 1567 |
sub Marpa::R2::Scanless::R::series_restart { |
| 1568 |
my ( $slr , @args ) = @_; |
| 1569 |
my $thick_g1_recce = |
| 1570 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1571 |
|
| 1572 |
# Reset SLIF (not NAIF) recognizer args to default |
| 1573 |
$slr->[Marpa::R2::Internal::Scanless::R::EXHAUSTION_ACTION] = 'fatal'; |
| 1574 |
$slr->[Marpa::R2::Internal::Scanless::R::REJECTION_ACTION] = 'fatal'; |
| 1575 |
|
| 1576 |
$thick_g1_recce->reset_evaluation(); |
| 1577 |
my ($g1_recce_args) = Marpa::R2::Internal::Scanless::R::set($slr, "series_restart", @args ); |
| 1578 |
$thick_g1_recce->set( $g1_recce_args ); |
| 1579 |
return 1; |
| 1580 |
} |
| 1581 |
|
| 1582 |
# Given a list of G1 locations, return the minimum span in the input string |
| 1583 |
# that includes them all |
| 1584 |
# Caller must ensure that there is an input, which is not the case |
| 1585 |
# when the parse is initialized. |
| 1586 |
sub g1_locations_to_input_range { |
| 1587 |
my ( $slr, @g1_locations ) = @_; |
| 1588 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 1589 |
my $first_pos = $thin_slr->input_length(); |
| 1590 |
my $last_pos = 0; |
| 1591 |
for my $g1_location (@g1_locations) { |
| 1592 |
my ( $input_start, $input_length ) = $thin_slr->span($g1_location); |
| 1593 |
my $input_end = $input_length ? $input_start + $input_length - 1 : $input_start; |
| 1594 |
$first_pos = $input_start if $input_start < $first_pos; |
| 1595 |
$last_pos = $input_end if $input_end > $last_pos; |
| 1596 |
} ## end for my $g1_location (@other_g1_locations) |
| 1597 |
return ($first_pos, $last_pos); |
| 1598 |
} |
| 1599 |
|
| 1600 |
sub input_range_describe { |
| 1601 |
my ( $slr, $first_pos, $last_pos ) = @_; |
| 1602 |
my ( $first_line, $first_column ) = $slr->line_column($first_pos); |
| 1603 |
my ( $last_line, $last_column ) = $slr->line_column($last_pos); |
| 1604 |
if ( $first_line == $last_line ) { |
| 1605 |
return join q{}, 'L', $first_line, 'c', $first_column |
| 1606 |
if $first_column == $last_column; |
| 1607 |
return join q{}, 'L', $first_line, 'c', $first_column, '-', |
| 1608 |
$last_column; |
| 1609 |
} ## end if ( $first_line == $last_line ) |
| 1610 |
return join q{}, 'L', $first_line, 'c', $first_column, '-L', $last_line, |
| 1611 |
'c', $last_column; |
| 1612 |
} ## end sub input_range_describe |
| 1613 |
|
| 1614 |
sub Marpa::R2::Scanless::R::show_progress { |
| 1615 |
my ( $slr, $start_ordinal, $end_ordinal ) = @_; |
| 1616 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 1617 |
my $recce = $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1618 |
my $grammar = $recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 1619 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1620 |
|
| 1621 |
my $last_ordinal = $recce->latest_earley_set(); |
| 1622 |
|
| 1623 |
if ( not defined $start_ordinal ) { |
| 1624 |
$start_ordinal = $last_ordinal; |
| 1625 |
} |
| 1626 |
if ( $start_ordinal < 0 ) { |
| 1627 |
$start_ordinal += $last_ordinal + 1; |
| 1628 |
} |
| 1629 |
else { |
| 1630 |
if ( $start_ordinal < 0 or $start_ordinal > $last_ordinal ) { |
| 1631 |
return |
| 1632 |
"Marpa::PP::Recognizer::show_progress start index is $start_ordinal, " |
| 1633 |
. "must be in range 0-$last_ordinal"; |
| 1634 |
} |
| 1635 |
} ## end else [ if ( $start_ordinal < 0 ) ] |
| 1636 |
|
| 1637 |
if ( not defined $end_ordinal ) { |
| 1638 |
$end_ordinal = $start_ordinal; |
| 1639 |
} |
| 1640 |
else { |
| 1641 |
my $end_ordinal_argument = $end_ordinal; |
| 1642 |
if ( $end_ordinal < 0 ) { |
| 1643 |
$end_ordinal += $last_ordinal + 1; |
| 1644 |
} |
| 1645 |
if ( $end_ordinal < 0 ) { |
| 1646 |
return |
| 1647 |
"Marpa::PP::Recognizer::show_progress end index is $end_ordinal_argument, " |
| 1648 |
. sprintf ' must be in range %d-%d', -( $last_ordinal + 1 ), |
| 1649 |
$last_ordinal; |
| 1650 |
} ## end if ( $end_ordinal < 0 ) |
| 1651 |
} ## end else [ if ( not defined $end_ordinal ) ] |
| 1652 |
|
| 1653 |
my $text = q{}; |
| 1654 |
for my $current_ordinal ( $start_ordinal .. $end_ordinal ) { |
| 1655 |
my $current_earleme = $recce->earleme($current_ordinal); |
| 1656 |
my %by_rule_by_position = (); |
| 1657 |
for my $progress_item ( @{ $recce->progress($current_ordinal) } ) { |
| 1658 |
my ( $rule_id, $position, $origin ) = @{$progress_item}; |
| 1659 |
if ( $position < 0 ) { |
| 1660 |
$position = $grammar_c->rule_length($rule_id); |
| 1661 |
} |
| 1662 |
$by_rule_by_position{$rule_id}->{$position}->{$origin}++; |
| 1663 |
} ## end for my $progress_item ( @{ $recce->progress($current_ordinal...)}) |
| 1664 |
|
| 1665 |
for my $rule_id ( sort { $a <=> $b } keys %by_rule_by_position ) { |
| 1666 |
my $by_position = $by_rule_by_position{$rule_id}; |
| 1667 |
for my $position ( sort { $a <=> $b } keys %{$by_position} ) { |
| 1668 |
my $raw_origins = $by_position->{$position}; |
| 1669 |
my @origins = sort { $a <=> $b } keys %{$raw_origins}; |
| 1670 |
my $origins_count = scalar @origins; |
| 1671 |
my $origin_desc; |
| 1672 |
if ( $origins_count <= 3 ) { |
| 1673 |
$origin_desc = join q{,}, @origins; |
| 1674 |
} |
| 1675 |
else { |
| 1676 |
$origin_desc = $origins[0] . q{...} . $origins[-1]; |
| 1677 |
} |
| 1678 |
|
| 1679 |
my $rhs_length = $grammar_c->rule_length($rule_id); |
| 1680 |
my @item_text; |
| 1681 |
|
| 1682 |
if ( $position >= $rhs_length ) { |
| 1683 |
push @item_text, "F$rule_id"; |
| 1684 |
} |
| 1685 |
elsif ($position) { |
| 1686 |
push @item_text, "R$rule_id:$position"; |
| 1687 |
} |
| 1688 |
else { |
| 1689 |
push @item_text, "P$rule_id"; |
| 1690 |
} |
| 1691 |
push @item_text, "x$origins_count" if $origins_count > 1; |
| 1692 |
push @item_text, q{@} . $origin_desc . q{-} . $current_earleme; |
| 1693 |
|
| 1694 |
if ( $current_earleme > 0 ) { |
| 1695 |
my $input_range = input_range_describe( |
| 1696 |
$slr, |
| 1697 |
g1_locations_to_input_range( |
| 1698 |
$slr, $current_earleme, @origins |
| 1699 |
) |
| 1700 |
); |
| 1701 |
push @item_text, $input_range; |
| 1702 |
} |
| 1703 |
else { |
| 1704 |
push @item_text, 'L0c0'; |
| 1705 |
} |
| 1706 |
|
| 1707 |
push @item_text, $slg->show_dotted_rule( $rule_id, $position ); |
| 1708 |
$text .= ( join q{ }, @item_text ) . "\n"; |
| 1709 |
} ## end for my $position ( sort { $a <=> $b } keys %{...}) |
| 1710 |
} ## end for my $rule_id ( sort { $a <=> $b } keys ...) |
| 1711 |
|
| 1712 |
} ## end for my $current_ordinal ( $start_ordinal .. $end_ordinal) |
| 1713 |
return $text; |
| 1714 |
} |
| 1715 |
|
| 1716 |
sub Marpa::R2::Scanless::R::progress { |
| 1717 |
my ( $self, @args ) = @_; |
| 1718 |
return $self->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE] |
| 1719 |
->progress(@args); |
| 1720 |
} |
| 1721 |
|
| 1722 |
sub Marpa::R2::Scanless::R::terminals_expected { |
| 1723 |
my ($self) = @_; |
| 1724 |
return $self->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE] |
| 1725 |
->terminals_expected(); |
| 1726 |
} |
| 1727 |
|
| 1728 |
sub Marpa::R2::Scanless::R::exhausted { |
| 1729 |
my ($self) = @_; |
| 1730 |
return $self->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE] |
| 1731 |
->exhausted(); |
| 1732 |
} |
| 1733 |
|
| 1734 |
# Latest and current G1 location are the same |
| 1735 |
sub Marpa::R2::Scanless::R::latest_g1_location { |
| 1736 |
my ($slg) = @_; |
| 1737 |
return $slg->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE] |
| 1738 |
->latest_earley_set(); |
| 1739 |
} |
| 1740 |
|
| 1741 |
# Latest and current G1 location are the same |
| 1742 |
sub Marpa::R2::Scanless::R::current_g1_location { |
| 1743 |
my ($slg) = @_; |
| 1744 |
return $slg->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE] |
| 1745 |
->latest_earley_set(); |
| 1746 |
} |
| 1747 |
|
| 1748 |
sub Marpa::R2::Scanless::R::lexeme_alternative { |
| 1749 |
my ( $slr, $symbol_name, @value ) = @_; |
| 1750 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 1751 |
|
| 1752 |
Marpa::R2::exception( |
| 1753 |
"slr->alternative(): symbol name is undefined\n", |
| 1754 |
" The symbol name cannot be undefined\n" |
| 1755 |
) if not defined $symbol_name; |
| 1756 |
|
| 1757 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 1758 |
my $g1_grammar = $slg->[Marpa::R2::Internal::Scanless::G::THICK_G1_GRAMMAR]; |
| 1759 |
my $g1_tracer = $g1_grammar->tracer(); |
| 1760 |
my $symbol_id = $g1_tracer->symbol_by_name($symbol_name); |
| 1761 |
if ( not defined $symbol_id ) { |
| 1762 |
Marpa::R2::exception( |
| 1763 |
qq{slr->alternative(): symbol "$symbol_name" does not exist}); |
| 1764 |
} |
| 1765 |
|
| 1766 |
my $result = $thin_slr->g1_alternative( $symbol_id, @value ); |
| 1767 |
return 1 if $result == $Marpa::R2::Error::NONE; |
| 1768 |
|
| 1769 |
# The last two are perhaps unnecessary or arguable, |
| 1770 |
# but they preserve compatibility with Marpa::XS |
| 1771 |
return |
| 1772 |
if $result == $Marpa::R2::Error::UNEXPECTED_TOKEN_ID |
| 1773 |
|| $result == $Marpa::R2::Error::NO_TOKEN_EXPECTED_HERE |
| 1774 |
|| $result == $Marpa::R2::Error::INACCESSIBLE_TOKEN; |
| 1775 |
|
| 1776 |
Marpa::R2::exception( qq{Problem reading symbol "$symbol_name": }, |
| 1777 |
( scalar $g1_grammar->error() ) ); |
| 1778 |
} ## end sub Marpa::R2::Scanless::R::lexeme_alternative |
| 1779 |
|
| 1780 |
# Returns 0 on unthrown failure, current location on success |
| 1781 |
sub Marpa::R2::Scanless::R::lexeme_complete { |
| 1782 |
my ( $slr, $start, $length ) = @_; |
| 1783 |
Marpa::R2::exception( q{Bad call to $recce->lexeme_complete(): }, |
| 1784 |
qq{start "$start" is not numeric}) |
| 1785 |
if defined $start |
| 1786 |
and not Scalar::Util::looks_like_number( $start ); |
| 1787 |
Marpa::R2::exception( q{Bad call to $recce->lexeme_complete(): }, |
| 1788 |
qq{length "$length" is not numeric}) |
| 1789 |
if defined $length |
| 1790 |
and not Scalar::Util::looks_like_number( $length ); |
| 1791 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 1792 |
$slr->[Marpa::R2::Internal::Scanless::R::EVENTS] = []; |
| 1793 |
my $thin_pos = $thin_slr->pos(); |
| 1794 |
$start //= $thin_pos; |
| 1795 |
if (not defined $length) { |
| 1796 |
my ($pause_start, $pause_length) = $thin_slr->pause_span(); |
| 1797 |
$length = ($pause_start == $thin_pos) ? $pause_length : -1; |
| 1798 |
} |
| 1799 |
my $return_value = $thin_slr->g1_lexeme_complete($start, $length); |
| 1800 |
Marpa::R2::Internal::Scanless::convert_libmarpa_events($slr); |
| 1801 |
die q{} . $thin_slr->g1()->error() if $return_value == 0; |
| 1802 |
return $return_value; |
| 1803 |
} ## end sub Marpa::R2::Scanless::R::lexeme_complete |
| 1804 |
|
| 1805 |
# Returns 0 on unthrown failure, current location on success, |
| 1806 |
# undef if lexeme not accepted. |
| 1807 |
sub Marpa::R2::Scanless::R::lexeme_read { |
| 1808 |
my ( $slr, $symbol_name, $start, $length, @value ) = @_; |
| 1809 |
return if not $slr->lexeme_alternative( $symbol_name, @value ); |
| 1810 |
return $slr->lexeme_complete( $start, $length ); |
| 1811 |
} |
| 1812 |
|
| 1813 |
sub Marpa::R2::Scanless::R::pause_span { |
| 1814 |
my ($slr) = @_; |
| 1815 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 1816 |
return $thin_slr->pause_span(); |
| 1817 |
} |
| 1818 |
|
| 1819 |
sub Marpa::R2::Scanless::R::pause_lexeme { |
| 1820 |
my ($slr) = @_; |
| 1821 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 1822 |
my $grammar = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 1823 |
my $thick_g1_grammar = |
| 1824 |
$grammar->[Marpa::R2::Internal::Scanless::G::THICK_G1_GRAMMAR]; |
| 1825 |
my $g1_tracer = $thick_g1_grammar->tracer(); |
| 1826 |
my $symbol = $thin_slr->pause_lexeme(); |
| 1827 |
return if not defined $symbol; |
| 1828 |
return $g1_tracer->symbol_name($symbol); |
| 1829 |
} ## end sub Marpa::R2::Scanless::R::pause_lexeme |
| 1830 |
|
| 1831 |
sub Marpa::R2::Scanless::R::line_column { |
| 1832 |
my ( $slr, $pos ) = @_; |
| 1833 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 1834 |
$pos //= $thin_slr->pos(); |
| 1835 |
return $thin_slr->line_column($pos); |
| 1836 |
} ## end sub Marpa::R2::Scanless::R::line_column |
| 1837 |
|
| 1838 |
sub Marpa::R2::Scanless::R::pos { |
| 1839 |
my ( $slr ) = @_; |
| 1840 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 1841 |
return $thin_slr->pos(); |
| 1842 |
} |
| 1843 |
|
| 1844 |
sub Marpa::R2::Scanless::R::input_length { |
| 1845 |
my ( $slr ) = @_; |
| 1846 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 1847 |
return $thin_slr->input_length(); |
| 1848 |
} |
| 1849 |
|
| 1850 |
# no return value documented |
| 1851 |
sub Marpa::R2::Scanless::R::activate { |
| 1852 |
my ( $slr, $event_name, $activate ) = @_; |
| 1853 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 1854 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 1855 |
$activate //= 1; |
| 1856 |
my $thick_g1_recce = |
| 1857 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1858 |
my $thin_g1_recce = $thick_g1_recce->thin(); |
| 1859 |
my $event_symbol_ids_by_type = |
| 1860 |
$slg |
| 1861 |
->[Marpa::R2::Internal::Scanless::G::SYMBOL_IDS_BY_EVENT_NAME_AND_TYPE] |
| 1862 |
->{$event_name}; |
| 1863 |
$thin_g1_recce->completion_symbol_activate( $_, $activate ) |
| 1864 |
for @{ $event_symbol_ids_by_type->{completion} }; |
| 1865 |
$thin_g1_recce->nulled_symbol_activate( $_, $activate ) |
| 1866 |
for @{ $event_symbol_ids_by_type->{nulled} }; |
| 1867 |
$thin_g1_recce->prediction_symbol_activate( $_, $activate ) |
| 1868 |
for @{ $event_symbol_ids_by_type->{prediction} }; |
| 1869 |
$thin_slr->lexeme_event_activate( $_, $activate ) |
| 1870 |
for @{ $event_symbol_ids_by_type->{lexeme} }; |
| 1871 |
return 1; |
| 1872 |
} ## end sub Marpa::R2::Scanless::R::activate |
| 1873 |
|
| 1874 |
# On success, returns the old priority value. |
| 1875 |
# Failures are thrown. |
| 1876 |
sub Marpa::R2::Scanless::R::lexeme_priority_set { |
| 1877 |
my ($slr, $lexeme_name, $new_priority) = @_; |
| 1878 |
my $thin_slr = $slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 1879 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 1880 |
my $thick_g1_grammar = |
| 1881 |
$slg->[Marpa::R2::Internal::Scanless::G::THICK_G1_GRAMMAR]; |
| 1882 |
my $g1_tracer = $thick_g1_grammar->tracer(); |
| 1883 |
my $lexeme_id = $g1_tracer->symbol_by_name($lexeme_name); |
| 1884 |
Marpa::R2::exception("Bad symbol in lexeme_priority_set(): $lexeme_name") |
| 1885 |
if not defined $lexeme_id; |
| 1886 |
return $thin_slr->lexeme_priority_set($lexeme_id, $new_priority); |
| 1887 |
} |
| 1888 |
|
| 1889 |
# Internal methods, not to be documented |
| 1890 |
|
| 1891 |
sub Marpa::R2::Scanless::R::thick_g1_grammar { |
| 1892 |
my ($slr) = @_; |
| 1893 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 1894 |
return $slg->[Marpa::R2::Internal::Scanless::G::THICK_G1_GRAMMAR]; |
| 1895 |
} |
| 1896 |
|
| 1897 |
sub Marpa::R2::Scanless::R::thick_g1_recce { |
| 1898 |
my ($slr) = @_; |
| 1899 |
return $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1900 |
} |
| 1901 |
|
| 1902 |
sub Marpa::R2::Scanless::R::default_g1_start_closure { |
| 1903 |
my ($slr) = @_; |
| 1904 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 1905 |
my $default_action_name = |
| 1906 |
$slg->[Marpa::R2::Internal::Scanless::G::DEFAULT_G1_START_ACTION]; |
| 1907 |
my $thick_g1_recce = |
| 1908 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1909 |
my $resolution = |
| 1910 |
Marpa::R2::Internal::Recognizer::resolve_action( $thick_g1_recce, |
| 1911 |
$default_action_name ); |
| 1912 |
return if not $resolution; |
| 1913 |
my ( undef, $closure ) = @{$resolution}; |
| 1914 |
return $closure; |
| 1915 |
} ## end sub Marpa::R2::Scanless::R::default_g1_start_closure |
| 1916 |
|
| 1917 |
# not to be documented |
| 1918 |
sub Marpa::R2::Scanless::R::latest_earley_set { |
| 1919 |
my ($self) = @_; |
| 1920 |
return $self->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE] |
| 1921 |
->latest_earley_set(); |
| 1922 |
} |
| 1923 |
|
| 1924 |
sub Marpa::R2::Scanless::R::earley_set_size { |
| 1925 |
my ($self, $set_id) = @_; |
| 1926 |
# OK if set ID is undef, just pass it on. |
| 1927 |
return $self->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE] |
| 1928 |
->earley_set_size($set_id); |
| 1929 |
} |
| 1930 |
|
| 1931 |
sub Marpa::R2::Scanless::R::verbose_or_nodes { |
| 1932 |
my ( $slr ) = @_; |
| 1933 |
my $thick_g1_recce = |
| 1934 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1935 |
return $thick_g1_recce->verbose_or_nodes(); |
| 1936 |
} |
| 1937 |
|
| 1938 |
sub Marpa::R2::Scanless::R::show_and_nodes { |
| 1939 |
my ( $slr, $verbose ) = @_; |
| 1940 |
$verbose //= 0; |
| 1941 |
my $thick_g1_recce = |
| 1942 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1943 |
return $thick_g1_recce->show_and_nodes($verbose); |
| 1944 |
} |
| 1945 |
|
| 1946 |
sub Marpa::R2::Scanless::R::show_bocage { |
| 1947 |
my ( $slr, $verbose ) = @_; |
| 1948 |
$verbose //= 0; |
| 1949 |
my $thick_g1_recce = |
| 1950 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1951 |
return $thick_g1_recce->show_bocage($verbose); |
| 1952 |
} |
| 1953 |
|
| 1954 |
sub Marpa::R2::Scanless::R::show_earley_sets { |
| 1955 |
my ( $slr, $verbose ) = @_; |
| 1956 |
$verbose //= 0; |
| 1957 |
my $thick_g1_recce = |
| 1958 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1959 |
return $thick_g1_recce->show_earley_sets($verbose); |
| 1960 |
} |
| 1961 |
|
| 1962 |
sub Marpa::R2::Scanless::R::show_leo_items { |
| 1963 |
my ( $slr, $ordinal ) = @_; |
| 1964 |
my $thick_g1_recce = |
| 1965 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 1966 |
my $grammar = $thick_g1_recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 1967 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1968 |
my $recce_c = $thick_g1_recce->[Marpa::R2::Internal::Recognizer::C]; |
| 1969 |
|
| 1970 |
my $last_ordinal = $thick_g1_recce->latest_earley_set(); |
| 1971 |
if ( $ordinal < 0 or $ordinal > $last_ordinal ) { |
| 1972 |
return |
| 1973 |
"Marpa::PP::Recognizer::show_leo_items start index is $ordinal, " |
| 1974 |
. "must be in range 0-$last_ordinal"; |
| 1975 |
} |
| 1976 |
die if not defined $recce_c->_marpa_r_earley_set_trace($ordinal); |
| 1977 |
my @lines = (); |
| 1978 |
POSTDOT_ITEM: |
| 1979 |
for ( |
| 1980 |
my $postdot_symbol_id = $recce_c->_marpa_r_first_postdot_item_trace() ; |
| 1981 |
defined $postdot_symbol_id ; |
| 1982 |
$postdot_symbol_id = $recce_c->_marpa_r_next_postdot_item_trace() |
| 1983 |
) |
| 1984 |
{ |
| 1985 |
|
| 1986 |
# If there is no base Earley item, |
| 1987 |
# then this is not a Leo item, so we skip it |
| 1988 |
my $leo_item_desc = $slr->show_leo_item(); |
| 1989 |
next POSTDOT_ITEM if not defined $leo_item_desc; |
| 1990 |
push @lines, $leo_item_desc; |
| 1991 |
} ## end POSTDOT_ITEM: for ( my $postdot_symbol_id = $recce_c...) |
| 1992 |
|
| 1993 |
return join "\n", @lines, ''; |
| 1994 |
} |
| 1995 |
|
| 1996 |
sub Marpa::R2::Scanless::R::show_leo_item { |
| 1997 |
my ($slr) = @_; |
| 1998 |
my $slg = $slr->[Marpa::R2::Internal::Scanless::R::GRAMMAR]; |
| 1999 |
my $thick_g1_recce = |
| 2000 |
$slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE]; |
| 2001 |
my $recce_c = $thick_g1_recce->[Marpa::R2::Internal::Recognizer::C]; |
| 2002 |
my $grammar = $thick_g1_recce->[Marpa::R2::Internal::Recognizer::GRAMMAR]; |
| 2003 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 2004 |
my $tracer = $grammar->[Marpa::R2::Internal::Grammar::TRACER]; |
| 2005 |
my $base_ahm_id = $recce_c->_marpa_r_leo_top_ahm(); |
| 2006 |
return if not defined $base_ahm_id; |
| 2007 |
my $trace_earley_set = $recce_c->_marpa_r_trace_earley_set(); |
| 2008 |
my $trace_earleme = $recce_c->earleme($trace_earley_set); |
| 2009 |
my $postdot_symbol_id = $recce_c->_marpa_r_postdot_item_symbol(); |
| 2010 |
my $postdot_symbol_name = $tracer->isy_name($postdot_symbol_id); |
| 2011 |
# my $predecessor_symbol_id = $recce_c->_marpa_r_leo_predecessor_symbol(); |
| 2012 |
my $base_origin_set_id = $recce_c->_marpa_r_leo_base_origin(); |
| 2013 |
my $base_origin_earleme = $recce_c->earleme($base_origin_set_id); |
| 2014 |
|
| 2015 |
my $base_irl_id = $grammar_c->_marpa_g_ahm_irl($base_ahm_id); |
| 2016 |
my $base_rule_id = $grammar_c->_marpa_g_source_xrl($base_irl_id); |
| 2017 |
my $base_desc; |
| 2018 |
if (defined $base_rule_id) { |
| 2019 |
$base_desc = '[' . $slg->show_dotted_rule($base_rule_id, -2) . ']'; |
| 2020 |
} else { |
| 2021 |
$base_desc = 'IRL#' . $base_irl_id; |
| 2022 |
} |
| 2023 |
|
| 2024 |
my $text = sprintf 'L%d', $trace_earleme; |
| 2025 |
my @link_texts = ($base_desc, qq{"$postdot_symbol_name"}, "$base_origin_earleme"); |
| 2026 |
$text .= ' [' . ( join '; ', @link_texts ) . ']'; |
| 2027 |
return $text; |
| 2028 |
} |
| 2029 |
|
| 2030 |
sub Marpa::R2::Scanless::R::show_parse_items { |
| 2031 |
my ( $slr, $ordinal ) = @_; |
| 2032 |
my $text = $slr->show_progress($ordinal); |
| 2033 |
$text .= $slr->show_leo_items($ordinal); |
| 2034 |
return $text; |
| 2035 |
} |
| 2036 |
|
| 2037 |
1; |
| 2038 |
|
| 2039 |
# vim: expandtab shiftwidth=4: |