| 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::Grammar; |
| 17 |
|
| 18 |
use 5.010001; |
| 19 |
|
| 20 |
use warnings; |
| 21 |
|
| 22 |
# There's a problem with this perlcritic check |
| 23 |
# as of 9 Aug 2010 |
| 24 |
no warnings qw(recursion qw); |
| 25 |
|
| 26 |
use strict; |
| 27 |
|
| 28 |
use vars qw($VERSION $STRING_VERSION); |
| 29 |
$VERSION = '14.000000'; |
| 30 |
$STRING_VERSION = $VERSION; |
| 31 |
## no critic(BuiltinFunctions::ProhibitStringyEval) |
| 32 |
$VERSION = eval $VERSION; |
| 33 |
## use critic |
| 34 |
|
| 35 |
package Marpa::R2::Internal::Grammar; |
| 36 |
|
| 37 |
use English qw( -no_match_vars ); |
| 38 |
|
| 39 |
use Marpa::R2::Thin::Trace; |
| 40 |
|
| 41 |
our %DEFAULT_SYMBOLS_RESERVED; |
| 42 |
%DEFAULT_SYMBOLS_RESERVED = map { ($_, 1) } split //xms, '}]>)'; |
| 43 |
|
| 44 |
sub Marpa::R2::uncaught_error { |
| 45 |
my ($error) = @_; |
| 46 |
|
| 47 |
# This would be Carp::confess, but in the testing |
| 48 |
# the stack trace includes the hoped for error |
| 49 |
# message, which causes spurious success reports. |
| 50 |
Carp::croak( "libmarpa reported an error which Marpa::R2 did not catch\n", |
| 51 |
$error ); |
| 52 |
} ## end sub Marpa::R2::uncaught_error |
| 53 |
|
| 54 |
package Marpa::R2::Internal::Grammar; |
| 55 |
|
| 56 |
sub Marpa::R2::Grammar::new { |
| 57 |
my ( $class, @arg_hashes ) = @_; |
| 58 |
|
| 59 |
my $grammar = []; |
| 60 |
bless $grammar, $class; |
| 61 |
|
| 62 |
# set the defaults and the default defaults |
| 63 |
$grammar->[Marpa::R2::Internal::Grammar::TRACE_FILE_HANDLE] = *STDERR; |
| 64 |
|
| 65 |
$grammar->[Marpa::R2::Internal::Grammar::TRACE_RULES] = 0; |
| 66 |
$grammar->[Marpa::R2::Internal::Grammar::WARNINGS] = 1; |
| 67 |
$grammar->[Marpa::R2::Internal::Grammar::INACCESSIBLE_OK] = {}; |
| 68 |
$grammar->[Marpa::R2::Internal::Grammar::UNPRODUCTIVE_OK] = {}; |
| 69 |
$grammar->[Marpa::R2::Internal::Grammar::INFINITE_ACTION] = 'fatal'; |
| 70 |
|
| 71 |
$grammar->[Marpa::R2::Internal::Grammar::SYMBOLS] = []; |
| 72 |
$grammar->[Marpa::R2::Internal::Grammar::RULES] = []; |
| 73 |
$grammar->[Marpa::R2::Internal::Grammar::RULE_ID_BY_TAG] = {}; |
| 74 |
|
| 75 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C] = |
| 76 |
Marpa::R2::Thin::G->new( { if => 1 } ); |
| 77 |
$grammar->[Marpa::R2::Internal::Grammar::TRACER] = |
| 78 |
Marpa::R2::Thin::Trace->new($grammar_c); |
| 79 |
|
| 80 |
$grammar->set(@arg_hashes); |
| 81 |
|
| 82 |
return $grammar; |
| 83 |
} ## end sub Marpa::R2::Grammar::new |
| 84 |
|
| 85 |
sub Marpa::R2::Grammar::tracer { |
| 86 |
return $_[0]->[Marpa::R2::Internal::Grammar::TRACER]; |
| 87 |
} |
| 88 |
|
| 89 |
sub Marpa::R2::Grammar::thin { |
| 90 |
return $_[0]->[Marpa::R2::Internal::Grammar::C]; |
| 91 |
} |
| 92 |
|
| 93 |
sub Marpa::R2::Grammar::thin_symbol { |
| 94 |
my ( $grammar, $symbol_name ) = @_; |
| 95 |
return $grammar->[Marpa::R2::Internal::Grammar::TRACER] |
| 96 |
->symbol_by_name($symbol_name); |
| 97 |
} |
| 98 |
|
| 99 |
sub Marpa::R2::Grammar::set { |
| 100 |
my ( $grammar, @arg_hashes ) = @_; |
| 101 |
|
| 102 |
# set trace_fh even if no tracing, because we may turn it on in this method |
| 103 |
my $trace_fh = |
| 104 |
$grammar->[Marpa::R2::Internal::Grammar::TRACE_FILE_HANDLE]; |
| 105 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 106 |
|
| 107 |
for my $args (@arg_hashes) { |
| 108 |
|
| 109 |
my $ref_type = ref $args; |
| 110 |
if ( not $ref_type ) { |
| 111 |
Carp::croak( |
| 112 |
'Marpa::R2::Grammar expects args as ref to HASH; arg was non-reference' |
| 113 |
); |
| 114 |
} |
| 115 |
if ( $ref_type ne 'HASH' ) { |
| 116 |
Carp::croak( |
| 117 |
"Marpa::R2::Grammar expects args as ref to HASH, got ref to $ref_type instead" |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
state $grammar_options = { |
| 122 |
map { ( $_, 1 ) } |
| 123 |
qw{ _internal_ |
| 124 |
action_object |
| 125 |
actions |
| 126 |
bless_package |
| 127 |
infinite_action |
| 128 |
default_action |
| 129 |
default_empty_action |
| 130 |
default_rank |
| 131 |
inaccessible_ok |
| 132 |
rules |
| 133 |
source |
| 134 |
start |
| 135 |
symbols |
| 136 |
terminals |
| 137 |
trace_file_handle |
| 138 |
unproductive_ok |
| 139 |
warnings |
| 140 |
} |
| 141 |
}; |
| 142 |
|
| 143 |
if (my @bad_options = |
| 144 |
grep { not exists $grammar_options->{$_} } |
| 145 |
keys %{$args} |
| 146 |
) |
| 147 |
{ |
| 148 |
Carp::croak( 'Unknown option(s) for Marpa::R2::Grammar: ', |
| 149 |
join q{ }, @bad_options ); |
| 150 |
} ## end if ( my @bad_options = grep { not exists $grammar_options...}) |
| 151 |
|
| 152 |
# First pass options: These affect processing of other |
| 153 |
# options and are expected to take force for the other |
| 154 |
# options, even if specified afterwards |
| 155 |
|
| 156 |
if ( defined( my $value = $args->{'_internal_'} ) ) { |
| 157 |
$grammar->[Marpa::R2::Internal::Grammar::INTERNAL] = $value; |
| 158 |
} |
| 159 |
|
| 160 |
if ( defined( my $value = $args->{'trace_file_handle'} ) ) { |
| 161 |
$trace_fh = |
| 162 |
$grammar->[Marpa::R2::Internal::Grammar::TRACE_FILE_HANDLE] = |
| 163 |
$value; |
| 164 |
} |
| 165 |
|
| 166 |
if ( defined( my $value = $args->{'default_rank'} ) ) { |
| 167 |
Marpa::R2::exception( |
| 168 |
'default_rank option not allowed after grammar is precomputed' |
| 169 |
) if $grammar_c->is_precomputed(); |
| 170 |
$grammar_c->default_rank_set($value); |
| 171 |
} ## end if ( defined( my $value = $args->{'default_rank'} ) ) |
| 172 |
|
| 173 |
|
| 174 |
# Second pass options |
| 175 |
|
| 176 |
if ( defined( my $value = $args->{'symbols'} ) ) { |
| 177 |
Marpa::R2::exception( |
| 178 |
'symbols option not allowed after grammar is precomputed') |
| 179 |
if $grammar_c->is_precomputed(); |
| 180 |
Marpa::R2::exception('symbols value must be REF to HASH') |
| 181 |
if ref $value ne 'HASH'; |
| 182 |
for my $symbol ( sort keys %{$value} ) { |
| 183 |
my $properties = $value->{$symbol}; |
| 184 |
assign_user_symbol( $grammar, $symbol, $properties ); |
| 185 |
} |
| 186 |
} ## end if ( defined( my $value = $args->{'symbols'} ) ) |
| 187 |
|
| 188 |
if ( defined( my $value = $args->{'terminals'} ) ) { |
| 189 |
Marpa::R2::exception( |
| 190 |
'terminals option not allowed after grammar is precomputed') |
| 191 |
if $grammar_c->is_precomputed(); |
| 192 |
Marpa::R2::exception('terminals value must be REF to ARRAY') |
| 193 |
if ref $value ne 'ARRAY'; |
| 194 |
for my $symbol ( @{$value} ) { |
| 195 |
assign_user_symbol( $grammar, $symbol, { terminal => 1 } ); |
| 196 |
} |
| 197 |
} ## end if ( defined( my $value = $args->{'terminals'} ) ) |
| 198 |
|
| 199 |
if ( defined( my $value = $args->{'start'} ) ) { |
| 200 |
Marpa::R2::exception( |
| 201 |
'start option not allowed after grammar is precomputed') |
| 202 |
if $grammar_c->is_precomputed(); |
| 203 |
$grammar->[Marpa::R2::Internal::Grammar::START_NAME] = $value; |
| 204 |
} ## end if ( defined( my $value = $args->{'start'} ) ) |
| 205 |
|
| 206 |
my $stuifzand_source; |
| 207 |
my $deprecated_source; |
| 208 |
|
| 209 |
if ( defined( my $value = $args->{'source'} ) ) { |
| 210 |
Marpa::R2::exception( |
| 211 |
'source option not allowed after grammar is precomputed') |
| 212 |
if $grammar_c->is_precomputed(); |
| 213 |
Marpa::R2::exception( |
| 214 |
q{"source" named argument must be string or ref to SCALAR} |
| 215 |
) if ref $value ne 'SCALAR'; |
| 216 |
$stuifzand_source = $value; |
| 217 |
} |
| 218 |
|
| 219 |
if ( defined( my $value = $args->{'rules'} ) ) { |
| 220 |
Marpa::R2::exception( |
| 221 |
'rules option not allowed after grammar is precomputed') |
| 222 |
if $grammar_c->is_precomputed(); |
| 223 |
DO_RULES: { |
| 224 |
## These hacks are for previous method of specifying Stuifzand |
| 225 |
## grammars. They are now deprecated and undocumented. |
| 226 |
## Eventually they may be eliminated. |
| 227 |
if ( ref $value eq 'ARRAY' |
| 228 |
and scalar @{$value} == 1 |
| 229 |
and not ref $value->[0] ) |
| 230 |
{ |
| 231 |
$value = $value->[0]; |
| 232 |
} ## end if ( ref $value eq 'ARRAY' and scalar @{$value} == 1...) |
| 233 |
if ( not ref $value ) { |
| 234 |
$deprecated_source = \$value; |
| 235 |
} |
| 236 |
if (defined $deprecated_source and defined $stuifzand_source) { |
| 237 |
Marpa::R2::exception( |
| 238 |
qq{Attempt to specify BNF via both 'rules' and 'source' named arguments\n}, |
| 239 |
q{ You must use one or the other}, |
| 240 |
) |
| 241 |
} |
| 242 |
if (defined $deprecated_source) { |
| 243 |
$stuifzand_source = $deprecated_source; |
| 244 |
last DO_RULES; |
| 245 |
} |
| 246 |
Marpa::R2::exception( |
| 247 |
q{"rules" named argument must be string or ref to ARRAY} |
| 248 |
) if ref $value ne 'ARRAY'; |
| 249 |
$grammar->[Marpa::R2::Internal::Grammar::INTERFACE] //= |
| 250 |
'standard'; |
| 251 |
Marpa::R2::exception( |
| 252 |
qq{Attempt to use the standard interface with a grammar that is already using the BNF interface\n}, |
| 253 |
q{ Mixing the BNF and standard interface is not allowed} |
| 254 |
) |
| 255 |
if $grammar->[Marpa::R2::Internal::Grammar::INTERFACE] ne |
| 256 |
'standard'; |
| 257 |
add_user_rules( $grammar, $value ); |
| 258 |
} ## end DO_RULES: |
| 259 |
} ## end if ( defined( my $value = $args->{'rules'} ) ) |
| 260 |
|
| 261 |
if ( defined $stuifzand_source ) { |
| 262 |
$grammar->[Marpa::R2::Internal::Grammar::INTERFACE] //= |
| 263 |
'stuifzand'; |
| 264 |
Marpa::R2::exception( |
| 265 |
qq{Attempt to use the standard interface with a grammar that is already using the BNF interface\n}, |
| 266 |
q{ Mixing the BNF and standard interface is not allowed} |
| 267 |
) |
| 268 |
if $grammar->[Marpa::R2::Internal::Grammar::INTERFACE] ne |
| 269 |
'stuifzand'; |
| 270 |
my $parse_result = |
| 271 |
Marpa::R2::Internal::Stuifzand::parse_rules( |
| 272 |
$stuifzand_source ); |
| 273 |
for my $rule ( @{ $parse_result->{rules} } ) { |
| 274 |
add_user_rule( $grammar, $rule ); |
| 275 |
} |
| 276 |
} ## end if ( defined $stuifzand_source ) |
| 277 |
|
| 278 |
if ( exists $args->{'default_empty_action'} ) { |
| 279 |
my $value = $args->{'default_empty_action'}; |
| 280 |
$grammar->[Marpa::R2::Internal::Grammar::DEFAULT_EMPTY_ACTION] = |
| 281 |
$value; |
| 282 |
} |
| 283 |
|
| 284 |
if ( defined( my $value = $args->{'actions'} ) ) { |
| 285 |
$grammar->[Marpa::R2::Internal::Grammar::ACTIONS] = $value; |
| 286 |
} |
| 287 |
|
| 288 |
if ( defined( my $value = $args->{'bless_package'} ) ) { |
| 289 |
$grammar->[Marpa::R2::Internal::Grammar::BLESS_PACKAGE] = $value; |
| 290 |
} |
| 291 |
|
| 292 |
if ( defined( my $value = $args->{'action_object'} ) ) { |
| 293 |
$grammar->[Marpa::R2::Internal::Grammar::ACTION_OBJECT] = $value; |
| 294 |
} |
| 295 |
|
| 296 |
if ( defined( my $value = $args->{'default_action'} ) ) { |
| 297 |
$grammar->[Marpa::R2::Internal::Grammar::DEFAULT_ACTION] = $value; |
| 298 |
} |
| 299 |
|
| 300 |
if ( defined( my $value = $args->{'infinite_action'} ) ) { |
| 301 |
if ( $value && $grammar_c->is_precomputed() ) { |
| 302 |
say {$trace_fh} |
| 303 |
'"infinite_action" option is useless after grammar is precomputed' |
| 304 |
or Marpa::R2::exception("Could not print: $ERRNO"); |
| 305 |
} |
| 306 |
state $allowed_values = |
| 307 |
{ map { ( $_, 1 ) } qw(warn quiet fatal) }; |
| 308 |
Marpa::R2::exception( |
| 309 |
q{infinite_action must be 'warn', 'quiet' or 'fatal'}) |
| 310 |
if not exists $allowed_values->{$value}; |
| 311 |
$grammar->[Marpa::R2::Internal::Grammar::INFINITE_ACTION] = |
| 312 |
$value; |
| 313 |
} ## end if ( defined( my $value = $args->{'infinite_action'}...)) |
| 314 |
|
| 315 |
if ( defined( my $value = $args->{'warnings'} ) ) { |
| 316 |
if ( $value && $grammar_c->is_precomputed() ) { |
| 317 |
say {$trace_fh} |
| 318 |
q{"warnings" option is useless after grammar is precomputed} |
| 319 |
or Marpa::R2::exception("Could not print: $ERRNO"); |
| 320 |
} |
| 321 |
$grammar->[Marpa::R2::Internal::Grammar::WARNINGS] = $value; |
| 322 |
} ## end if ( defined( my $value = $args->{'warnings'} ) ) |
| 323 |
|
| 324 |
if ( defined( my $value = $args->{'inaccessible_ok'} ) ) { |
| 325 |
if ( $value && $grammar_c->is_precomputed() ) { |
| 326 |
say {$trace_fh} |
| 327 |
q{"inaccessible_ok" option is useless after grammar is precomputed} |
| 328 |
or Marpa::R2::exception("Could not print: $ERRNO"); |
| 329 |
|
| 330 |
} ## end if ( $value && $grammar_c->is_precomputed() ) |
| 331 |
GIVEN_REF_VALUE: { |
| 332 |
my $ref_value = ref $value; |
| 333 |
if ( $ref_value eq q{} ) { |
| 334 |
$value //= {}; |
| 335 |
last GIVEN_REF_VALUE; |
| 336 |
} |
| 337 |
if ( $ref_value eq 'ARRAY' ) { |
| 338 |
$value = { map { ( $_, 1 ) } @{$value} }; |
| 339 |
last GIVEN_REF_VALUE; |
| 340 |
} |
| 341 |
Marpa::R2::exception( |
| 342 |
'value of inaccessible_ok option must be boolean or an array ref' |
| 343 |
); |
| 344 |
} ## end GIVEN_REF_VALUE: |
| 345 |
$grammar->[Marpa::R2::Internal::Grammar::INACCESSIBLE_OK] = |
| 346 |
$value; |
| 347 |
} ## end if ( defined( my $value = $args->{'inaccessible_ok'}...)) |
| 348 |
|
| 349 |
if ( defined( my $value = $args->{'unproductive_ok'} ) ) { |
| 350 |
if ( $value && $grammar_c->is_precomputed() ) { |
| 351 |
say {$trace_fh} |
| 352 |
q{"unproductive_ok" option is useless after grammar is precomputed} |
| 353 |
or Marpa::R2::exception("Could not print: $ERRNO"); |
| 354 |
} |
| 355 |
GIVEN_REF_VALUE: { |
| 356 |
my $ref_value = ref $value; |
| 357 |
if ( $ref_value eq q{} ) { |
| 358 |
$value //= {}; |
| 359 |
last GIVEN_REF_VALUE; |
| 360 |
} |
| 361 |
if ( $ref_value eq 'ARRAY' ) { |
| 362 |
$value = { map { ( $_, 1 ) } @{$value} }; |
| 363 |
last GIVEN_REF_VALUE; |
| 364 |
} |
| 365 |
Marpa::R2::exception( |
| 366 |
'value of unproductive_ok option must be boolean or an array ref' |
| 367 |
); |
| 368 |
} ## end GIVEN_REF_VALUE: |
| 369 |
$grammar->[Marpa::R2::Internal::Grammar::UNPRODUCTIVE_OK] = |
| 370 |
$value; |
| 371 |
} ## end if ( defined( my $value = $args->{'unproductive_ok'}...)) |
| 372 |
|
| 373 |
} ## end for my $args (@arg_hashes) |
| 374 |
|
| 375 |
return 1; |
| 376 |
} ## end sub Marpa::R2::Grammar::set |
| 377 |
|
| 378 |
sub Marpa::R2::Grammar::symbol_reserved_set { |
| 379 |
my ( $grammar, $final_character, $boolean ) = @_; |
| 380 |
if ( length $final_character != 1 ) { |
| 381 |
Marpa::R2::exception( 'symbol_reserved_set(): "', |
| 382 |
$final_character, '" is not a symbol' ); |
| 383 |
} |
| 384 |
if ( $final_character eq ']' ) { |
| 385 |
return if $boolean; |
| 386 |
Marpa::R2::exception( |
| 387 |
q{symbol_reserved_set(): Attempt to unreserve ']'; this is not allowed} |
| 388 |
); |
| 389 |
} ## end if ( $final_character eq ']' ) ([) |
| 390 |
if ( not exists $DEFAULT_SYMBOLS_RESERVED{$final_character} ) { |
| 391 |
Marpa::R2::exception( |
| 392 |
qq{symbol_reserved_set(): "$final_character" is not a reservable symbol} |
| 393 |
); |
| 394 |
} |
| 395 |
# Return a value to make perlcritic happy |
| 396 |
return $DEFAULT_SYMBOLS_RESERVED{$final_character} = $boolean ? 1 : 0; |
| 397 |
} ## end sub Marpa::R2::Grammar::symbol_reserved_set |
| 398 |
|
| 399 |
sub Marpa::R2::Grammar::precompute { |
| 400 |
my $grammar = shift; |
| 401 |
|
| 402 |
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES]; |
| 403 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 404 |
my $trace_fh = |
| 405 |
$grammar->[Marpa::R2::Internal::Grammar::TRACE_FILE_HANDLE]; |
| 406 |
|
| 407 |
my $problems = $grammar->[Marpa::R2::Internal::Grammar::PROBLEMS]; |
| 408 |
if ($problems) { |
| 409 |
Marpa::R2::exception( |
| 410 |
Marpa::R2::Grammar::show_problems($grammar), |
| 411 |
"Second attempt to precompute grammar with fatal problems\n", |
| 412 |
'Marpa::R2 cannot proceed' |
| 413 |
); |
| 414 |
} ## end if ($problems) |
| 415 |
|
| 416 |
return $grammar if $grammar_c->is_precomputed(); |
| 417 |
|
| 418 |
set_start_symbol($grammar); |
| 419 |
|
| 420 |
# Catch errors in precomputation |
| 421 |
my $precompute_error_code = $Marpa::R2::Error::NONE; |
| 422 |
$grammar_c->throw_set(0); |
| 423 |
my $precompute_result = $grammar_c->precompute(); |
| 424 |
$grammar_c->throw_set(1); |
| 425 |
|
| 426 |
if ( $precompute_result < 0 ) { |
| 427 |
($precompute_error_code) = $grammar_c->error(); |
| 428 |
if ( not defined $precompute_error_code ) { |
| 429 |
Marpa::R2::exception( |
| 430 |
'libmarpa error, but no error code returned'); |
| 431 |
} |
| 432 |
|
| 433 |
# If already precomputed, just return success |
| 434 |
return $grammar |
| 435 |
if $precompute_error_code == $Marpa::R2::Error::PRECOMPUTED; |
| 436 |
|
| 437 |
# Cycles are not necessarily errors, |
| 438 |
# and get special handling |
| 439 |
$precompute_error_code = $Marpa::R2::Error::NONE |
| 440 |
if $precompute_error_code == $Marpa::R2::Error::GRAMMAR_HAS_CYCLE; |
| 441 |
|
| 442 |
} ## end if ( $precompute_result < 0 ) |
| 443 |
|
| 444 |
if ( $precompute_error_code != $Marpa::R2::Error::NONE ) { |
| 445 |
|
| 446 |
# Report the errors, then return failure |
| 447 |
|
| 448 |
if ( $precompute_error_code == $Marpa::R2::Error::NO_RULES ) { |
| 449 |
Marpa::R2::exception( |
| 450 |
'Attempted to precompute grammar with no rules'); |
| 451 |
} |
| 452 |
if ( $precompute_error_code == $Marpa::R2::Error::NULLING_TERMINAL ) { |
| 453 |
my @nulling_terminals = (); |
| 454 |
my $event_count = $grammar_c->event_count(); |
| 455 |
EVENT: |
| 456 |
for ( my $event_ix = 0; $event_ix < $event_count; $event_ix++ ) { |
| 457 |
my ( $event_type, $value ) = $grammar_c->event($event_ix); |
| 458 |
if ( $event_type eq 'MARPA_EVENT_NULLING_TERMINAL' ) { |
| 459 |
push @nulling_terminals, $grammar->symbol_name($value); |
| 460 |
} |
| 461 |
} ## end EVENT: for ( my $event_ix = 0; $event_ix < $event_count; ...) |
| 462 |
my @nulling_terminal_messages = |
| 463 |
map {qq{Nulling symbol "$_" is also a terminal\n}} |
| 464 |
@nulling_terminals; |
| 465 |
Marpa::R2::exception( @nulling_terminal_messages, |
| 466 |
'A terminal symbol cannot also be a nulling symbol' ); |
| 467 |
} ## end if ( $precompute_error_code == ...) |
| 468 |
if ( $precompute_error_code == $Marpa::R2::Error::COUNTED_NULLABLE ) { |
| 469 |
my @counted_nullables = (); |
| 470 |
my $event_count = $grammar_c->event_count(); |
| 471 |
EVENT: |
| 472 |
for ( my $event_ix = 0; $event_ix < $event_count; $event_ix++ ) { |
| 473 |
my ( $event_type, $value ) = $grammar_c->event($event_ix); |
| 474 |
if ( $event_type eq 'MARPA_EVENT_COUNTED_NULLABLE' ) { |
| 475 |
push @counted_nullables, $grammar->symbol_name($value); |
| 476 |
} |
| 477 |
} ## end EVENT: for ( my $event_ix = 0; $event_ix < $event_count; ...) |
| 478 |
my @counted_nullable_messages = map { |
| 479 |
q{Nullable symbol "} |
| 480 |
. $_ |
| 481 |
. qq{" is on RHS of counted rule\n} |
| 482 |
} @counted_nullables; |
| 483 |
Marpa::R2::exception( @counted_nullable_messages, |
| 484 |
'Counted nullables confuse Marpa -- please rewrite the grammar' |
| 485 |
); |
| 486 |
} ## end if ( $precompute_error_code == ...) |
| 487 |
|
| 488 |
if ( $precompute_error_code == $Marpa::R2::Error::NO_START_SYMBOL ) { |
| 489 |
Marpa::R2::exception('No start symbol'); |
| 490 |
} |
| 491 |
if ( $precompute_error_code == $Marpa::R2::Error::START_NOT_LHS ) { |
| 492 |
my $name = $grammar->[Marpa::R2::Internal::Grammar::START_NAME]; |
| 493 |
Marpa::R2::exception( |
| 494 |
qq{Start symbol "$name" not on LHS of any rule}); |
| 495 |
} |
| 496 |
if ( $precompute_error_code == $Marpa::R2::Error::UNPRODUCTIVE_START ) |
| 497 |
{ |
| 498 |
my $name = $grammar->[Marpa::R2::Internal::Grammar::START_NAME]; |
| 499 |
Marpa::R2::exception(qq{Unproductive start symbol: "$name"}); |
| 500 |
} |
| 501 |
|
| 502 |
Marpa::R2::uncaught_error( scalar $grammar_c->error() ); |
| 503 |
|
| 504 |
} ## end if ( $precompute_error_code != $Marpa::R2::Error::NONE) |
| 505 |
|
| 506 |
# Shadow all the new rules |
| 507 |
{ |
| 508 |
my $highest_rule_id = $grammar_c->highest_rule_id(); |
| 509 |
RULE: |
| 510 |
for ( my $rule_id = 0; $rule_id <= $highest_rule_id; $rule_id++ ) { |
| 511 |
next RULE if defined $rules->[$rule_id]; |
| 512 |
|
| 513 |
# The Marpa::R2 logic assumes no "gaps" in the rule numbering, |
| 514 |
# which is currently the case for Libmarpa, |
| 515 |
# but not guaranteed. |
| 516 |
shadow_rule( $grammar, $rule_id ); |
| 517 |
} ## end RULE: for ( my $rule_id = 0; $rule_id <= $highest_rule_id; ...) |
| 518 |
} |
| 519 |
|
| 520 |
my $infinite_action = |
| 521 |
$grammar->[Marpa::R2::Internal::Grammar::INFINITE_ACTION]; |
| 522 |
|
| 523 |
# Above I went through the error events |
| 524 |
# Here I go through the events for situations where there was no |
| 525 |
# hard error returned from libmarpa |
| 526 |
my $loop_rule_count = 0; |
| 527 |
{ |
| 528 |
my $event_count = $grammar_c->event_count(); |
| 529 |
EVENT: |
| 530 |
for ( my $event_ix = 0; $event_ix < $event_count; $event_ix++ ) { |
| 531 |
my ( $event_type, $value ) = $grammar_c->event($event_ix); |
| 532 |
if ( $event_type ne 'MARPA_EVENT_LOOP_RULES' ) { |
| 533 |
Marpa::R2::exception( |
| 534 |
qq{Unknown grammar precomputation event; type="$event_type"} |
| 535 |
); |
| 536 |
} |
| 537 |
$loop_rule_count = $value; |
| 538 |
} ## end EVENT: for ( my $event_ix = 0; $event_ix < $event_count; ...) |
| 539 |
} |
| 540 |
|
| 541 |
if ( $loop_rule_count and $infinite_action ne 'quiet' ) { |
| 542 |
my @loop_rules = |
| 543 |
grep { $grammar_c->rule_is_loop($_) } ( 0 .. $#{$rules} ); |
| 544 |
for my $rule_id (@loop_rules) { |
| 545 |
print {$trace_fh} |
| 546 |
'Cycle found involving rule: ', |
| 547 |
$grammar->brief_rule($rule_id), "\n" |
| 548 |
or Marpa::R2::exception("Could not print: $ERRNO"); |
| 549 |
} ## end for my $rule_id (@loop_rules) |
| 550 |
Marpa::R2::exception('Cycles in grammar, fatal error') |
| 551 |
if $infinite_action eq 'fatal'; |
| 552 |
} ## end if ( $loop_rule_count and $infinite_action ne 'quiet') |
| 553 |
|
| 554 |
# A bit hackish here: INACCESSIBLE_OK is not a HASH ref iff |
| 555 |
# it is a Boolean TRUE indicating that all inaccessibles are OK. |
| 556 |
# A Boolean FALSE will have been replaced with an empty hash. |
| 557 |
if ($grammar->[Marpa::R2::Internal::Grammar::WARNINGS] |
| 558 |
and ref( |
| 559 |
my $ok = $grammar->[Marpa::R2::Internal::Grammar::INACCESSIBLE_OK] |
| 560 |
) eq 'HASH' |
| 561 |
) |
| 562 |
{ |
| 563 |
SYMBOL: |
| 564 |
for my $symbol ( |
| 565 |
@{ Marpa::R2::Grammar::inaccessible_symbols($grammar) } ) |
| 566 |
{ |
| 567 |
|
| 568 |
# Inaccessible internal symbols may be created |
| 569 |
# from inaccessible use symbols -- ignore these. |
| 570 |
# This assumes that Marpa's logic |
| 571 |
# is correct and that |
| 572 |
# it is not creating inaccessible symbols from |
| 573 |
# accessible ones. |
| 574 |
next SYMBOL if $symbol =~ /\]/xms; |
| 575 |
next SYMBOL if $ok->{$symbol}; |
| 576 |
say {$trace_fh} "Inaccessible symbol: $symbol" |
| 577 |
or Marpa::R2::exception("Could not print: $ERRNO"); |
| 578 |
} ## end SYMBOL: for my $symbol ( @{ ...}) |
| 579 |
} ## end if ( $grammar->[Marpa::R2::Internal::Grammar::WARNINGS...]) |
| 580 |
|
| 581 |
# A bit hackish here: UNPRODUCTIVE_OK is not a HASH ref iff |
| 582 |
# it is a Boolean TRUE indicating that all inaccessibles are OK. |
| 583 |
# A Boolean FALSE will have been replaced with an empty hash. |
| 584 |
if ($grammar->[Marpa::R2::Internal::Grammar::WARNINGS] |
| 585 |
and ref( |
| 586 |
my $ok = $grammar->[Marpa::R2::Internal::Grammar::UNPRODUCTIVE_OK] |
| 587 |
) eq 'HASH' |
| 588 |
) |
| 589 |
{ |
| 590 |
SYMBOL: |
| 591 |
for my $symbol ( |
| 592 |
@{ Marpa::R2::Grammar::unproductive_symbols($grammar) } ) |
| 593 |
{ |
| 594 |
|
| 595 |
# Unproductive internal symbols may be created |
| 596 |
# from unproductive use symbols -- ignore these. |
| 597 |
# This assumes that Marpa's logic |
| 598 |
# is correct and that |
| 599 |
# it is not creating unproductive symbols from |
| 600 |
# productive ones. |
| 601 |
next SYMBOL if $symbol =~ /\]/xms; |
| 602 |
next SYMBOL if $ok->{$symbol}; |
| 603 |
say {$trace_fh} "Unproductive symbol: $symbol" |
| 604 |
or Marpa::R2::exception("Could not print: $ERRNO"); |
| 605 |
} ## end SYMBOL: for my $symbol ( @{ ...}) |
| 606 |
} ## end if ( $grammar->[Marpa::R2::Internal::Grammar::WARNINGS...]) |
| 607 |
|
| 608 |
# If we are using scannerless parsing, set that up |
| 609 |
Marpa::R2::exception("Internal error; precompute called for SLIF grammar") |
| 610 |
if $grammar->[Marpa::R2::Internal::Grammar::CHARACTER_CLASSES]; |
| 611 |
|
| 612 |
return $grammar; |
| 613 |
|
| 614 |
} ## end sub Marpa::R2::Grammar::precompute |
| 615 |
|
| 616 |
# A custom precompute for SLIF grammars |
| 617 |
sub Marpa::R2::Internal::Grammar::slif_precompute { |
| 618 |
my $grammar = shift; |
| 619 |
|
| 620 |
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES]; |
| 621 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 622 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 623 |
my $trace_fh = |
| 624 |
$grammar->[Marpa::R2::Internal::Grammar::TRACE_FILE_HANDLE]; |
| 625 |
|
| 626 |
my $problems = $grammar->[Marpa::R2::Internal::Grammar::PROBLEMS]; |
| 627 |
if ($problems) { |
| 628 |
Marpa::R2::exception( |
| 629 |
Marpa::R2::Grammar::show_problems($grammar), |
| 630 |
"Second attempt to precompute grammar with fatal problems\n", |
| 631 |
'Marpa::R2 cannot proceed' |
| 632 |
); |
| 633 |
} ## end if ($problems) |
| 634 |
|
| 635 |
return if $grammar_c->is_precomputed(); |
| 636 |
if ($grammar_c->force_valued() < 0) { |
| 637 |
Marpa::R2::uncaught_error( scalar $grammar_c->error() ); |
| 638 |
} |
| 639 |
|
| 640 |
set_start_symbol($grammar); |
| 641 |
|
| 642 |
# Catch errors in precomputation |
| 643 |
my $precompute_error_code = $Marpa::R2::Error::NONE; |
| 644 |
$grammar_c->throw_set(0); |
| 645 |
my $precompute_result = $grammar_c->precompute(); |
| 646 |
$grammar_c->throw_set(1); |
| 647 |
|
| 648 |
if ( $precompute_result < 0 ) { |
| 649 |
($precompute_error_code) = $grammar_c->error(); |
| 650 |
if ( not defined $precompute_error_code ) { |
| 651 |
Marpa::R2::exception( |
| 652 |
'libmarpa error, but no error code returned'); |
| 653 |
} |
| 654 |
|
| 655 |
# If already precomputed, let higher level know |
| 656 |
return $precompute_error_code |
| 657 |
if $precompute_error_code == $Marpa::R2::Error::PRECOMPUTED; |
| 658 |
|
| 659 |
# Cycles are not necessarily errors, |
| 660 |
# and get special handling |
| 661 |
$precompute_error_code = $Marpa::R2::Error::NONE |
| 662 |
if $precompute_error_code == $Marpa::R2::Error::GRAMMAR_HAS_CYCLE; |
| 663 |
|
| 664 |
} ## end if ( $precompute_result < 0 ) |
| 665 |
|
| 666 |
if ( $precompute_error_code != $Marpa::R2::Error::NONE ) { |
| 667 |
|
| 668 |
# Report the errors, then return failure |
| 669 |
|
| 670 |
if ( $precompute_error_code == $Marpa::R2::Error::NO_RULES ) { |
| 671 |
Marpa::R2::exception( |
| 672 |
'Attempted to precompute grammar with no rules'); |
| 673 |
} |
| 674 |
if ( $precompute_error_code == $Marpa::R2::Error::NULLING_TERMINAL ) { |
| 675 |
my @nulling_terminals = (); |
| 676 |
my $event_count = $grammar_c->event_count(); |
| 677 |
EVENT: |
| 678 |
for ( my $event_ix = 0; $event_ix < $event_count; $event_ix++ ) { |
| 679 |
my ( $event_type, $value ) = $grammar_c->event($event_ix); |
| 680 |
if ( $event_type eq 'MARPA_EVENT_NULLING_TERMINAL' ) { |
| 681 |
push @nulling_terminals, $grammar->symbol_name($value); |
| 682 |
} |
| 683 |
} ## end EVENT: for ( my $event_ix = 0; $event_ix < $event_count; ...) |
| 684 |
my @nulling_terminal_messages = |
| 685 |
map {qq{Nulling symbol "$_" is also a terminal\n}} |
| 686 |
@nulling_terminals; |
| 687 |
Marpa::R2::exception( @nulling_terminal_messages, |
| 688 |
'A terminal symbol cannot also be a nulling symbol' ); |
| 689 |
} ## end if ( $precompute_error_code == ...) |
| 690 |
if ( $precompute_error_code == $Marpa::R2::Error::COUNTED_NULLABLE ) { |
| 691 |
my @counted_nullables = (); |
| 692 |
my $event_count = $grammar_c->event_count(); |
| 693 |
EVENT: |
| 694 |
for ( my $event_ix = 0; $event_ix < $event_count; $event_ix++ ) { |
| 695 |
my ( $event_type, $value ) = $grammar_c->event($event_ix); |
| 696 |
if ( $event_type eq 'MARPA_EVENT_COUNTED_NULLABLE' ) { |
| 697 |
push @counted_nullables, $grammar->symbol_name($value); |
| 698 |
} |
| 699 |
} ## end EVENT: for ( my $event_ix = 0; $event_ix < $event_count; ...) |
| 700 |
my @counted_nullable_messages = map { |
| 701 |
q{Nullable symbol "} |
| 702 |
. $_ |
| 703 |
. qq{" is on RHS of counted rule\n} |
| 704 |
} @counted_nullables; |
| 705 |
Marpa::R2::exception( @counted_nullable_messages, |
| 706 |
'Counted nullables confuse Marpa -- please rewrite the grammar' |
| 707 |
); |
| 708 |
} ## end if ( $precompute_error_code == ...) |
| 709 |
|
| 710 |
if ( $precompute_error_code == $Marpa::R2::Error::NO_START_SYMBOL ) { |
| 711 |
Marpa::R2::exception('No start symbol'); |
| 712 |
} |
| 713 |
if ( $precompute_error_code == $Marpa::R2::Error::START_NOT_LHS ) { |
| 714 |
my $name = $grammar->[Marpa::R2::Internal::Grammar::START_NAME]; |
| 715 |
Marpa::R2::exception( |
| 716 |
qq{Start symbol "$name" not on LHS of any rule}); |
| 717 |
} |
| 718 |
|
| 719 |
return $precompute_error_code |
| 720 |
if $precompute_error_code |
| 721 |
== $Marpa::R2::Error::UNPRODUCTIVE_START; |
| 722 |
|
| 723 |
Marpa::R2::uncaught_error( scalar $grammar_c->error() ); |
| 724 |
|
| 725 |
} ## end if ( $precompute_error_code != $Marpa::R2::Error::NONE) |
| 726 |
|
| 727 |
# Shadow all the new rules |
| 728 |
{ |
| 729 |
my $highest_rule_id = $grammar_c->highest_rule_id(); |
| 730 |
RULE: |
| 731 |
for ( my $rule_id = 0; $rule_id <= $highest_rule_id; $rule_id++ ) { |
| 732 |
next RULE if defined $rules->[$rule_id]; |
| 733 |
|
| 734 |
# The Marpa::R2 logic assumes no "gaps" in the rule numbering, |
| 735 |
# which is currently the case for Libmarpa, |
| 736 |
# but not guaranteed. |
| 737 |
shadow_rule( $grammar, $rule_id ); |
| 738 |
} ## end RULE: for ( my $rule_id = 0; $rule_id <= $highest_rule_id; ...) |
| 739 |
} |
| 740 |
|
| 741 |
my $infinite_action = |
| 742 |
$grammar->[Marpa::R2::Internal::Grammar::INFINITE_ACTION]; |
| 743 |
|
| 744 |
# Above I went through the error events |
| 745 |
# Here I go through the events for situations where there was no |
| 746 |
# hard error returned from libmarpa |
| 747 |
my $loop_rule_count = 0; |
| 748 |
{ |
| 749 |
my $event_count = $grammar_c->event_count(); |
| 750 |
EVENT: |
| 751 |
for ( my $event_ix = 0; $event_ix < $event_count; $event_ix++ ) { |
| 752 |
my ( $event_type, $value ) = $grammar_c->event($event_ix); |
| 753 |
if ( $event_type ne 'MARPA_EVENT_LOOP_RULES' ) { |
| 754 |
Marpa::R2::exception( |
| 755 |
qq{Unknown grammar precomputation event; type="$event_type"} |
| 756 |
); |
| 757 |
} |
| 758 |
$loop_rule_count = $value; |
| 759 |
} ## end EVENT: for ( my $event_ix = 0; $event_ix < $event_count; ...) |
| 760 |
} |
| 761 |
|
| 762 |
if ( $loop_rule_count and $infinite_action ne 'quiet' ) { |
| 763 |
my @loop_rules = |
| 764 |
grep { $grammar_c->rule_is_loop($_) } ( 0 .. $#{$rules} ); |
| 765 |
for my $rule_id (@loop_rules) { |
| 766 |
print {$trace_fh} |
| 767 |
'Cycle found involving rule: ', |
| 768 |
$grammar->brief_rule($rule_id), "\n" |
| 769 |
or Marpa::R2::exception("Could not print: $ERRNO"); |
| 770 |
} ## end for my $rule_id (@loop_rules) |
| 771 |
Marpa::R2::exception('Cycles in grammar, fatal error') |
| 772 |
if $infinite_action eq 'fatal'; |
| 773 |
} ## end if ( $loop_rule_count and $infinite_action ne 'quiet') |
| 774 |
|
| 775 |
my $default_if_inaccessible = |
| 776 |
$grammar->[Marpa::R2::Internal::Grammar::INTERNAL]->{if_inaccessible} |
| 777 |
// 'warn'; |
| 778 |
SYMBOL: |
| 779 |
for my $symbol_id ( grep { !$grammar_c->symbol_is_accessible($_) } |
| 780 |
( 0 .. $#{$symbols} ) ) |
| 781 |
{ |
| 782 |
|
| 783 |
my $symbol = $symbols->[$symbol_id]; |
| 784 |
my $symbol_name = $grammar->symbol_name($symbol_id); |
| 785 |
|
| 786 |
# Inaccessible internal symbols may be created |
| 787 |
# from inaccessible use symbols -- ignore these. |
| 788 |
# This assumes that Marpa's logic |
| 789 |
# is correct and that |
| 790 |
# it is not creating inaccessible symbols from |
| 791 |
# accessible ones. |
| 792 |
next SYMBOL if $symbol_name =~ /\]/xms; |
| 793 |
my $treatment = |
| 794 |
$symbol->[Marpa::R2::Internal::Symbol::IF_INACCESSIBLE] // |
| 795 |
$default_if_inaccessible; |
| 796 |
next SYMBOL if $treatment eq 'ok'; |
| 797 |
my $message = "Inaccessible symbol: $symbol_name"; |
| 798 |
Marpa::R2::exception($message) if $treatment eq 'fatal'; |
| 799 |
say {$trace_fh} $message |
| 800 |
or Marpa::R2::exception("Could not print: $ERRNO"); |
| 801 |
} ## end for my $symbol_id ( grep { !$grammar_c->...}) |
| 802 |
|
| 803 |
# A bit hackish here: UNPRODUCTIVE_OK is not a HASH ref iff |
| 804 |
# it is a Boolean TRUE indicating that all inaccessibles are OK. |
| 805 |
# A Boolean FALSE will have been replaced with an empty hash. |
| 806 |
if ($grammar->[Marpa::R2::Internal::Grammar::WARNINGS] |
| 807 |
and ref( |
| 808 |
my $ok = $grammar->[Marpa::R2::Internal::Grammar::UNPRODUCTIVE_OK] |
| 809 |
) eq 'HASH' |
| 810 |
) |
| 811 |
{ |
| 812 |
SYMBOL: |
| 813 |
for my $symbol ( |
| 814 |
@{ Marpa::R2::Grammar::unproductive_symbols($grammar) } ) |
| 815 |
{ |
| 816 |
|
| 817 |
# Unproductive internal symbols may be created |
| 818 |
# from unproductive use symbols -- ignore these. |
| 819 |
# This assumes that Marpa's logic |
| 820 |
# is correct and that |
| 821 |
# it is not creating unproductive symbols from |
| 822 |
# productive ones. |
| 823 |
next SYMBOL if $symbol =~ /\]/xms; |
| 824 |
next SYMBOL if $ok->{$symbol}; |
| 825 |
say {$trace_fh} "Unproductive symbol: $symbol" |
| 826 |
or Marpa::R2::exception("Could not print: $ERRNO"); |
| 827 |
} ## end SYMBOL: for my $symbol ( @{ ...}) |
| 828 |
} ## end if ( $grammar->[Marpa::R2::Internal::Grammar::WARNINGS...]) |
| 829 |
|
| 830 |
my $cc_hash = $grammar->[Marpa::R2::Internal::Grammar::CHARACTER_CLASSES]; |
| 831 |
if ( defined $cc_hash ) { |
| 832 |
my $class_table = |
| 833 |
$grammar->[Marpa::R2::Internal::Grammar::CHARACTER_CLASS_TABLE] = |
| 834 |
[]; |
| 835 |
for my $cc_symbol ( sort keys %{$cc_hash} ) { |
| 836 |
my $cc_components = $cc_hash->{$cc_symbol}; |
| 837 |
push @{$class_table}, |
| 838 |
[ $grammar->thin_symbol($cc_symbol), $cc_components ]; |
| 839 |
} |
| 840 |
} ## end if ( defined $cc_hash ) |
| 841 |
|
| 842 |
# Save some memory |
| 843 |
$grammar->[Marpa::R2::Internal::Grammar::CHARACTER_CLASSES] = undef; |
| 844 |
|
| 845 |
return ; |
| 846 |
|
| 847 |
} ## end sub Marpa::R2::Grammar::slif_precompute |
| 848 |
|
| 849 |
sub Marpa::R2::Grammar::show_problems { |
| 850 |
my ($grammar) = @_; |
| 851 |
|
| 852 |
my $problems = $grammar->[Marpa::R2::Internal::Grammar::PROBLEMS]; |
| 853 |
if ($problems) { |
| 854 |
my $problem_count = scalar @{$problems}; |
| 855 |
return |
| 856 |
"Grammar has $problem_count problems:\n" |
| 857 |
. ( join "\n", @{$problems} ) . "\n"; |
| 858 |
} ## end if ($problems) |
| 859 |
return "Grammar has no problems\n"; |
| 860 |
} ## end sub Marpa::R2::Grammar::show_problems |
| 861 |
|
| 862 |
# Return DSL form of symbol |
| 863 |
# Does no checking |
| 864 |
sub Marpa::R2::Grammar::symbol_dsl_form { |
| 865 |
my ( $grammar, $symbol_id ) = @_; |
| 866 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 867 |
my $symbol = $symbols->[$symbol_id]; |
| 868 |
return $symbol->[Marpa::R2::Internal::Symbol::DSL_FORM]; |
| 869 |
} |
| 870 |
|
| 871 |
# Return description of symbol |
| 872 |
# Does no checking |
| 873 |
sub Marpa::R2::Grammar::symbol_description { |
| 874 |
my ( $grammar, $symbol_id ) = @_; |
| 875 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 876 |
my $symbol = $symbols->[$symbol_id]; |
| 877 |
return $symbol->[Marpa::R2::Internal::Symbol::DESCRIPTION]; |
| 878 |
} |
| 879 |
|
| 880 |
# Return display form of symbol |
| 881 |
# Does lots of checking and makes use of alternatives. |
| 882 |
sub Marpa::R2::Grammar::symbol_in_display_form { |
| 883 |
my ( $grammar, $symbol_id ) = @_; |
| 884 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 885 |
my $symbol = $symbols->[$symbol_id]; |
| 886 |
return "<!No symbol with ID $symbol_id!>" if not defined $symbol; |
| 887 |
my $text = $symbol->[Marpa::R2::Internal::Symbol::DISPLAY_FORM]; |
| 888 |
return $text if defined $text; |
| 889 |
$text = $symbol->[Marpa::R2::Internal::Symbol::DSL_FORM] // |
| 890 |
$grammar->symbol_name($symbol_id); |
| 891 |
return ($text =~ m/\s/xms) ? "<$text>" : $text; |
| 892 |
} |
| 893 |
|
| 894 |
sub Marpa::R2::Grammar::show_symbol { |
| 895 |
my ( $grammar, $symbol ) = @_; |
| 896 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 897 |
my $text = q{}; |
| 898 |
my $symbol_id = $symbol->[Marpa::R2::Internal::Symbol::ID]; |
| 899 |
|
| 900 |
my $name = $grammar->symbol_name($symbol_id); |
| 901 |
$text .= "$symbol_id: $name"; |
| 902 |
|
| 903 |
my @tag_list = (); |
| 904 |
$grammar_c->symbol_is_productive($symbol_id) |
| 905 |
or push @tag_list, 'unproductive'; |
| 906 |
$grammar_c->symbol_is_accessible($symbol_id) |
| 907 |
or push @tag_list, 'inaccessible'; |
| 908 |
$grammar_c->symbol_is_nulling($symbol_id) and push @tag_list, 'nulling'; |
| 909 |
$grammar_c->symbol_is_terminal($symbol_id) and push @tag_list, 'terminal'; |
| 910 |
|
| 911 |
$text .= join q{ }, q{,}, @tag_list if scalar @tag_list; |
| 912 |
$text .= "\n"; |
| 913 |
return $text; |
| 914 |
|
| 915 |
} ## end sub Marpa::R2::Grammar::show_symbol |
| 916 |
|
| 917 |
sub Marpa::R2::Grammar::show_symbols { |
| 918 |
my ($grammar) = @_; |
| 919 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 920 |
my $text = q{}; |
| 921 |
for my $symbol_ref ( @{$symbols} ) { |
| 922 |
$text .= $grammar->show_symbol($symbol_ref); |
| 923 |
} |
| 924 |
return $text; |
| 925 |
} ## end sub Marpa::R2::Grammar::show_symbols |
| 926 |
|
| 927 |
sub Marpa::R2::Grammar::show_nulling_symbols { |
| 928 |
my ($grammar) = @_; |
| 929 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 930 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 931 |
return join q{ }, sort map { $grammar->symbol_name($_) } |
| 932 |
grep { $grammar_c->symbol_is_nulling($_) } ( 0 .. $#{$symbols} ); |
| 933 |
} ## end sub Marpa::R2::Grammar::show_nulling_symbols |
| 934 |
|
| 935 |
sub Marpa::R2::Grammar::show_productive_symbols { |
| 936 |
my ($grammar) = @_; |
| 937 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 938 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 939 |
return join q{ }, sort map { $grammar->symbol_name($_) } |
| 940 |
grep { $grammar_c->symbol_is_productive($_) } ( 0 .. $#{$symbols} ); |
| 941 |
} ## end sub Marpa::R2::Grammar::show_productive_symbols |
| 942 |
|
| 943 |
sub Marpa::R2::Grammar::show_accessible_symbols { |
| 944 |
my ($grammar) = @_; |
| 945 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 946 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 947 |
return join q{ }, sort map { $grammar->symbol_name($_) } |
| 948 |
grep { $grammar_c->symbol_is_accessible($_) } ( 0 .. $#{$symbols} ); |
| 949 |
} ## end sub Marpa::R2::Grammar::show_accessible_symbols |
| 950 |
|
| 951 |
sub Marpa::R2::Grammar::inaccessible_symbols { |
| 952 |
my ($grammar) = @_; |
| 953 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 954 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 955 |
return [ |
| 956 |
sort map { $grammar->symbol_name($_) } |
| 957 |
grep { !$grammar_c->symbol_is_accessible($_) } |
| 958 |
( 0 .. $#{$symbols} ) |
| 959 |
]; |
| 960 |
} ## end sub Marpa::R2::Grammar::inaccessible_symbols |
| 961 |
|
| 962 |
sub Marpa::R2::Grammar::unproductive_symbols { |
| 963 |
my ($grammar) = @_; |
| 964 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 965 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 966 |
return [ |
| 967 |
sort map { $grammar->symbol_name($_) } |
| 968 |
grep { !$grammar_c->symbol_is_productive($_) } |
| 969 |
( 0 .. $#{$symbols} ) |
| 970 |
]; |
| 971 |
} ## end sub Marpa::R2::Grammar::unproductive_symbols |
| 972 |
|
| 973 |
sub Marpa::R2::Grammar::start_symbol { |
| 974 |
my ( $grammar ) = @_; |
| 975 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 976 |
return $grammar_c->start_symbol(); |
| 977 |
} |
| 978 |
|
| 979 |
sub Marpa::R2::Grammar::rule_name { |
| 980 |
my ( $grammar, $rule_id ) = @_; |
| 981 |
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES]; |
| 982 |
my $rule = $rules->[$rule_id]; |
| 983 |
return "Non-existent rule $rule_id" if not defined $rule; |
| 984 |
my $name = $rule->[Marpa::R2::Internal::Rule::NAME]; |
| 985 |
return $name if defined $name; |
| 986 |
my $tracer = $grammar->[Marpa::R2::Internal::Grammar::TRACER]; |
| 987 |
my ( $lhs_id ) = $tracer->rule_expand($rule_id); |
| 988 |
return $grammar->symbol_name($lhs_id); |
| 989 |
} ## end sub Marpa::R2::Grammar::rule_name |
| 990 |
|
| 991 |
# Undocumented -- assumes it is called internally, |
| 992 |
# by the SLIF |
| 993 |
sub Marpa::R2::Grammar::tag { |
| 994 |
my ( $grammar, $rule_id ) = @_; |
| 995 |
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES]; |
| 996 |
my $rule = $rules->[$rule_id]; |
| 997 |
return $rule->[Marpa::R2::Internal::Rule::SLIF_TAG]; |
| 998 |
} ## end sub Marpa::R2::Grammar::rule_name |
| 999 |
|
| 1000 |
sub Marpa::R2::Grammar::brief_rule { |
| 1001 |
my ( $grammar, $rule_id ) = @_; |
| 1002 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1003 |
my ( $lhs, @rhs ) = $grammar->rule($rule_id); |
| 1004 |
my $minimum = $grammar_c->sequence_min($rule_id); |
| 1005 |
my $quantifier = defined $minimum ? $minimum <= 0 ? q{*} : q{+} : q{}; |
| 1006 |
return ( join q{ }, "$rule_id:", $lhs, '->', @rhs ) . $quantifier; |
| 1007 |
} ## end sub Marpa::R2::Grammar::brief_rule |
| 1008 |
|
| 1009 |
sub Marpa::R2::Grammar::show_rule { |
| 1010 |
my ( $grammar, $rule ) = @_; |
| 1011 |
|
| 1012 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1013 |
my $rule_id = $rule->[Marpa::R2::Internal::Rule::ID]; |
| 1014 |
my @comment = (); |
| 1015 |
|
| 1016 |
$grammar_c->rule_length($rule_id) == 0 and push @comment, 'empty'; |
| 1017 |
$grammar->rule_is_used($rule_id) or push @comment, '!used'; |
| 1018 |
$grammar_c->rule_is_productive($rule_id) or push @comment, 'unproductive'; |
| 1019 |
$grammar_c->rule_is_accessible($rule_id) or push @comment, 'inaccessible'; |
| 1020 |
$rule->[Marpa::R2::Internal::Rule::DISCARD_SEPARATION] |
| 1021 |
and push @comment, 'discard_sep'; |
| 1022 |
|
| 1023 |
my $text = $grammar->brief_rule($rule_id); |
| 1024 |
|
| 1025 |
if (@comment) { |
| 1026 |
$text .= q{ } . ( join q{ }, q{/*}, @comment, q{*/} ); |
| 1027 |
} |
| 1028 |
|
| 1029 |
return $text .= "\n"; |
| 1030 |
|
| 1031 |
} # sub show_rule |
| 1032 |
|
| 1033 |
sub Marpa::R2::Grammar::show_rules { |
| 1034 |
my ($grammar) = @_; |
| 1035 |
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES]; |
| 1036 |
my $text; |
| 1037 |
|
| 1038 |
for my $rule ( @{$rules} ) { |
| 1039 |
$text .= $grammar->show_rule($rule); |
| 1040 |
} |
| 1041 |
return $text; |
| 1042 |
} ## end sub Marpa::R2::Grammar::show_rules |
| 1043 |
|
| 1044 |
# This logic deals with gaps in the rule numbering. |
| 1045 |
# Currently there are none, but Libmarpa does not |
| 1046 |
# guarantee this. |
| 1047 |
sub Marpa::R2::Grammar::rule_ids { |
| 1048 |
my ($grammar) = @_; |
| 1049 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1050 |
return 0 .. $grammar_c->highest_rule_id(); |
| 1051 |
} ## end sub Marpa::R2::Grammar::rule_ids |
| 1052 |
|
| 1053 |
# This logic deals with gaps in the symbol numbering. |
| 1054 |
# Currently there are none, but Libmarpa does not |
| 1055 |
# guarantee this. |
| 1056 |
sub Marpa::R2::Grammar::symbol_ids { |
| 1057 |
my ($grammar) = @_; |
| 1058 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1059 |
return 0 .. $grammar_c->highest_symbol_id(); |
| 1060 |
} ## end sub Marpa::R2::Grammar::rule_ids |
| 1061 |
|
| 1062 |
# Returns empty array if not such rule |
| 1063 |
sub Marpa::R2::Grammar::rule { |
| 1064 |
my ( $grammar, $rule_id ) = @_; |
| 1065 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 1066 |
my $tracer = $grammar->[Marpa::R2::Internal::Grammar::TRACER]; |
| 1067 |
my @symbol_names = (); |
| 1068 |
|
| 1069 |
my @symbols = $tracer->rule_expand($rule_id); |
| 1070 |
SYMBOL_ID: for my $symbol_id (@symbols) { |
| 1071 |
## The name of the symbols, before the BNF rewrites |
| 1072 |
my $name = |
| 1073 |
$symbols->[$symbol_id]->[Marpa::R2::Internal::Symbol::LEGACY_NAME] |
| 1074 |
// $grammar->symbol_name($symbol_id); |
| 1075 |
push @symbol_names, $name; |
| 1076 |
} ## end SYMBOL_ID: for my $symbol_id (@symbol_ids) |
| 1077 |
return @symbol_names; |
| 1078 |
} ## end sub Marpa::R2::Grammar::rule |
| 1079 |
|
| 1080 |
# Internal, for use with in coordinating thin and thick |
| 1081 |
# interfaces. NOT DOCUMENTED. |
| 1082 |
sub Marpa::R2::Grammar::_rule_mask { |
| 1083 |
my ( $grammar, $rule_id ) = @_; |
| 1084 |
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES]; |
| 1085 |
my $rule = $rules->[$rule_id]; |
| 1086 |
return $rule->[Marpa::R2::Internal::Rule::MASK]; |
| 1087 |
} ## end sub Marpa::R2::Grammar::rule |
| 1088 |
|
| 1089 |
# Deprecated and for removal |
| 1090 |
# Used in blog post, and part of |
| 1091 |
# CPAN version 2.023_008 but |
| 1092 |
# never documented in any CPAN version |
| 1093 |
sub Marpa::R2::Grammar::bnf_rule { |
| 1094 |
goto &Marpa::R2::Grammar::rule; |
| 1095 |
} ## end sub Marpa::R2::Grammar::bnf_rule |
| 1096 |
|
| 1097 |
sub Marpa::R2::Grammar::show_dotted_rule { |
| 1098 |
my ( $grammar, $rule_id, $dot_position ) = @_; |
| 1099 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1100 |
my ( $lhs, @rhs ) = $grammar->rule($rule_id); |
| 1101 |
|
| 1102 |
my $minimum = $grammar_c->sequence_min($rule_id); |
| 1103 |
if (defined $minimum) { |
| 1104 |
my $quantifier = $minimum <= 0 ? q{*} : q{+} ; |
| 1105 |
$rhs[0] .= $quantifier; |
| 1106 |
} |
| 1107 |
$dot_position = 0 if $dot_position < 0; |
| 1108 |
splice @rhs, $dot_position, 0, q{.}; |
| 1109 |
return join q{ }, $lhs, q{->}, @rhs; |
| 1110 |
} ## end sub Marpa::R2::Grammar::show_dotted_rule |
| 1111 |
|
| 1112 |
# Used by lexers to check that symbol is a terminal |
| 1113 |
sub Marpa::R2::Grammar::check_terminal { |
| 1114 |
my ( $grammar, $name ) = @_; |
| 1115 |
return 0 if not defined $name; |
| 1116 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1117 |
my $symbol_id = |
| 1118 |
$grammar->[Marpa::R2::Internal::Grammar::TRACER] |
| 1119 |
->symbol_by_name($name); |
| 1120 |
return 0 if not defined $symbol_id; |
| 1121 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 1122 |
my $symbol = $symbols->[$symbol_id]; |
| 1123 |
return $grammar_c->symbol_is_terminal($symbol_id) ? 1 : 0; |
| 1124 |
} ## end sub Marpa::R2::Grammar::check_terminal |
| 1125 |
|
| 1126 |
sub Marpa::R2::Grammar::symbol_name { |
| 1127 |
my ( $grammar, $id ) = @_; |
| 1128 |
my $symbol_name = |
| 1129 |
$grammar->[Marpa::R2::Internal::Grammar::TRACER]->symbol_name($id); |
| 1130 |
return defined $symbol_name ? $symbol_name : '[SYMBOL#' . $id . ']'; |
| 1131 |
} ## end sub Marpa::R2::Grammar::symbol_name |
| 1132 |
|
| 1133 |
sub shadow_symbol { |
| 1134 |
my ( $grammar, $symbol_id ) = @_; |
| 1135 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 1136 |
my $symbol = $symbols->[$symbol_id] = []; |
| 1137 |
$symbol->[Marpa::R2::Internal::Symbol::ID] = $symbol_id; |
| 1138 |
return $symbol; |
| 1139 |
} ## end sub shadow_symbol |
| 1140 |
|
| 1141 |
# Create the structure which "shadows" the libmarpa rule |
| 1142 |
sub shadow_rule { |
| 1143 |
my ( $grammar, $rule_id ) = @_; |
| 1144 |
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES]; |
| 1145 |
my $new_rule = $rules->[$rule_id] = []; |
| 1146 |
$new_rule->[Marpa::R2::Internal::Rule::ID] = $rule_id; |
| 1147 |
return $new_rule; |
| 1148 |
} ## end sub shadow_rule |
| 1149 |
|
| 1150 |
sub assign_symbol { |
| 1151 |
my ( $grammar, $name, $options ) = @_; |
| 1152 |
|
| 1153 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1154 |
my $tracer = $grammar->[Marpa::R2::Internal::Grammar::TRACER]; |
| 1155 |
my $symbol_id = $tracer->symbol_by_name($name); |
| 1156 |
if ( defined $symbol_id ) { |
| 1157 |
my $symbols = $grammar->[Marpa::R2::Internal::Grammar::SYMBOLS]; |
| 1158 |
return $symbols->[$symbol_id]; |
| 1159 |
} |
| 1160 |
$symbol_id = $tracer->symbol_new($name); |
| 1161 |
my $symbol = shadow_symbol( $grammar, $symbol_id ); |
| 1162 |
|
| 1163 |
PROPERTY: for my $property ( sort keys %{$options} ) { |
| 1164 |
if ( $property eq 'semantics' ) { |
| 1165 |
my $value = $options->{$property}; |
| 1166 |
$symbol->[Marpa::R2::Internal::Symbol::LEXEME_SEMANTICS] = $value; |
| 1167 |
next PROPERTY; |
| 1168 |
} |
| 1169 |
if ( $property eq 'bless' ) { |
| 1170 |
my $value = $options->{$property}; |
| 1171 |
$symbol->[Marpa::R2::Internal::Symbol::BLESSING] = $value; |
| 1172 |
next PROPERTY; |
| 1173 |
} |
| 1174 |
if ( $property eq 'terminal' ) { |
| 1175 |
my $value = $options->{$property}; |
| 1176 |
$grammar_c->symbol_is_terminal_set( $symbol_id, $value ); |
| 1177 |
next PROPERTY; |
| 1178 |
} |
| 1179 |
if ( $property eq 'rank' ) { |
| 1180 |
my $value = $options->{$property}; |
| 1181 |
Marpa::R2::exception(qq{Symbol "$name": rank must be an integer}) |
| 1182 |
if not Scalar::Util::looks_like_number($value) |
| 1183 |
or int($value) != $value; |
| 1184 |
$grammar_c->symbol_rank_set($symbol_id) = $value; |
| 1185 |
next PROPERTY; |
| 1186 |
} ## end if ( $property eq 'rank' ) |
| 1187 |
if ( $property eq 'description' ) { |
| 1188 |
my $value = $options->{$property}; |
| 1189 |
$symbol->[Marpa::R2::Internal::Symbol::DESCRIPTION] = $value; |
| 1190 |
next PROPERTY; |
| 1191 |
} |
| 1192 |
if ( $property eq 'dsl_form' ) { |
| 1193 |
my $value = $options->{$property}; |
| 1194 |
$symbol->[Marpa::R2::Internal::Symbol::DSL_FORM] = $value; |
| 1195 |
next PROPERTY; |
| 1196 |
} |
| 1197 |
if ( $property eq 'legacy_name' ) { |
| 1198 |
my $value = $options->{$property}; |
| 1199 |
$symbol->[Marpa::R2::Internal::Symbol::LEGACY_NAME] = $value; |
| 1200 |
next PROPERTY; |
| 1201 |
} |
| 1202 |
if ( $property eq 'display_form' ) { |
| 1203 |
my $value = $options->{$property}; |
| 1204 |
$symbol->[Marpa::R2::Internal::Symbol::DISPLAY_FORM] = $value; |
| 1205 |
next PROPERTY; |
| 1206 |
} |
| 1207 |
if ( $property eq 'if_inaccessible' ) { |
| 1208 |
my $value = $options->{$property}; |
| 1209 |
$symbol->[Marpa::R2::Internal::Symbol::IF_INACCESSIBLE] = $value; |
| 1210 |
next PROPERTY; |
| 1211 |
} |
| 1212 |
Marpa::R2::exception(qq{Unknown symbol property "$property"}); |
| 1213 |
} ## end PROPERTY: for my $property ( keys %{$options} ) |
| 1214 |
|
| 1215 |
return $symbol; |
| 1216 |
|
| 1217 |
} ## end sub assign_symbol |
| 1218 |
|
| 1219 |
sub assign_user_symbol { |
| 1220 |
my $grammar = shift; |
| 1221 |
my $name = shift; |
| 1222 |
my $options = shift; |
| 1223 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1224 |
|
| 1225 |
if ( my $type = ref $name ) { |
| 1226 |
Marpa::R2::exception( |
| 1227 |
"Symbol name was ref to $type; it must be a scalar string"); |
| 1228 |
} |
| 1229 |
if ( not $grammar->[Marpa::R2::Internal::Grammar::INTERNAL] ) { |
| 1230 |
my $final_symbol = substr $name, -1; |
| 1231 |
if ( $DEFAULT_SYMBOLS_RESERVED{$final_symbol} ) { |
| 1232 |
Marpa::R2::exception( |
| 1233 |
qq{Symbol name $name ends in "$final_symbol": that's not allowed} |
| 1234 |
); |
| 1235 |
} |
| 1236 |
} ## end if ( not $grammar->[Marpa::R2::Internal::Grammar::INTERNAL...]) |
| 1237 |
my $symbol = assign_symbol( $grammar, $name, $options ); |
| 1238 |
|
| 1239 |
return $symbol; |
| 1240 |
|
| 1241 |
} ## end sub assign_user_symbol |
| 1242 |
|
| 1243 |
# add one or more rules |
| 1244 |
sub add_user_rules { |
| 1245 |
my ( $grammar, $rules ) = @_; |
| 1246 |
|
| 1247 |
my @hash_rules = (); |
| 1248 |
RULE: for my $rule ( @{$rules} ) { |
| 1249 |
|
| 1250 |
# Translate other rule formats into hash rules |
| 1251 |
my $ref_rule = ref $rule; |
| 1252 |
if ( $ref_rule eq 'HASH' ) { |
| 1253 |
push @hash_rules, $rule; |
| 1254 |
next RULE; |
| 1255 |
} |
| 1256 |
if ( $ref_rule eq 'ARRAY' ) { |
| 1257 |
my $arg_count = @{$rule}; |
| 1258 |
|
| 1259 |
if ( $arg_count > 4 or $arg_count < 1 ) { |
| 1260 |
Marpa::R2::exception( |
| 1261 |
"Rule has $arg_count arguments: " |
| 1262 |
. join( ', ', |
| 1263 |
map { defined $_ ? $_ : 'undef' } @{$rule} ) |
| 1264 |
. "\n" |
| 1265 |
. 'Rule must have from 1 to 4 arguments' |
| 1266 |
); |
| 1267 |
} ## end if ( $arg_count > 4 or $arg_count < 1 ) |
| 1268 |
my ( $lhs, $rhs, $action ) = @{$rule}; |
| 1269 |
push @hash_rules, |
| 1270 |
{ |
| 1271 |
lhs => $lhs, |
| 1272 |
rhs => $rhs, |
| 1273 |
action => $action, |
| 1274 |
}; |
| 1275 |
next RULE; |
| 1276 |
} ## end if ( $ref_rule eq 'ARRAY' ) |
| 1277 |
Marpa::R2::exception( |
| 1278 |
'Invalid rule: ', |
| 1279 |
Data::Dumper->new( [$rule], ['Invalid_Rule'] )->Indent(2) |
| 1280 |
->Terse(1)->Maxdepth(2)->Dump, |
| 1281 |
'Rule must be ref to HASH or ARRAY' |
| 1282 |
); |
| 1283 |
|
| 1284 |
} # RULE |
| 1285 |
|
| 1286 |
for my $hash_rule (@hash_rules) { |
| 1287 |
add_user_rule( $grammar, $hash_rule ); |
| 1288 |
} |
| 1289 |
|
| 1290 |
return; |
| 1291 |
|
| 1292 |
} ## end sub add_user_rules |
| 1293 |
|
| 1294 |
sub add_user_rule { |
| 1295 |
my ( $grammar, $options ) = @_; |
| 1296 |
|
| 1297 |
Marpa::R2::exception('Missing argument to add_user_rule') |
| 1298 |
if not defined $grammar |
| 1299 |
or not defined $options; |
| 1300 |
|
| 1301 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1302 |
my $tracer = $grammar->[Marpa::R2::Internal::Grammar::TRACER]; |
| 1303 |
my $rules = $grammar->[Marpa::R2::Internal::Grammar::RULES]; |
| 1304 |
my $default_rank = $grammar_c->default_rank(); |
| 1305 |
|
| 1306 |
my ( $lhs_name, $rhs_names, $action, $blessing ); |
| 1307 |
my ( $min, $separator_name ); |
| 1308 |
my $rank; |
| 1309 |
my $null_ranking; |
| 1310 |
my $rule_name; |
| 1311 |
my $slif_tag; |
| 1312 |
my $mask; |
| 1313 |
my $proper_separation = 0; |
| 1314 |
my $keep_separation = 0; |
| 1315 |
my $description; |
| 1316 |
|
| 1317 |
OPTION: for my $option ( keys %{$options} ) { |
| 1318 |
my $value = $options->{$option}; |
| 1319 |
if ( $option eq 'name' ) { $rule_name = $value; next OPTION; } |
| 1320 |
if ( $option eq 'tag' ) { $slif_tag = $value; next OPTION; } |
| 1321 |
if ( $option eq 'rhs' ) { $rhs_names = $value; next OPTION } |
| 1322 |
if ( $option eq 'lhs' ) { $lhs_name = $value; next OPTION } |
| 1323 |
if ( $option eq 'action' ) { $action = $value; next OPTION } |
| 1324 |
if ( $option eq 'bless' ) { $blessing = $value; next OPTION } |
| 1325 |
if ( $option eq 'rank' ) { $rank = $value; next OPTION } |
| 1326 |
if ( $option eq 'null_ranking' ) { |
| 1327 |
$null_ranking = $value; |
| 1328 |
next OPTION; |
| 1329 |
} |
| 1330 |
if ( $option eq 'min' ) { $min = $value; next OPTION } |
| 1331 |
if ( $option eq 'separator' ) { |
| 1332 |
$separator_name = $value; |
| 1333 |
next OPTION; |
| 1334 |
} |
| 1335 |
if ( $option eq 'proper' ) { |
| 1336 |
$proper_separation = $value; |
| 1337 |
next OPTION; |
| 1338 |
} |
| 1339 |
if ( $option eq 'keep' ) { $keep_separation = $value; next OPTION } |
| 1340 |
if ( $option eq 'mask' ) { $mask = $value; next OPTION } |
| 1341 |
if ( $option eq 'description' ) { $description = $value; next OPTION } |
| 1342 |
Marpa::R2::exception("Unknown user rule option: $option"); |
| 1343 |
} ## end OPTION: for my $option ( keys %{$options} ) |
| 1344 |
|
| 1345 |
if ( defined $min and not Scalar::Util::looks_like_number($min) ) { |
| 1346 |
Marpa::R2::exception( |
| 1347 |
q{"min" must be undefined or a valid Perl number}); |
| 1348 |
} |
| 1349 |
my $stuifzand_interface = |
| 1350 |
$grammar->[Marpa::R2::Internal::Grammar::INTERFACE] eq 'stuifzand'; |
| 1351 |
|
| 1352 |
my $grammar_is_internal = $stuifzand_interface |
| 1353 |
|| $grammar->[Marpa::R2::Internal::Grammar::INTERNAL]; |
| 1354 |
|
| 1355 |
my $lhs = |
| 1356 |
$grammar_is_internal |
| 1357 |
? assign_symbol( $grammar, $lhs_name ) |
| 1358 |
: assign_user_symbol( $grammar, $lhs_name ); |
| 1359 |
$rhs_names //= []; |
| 1360 |
|
| 1361 |
my @rule_problems = (); |
| 1362 |
|
| 1363 |
my $rhs_ref_type = ref $rhs_names; |
| 1364 |
if ( not $rhs_ref_type or $rhs_ref_type ne 'ARRAY' ) { |
| 1365 |
my $problem = |
| 1366 |
"RHS is not ref to ARRAY\n" |
| 1367 |
. ' Type of rhs is ' |
| 1368 |
. ( $rhs_ref_type ? $rhs_ref_type : 'not a ref' ) . "\n"; |
| 1369 |
my $d = Data::Dumper->new( [$rhs_names], ['rhs'] ); |
| 1370 |
$problem .= $d->Dump(); |
| 1371 |
push @rule_problems, $problem; |
| 1372 |
} ## end if ( not $rhs_ref_type or $rhs_ref_type ne 'ARRAY' ) |
| 1373 |
if ( not defined $lhs_name ) { |
| 1374 |
push @rule_problems, "Missing LHS\n"; |
| 1375 |
} |
| 1376 |
|
| 1377 |
if ( defined $rank |
| 1378 |
and |
| 1379 |
( not Scalar::Util::looks_like_number($rank) or int($rank) != $rank ) |
| 1380 |
) |
| 1381 |
{ |
| 1382 |
push @rule_problems, "Rank must be undefined or an integer\n"; |
| 1383 |
} ## end if ( defined $rank and ( not Scalar::Util::looks_like_number...)) |
| 1384 |
$rank //= $default_rank; |
| 1385 |
|
| 1386 |
$null_ranking //= 'low'; |
| 1387 |
if ( $null_ranking ne 'high' and $null_ranking ne 'low' ) { |
| 1388 |
push @rule_problems, |
| 1389 |
"Null Ranking must be undefined, 'high' or 'low'\n"; |
| 1390 |
} |
| 1391 |
|
| 1392 |
if ( scalar @rule_problems ) { |
| 1393 |
my %dump_options = %{$options}; |
| 1394 |
delete $dump_options{grammar}; |
| 1395 |
my $msg = ( scalar @rule_problems ) |
| 1396 |
. " problem(s) in the following rule:\n"; |
| 1397 |
my $d = Data::Dumper->new( [ \%dump_options ], ['rule'] ); |
| 1398 |
$msg .= $d->Dump(); |
| 1399 |
for my $problem_number ( 0 .. $#rule_problems ) { |
| 1400 |
$msg |
| 1401 |
.= 'Problem ' |
| 1402 |
. ( $problem_number + 1 ) . q{: } |
| 1403 |
. $rule_problems[$problem_number] . "\n"; |
| 1404 |
} ## end for my $problem_number ( 0 .. $#rule_problems ) |
| 1405 |
Marpa::R2::exception($msg); |
| 1406 |
} ## end if ( scalar @rule_problems ) |
| 1407 |
|
| 1408 |
my $rhs = [ |
| 1409 |
map { |
| 1410 |
$grammar_is_internal |
| 1411 |
? assign_symbol( $grammar, $_ ) |
| 1412 |
: assign_user_symbol( $grammar, $_ ); |
| 1413 |
} @{$rhs_names} |
| 1414 |
]; |
| 1415 |
|
| 1416 |
# Is this is an ordinary, non-counted rule? |
| 1417 |
my $is_ordinary_rule = scalar @{$rhs_names} == 0 || !defined $min; |
| 1418 |
if ( defined $separator_name and $is_ordinary_rule ) { |
| 1419 |
if ( defined $separator_name ) { |
| 1420 |
Marpa::R2::exception( |
| 1421 |
'separator defined for rule without repetitions'); |
| 1422 |
} |
| 1423 |
} ## end if ( defined $separator_name and $is_ordinary_rule ) |
| 1424 |
|
| 1425 |
my @rhs_ids = map { $_->[Marpa::R2::Internal::Symbol::ID] } @{$rhs}; |
| 1426 |
my $lhs_id = $lhs->[Marpa::R2::Internal::Symbol::ID]; |
| 1427 |
|
| 1428 |
my $base_rule_id; |
| 1429 |
my $separator_id = -1; |
| 1430 |
|
| 1431 |
if ($is_ordinary_rule) { |
| 1432 |
|
| 1433 |
# Capture errors |
| 1434 |
$grammar_c->throw_set(0); |
| 1435 |
$base_rule_id = $grammar_c->rule_new( $lhs_id, \@rhs_ids ); |
| 1436 |
$grammar_c->throw_set(1); |
| 1437 |
|
| 1438 |
} ## end if ($is_ordinary_rule) |
| 1439 |
else { |
| 1440 |
Marpa::R2::exception('Only one rhs symbol allowed for counted rule') |
| 1441 |
if scalar @{$rhs_names} != 1; |
| 1442 |
|
| 1443 |
# create the separator symbol, if we're using one |
| 1444 |
if ( defined $separator_name ) { |
| 1445 |
my $separator = |
| 1446 |
$grammar_is_internal |
| 1447 |
? assign_symbol( $grammar, $separator_name ) |
| 1448 |
: assign_user_symbol( $grammar, $separator_name ); |
| 1449 |
$separator_id = $separator->[Marpa::R2::Internal::Symbol::ID]; |
| 1450 |
} ## end if ( defined $separator_name ) |
| 1451 |
|
| 1452 |
$grammar_c->throw_set(0); |
| 1453 |
|
| 1454 |
# The original rule for a sequence rule is |
| 1455 |
# not actually used in parsing, |
| 1456 |
# but some of the rewritten sequence rules are its |
| 1457 |
# semantic equivalents. |
| 1458 |
|
| 1459 |
$base_rule_id = $grammar_c->sequence_new( |
| 1460 |
$lhs_id, |
| 1461 |
$rhs_ids[0], |
| 1462 |
{ separator => $separator_id, |
| 1463 |
proper => $proper_separation, |
| 1464 |
min => $min, |
| 1465 |
} |
| 1466 |
); |
| 1467 |
$grammar_c->throw_set(1); |
| 1468 |
} ## end else [ if ($is_ordinary_rule) ] |
| 1469 |
|
| 1470 |
if ( not defined $base_rule_id or $base_rule_id < 0 ) { |
| 1471 |
my $rule_description = rule_describe( $lhs_name, $rhs_names ); |
| 1472 |
my ( $error_code, $error_string ) = $grammar_c->error(); |
| 1473 |
$error_code //= -1; |
| 1474 |
my $problem_description = |
| 1475 |
$error_code == $Marpa::R2::Error::DUPLICATE_RULE |
| 1476 |
? 'Duplicate rule' |
| 1477 |
: $error_string; |
| 1478 |
Marpa::R2::exception("$problem_description: $rule_description"); |
| 1479 |
} ## end if ( not defined $base_rule_id or $base_rule_id < 0 ) |
| 1480 |
|
| 1481 |
my $base_rule = shadow_rule( $grammar, $base_rule_id ); |
| 1482 |
|
| 1483 |
if ($is_ordinary_rule) { |
| 1484 |
|
| 1485 |
# Only internal grammars can set a custom mask |
| 1486 |
if ( not defined $mask or not $grammar_is_internal ) { |
| 1487 |
$mask = [ (1) x scalar @rhs_ids ]; |
| 1488 |
} |
| 1489 |
$base_rule->[Marpa::R2::Internal::Rule::MASK] = $mask; |
| 1490 |
} ## end if ($is_ordinary_rule) |
| 1491 |
|
| 1492 |
$base_rule->[Marpa::R2::Internal::Rule::DISCARD_SEPARATION] = |
| 1493 |
$separator_id >= 0 && !$keep_separation; |
| 1494 |
|
| 1495 |
$base_rule->[Marpa::R2::Internal::Rule::ACTION_NAME] = $action; |
| 1496 |
$grammar_c->rule_null_high_set( $base_rule_id, |
| 1497 |
( $null_ranking eq 'high' ? 1 : 0 ) ); |
| 1498 |
$grammar_c->rule_rank_set( $base_rule_id, $rank ); |
| 1499 |
|
| 1500 |
if ( defined $rule_name ) { |
| 1501 |
$base_rule->[Marpa::R2::Internal::Rule::NAME] = $rule_name; |
| 1502 |
} |
| 1503 |
if ( defined $slif_tag ) { |
| 1504 |
$base_rule->[Marpa::R2::Internal::Rule::SLIF_TAG] = $slif_tag; |
| 1505 |
my $rule_id_by_tag = |
| 1506 |
$grammar->[Marpa::R2::Internal::Grammar::RULE_ID_BY_TAG]; |
| 1507 |
if ( $rule_id_by_tag->{$slif_tag} ) { |
| 1508 |
Marpa::R2::exception( |
| 1509 |
qq{Duplicate tag in SLIF rule, tag was "$slif_tag"}); |
| 1510 |
} |
| 1511 |
$rule_id_by_tag->{$slif_tag} = $base_rule_id; |
| 1512 |
} ## end if ( defined $slif_tag ) |
| 1513 |
if ( defined $blessing ) { |
| 1514 |
$base_rule->[Marpa::R2::Internal::Rule::BLESSING] = $blessing; |
| 1515 |
} |
| 1516 |
if ( defined $description ) { |
| 1517 |
$base_rule->[Marpa::R2::Internal::Rule::DESCRIPTION] = $description; |
| 1518 |
} |
| 1519 |
|
| 1520 |
return; |
| 1521 |
|
| 1522 |
} ## end sub add_user_rule |
| 1523 |
|
| 1524 |
sub rule_describe { |
| 1525 |
my ( $lhs_name, $rhs_names ) = @_; |
| 1526 |
# wrap symbol names with whitespaces allowed by SLIF |
| 1527 |
$lhs_name = "<$lhs_name>" if $lhs_name =~ / /; |
| 1528 |
return "$lhs_name -> " . ( join q{ }, map { / / ? "<$_>" : $_ } @{$rhs_names} ); |
| 1529 |
} ## end sub rule_describe |
| 1530 |
|
| 1531 |
sub set_start_symbol { |
| 1532 |
my $grammar = shift; |
| 1533 |
|
| 1534 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1535 |
state $default_start_name = '[:start]'; |
| 1536 |
my $tracer = $grammar->[Marpa::R2::Internal::Grammar::TRACER]; |
| 1537 |
my $default_start_id = $tracer->symbol_by_name($default_start_name); |
| 1538 |
my $start_id; |
| 1539 |
VALIDATE_START_NAME: { |
| 1540 |
my $named_arg_start_name = |
| 1541 |
$grammar->[Marpa::R2::Internal::Grammar::START_NAME]; |
| 1542 |
if ( defined $named_arg_start_name and defined $start_id ) { |
| 1543 |
Marpa::R2::exception( |
| 1544 |
qq{Start symbol specified as '[:start]', but also with named argument\n}, |
| 1545 |
qq{ You must use one or the other\n} |
| 1546 |
); |
| 1547 |
} ## end if ( defined $named_arg_start_name and defined $start_id) |
| 1548 |
if ( defined $named_arg_start_name ) { |
| 1549 |
$start_id = $tracer->symbol_by_name($named_arg_start_name); |
| 1550 |
Marpa::R2::exception( |
| 1551 |
qq{Start symbol "$named_arg_start_name" not in grammar}) |
| 1552 |
if not defined $start_id; |
| 1553 |
last VALIDATE_START_NAME; |
| 1554 |
} ## end if ( defined $named_arg_start_name ) |
| 1555 |
if ( defined $default_start_id ) { |
| 1556 |
$start_id = $default_start_id; |
| 1557 |
$grammar->[Marpa::R2::Internal::Grammar::START_NAME] = |
| 1558 |
$named_arg_start_name; |
| 1559 |
last VALIDATE_START_NAME; |
| 1560 |
} ## end if ( defined $default_start_id ) |
| 1561 |
Marpa::R2::exception(qq{No start symbol specified in grammar\n}); |
| 1562 |
} ## end VALIDATE_START_NAME: |
| 1563 |
|
| 1564 |
if ( not defined $grammar_c->start_symbol_set($start_id) ) { |
| 1565 |
Marpa::R2::uncaught_error( $grammar_c->error() ); |
| 1566 |
} |
| 1567 |
return 1; |
| 1568 |
} ## end sub set_start_symbol |
| 1569 |
|
| 1570 |
sub Marpa::R2::Grammar::error { |
| 1571 |
my ($grammar) = @_; |
| 1572 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1573 |
return $grammar_c->error(); |
| 1574 |
} |
| 1575 |
|
| 1576 |
# INTERNAL OK AFTER HERE _marpa_ |
| 1577 |
|
| 1578 |
sub Marpa::R2::Grammar::show_isy { |
| 1579 |
my ( $grammar, $isy_id ) = @_; |
| 1580 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1581 |
my $tracer = $grammar->[Marpa::R2::Internal::Grammar::TRACER]; |
| 1582 |
my $text = q{}; |
| 1583 |
|
| 1584 |
my $name = $tracer->isy_name($isy_id); |
| 1585 |
$text .= "$isy_id: $name"; |
| 1586 |
|
| 1587 |
my @tag_list = (); |
| 1588 |
$grammar_c->_marpa_g_nsy_is_nulling($isy_id) |
| 1589 |
and push @tag_list, 'nulling'; |
| 1590 |
|
| 1591 |
$text .= join q{ }, q{,}, @tag_list if scalar @tag_list; |
| 1592 |
$text .= "\n"; |
| 1593 |
|
| 1594 |
return $text; |
| 1595 |
|
| 1596 |
} ## end sub Marpa::R2::Grammar::show_isy |
| 1597 |
|
| 1598 |
sub Marpa::R2::Grammar::show_isys { |
| 1599 |
my ($grammar) = @_; |
| 1600 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1601 |
my $text = q{}; |
| 1602 |
for my $isy_id ( 0 .. $grammar_c->_marpa_g_nsy_count() - 1 ) { |
| 1603 |
$text .= $grammar->show_isy($isy_id); |
| 1604 |
} |
| 1605 |
return $text; |
| 1606 |
} ## end sub Marpa::R2::Grammar::show_isys |
| 1607 |
|
| 1608 |
sub Marpa::R2::Grammar::brief_irl { |
| 1609 |
my ( $grammar, $irl_id ) = @_; |
| 1610 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1611 |
my $tracer = $grammar->[Marpa::R2::Internal::Grammar::TRACER]; |
| 1612 |
my $lhs_id = $grammar_c->_marpa_g_irl_lhs($irl_id); |
| 1613 |
my $text = $irl_id . ': ' . $tracer->isy_name($lhs_id) . ' ->'; |
| 1614 |
if ( my $rh_length = $grammar_c->_marpa_g_irl_length($irl_id) ) { |
| 1615 |
my @rhs_ids = (); |
| 1616 |
for my $ix ( 0 .. $rh_length - 1 ) { |
| 1617 |
push @rhs_ids, $grammar_c->_marpa_g_irl_rhs( $irl_id, $ix ); |
| 1618 |
} |
| 1619 |
$text .= q{ } . ( join q{ }, map { $tracer->isy_name($_) } @rhs_ids ); |
| 1620 |
} ## end if ( my $rh_length = $grammar_c->_marpa_g_irl_length...) |
| 1621 |
return $text; |
| 1622 |
} ## end sub Marpa::R2::Grammar::brief_irl |
| 1623 |
|
| 1624 |
sub Marpa::R2::Grammar::show_irls { |
| 1625 |
my ($grammar) = @_; |
| 1626 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1627 |
my $text = q{}; |
| 1628 |
for my $irl_id ( 0 .. $grammar_c->_marpa_g_irl_count() - 1 ) { |
| 1629 |
$text .= $grammar->brief_irl($irl_id) . "\n"; |
| 1630 |
} |
| 1631 |
return $text; |
| 1632 |
} ## end sub Marpa::R2::Grammar::show_irls |
| 1633 |
|
| 1634 |
sub Marpa::R2::Grammar::rule_is_used { |
| 1635 |
my ( $grammar, $rule_id ) = @_; |
| 1636 |
my $grammar_c = $grammar->[Marpa::R2::Internal::Grammar::C]; |
| 1637 |
return $grammar_c->_marpa_g_rule_is_used($rule_id); |
| 1638 |
} |
| 1639 |
|
| 1640 |
sub Marpa::R2::Grammar::show_ahms { |
| 1641 |
my ( $grammar, $verbose ) = @_; |
| 1642 |
return $grammar->[Marpa::R2::Internal::Grammar::TRACER] |
| 1643 |
->show_ahms($verbose); |
| 1644 |
} |
| 1645 |
|
| 1646 |
1; |
| 1647 |
|
| 1648 |
# vim: expandtab shiftwidth=4: |