| 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::G; |
| 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::G; |
| 30 |
|
| 31 |
use Scalar::Util 'blessed'; |
| 32 |
use English qw( -no_match_vars ); |
| 33 |
|
| 34 |
# names of packages for strings |
| 35 |
our $PACKAGE = 'Marpa::R2::Scanless::G'; |
| 36 |
|
| 37 |
sub Marpa::R2::Internal::Scanless::meta_grammar { |
| 38 |
|
| 39 |
my $meta_slg = bless [], 'Marpa::R2::Scanless::G'; |
| 40 |
state $hashed_metag = Marpa::R2::Internal::MetaG::hashed_grammar(); |
| 41 |
$meta_slg->[Marpa::R2::Internal::Scanless::G::TRACE_TERMINALS] = 0; |
| 42 |
Marpa::R2::Internal::Scanless::G::hash_to_runtime( $meta_slg, |
| 43 |
$hashed_metag, |
| 44 |
{ bless_package => 'Marpa::R2::Internal::MetaAST_Nodes' } ); |
| 45 |
|
| 46 |
my $thick_g1_grammar = |
| 47 |
$meta_slg->[Marpa::R2::Internal::Scanless::G::THICK_G1_GRAMMAR]; |
| 48 |
my @mask_by_rule_id; |
| 49 |
$mask_by_rule_id[$_] = $thick_g1_grammar->_rule_mask($_) |
| 50 |
for $thick_g1_grammar->rule_ids(); |
| 51 |
$meta_slg->[Marpa::R2::Internal::Scanless::G::MASK_BY_RULE_ID] = |
| 52 |
\@mask_by_rule_id; |
| 53 |
$meta_slg->[Marpa::R2::Internal::Scanless::G::TRACE_TERMINALS] = 0; |
| 54 |
|
| 55 |
return $meta_slg; |
| 56 |
|
| 57 |
} ## end sub Marpa::R2::Internal::Scanless::meta_grammar |
| 58 |
|
| 59 |
sub Marpa::R2::Scanless::G::new { |
| 60 |
my ( $class, @hash_ref_args ) = @_; |
| 61 |
|
| 62 |
my $slg = []; |
| 63 |
bless $slg, $class; |
| 64 |
|
| 65 |
my ($dsl, $g1_args) = Marpa::R2::Internal::Scanless::G::set ( $slg, 'new', @hash_ref_args ); |
| 66 |
my $ast = Marpa::R2::Internal::MetaAST->new( $dsl ); |
| 67 |
my $hashed_ast = $ast->ast_to_hash(); |
| 68 |
Marpa::R2::Internal::Scanless::G::hash_to_runtime($slg, $hashed_ast, $g1_args); |
| 69 |
return $slg; |
| 70 |
} ## end sub Marpa::R2::Scanless::G::new |
| 71 |
|
| 72 |
sub Marpa::R2::Scanless::G::set { |
| 73 |
my ( $slg, @hash_ref_args ) = @_; |
| 74 |
Marpa::R2::Internal::Scanless::G::set ( $slg, 'set', @hash_ref_args ); |
| 75 |
return $slg; |
| 76 |
} |
| 77 |
|
| 78 |
# The context flag indicates whether this ::set() is called directly by the user; |
| 79 |
# is for the external constructor; or is for the internal ("meta") constructor. |
| 80 |
# "Context" flags of this kind |
| 81 |
# are much decried practice, and for good reason, but in this case |
| 82 |
# I think it is justified. |
| 83 |
# This logic really needs to be all in one place, and so a flag |
| 84 |
# to trigger the minor differences needed by the various calling |
| 85 |
# contexts is a small price to pay. |
| 86 |
sub Marpa::R2::Internal::Scanless::G::set { |
| 87 |
my ( $slg, $method, @hash_ref_args ) = @_; |
| 88 |
|
| 89 |
# Other possible grammar options: |
| 90 |
# default_rank |
| 91 |
# inaccessible_ok |
| 92 |
# unproductive_ok |
| 93 |
# warnings |
| 94 |
|
| 95 |
state $copy_to_g1_args = |
| 96 |
{ map { ( $_, 1 ); } |
| 97 |
qw(trace_file_handle action_object default_action bless_package) }; |
| 98 |
state $set_method_args = |
| 99 |
{ map { ( $_, 1 ); } qw(trace_file_handle trace_terminals) }; |
| 100 |
state $new_method_args = { |
| 101 |
map { ( $_, 1 ); } qw(source trace_terminals), keys %{$copy_to_g1_args} |
| 102 |
}; |
| 103 |
for my $args (@hash_ref_args) { |
| 104 |
my $ref_type = ref $args; |
| 105 |
if ( not $ref_type ) { |
| 106 |
Marpa::R2::exception( q{$slg->} |
| 107 |
. $method |
| 108 |
. qq{() expects args as ref to HASH; got non-reference instead} |
| 109 |
); |
| 110 |
} ## end if ( not $ref_type ) |
| 111 |
if ( $ref_type ne 'HASH' ) { |
| 112 |
Marpa::R2::exception( q{$slg->} |
| 113 |
. $method |
| 114 |
. qq{() expects args as ref to HASH, got ref to $ref_type instead} |
| 115 |
); |
| 116 |
} ## end if ( $ref_type ne 'HASH' ) |
| 117 |
} ## end for my $args (@hash_ref_args) |
| 118 |
|
| 119 |
my %flat_args = (); |
| 120 |
for my $hash_ref (@hash_ref_args) { |
| 121 |
ARG: for my $arg_name ( keys %{$hash_ref} ) { |
| 122 |
$flat_args{$arg_name} = $hash_ref->{$arg_name}; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
my $ok_args = $set_method_args; |
| 127 |
$ok_args = $new_method_args if $method eq 'new'; |
| 128 |
my @bad_args = grep { not $ok_args->{$_} } keys %flat_args; |
| 129 |
if ( scalar @bad_args ) { |
| 130 |
Marpa::R2::exception( |
| 131 |
q{Bad named argument(s) to $slg->} |
| 132 |
. $method |
| 133 |
. q{() method: } |
| 134 |
. join q{ }, |
| 135 |
@bad_args |
| 136 |
); |
| 137 |
} ## end if ( scalar @bad_args ) |
| 138 |
|
| 139 |
my $dsl; |
| 140 |
if ( $method eq 'new' ) { |
| 141 |
state $arg_name = 'source'; |
| 142 |
$dsl = $flat_args{$arg_name}; |
| 143 |
Marpa::R2::exception( |
| 144 |
qq{Marpa::R2::Scanless::G::new() called without a "$arg_name" argument} |
| 145 |
) if not defined $dsl; |
| 146 |
my $ref_type = ref $dsl; |
| 147 |
if ( $ref_type ne 'SCALAR' ) { |
| 148 |
my $desc = $ref_type ? "a ref to $ref_type" : 'not a ref'; |
| 149 |
Marpa::R2::exception( |
| 150 |
qq{'$arg_name' name argument to Marpa::R2::Scanless::G->new() is $desc\n}, |
| 151 |
" It should be a ref to a string\n" |
| 152 |
); |
| 153 |
} ## end if ( $ref_type ne 'SCALAR' ) |
| 154 |
if ( not defined ${$dsl} ) { |
| 155 |
Marpa::R2::exception( |
| 156 |
qq{'$arg_name' name argument to Marpa::R2::Scanless::G->new() is a ref to a an undef\n}, |
| 157 |
" It should be a ref to a string\n" |
| 158 |
); |
| 159 |
} ## end if ( $ref_type ne 'SCALAR' ) |
| 160 |
} ## end if ( $method eq 'new' ) |
| 161 |
|
| 162 |
# A bit hack-ish, but some named args will be copies straight to a member of |
| 163 |
# the Scanless::G class, so this maps named args to the index of the array |
| 164 |
# that holds the members. |
| 165 |
state $copy_arg_to_index = { |
| 166 |
trace_file_handle => Marpa::R2::Internal::Scanless::G::TRACE_FILE_HANDLE, |
| 167 |
trace_terminals => Marpa::R2::Internal::Scanless::G::TRACE_TERMINALS |
| 168 |
}; |
| 169 |
|
| 170 |
ARG: for my $arg_name ( keys %flat_args ) { |
| 171 |
my $index = $copy_arg_to_index->{$arg_name}; |
| 172 |
next ARG if not defined $index; |
| 173 |
my $value = $flat_args{$arg_name}; |
| 174 |
$slg->[$index] = $value; |
| 175 |
} ## end ARG: for my $arg_name ( keys %flat_args ) |
| 176 |
|
| 177 |
# Normalize trace_terminals |
| 178 |
$slg->[Marpa::R2::Internal::Scanless::G::TRACE_TERMINALS] = 0 |
| 179 |
if not Scalar::Util::looks_like_number( |
| 180 |
$slg->[Marpa::R2::Internal::Scanless::G::TRACE_TERMINALS] ); |
| 181 |
|
| 182 |
# Trace file handle needs to be populated downwards |
| 183 |
if ( defined( my $trace_file_handle = $flat_args{trace_file_handle} ) ) { |
| 184 |
GRAMMAR: |
| 185 |
for my $naif_grammar ( |
| 186 |
$slg->[Marpa::R2::Internal::Scanless::G::THICK_G1_GRAMMAR], |
| 187 |
@{ $slg->[Marpa::R2::Internal::Scanless::G::THICK_LEX_GRAMMARS] } |
| 188 |
) |
| 189 |
{ |
| 190 |
next GRAMMAR if not defined $naif_grammar; |
| 191 |
$naif_grammar->set( { trace_file_handle => $trace_file_handle } ); |
| 192 |
} ## end GRAMMAR: for my $naif_grammar ( $slg->[...]) |
| 193 |
} ## end if ( defined( my $trace_file_handle = $flat_args{...})) |
| 194 |
|
| 195 |
if ( $method eq 'new' ) { |
| 196 |
|
| 197 |
# Prune flat args of all those named args which are NOT to be copied |
| 198 |
# into the NAIF recce args |
| 199 |
for my $arg_name ( keys %flat_args ) { |
| 200 |
delete $flat_args{$arg_name} |
| 201 |
if not $copy_to_g1_args->{$arg_name}; |
| 202 |
} |
| 203 |
|
| 204 |
# trace file handle must always be defined |
| 205 |
$slg->[Marpa::R2::Internal::Scanless::G::TRACE_FILE_HANDLE] //= \*STDERR; |
| 206 |
|
| 207 |
return ($dsl, \%flat_args); |
| 208 |
} ## end if ( $method eq 'new' ) |
| 209 |
|
| 210 |
return; |
| 211 |
|
| 212 |
} ## end sub Marpa::R2::Internal::Scanless::G::set |
| 213 |
|
| 214 |
sub Marpa::R2::Internal::Scanless::G::hash_to_runtime { |
| 215 |
my ( $slg, $hashed_source, $g1_args ) = @_; |
| 216 |
|
| 217 |
my $trace_terminals = |
| 218 |
$slg->[Marpa::R2::Internal::Scanless::G::TRACE_TERMINALS]; |
| 219 |
|
| 220 |
# Pre-lexer G1 processing |
| 221 |
|
| 222 |
my $start_lhs = $hashed_source->{'start_lhs'} |
| 223 |
// $hashed_source->{'first_lhs'}; |
| 224 |
Marpa::R2::exception('No rules in SLIF grammar') |
| 225 |
if not defined $start_lhs; |
| 226 |
Marpa::R2::Internal::MetaAST::start_rule_create( $hashed_source, |
| 227 |
$start_lhs ); |
| 228 |
|
| 229 |
$slg->[Marpa::R2::Internal::Scanless::G::CACHE_RULEIDS_BY_LHS_NAME] = {}; |
| 230 |
$slg->[Marpa::R2::Internal::Scanless::G::DEFAULT_G1_START_ACTION] = |
| 231 |
$hashed_source->{'default_g1_start_action'}; |
| 232 |
|
| 233 |
my $trace_fh = |
| 234 |
$slg->[Marpa::R2::Internal::Scanless::G::TRACE_FILE_HANDLE] = |
| 235 |
$g1_args->{trace_file_handle} // \*STDERR; |
| 236 |
|
| 237 |
my $if_inaccessible_default = |
| 238 |
$hashed_source->{defaults}->{if_inaccessible} // 'warn'; |
| 239 |
|
| 240 |
# Prepare the arguments for the G1 grammar |
| 241 |
$g1_args->{rules} = $hashed_source->{rules}->{G1}; |
| 242 |
$g1_args->{symbols} = $hashed_source->{symbols}->{G1}; |
| 243 |
state $g1_target_symbol = '[:start]'; |
| 244 |
$g1_args->{start} = $g1_target_symbol; |
| 245 |
$g1_args->{'_internal_'} = |
| 246 |
{ 'if_inaccessible' => $if_inaccessible_default }; |
| 247 |
|
| 248 |
my $thick_g1_grammar = Marpa::R2::Grammar->new($g1_args); |
| 249 |
my $g1_tracer = $thick_g1_grammar->tracer(); |
| 250 |
my $g1_thin = $g1_tracer->grammar(); |
| 251 |
|
| 252 |
my $symbol_ids_by_event_name_and_type = {}; |
| 253 |
$slg->[ |
| 254 |
Marpa::R2::Internal::Scanless::G::SYMBOL_IDS_BY_EVENT_NAME_AND_TYPE] |
| 255 |
= $symbol_ids_by_event_name_and_type; |
| 256 |
|
| 257 |
my $completion_events_by_name = $hashed_source->{completion_events}; |
| 258 |
my $completion_events_by_id = |
| 259 |
$slg->[Marpa::R2::Internal::Scanless::G::COMPLETION_EVENT_BY_ID] = []; |
| 260 |
for my $symbol_name ( keys %{$completion_events_by_name} ) { |
| 261 |
my ( $event_name, $is_active ) = |
| 262 |
@{ $completion_events_by_name->{$symbol_name} }; |
| 263 |
my $symbol_id = $g1_tracer->symbol_by_name($symbol_name); |
| 264 |
if ( not defined $symbol_id ) { |
| 265 |
Marpa::R2::exception( |
| 266 |
"Completion event defined for non-existent symbol: $symbol_name\n" |
| 267 |
); |
| 268 |
} |
| 269 |
|
| 270 |
# Must be done before precomputation |
| 271 |
$g1_thin->symbol_is_completion_event_set( $symbol_id, 1 ); |
| 272 |
$g1_thin->completion_symbol_activate( $symbol_id, 0 ) |
| 273 |
if not $is_active; |
| 274 |
$slg->[Marpa::R2::Internal::Scanless::G::COMPLETION_EVENT_BY_ID] |
| 275 |
->[$symbol_id] = $event_name; |
| 276 |
push |
| 277 |
@{ $symbol_ids_by_event_name_and_type->{$event_name}->{completion} |
| 278 |
}, $symbol_id; |
| 279 |
} ## end for my $symbol_name ( keys %{$completion_events_by_name...}) |
| 280 |
|
| 281 |
my $nulled_events_by_name = $hashed_source->{nulled_events}; |
| 282 |
my $nulled_events_by_id = |
| 283 |
$slg->[Marpa::R2::Internal::Scanless::G::NULLED_EVENT_BY_ID] = []; |
| 284 |
for my $symbol_name ( keys %{$nulled_events_by_name} ) { |
| 285 |
my ( $event_name, $is_active ) = |
| 286 |
@{ $nulled_events_by_name->{$symbol_name} }; |
| 287 |
my $symbol_id = $g1_tracer->symbol_by_name($symbol_name); |
| 288 |
if ( not defined $symbol_id ) { |
| 289 |
Marpa::R2::exception( |
| 290 |
"nulled event defined for non-existent symbol: $symbol_name\n" |
| 291 |
); |
| 292 |
} |
| 293 |
|
| 294 |
# Must be done before precomputation |
| 295 |
$g1_thin->symbol_is_nulled_event_set( $symbol_id, 1 ); |
| 296 |
$g1_thin->nulled_symbol_activate( $symbol_id, 0 ) if not $is_active; |
| 297 |
$slg->[Marpa::R2::Internal::Scanless::G::NULLED_EVENT_BY_ID] |
| 298 |
->[$symbol_id] = $event_name; |
| 299 |
push @{ $symbol_ids_by_event_name_and_type->{$event_name}->{nulled} }, |
| 300 |
$symbol_id; |
| 301 |
} ## end for my $symbol_name ( keys %{$nulled_events_by_name} ) |
| 302 |
|
| 303 |
my $prediction_events_by_name = $hashed_source->{prediction_events}; |
| 304 |
my $prediction_events_by_id = |
| 305 |
$slg->[Marpa::R2::Internal::Scanless::G::PREDICTION_EVENT_BY_ID] = []; |
| 306 |
for my $symbol_name ( keys %{$prediction_events_by_name} ) { |
| 307 |
my ( $event_name, $is_active ) = |
| 308 |
@{ $prediction_events_by_name->{$symbol_name} }; |
| 309 |
my $symbol_id = $g1_tracer->symbol_by_name($symbol_name); |
| 310 |
if ( not defined $symbol_id ) { |
| 311 |
Marpa::R2::exception( |
| 312 |
"prediction event defined for non-existent symbol: $symbol_name\n" |
| 313 |
); |
| 314 |
} |
| 315 |
|
| 316 |
# Must be done before precomputation |
| 317 |
$g1_thin->symbol_is_prediction_event_set( $symbol_id, 1 ); |
| 318 |
$g1_thin->prediction_symbol_activate( $symbol_id, 0 ) |
| 319 |
if not $is_active; |
| 320 |
$slg->[Marpa::R2::Internal::Scanless::G::PREDICTION_EVENT_BY_ID] |
| 321 |
->[$symbol_id] = $event_name; |
| 322 |
push |
| 323 |
@{ $symbol_ids_by_event_name_and_type->{$event_name}->{prediction} |
| 324 |
}, $symbol_id; |
| 325 |
} ## end for my $symbol_name ( keys %{$prediction_events_by_name...}) |
| 326 |
|
| 327 |
my $lexeme_events_by_id = |
| 328 |
$slg->[Marpa::R2::Internal::Scanless::G::LEXEME_EVENT_BY_ID] = []; |
| 329 |
|
| 330 |
if (defined( |
| 331 |
my $precompute_error = |
| 332 |
Marpa::R2::Internal::Grammar::slif_precompute( |
| 333 |
$thick_g1_grammar) |
| 334 |
) |
| 335 |
) |
| 336 |
{ |
| 337 |
if ( $precompute_error == $Marpa::R2::Error::UNPRODUCTIVE_START ) { |
| 338 |
|
| 339 |
# Maybe someday improve this by finding the start rule and showing |
| 340 |
# its RHS -- for now it is clear enough |
| 341 |
Marpa::R2::exception(qq{Unproductive start symbol}); |
| 342 |
} ## end if ( $precompute_error == ...) |
| 343 |
Marpa::R2::exception( |
| 344 |
'Internal errror: unnkown precompute error code ', |
| 345 |
$precompute_error ); |
| 346 |
} ## end if ( defined( my $precompute_error = ...)) |
| 347 |
|
| 348 |
# Find out the list of lexemes according to G1 |
| 349 |
my %g1_id_by_lexeme_name = (); |
| 350 |
SYMBOL: for my $symbol_id ( 0 .. $g1_thin->highest_symbol_id() ) { |
| 351 |
|
| 352 |
# Not a lexeme, according to G1 |
| 353 |
next SYMBOL if not $g1_thin->symbol_is_terminal($symbol_id); |
| 354 |
|
| 355 |
my $symbol_name = $g1_tracer->symbol_name($symbol_id); |
| 356 |
$g1_id_by_lexeme_name{$symbol_name} = $symbol_id; |
| 357 |
|
| 358 |
} ## end SYMBOL: for my $symbol_id ( 0 .. $g1_thin->highest_symbol_id(...)) |
| 359 |
|
| 360 |
# A first phase of applying defaults |
| 361 |
my $discard_default_adverbs = $hashed_source->{discard_default_adverbs}; |
| 362 |
my $lexeme_declarations = $hashed_source->{lexeme_declarations}; |
| 363 |
my $lexeme_default_adverbs = $hashed_source->{lexeme_default_adverbs}; |
| 364 |
my $latm_default_value = $lexeme_default_adverbs->{latm} // 0; |
| 365 |
|
| 366 |
# Current lexeme data is spread out in many places. |
| 367 |
# Change so that it all resides in this hash, indexed by |
| 368 |
# name |
| 369 |
my %lexeme_data = (); |
| 370 |
|
| 371 |
# Determine "latm" status |
| 372 |
LEXEME: for my $lexeme_name ( keys %g1_id_by_lexeme_name ) { |
| 373 |
my $declarations = $lexeme_declarations->{$lexeme_name}; |
| 374 |
my $latm_value = $declarations->{latm} // $latm_default_value; |
| 375 |
$lexeme_data{$lexeme_name}{latm} = $latm_value; |
| 376 |
} |
| 377 |
|
| 378 |
# Lexers |
| 379 |
|
| 380 |
my $lexer_id = 0; |
| 381 |
my $lexer_name = 'L0'; |
| 382 |
|
| 383 |
my %lexer_id_by_name = (); |
| 384 |
my %thick_grammar_by_lexer_name = (); |
| 385 |
my @discard_event_by_lexer_rule_id = (); |
| 386 |
my %lexer_and_rule_to_g1_lexeme = (); |
| 387 |
my %character_class_table_by_lexer_name = (); |
| 388 |
state $lex_start_symbol_name = '[:start_lex]'; |
| 389 |
state $discard_symbol_name = '[:discard]'; |
| 390 |
|
| 391 |
my $lexer_rules = $hashed_source->{rules}->{$lexer_name}; |
| 392 |
my $character_class_hash = $hashed_source->{character_classes}; |
| 393 |
my $lexer_symbols = $hashed_source->{symbols}->{'L'}; |
| 394 |
|
| 395 |
# If no lexer rules, fake a lexer |
| 396 |
# Fake a lexer -- it discards symbols in character classes which |
| 397 |
# never matches |
| 398 |
if ( not $lexer_rules ) { |
| 399 |
$character_class_hash = { '[[^\\d\\D]]' => [ '[^\\d\\D]', '' ] }; |
| 400 |
$lexer_rules = [ |
| 401 |
{ 'rhs' => [ '[[^\\d\\D]]' ], |
| 402 |
'lhs' => '[:discard]', |
| 403 |
'symbol_as_event' => '[^\\d\\D]', |
| 404 |
'description' => 'Discard rule for <[[^\\d\\D]]>' |
| 405 |
}, |
| 406 |
]; |
| 407 |
$lexer_symbols = { |
| 408 |
'[:discard]' => { |
| 409 |
'display_form' => ':discard', |
| 410 |
'description' => 'Internal LHS for lexer "L0" discard' |
| 411 |
}, |
| 412 |
'[[^\\d\\D]]' => { |
| 413 |
'dsl_form' => '[^\\d\\D]', |
| 414 |
'display_form' => '[^\\d\\D]', |
| 415 |
'description' => 'Character class: [^\\d\\D]' |
| 416 |
} |
| 417 |
}; |
| 418 |
} ## end if ( not $lexer_rules ) |
| 419 |
|
| 420 |
my %lex_lhs = (); |
| 421 |
my %lex_rhs = (); |
| 422 |
my %lex_separator = (); |
| 423 |
my %lexer_rule_by_tag = (); |
| 424 |
|
| 425 |
my $rule_tag = 'rule0'; |
| 426 |
for my $lex_rule ( @{$lexer_rules} ) { |
| 427 |
$lex_rule->{tag} = ++$rule_tag; |
| 428 |
my %lex_rule_copy = %{$lex_rule}; |
| 429 |
$lexer_rule_by_tag{$rule_tag} = \%lex_rule_copy; |
| 430 |
delete $lex_rule->{event}; |
| 431 |
delete $lex_rule->{symbol_as_event}; |
| 432 |
$lex_lhs{ $lex_rule->{lhs} } = 1; |
| 433 |
$lex_rhs{$_} = 1 for @{ $lex_rule->{rhs} }; |
| 434 |
if ( defined( my $separator = $lex_rule->{separator} ) ) { |
| 435 |
$lex_separator{$separator} = 1; |
| 436 |
} |
| 437 |
} ## end for my $lex_rule ( @{$lexer_rules} ) |
| 438 |
|
| 439 |
my %this_lexer_symbols = (); |
| 440 |
SYMBOL: |
| 441 |
for my $symbol_name ( ( keys %lex_lhs ), ( keys %lex_rhs ), |
| 442 |
( keys %lex_separator ) ) |
| 443 |
{ |
| 444 |
my $symbol_data = $lexer_symbols->{$symbol_name}; |
| 445 |
$this_lexer_symbols{$symbol_name} = $symbol_data |
| 446 |
if defined $symbol_data; |
| 447 |
} ## end SYMBOL: for my $symbol_name ( ( keys %lex_lhs ), ( keys %lex_rhs...)) |
| 448 |
|
| 449 |
my %is_lexeme_in_this_lexer = map { $_ => 1 } |
| 450 |
grep { not $lex_rhs{$_} and not $lex_separator{$_} } |
| 451 |
keys %lex_lhs; |
| 452 |
|
| 453 |
my @lex_lexeme_names = keys %is_lexeme_in_this_lexer; |
| 454 |
|
| 455 |
Marpa::R2::exception( "No lexemes in lexer: $lexer_name\n", |
| 456 |
" An SLIF grammar must have at least one lexeme\n" ) |
| 457 |
if not scalar @lex_lexeme_names; |
| 458 |
|
| 459 |
# Do I need this? |
| 460 |
my @unproductive = |
| 461 |
map {"<$_>"} |
| 462 |
grep { not $lex_lhs{$_} and not $_ =~ /\A \[\[ /xms } |
| 463 |
( keys %lex_rhs, keys %lex_separator ); |
| 464 |
if (@unproductive) { |
| 465 |
Marpa::R2::exception( 'Unproductive lexical symbols: ', |
| 466 |
join q{ }, @unproductive ); |
| 467 |
} |
| 468 |
|
| 469 |
$this_lexer_symbols{$lex_start_symbol_name}->{display_form} = |
| 470 |
':start_lex'; |
| 471 |
$this_lexer_symbols{$lex_start_symbol_name}->{description} = |
| 472 |
'Internal L0 (lexical) start symbol'; |
| 473 |
push @{$lexer_rules}, map { |
| 474 |
; |
| 475 |
{ description => "Internal lexical start rule for <$_>", |
| 476 |
lhs => $lex_start_symbol_name, |
| 477 |
rhs => [$_] |
| 478 |
} |
| 479 |
} sort keys %is_lexeme_in_this_lexer; |
| 480 |
|
| 481 |
# Prepare the arguments for the lex grammar |
| 482 |
my %lex_args = (); |
| 483 |
$lex_args{trace_file_handle} = $trace_fh; |
| 484 |
$lex_args{start} = $lex_start_symbol_name; |
| 485 |
$lex_args{'_internal_'} = |
| 486 |
{ 'if_inaccessible' => $if_inaccessible_default }; |
| 487 |
$lex_args{rules} = $lexer_rules; |
| 488 |
$lex_args{symbols} = \%this_lexer_symbols; |
| 489 |
|
| 490 |
# Create the thick lex grammar |
| 491 |
my $lex_grammar = Marpa::R2::Grammar->new( \%lex_args ); |
| 492 |
$thick_grammar_by_lexer_name{$lexer_name} = $lex_grammar; |
| 493 |
my $lex_tracer = $lex_grammar->tracer(); |
| 494 |
my $lex_thin = $lex_tracer->grammar(); |
| 495 |
|
| 496 |
my $lex_discard_symbol_id = |
| 497 |
$lex_tracer->symbol_by_name($discard_symbol_name) // -1; |
| 498 |
my @lex_lexeme_to_g1_symbol; |
| 499 |
$lex_lexeme_to_g1_symbol[$_] = -1 for 0 .. $g1_thin->highest_symbol_id(); |
| 500 |
|
| 501 |
LEXEME_NAME: for my $lexeme_name (@lex_lexeme_names) { |
| 502 |
next LEXEME_NAME if $lexeme_name eq $discard_symbol_name; |
| 503 |
next LEXEME_NAME if $lexeme_name eq $lex_start_symbol_name; |
| 504 |
my $g1_symbol_id = $g1_id_by_lexeme_name{$lexeme_name}; |
| 505 |
if ( not defined $g1_symbol_id ) { |
| 506 |
Marpa::R2::exception( |
| 507 |
qq{<$lexeme_name> is a lexeme but it is not a legal lexeme in G1:\n}, |
| 508 |
qq{ Lexemes must be G1 symbols that do not appear on a G1 LHS.\n} |
| 509 |
); |
| 510 |
} |
| 511 |
if ( not $g1_thin->symbol_is_accessible($g1_symbol_id) ) { |
| 512 |
my $message = |
| 513 |
"A lexeme in lexer $lexer_name is not accessible from the G1 start symbol: $lexeme_name"; |
| 514 |
say {$trace_fh} $message |
| 515 |
if $if_inaccessible_default eq 'warn'; |
| 516 |
Marpa::R2::exception($message) |
| 517 |
if $if_inaccessible_default eq 'fatal'; |
| 518 |
} ## end if ( not $g1_thin->symbol_is_accessible($g1_symbol_id...)) |
| 519 |
my $lex_symbol_id = $lex_tracer->symbol_by_name($lexeme_name); |
| 520 |
$lexeme_data{$lexeme_name}{lexers}{$lexer_name}{'id'} = |
| 521 |
$lex_symbol_id; |
| 522 |
$lex_lexeme_to_g1_symbol[$lex_symbol_id] = $g1_symbol_id; |
| 523 |
} ## end LEXEME_NAME: for my $lexeme_name (@lex_lexeme_names) |
| 524 |
|
| 525 |
my @lex_rule_to_g1_lexeme; |
| 526 |
my $lex_start_symbol_id = |
| 527 |
$lex_tracer->symbol_by_name($lex_start_symbol_name); |
| 528 |
RULE_ID: for my $rule_id ( 0 .. $lex_thin->highest_rule_id() ) { |
| 529 |
my $lhs_id = $lex_thin->rule_lhs($rule_id); |
| 530 |
if ( $lhs_id == $lex_discard_symbol_id ) { |
| 531 |
$lex_rule_to_g1_lexeme[$rule_id] = -2; |
| 532 |
next RULE_ID; |
| 533 |
} |
| 534 |
if ( $lhs_id != $lex_start_symbol_id ) { |
| 535 |
$lex_rule_to_g1_lexeme[$rule_id] = -1; |
| 536 |
next RULE_ID; |
| 537 |
} |
| 538 |
my $lexer_lexeme_id = $lex_thin->rule_rhs( $rule_id, 0 ); |
| 539 |
if ( $lexer_lexeme_id == $lex_discard_symbol_id ) { |
| 540 |
$lex_rule_to_g1_lexeme[$rule_id] = -1; |
| 541 |
next RULE_ID; |
| 542 |
} |
| 543 |
my $lexeme_id = $lex_lexeme_to_g1_symbol[$lexer_lexeme_id] // -1; |
| 544 |
$lex_rule_to_g1_lexeme[$rule_id] = $lexeme_id; |
| 545 |
next RULE_ID if $lexeme_id < 0; |
| 546 |
my $lexeme_name = $g1_tracer->symbol_name($lexeme_id); |
| 547 |
|
| 548 |
# If 1 is the default, we don't need an assertion |
| 549 |
next RULE_ID if not $lexeme_data{$lexeme_name}{latm}; |
| 550 |
|
| 551 |
my $assertion_id = |
| 552 |
$lexeme_data{$lexeme_name}{lexers}{$lexer_name}{'assertion'}; |
| 553 |
if ( not defined $assertion_id ) { |
| 554 |
$assertion_id = $lex_thin->zwa_new(0); |
| 555 |
|
| 556 |
if ( $trace_terminals >= 2 ) { |
| 557 |
say {$trace_fh} "Assertion $assertion_id defaults to 0"; |
| 558 |
} |
| 559 |
|
| 560 |
$lexeme_data{$lexeme_name}{lexers}{$lexer_name}{'assertion'} = |
| 561 |
$assertion_id; |
| 562 |
} ## end if ( not defined $assertion_id ) |
| 563 |
$lex_thin->zwa_place( $assertion_id, $rule_id, 0 ); |
| 564 |
if ( $trace_terminals >= 2 ) { |
| 565 |
say {$trace_fh} |
| 566 |
"Assertion $assertion_id applied to $lexer_name rule ", |
| 567 |
slg_rule_show( $slg, $rule_id, $lex_grammar ); |
| 568 |
} |
| 569 |
} ## end RULE_ID: for my $rule_id ( 0 .. $lex_thin->highest_rule_id() ) |
| 570 |
|
| 571 |
Marpa::R2::Internal::Grammar::slif_precompute($lex_grammar); |
| 572 |
|
| 573 |
my @class_table = (); |
| 574 |
|
| 575 |
CLASS_SYMBOL: |
| 576 |
for my $class_symbol ( sort keys %{$character_class_hash} ) { |
| 577 |
my $symbol_id = $lex_tracer->symbol_by_name($class_symbol); |
| 578 |
next CLASS_SYMBOL if not defined $symbol_id; |
| 579 |
my $cc_components = $character_class_hash->{$class_symbol}; |
| 580 |
my ( $compiled_re, $error ) = |
| 581 |
Marpa::R2::Internal::MetaAST::char_class_to_re($cc_components); |
| 582 |
if ( not $compiled_re ) { |
| 583 |
$error =~ s/^/ /gxms; #indent all lines |
| 584 |
Marpa::R2::exception( |
| 585 |
"Failed belatedly to evaluate character class\n", $error ); |
| 586 |
} |
| 587 |
push @class_table, [ $symbol_id, $compiled_re ]; |
| 588 |
} ## end CLASS_SYMBOL: for my $class_symbol ( sort keys %{...}) |
| 589 |
$character_class_table_by_lexer_name{$lexer_name} = \@class_table; |
| 590 |
|
| 591 |
$lexer_and_rule_to_g1_lexeme{$lexer_name} = \@lex_rule_to_g1_lexeme; |
| 592 |
|
| 593 |
# Apply defaults to determine the discard event for every |
| 594 |
# rule id of the lexer. |
| 595 |
|
| 596 |
my $default_discard_event = $discard_default_adverbs->{event}; |
| 597 |
RULE_ID: for my $rule_id ( 0 .. $lex_thin->highest_rule_id() ) { |
| 598 |
my $tag = $lex_grammar->tag($rule_id); |
| 599 |
next RULE_ID if not defined $tag; |
| 600 |
my $event; |
| 601 |
FIND_EVENT: { |
| 602 |
$event = $lexer_rule_by_tag{$tag}->{event}; |
| 603 |
last FIND_EVENT if defined $event; |
| 604 |
my $lhs_id = $lex_thin->rule_lhs($rule_id); |
| 605 |
last FIND_EVENT if $lhs_id != $lex_discard_symbol_id; |
| 606 |
$event = $default_discard_event; |
| 607 |
} ## end FIND_EVENT: |
| 608 |
next RULE_ID if not defined $event; |
| 609 |
|
| 610 |
my ( $event_name, $event_starts_active ) = @{$event}; |
| 611 |
if ( $event_name eq q{'symbol} ) { |
| 612 |
my @event = ( |
| 613 |
$lexer_rule_by_tag{$tag}->{symbol_as_event}, |
| 614 |
$event_starts_active |
| 615 |
); |
| 616 |
$discard_event_by_lexer_rule_id[$rule_id] = \@event; |
| 617 |
next RULE_ID; |
| 618 |
} ## end if ( $event_name eq q{'symbol} ) |
| 619 |
if ( ( substr $event_name, 0, 1 ) ne q{'} ) { |
| 620 |
$discard_event_by_lexer_rule_id[$rule_id] = $event; |
| 621 |
next RULE_ID; |
| 622 |
} |
| 623 |
Marpa::R2::exception( |
| 624 |
qq{Discard event has unknown name: "$event_name"} |
| 625 |
); |
| 626 |
|
| 627 |
} ## end RULE_ID: for my $rule_id ( 0 .. $lex_thin->highest_rule_id() ) |
| 628 |
|
| 629 |
# Post-lexer G1 processing |
| 630 |
|
| 631 |
my $thick_L0 = $thick_grammar_by_lexer_name{'L0'}; |
| 632 |
my $thin_L0 = $thick_L0->[Marpa::R2::Internal::Grammar::C]; |
| 633 |
my $thin_slg = $slg->[Marpa::R2::Internal::Scanless::G::C] = |
| 634 |
Marpa::R2::Thin::SLG->new( $thin_L0, $g1_tracer->grammar() ); |
| 635 |
|
| 636 |
# Relies on default lexer being given number zero |
| 637 |
$lexer_id_by_name{'L0'} = 0; |
| 638 |
|
| 639 |
LEXEME: for my $lexeme_name ( keys %g1_id_by_lexeme_name ) { |
| 640 |
Marpa::R2::exception( |
| 641 |
"A lexeme in G1 is not a lexeme in any of the lexers: $lexeme_name" |
| 642 |
) if not defined $lexeme_data{$lexeme_name}{'lexers'}; |
| 643 |
} |
| 644 |
|
| 645 |
# At this point we know which symbols are lexemes. |
| 646 |
# So now let's check for inconsistencies |
| 647 |
|
| 648 |
# Check for lexeme declarations for things which are not lexemes |
| 649 |
for my $lexeme_name ( keys %{$lexeme_declarations} ) { |
| 650 |
Marpa::R2::exception( |
| 651 |
"Symbol <$lexeme_name> is declared as a lexeme, but it is not used as one.\n" |
| 652 |
) if not defined $g1_id_by_lexeme_name{$lexeme_name}; |
| 653 |
} |
| 654 |
|
| 655 |
# Now that we know the lexemes, check attempts to defined a |
| 656 |
# completion or a nulled event for one |
| 657 |
for my $symbol_name ( keys %{$completion_events_by_name} ) { |
| 658 |
Marpa::R2::exception( |
| 659 |
"A completion event is declared for <$symbol_name>, but it is a lexeme.\n", |
| 660 |
" Completion events are only valid for symbols on the LHS of G1 rules.\n" |
| 661 |
) if defined $g1_id_by_lexeme_name{$symbol_name}; |
| 662 |
} ## end for my $symbol_name ( keys %{$completion_events_by_name...}) |
| 663 |
|
| 664 |
for my $symbol_name ( keys %{$nulled_events_by_name} ) { |
| 665 |
Marpa::R2::exception( |
| 666 |
"A nulled event is declared for <$symbol_name>, but it is a G1 lexeme.\n", |
| 667 |
" nulled events are only valid for symbols on the LHS of G1 rules.\n" |
| 668 |
) if defined $g1_id_by_lexeme_name{$symbol_name}; |
| 669 |
} ## end for my $symbol_name ( keys %{$nulled_events_by_name} ) |
| 670 |
|
| 671 |
# Mark the lexemes, and set their data |
| 672 |
# Now that we have created the SLG, we can set the latm value, |
| 673 |
# already determined above. |
| 674 |
LEXEME: for my $lexeme_name ( keys %g1_id_by_lexeme_name ) { |
| 675 |
my $g1_lexeme_id = $g1_id_by_lexeme_name{$lexeme_name}; |
| 676 |
my $declarations = $lexeme_declarations->{$lexeme_name}; |
| 677 |
my $priority = $declarations->{priority} // 0; |
| 678 |
$thin_slg->g1_lexeme_set( $g1_lexeme_id, $priority ); |
| 679 |
my $latm_value = $lexeme_data{$lexeme_name}{latm} // 0; |
| 680 |
$thin_slg->g1_lexeme_latm_set( $g1_lexeme_id, $latm_value ); |
| 681 |
my $pause_value = $declarations->{pause}; |
| 682 |
if ( defined $pause_value ) { |
| 683 |
$thin_slg->g1_lexeme_pause_set( $g1_lexeme_id, $pause_value ); |
| 684 |
my $is_active = 1; |
| 685 |
|
| 686 |
if ( defined( my $event_data = $declarations->{'event'} ) ) { |
| 687 |
my $event_name; |
| 688 |
( $event_name, $is_active ) = @{$event_data}; |
| 689 |
$lexeme_events_by_id->[$g1_lexeme_id] = $event_name; |
| 690 |
push @{ $symbol_ids_by_event_name_and_type->{$event_name} |
| 691 |
->{lexeme} }, $g1_lexeme_id; |
| 692 |
} ## end if ( defined( my $event_data = $declarations->{'event'...})) |
| 693 |
|
| 694 |
$thin_slg->g1_lexeme_pause_activate( $g1_lexeme_id, $is_active ); |
| 695 |
} ## end if ( defined $pause_value ) |
| 696 |
|
| 697 |
} ## end LEXEME: for my $lexeme_name ( keys %g1_id_by_lexeme_name ) |
| 698 |
|
| 699 |
# Second phase of lexer processing |
| 700 |
my $lexer_rule_to_g1_lexeme = $lexer_and_rule_to_g1_lexeme{$lexer_name}; |
| 701 |
|
| 702 |
RULE_ID: for my $lexer_rule_id ( 0 .. $#{$lexer_rule_to_g1_lexeme} ) { |
| 703 |
my $g1_lexeme_id = $lexer_rule_to_g1_lexeme->[$lexer_rule_id]; |
| 704 |
my $lexeme_name = $g1_tracer->symbol_name($g1_lexeme_id); |
| 705 |
my $assertion_id = |
| 706 |
$lexeme_data{$lexeme_name}{lexers}{$lexer_name}{'assertion'} |
| 707 |
// -1; |
| 708 |
$thin_slg->lexer_rule_to_g1_lexeme_set( $lexer_rule_id, |
| 709 |
$g1_lexeme_id, $assertion_id ); |
| 710 |
my $discard_event = $discard_event_by_lexer_rule_id[$lexer_rule_id]; |
| 711 |
if ( defined $discard_event ) { |
| 712 |
my ( $event_name, $is_active ) = @{$discard_event}; |
| 713 |
$slg->[ |
| 714 |
Marpa::R2::Internal::Scanless::G::DISCARD_EVENT_BY_LEXER_RULE |
| 715 |
]->[$lexer_rule_id] = $event_name; |
| 716 |
push @{ $symbol_ids_by_event_name_and_type->{$event_name} |
| 717 |
->{discard} }, $lexer_rule_id; |
| 718 |
$thin_slg->discard_event_set( $lexer_rule_id, 1 ); |
| 719 |
$thin_slg->discard_event_activate( $lexer_rule_id, 1 ) |
| 720 |
if $is_active; |
| 721 |
} ## end if ( defined $discard_event ) |
| 722 |
} ## end RULE_ID: for my $lexer_rule_id ( 0 .. $#{$lexer_rule_to_g1_lexeme...}) |
| 723 |
|
| 724 |
# Second phase of G1 processing |
| 725 |
|
| 726 |
$thin_slg->precompute(); |
| 727 |
$slg->[Marpa::R2::Internal::Scanless::G::THICK_G1_GRAMMAR] = |
| 728 |
$thick_g1_grammar; |
| 729 |
|
| 730 |
# More lexer processing |
| 731 |
# Determine events by lexer rule, applying the defaults |
| 732 |
|
| 733 |
{ |
| 734 |
my $character_class_table = |
| 735 |
$character_class_table_by_lexer_name{$lexer_name}; |
| 736 |
$slg->[Marpa::R2::Internal::Scanless::G::CHARACTER_CLASS_TABLES] |
| 737 |
->[$lexer_id] = $character_class_table; |
| 738 |
$slg->[Marpa::R2::Internal::Scanless::G::THICK_LEX_GRAMMARS] |
| 739 |
->[$lexer_id] = $thick_grammar_by_lexer_name{$lexer_name}; |
| 740 |
} |
| 741 |
|
| 742 |
# This section violates the NAIF interface, directly changing some |
| 743 |
# of its internal structures. |
| 744 |
# |
| 745 |
# Some lexeme default adverbs are applied in earlier phases. |
| 746 |
# |
| 747 |
APPLY_DEFAULT_LEXEME_ADVERBS: { |
| 748 |
last APPLY_DEFAULT_LEXEME_ADVERBS if not $lexeme_default_adverbs; |
| 749 |
|
| 750 |
my $action = $lexeme_default_adverbs->{action}; |
| 751 |
my $g1_symbols = |
| 752 |
$thick_g1_grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 753 |
LEXEME: |
| 754 |
for my $lexeme_name ( keys %g1_id_by_lexeme_name ) { |
| 755 |
my $g1_lexeme_id = $g1_id_by_lexeme_name{$lexeme_name}; |
| 756 |
my $g1_symbol = $g1_symbols->[$g1_lexeme_id]; |
| 757 |
next LEXEME if $lexeme_name =~ m/ \] \z/xms; |
| 758 |
$g1_symbol->[Marpa::R2::Internal::Symbol::LEXEME_SEMANTICS] //= |
| 759 |
$action; |
| 760 |
} ## end LEXEME: for my $lexeme_name ( keys %g1_id_by_lexeme_name ) |
| 761 |
|
| 762 |
my $blessing = $lexeme_default_adverbs->{bless}; |
| 763 |
last APPLY_DEFAULT_LEXEME_ADVERBS if not $blessing; |
| 764 |
last APPLY_DEFAULT_LEXEME_ADVERBS if $blessing eq '::undef'; |
| 765 |
|
| 766 |
LEXEME: |
| 767 |
for my $lexeme_name ( keys %g1_id_by_lexeme_name ) { |
| 768 |
my $g1_lexeme_id = $g1_id_by_lexeme_name{$lexeme_name}; |
| 769 |
my $g1_symbol = $g1_symbols->[$g1_lexeme_id]; |
| 770 |
next LEXEME if $lexeme_name =~ m/ \] \z/xms; |
| 771 |
if ( $blessing eq '::name' ) { |
| 772 |
if ( $lexeme_name =~ / [^ [:alnum:]] /xms ) { |
| 773 |
Marpa::R2::exception( |
| 774 |
qq{Lexeme blessing by '::name' only allowed if lexeme name is whitespace and alphanumerics\n}, |
| 775 |
qq{ Problematic lexeme was <$lexeme_name>\n} |
| 776 |
); |
| 777 |
} ## end if ( $lexeme_name =~ / [^ [:alnum:]] /xms ) |
| 778 |
my $blessing_by_name = $lexeme_name; |
| 779 |
$blessing_by_name =~ s/[ ]/_/gxms; |
| 780 |
$g1_symbol->[Marpa::R2::Internal::Symbol::BLESSING] //= |
| 781 |
$blessing_by_name; |
| 782 |
next LEXEME; |
| 783 |
} ## end if ( $blessing eq '::name' ) |
| 784 |
if ( $blessing =~ / [\W] /xms ) { |
| 785 |
Marpa::R2::exception( |
| 786 |
qq{Blessing lexeme as '$blessing' is not allowed\n}, |
| 787 |
qq{ Problematic lexeme was <$lexeme_name>\n} |
| 788 |
); |
| 789 |
} ## end if ( $blessing =~ / [\W] /xms ) |
| 790 |
$g1_symbol->[Marpa::R2::Internal::Symbol::BLESSING] //= $blessing; |
| 791 |
} ## end LEXEME: for my $lexeme_name ( keys %g1_id_by_lexeme_name ) |
| 792 |
|
| 793 |
} ## end APPLY_DEFAULT_LEXEME_ADVERBS: |
| 794 |
|
| 795 |
return $slg; |
| 796 |
|
| 797 |
} ## end sub Marpa::R2::Internal::Scanless::G::hash_to_runtime |
| 798 |
|
| 799 |
sub thick_subgrammar_by_name { |
| 800 |
my ( $slg, $subgrammar ) = @_; |
| 801 |
|
| 802 |
# Allow G0 as legacy synonym for L0 |
| 803 |
state $grammar_names = { 'G0' => 1, 'G1' => 1, 'L0' => 1 }; |
| 804 |
$subgrammar //= 'G1'; |
| 805 |
|
| 806 |
Marpa::R2::exception(qq{No lexer named "$subgrammar"}) |
| 807 |
if not defined $grammar_names->{$subgrammar}; |
| 808 |
|
| 809 |
return $slg->[Marpa::R2::Internal::Scanless::G::THICK_G1_GRAMMAR] |
| 810 |
if $subgrammar eq 'G1'; |
| 811 |
|
| 812 |
return $slg->[Marpa::R2::Internal::Scanless::G::THICK_LEX_GRAMMARS] |
| 813 |
->[0]; |
| 814 |
} ## end sub thick_subgrammar_by_name |
| 815 |
|
| 816 |
sub Marpa::R2::Scanless::G::start_symbol_id { |
| 817 |
my ( $slg, $rule_id, $subgrammar ) = @_; |
| 818 |
return thick_subgrammar_by_name( $slg, $subgrammar )->start_symbol(); |
| 819 |
} |
| 820 |
|
| 821 |
sub Marpa::R2::Scanless::G::rule_name { |
| 822 |
my ( $slg, $rule_id, $subgrammar ) = @_; |
| 823 |
return thick_subgrammar_by_name( $slg, $subgrammar )->rule_name($rule_id); |
| 824 |
} |
| 825 |
|
| 826 |
sub Marpa::R2::Scanless::G::rule_expand { |
| 827 |
my ( $slg, $rule_id, $subgrammar ) = @_; |
| 828 |
return thick_subgrammar_by_name( $slg, $subgrammar )->tracer() |
| 829 |
->rule_expand($rule_id); |
| 830 |
} |
| 831 |
|
| 832 |
sub Marpa::R2::Scanless::G::symbol_name { |
| 833 |
my ( $slg, $symbol_id, $subgrammar ) = @_; |
| 834 |
return thick_subgrammar_by_name($slg, $subgrammar)->tracer() |
| 835 |
->symbol_name($symbol_id); |
| 836 |
} |
| 837 |
|
| 838 |
sub Marpa::R2::Scanless::G::symbol_display_form { |
| 839 |
my ( $slg, $symbol_id, $subgrammar ) = @_; |
| 840 |
return thick_subgrammar_by_name( $slg, $subgrammar ) |
| 841 |
->symbol_in_display_form($symbol_id); |
| 842 |
} |
| 843 |
|
| 844 |
sub Marpa::R2::Scanless::G::symbol_dsl_form { |
| 845 |
my ( $slg, $symbol_id, $subgrammar ) = @_; |
| 846 |
return thick_subgrammar_by_name( $slg, $subgrammar ) |
| 847 |
->symbol_dsl_form($symbol_id); |
| 848 |
} |
| 849 |
|
| 850 |
sub Marpa::R2::Scanless::G::symbol_description { |
| 851 |
my ( $slg, $symbol_id, $subgrammar ) = @_; |
| 852 |
return thick_subgrammar_by_name($slg, $subgrammar) |
| 853 |
->symbol_description($symbol_id); |
| 854 |
} |
| 855 |
|
| 856 |
sub Marpa::R2::Scanless::G::rule_show |
| 857 |
{ |
| 858 |
my ( $slg, $rule_id, $subgrammar) = @_; |
| 859 |
return slg_rule_show($slg, $rule_id, thick_subgrammar_by_name($slg, $subgrammar)); |
| 860 |
} |
| 861 |
|
| 862 |
sub slg_rule_show { |
| 863 |
my ( $slg, $rule_id, $subgrammar ) = @_; |
| 864 |
my $tracer = $subgrammar->tracer(); |
| 865 |
my $subgrammar_c = $subgrammar->[Marpa::R2::Internal::Grammar::C]; |
| 866 |
my @symbol_ids = $tracer->rule_expand($rule_id); |
| 867 |
return if not scalar @symbol_ids; |
| 868 |
my ( $lhs, @rhs ) = |
| 869 |
map { $subgrammar->symbol_in_display_form($_) } @symbol_ids; |
| 870 |
my $minimum = $subgrammar_c->sequence_min($rule_id); |
| 871 |
my @quantifier = (); |
| 872 |
|
| 873 |
if ( defined $minimum ) { |
| 874 |
@quantifier = ( $minimum <= 0 ? q{*} : q{+} ); |
| 875 |
} |
| 876 |
return join q{ }, $lhs, q{::=}, @rhs, @quantifier; |
| 877 |
} ## end sub slg_rule_show |
| 878 |
|
| 879 |
sub Marpa::R2::Scanless::G::show_rules { |
| 880 |
my ( $slg, $verbose, $subgrammar ) = @_; |
| 881 |
my $text = q{}; |
| 882 |
$verbose //= 0; |
| 883 |
$subgrammar //= 'G1'; |
| 884 |
|
| 885 |
my $thick_grammar = thick_subgrammar_by_name($slg, $subgrammar); |
| 886 |
|
| 887 |
my $rules = $thick_grammar->[Marpa::R2::Internal::Grammar::RULES]; |
| 888 |
my $grammar_c = $thick_grammar->[Marpa::R2::Internal::Grammar::C]; |
| 889 |
|
| 890 |
for my $rule ( @{$rules} ) { |
| 891 |
my $rule_id = $rule->[Marpa::R2::Internal::Rule::ID]; |
| 892 |
|
| 893 |
my $minimum = $grammar_c->sequence_min($rule_id); |
| 894 |
my @quantifier = |
| 895 |
defined $minimum ? $minimum <= 0 ? (q{*}) : (q{+}) : (); |
| 896 |
my $lhs_id = $grammar_c->rule_lhs($rule_id); |
| 897 |
my $rule_length = $grammar_c->rule_length($rule_id); |
| 898 |
my @rhs_ids = |
| 899 |
map { $grammar_c->rule_rhs( $rule_id, $_ ) } |
| 900 |
( 0 .. $rule_length - 1 ); |
| 901 |
$text .= join q{ }, $subgrammar, "R$rule_id", |
| 902 |
$thick_grammar->symbol_in_display_form($lhs_id), |
| 903 |
'::=', |
| 904 |
( map { $thick_grammar->symbol_in_display_form($_) } @rhs_ids ), |
| 905 |
@quantifier; |
| 906 |
$text .= "\n"; |
| 907 |
|
| 908 |
if ( $verbose >= 2 ) { |
| 909 |
|
| 910 |
my $description = $rule->[Marpa::R2::Internal::Rule::DESCRIPTION]; |
| 911 |
$text .= " $description\n" if $description; |
| 912 |
my @comment = (); |
| 913 |
$grammar_c->rule_length($rule_id) == 0 |
| 914 |
and push @comment, 'empty'; |
| 915 |
$thick_grammar->rule_is_used($rule_id) |
| 916 |
or push @comment, '!used'; |
| 917 |
$grammar_c->rule_is_productive($rule_id) |
| 918 |
or push @comment, 'unproductive'; |
| 919 |
$grammar_c->rule_is_accessible($rule_id) |
| 920 |
or push @comment, 'inaccessible'; |
| 921 |
$rule->[Marpa::R2::Internal::Rule::DISCARD_SEPARATION] |
| 922 |
and push @comment, 'discard_sep'; |
| 923 |
|
| 924 |
if (@comment) { |
| 925 |
$text .= q{ } . ( join q{ }, q{/*}, @comment, q{*/} ) . "\n"; |
| 926 |
} |
| 927 |
|
| 928 |
$text .= " Symbol IDs: <$lhs_id> ::= " |
| 929 |
. ( join q{ }, map {"<$_>"} @rhs_ids ) . "\n"; |
| 930 |
|
| 931 |
} ## end if ( $verbose >= 2 ) |
| 932 |
|
| 933 |
if ( $verbose >= 3 ) { |
| 934 |
|
| 935 |
my $tracer = $thick_grammar->tracer(); |
| 936 |
|
| 937 |
$text |
| 938 |
.= " Internal symbols: <" |
| 939 |
. $tracer->symbol_name($lhs_id) |
| 940 |
. q{> ::= } |
| 941 |
. ( |
| 942 |
join q{ }, |
| 943 |
map { '<' . $tracer->symbol_name($_) . '>' } @rhs_ids |
| 944 |
) . "\n"; |
| 945 |
|
| 946 |
} ## end if ( $verbose >= 3 ) |
| 947 |
|
| 948 |
} ## end for my $rule ( @{$rules} ) |
| 949 |
|
| 950 |
return $text; |
| 951 |
} ## end sub Marpa::R2::Scanless::G::show_rules |
| 952 |
|
| 953 |
sub Marpa::R2::Scanless::G::show_symbols { |
| 954 |
my ( $slg, $verbose, $subgrammar ) = @_; |
| 955 |
my $text = q{}; |
| 956 |
$verbose //= 0; |
| 957 |
$subgrammar //= 'G1'; |
| 958 |
|
| 959 |
my $thick_grammar = thick_subgrammar_by_name($slg, $subgrammar); |
| 960 |
|
| 961 |
my $symbols = $thick_grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 962 |
my $grammar_c = $thick_grammar->[Marpa::R2::Internal::Grammar::C]; |
| 963 |
|
| 964 |
for my $symbol ( @{$symbols} ) { |
| 965 |
my $symbol_id = $symbol->[Marpa::R2::Internal::Symbol::ID]; |
| 966 |
|
| 967 |
$text .= join q{ }, $subgrammar, "S$symbol_id", |
| 968 |
$thick_grammar->symbol_in_display_form($symbol_id); |
| 969 |
|
| 970 |
my $description = $symbol->[Marpa::R2::Internal::Symbol::DESCRIPTION]; |
| 971 |
if ($description) { |
| 972 |
$text .= " -- $description"; |
| 973 |
} |
| 974 |
$text .= "\n"; |
| 975 |
|
| 976 |
if ( $verbose >= 2 ) { |
| 977 |
|
| 978 |
my @tag_list = (); |
| 979 |
$grammar_c->symbol_is_productive($symbol_id) |
| 980 |
or push @tag_list, 'unproductive'; |
| 981 |
$grammar_c->symbol_is_accessible($symbol_id) |
| 982 |
or push @tag_list, 'inaccessible'; |
| 983 |
$grammar_c->symbol_is_nulling($symbol_id) |
| 984 |
and push @tag_list, 'nulling'; |
| 985 |
$grammar_c->symbol_is_terminal($symbol_id) |
| 986 |
and push @tag_list, 'terminal'; |
| 987 |
|
| 988 |
if (@tag_list) { |
| 989 |
$text |
| 990 |
.= q{ } . ( join q{ }, q{/*}, @tag_list, q{*/} ) . "\n"; |
| 991 |
} |
| 992 |
|
| 993 |
my $tracer = $thick_grammar->tracer(); |
| 994 |
$text .= " Internal name: <" |
| 995 |
. $tracer->symbol_name($symbol_id) . qq{>\n}; |
| 996 |
|
| 997 |
} ## end if ( $verbose >= 2 ) |
| 998 |
|
| 999 |
if ( $verbose >= 3 ) { |
| 1000 |
|
| 1001 |
my $dsl_form = $symbol->[Marpa::R2::Internal::Symbol::DSL_FORM]; |
| 1002 |
if ($dsl_form) { $text .= qq{ SLIF name: $dsl_form\n}; } |
| 1003 |
|
| 1004 |
} ## end if ( $verbose >= 3 ) |
| 1005 |
|
| 1006 |
} ## end for my $symbol ( @{$symbols} ) |
| 1007 |
|
| 1008 |
return $text; |
| 1009 |
} ## end sub Marpa::R2::Scanless::G::show_symbols |
| 1010 |
|
| 1011 |
sub Marpa::R2::Scanless::G::show_dotted_rule { |
| 1012 |
my ( $slg, $rule_id, $dot_position ) = @_; |
| 1013 |
my $grammar = $slg->[Marpa::R2::Internal::Scanless::G::THICK_G1_GRAMMAR]; |
| 1014 |
my $tracer = $grammar->tracer(); |
| 1015 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1016 |
my ( $lhs, @rhs ) = |
| 1017 |
map { $grammar->symbol_in_display_form($_) } $tracer->rule_expand($rule_id); |
| 1018 |
my $rhs_length = scalar @rhs; |
| 1019 |
|
| 1020 |
my $minimum = $grammar_c->sequence_min($rule_id); |
| 1021 |
my @quantifier = (); |
| 1022 |
if (defined $minimum) { |
| 1023 |
@quantifier = ($minimum <= 0 ? q{*} : q{+} ); |
| 1024 |
} |
| 1025 |
$dot_position += ($rhs_length + 1) if $dot_position < 0; |
| 1026 |
$dot_position = 0 if $dot_position < 0; |
| 1027 |
if ($dot_position < $rhs_length) { |
| 1028 |
splice @rhs, $dot_position, 0, q{.}; |
| 1029 |
return join q{ }, $lhs, q{->}, @rhs, @quantifier; |
| 1030 |
} else { |
| 1031 |
return join q{ }, $lhs, q{->}, @rhs, @quantifier, q{.}; |
| 1032 |
} |
| 1033 |
} ## end sub Marpa::R2::Grammar::show_dotted_rule |
| 1034 |
|
| 1035 |
sub Marpa::R2::Scanless::G::rule { |
| 1036 |
my ( $slg, @args ) = @_; |
| 1037 |
return $slg->[Marpa::R2::Internal::Scanless::G::THICK_G1_GRAMMAR] |
| 1038 |
->rule(@args); |
| 1039 |
} |
| 1040 |
|
| 1041 |
sub Marpa::R2::Scanless::G::rule_ids { |
| 1042 |
my ($slg, $subgrammar) = @_; |
| 1043 |
return thick_subgrammar_by_name($slg, $subgrammar)->rule_ids(); |
| 1044 |
} |
| 1045 |
|
| 1046 |
sub Marpa::R2::Scanless::G::symbol_ids { |
| 1047 |
my ($slg, $subgrammar) = @_; |
| 1048 |
return thick_subgrammar_by_name($slg, $subgrammar)->symbol_ids(); |
| 1049 |
} |
| 1050 |
|
| 1051 |
sub Marpa::R2::Scanless::G::g1_rule_ids { |
| 1052 |
my ($slg) = @_; |
| 1053 |
return $slg->rule_ids(); |
| 1054 |
} |
| 1055 |
|
| 1056 |
sub Marpa::R2::Scanless::G::g0_rule_ids { |
| 1057 |
my ($slg) = @_; |
| 1058 |
return $slg->rule_ids('L0'); |
| 1059 |
} |
| 1060 |
|
| 1061 |
sub Marpa::R2::Scanless::G::g0_rule { |
| 1062 |
my ( $slg, @args ) = @_; |
| 1063 |
return $slg->[Marpa::R2::Internal::Scanless::G::THICK_LEX_GRAMMARS]->[0] |
| 1064 |
->rule(@args); |
| 1065 |
} |
| 1066 |
|
| 1067 |
# Internal methods, not to be documented |
| 1068 |
|
| 1069 |
sub Marpa::R2::Scanless::G::thick_g1_grammar { |
| 1070 |
my ($slg) = @_; |
| 1071 |
return $slg->[Marpa::R2::Internal::Scanless::G::THICK_G1_GRAMMAR]; |
| 1072 |
} |
| 1073 |
|
| 1074 |
sub Marpa::R2::Scanless::G::show_irls { |
| 1075 |
my ($slg, $subgrammar) = @_; |
| 1076 |
return thick_subgrammar_by_name($slg, $subgrammar)->show_irls(); |
| 1077 |
} |
| 1078 |
|
| 1079 |
sub Marpa::R2::Scanless::G::show_isys { |
| 1080 |
my ($slg, $subgrammar) = @_; |
| 1081 |
return thick_subgrammar_by_name($slg, $subgrammar)->show_isys(); |
| 1082 |
} |
| 1083 |
|
| 1084 |
1; |
| 1085 |
|
| 1086 |
# vim: expandtab shiftwidth=4: |