| 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::MetaAST; |
| 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::MetaAST; |
| 30 |
|
| 31 |
use English qw( -no_match_vars ); |
| 32 |
|
| 33 |
sub new { |
| 34 |
my ( $class, $p_rules_source ) = @_; |
| 35 |
my $meta_recce = Marpa::R2::Internal::Scanless::meta_recce(); |
| 36 |
eval { $meta_recce->read($p_rules_source) } |
| 37 |
or Marpa::R2::exception( "Parse of BNF/Scanless source failed\n", |
| 38 |
$EVAL_ERROR ); |
| 39 |
if ( my $ambiguity_status = $meta_recce->ambiguous() ) { |
| 40 |
Marpa::R2::exception( "Parse of BNF/Scanless source failed:\n", |
| 41 |
$ambiguity_status ); |
| 42 |
} |
| 43 |
my $value_ref = $meta_recce->value(); |
| 44 |
Marpa::R2::exception('Parse of BNF/Scanless source failed') |
| 45 |
if not defined $value_ref; |
| 46 |
my $ast = { meta_recce => $meta_recce, top_node => ${$value_ref} }; |
| 47 |
return bless $ast, $class; |
| 48 |
} ## end sub new |
| 49 |
|
| 50 |
sub Marpa::R2::Internal::MetaAST::Parse::substring { |
| 51 |
my ( $parse, $start, $length ) = @_; |
| 52 |
my $meta_slr = $parse->{meta_recce}; |
| 53 |
my $thin_meta_slr = $meta_slr->[Marpa::R2::Internal::Scanless::R::C]; |
| 54 |
my $string = $thin_meta_slr->substring( $start, $length ); |
| 55 |
chomp $string; |
| 56 |
return $string; |
| 57 |
} ## end sub Marpa::R2::Internal::MetaAST::Parse::substring |
| 58 |
|
| 59 |
sub ast_to_hash { |
| 60 |
my ($ast) = @_; |
| 61 |
my $hashed_ast = {}; |
| 62 |
|
| 63 |
$hashed_ast->{meta_recce} = $ast->{meta_recce}; |
| 64 |
bless $hashed_ast, 'Marpa::R2::Internal::MetaAST::Parse'; |
| 65 |
|
| 66 |
$hashed_ast->{current_lexer} = 'L0'; |
| 67 |
$hashed_ast->{rules}->{G1} = []; |
| 68 |
my $g1_symbols = $hashed_ast->{symbols}->{G1} = {}; |
| 69 |
|
| 70 |
my ( undef, undef, @statements ) = @{ $ast->{top_node} }; |
| 71 |
|
| 72 |
# This is the last ditch exception catcher |
| 73 |
# It forces all Marpa exceptions to be die's, |
| 74 |
# then catches them and rethrows using Carp. |
| 75 |
# |
| 76 |
# The plan is to use die(), with higher levels |
| 77 |
# catching and re-die()'ing after adding |
| 78 |
# helpful location information. After the |
| 79 |
# re-throw it is caught here and passed to |
| 80 |
# Carp. |
| 81 |
my $eval_ok = eval { |
| 82 |
local $Marpa::R2::JUST_DIE = 1; |
| 83 |
$_->evaluate($hashed_ast) for @statements; |
| 84 |
1; |
| 85 |
}; |
| 86 |
Marpa::R2::exception($EVAL_ERROR) if not $eval_ok; |
| 87 |
|
| 88 |
my %grammars = (); |
| 89 |
$grammars{$_} = 1 for keys %{ $hashed_ast->{rules} }; |
| 90 |
my @lexers = |
| 91 |
grep { ( substr $_, 0, 1 ) eq 'L' } keys %grammars; |
| 92 |
|
| 93 |
for my $lexer (@lexers) { |
| 94 |
my $lexer_name = $lexer; |
| 95 |
NAME_LEXER: { |
| 96 |
if ( $lexer eq 'L0' ) { |
| 97 |
$lexer_name = "L0 (the default)"; |
| 98 |
last NAME_LEXER; |
| 99 |
} |
| 100 |
last NAME_LEXER if ( substr $lexer_name, 0, 2 ) ne 'L-'; |
| 101 |
$lexer_name = substr $lexer_name, 2; |
| 102 |
} ## end NAME_LEXER: |
| 103 |
} ## end for my $lexer (@lexers) |
| 104 |
|
| 105 |
my %stripped_character_classes = (); |
| 106 |
{ |
| 107 |
my $character_classes = $hashed_ast->{character_classes}; |
| 108 |
for my $symbol_name ( sort keys %{$character_classes} ) { |
| 109 |
my ($re) = @{ $character_classes->{$symbol_name} }; |
| 110 |
$stripped_character_classes{$symbol_name} = $re; |
| 111 |
} |
| 112 |
} |
| 113 |
$hashed_ast->{character_classes} = \%stripped_character_classes; |
| 114 |
|
| 115 |
return $hashed_ast; |
| 116 |
} ## end sub ast_to_hash |
| 117 |
|
| 118 |
sub Marpa::R2::Internal::MetaAST::Parse::start_rule_setup { |
| 119 |
my ($ast) = @_; |
| 120 |
my $start_lhs = $ast->{'start_lhs'} // $ast->{'first_lhs'}; |
| 121 |
Marpa::R2::exception('No rules in SLIF grammar') |
| 122 |
if not defined $start_lhs; |
| 123 |
Marpa::R2::Internal::MetaAST::start_rule_create( $ast, $start_lhs ); |
| 124 |
} ## end sub Marpa::R2::Internal::MetaAST::Parse::start_rule_setup |
| 125 |
|
| 126 |
# This class is for pieces of RHS alternatives, as they are |
| 127 |
# being constructed |
| 128 |
my $PROTO_ALTERNATIVE = 'Marpa::R2::Internal::MetaAST::Proto_Alternative'; |
| 129 |
|
| 130 |
sub Marpa::R2::Internal::MetaAST::Proto_Alternative::combine { |
| 131 |
my ( $class, @hashes ) = @_; |
| 132 |
my $self = bless {}, $class; |
| 133 |
for my $hash_to_add (@hashes) { |
| 134 |
for my $key ( keys %{$hash_to_add} ) { |
| 135 |
## expect to be caught and rethrown |
| 136 |
die qq{A Marpa rule contained a duplicate key\n}, |
| 137 |
qq{ The key was "$key"\n} |
| 138 |
if exists $self->{$key}; |
| 139 |
$self->{$key} = $hash_to_add->{$key}; |
| 140 |
} ## end for my $key ( keys %{$hash_to_add} ) |
| 141 |
} ## end for my $hash_to_add (@hashes) |
| 142 |
return $self; |
| 143 |
} ## end sub Marpa::R2::Internal::MetaAST::Proto_Alternative::combine |
| 144 |
|
| 145 |
sub Marpa::R2::Internal::MetaAST::Parse::bless_hash_rule { |
| 146 |
my ( $parse, $hash_rule, $blessing, $naming, $original_lhs ) = @_; |
| 147 |
return if (substr $Marpa::R2::Internal::SUBGRAMMAR, 0, 1) eq 'L'; |
| 148 |
|
| 149 |
$naming //= $original_lhs; |
| 150 |
$hash_rule->{name} = $naming; |
| 151 |
|
| 152 |
return if not defined $blessing; |
| 153 |
FIND_BLESSING: { |
| 154 |
last FIND_BLESSING if $blessing =~ /\A [\w] /xms; |
| 155 |
return if $blessing eq '::undef'; |
| 156 |
|
| 157 |
# Rule may be half-formed, but assume we have lhs |
| 158 |
if ( $blessing eq '::lhs' ) { |
| 159 |
$blessing = $original_lhs; |
| 160 |
if ( $blessing =~ / [^ [:alnum:]] /xms ) { |
| 161 |
Marpa::R2::exception( |
| 162 |
qq{"::lhs" blessing only allowed if LHS is whitespace and alphanumerics\n}, |
| 163 |
qq{ LHS was <$original_lhs>\n} |
| 164 |
); |
| 165 |
} ## end if ( $blessing =~ / [^ [:alnum:]] /xms ) |
| 166 |
$blessing =~ s/[ ]/_/gxms; |
| 167 |
last FIND_BLESSING; |
| 168 |
} ## end if ( $blessing eq '::lhs' ) |
| 169 |
Marpa::R2::exception( qq{Unknown blessing "$blessing"\n} ); |
| 170 |
} ## end FIND_BLESSING: |
| 171 |
$hash_rule->{bless} = $blessing; |
| 172 |
return 1; |
| 173 |
} ## end sub Marpa::R2::Internal::MetaAST::Parse::bless_hash_rule |
| 174 |
|
| 175 |
sub Marpa::R2::Internal::MetaAST_Nodes::bare_name::name { return $_[0]->[2] } |
| 176 |
|
| 177 |
sub Marpa::R2::Internal::MetaAST_Nodes::reserved_action_name::name { |
| 178 |
my ( $self, $parse ) = @_; |
| 179 |
return $self->[2]; |
| 180 |
} |
| 181 |
|
| 182 |
sub Marpa::R2::Internal::MetaAST_Nodes::reserved_event_name::name { |
| 183 |
my ( $self, $parse ) = @_; |
| 184 |
my $name = $self->[2]; |
| 185 |
$name =~ s/\A : /'/xms; |
| 186 |
return $name; |
| 187 |
} |
| 188 |
|
| 189 |
sub Marpa::R2::Internal::MetaAST_Nodes::action_name::name { |
| 190 |
my ( $self, $parse ) = @_; |
| 191 |
return $self->[2]->name($parse); |
| 192 |
} |
| 193 |
|
| 194 |
sub Marpa::R2::Internal::MetaAST_Nodes::alternative_name::name { |
| 195 |
my ( $self, $parse ) = @_; |
| 196 |
return $self->[2]->name($parse); |
| 197 |
} |
| 198 |
|
| 199 |
sub Marpa::R2::Internal::MetaAST_Nodes::event_name::name { |
| 200 |
my ( $self, $parse ) = @_; |
| 201 |
return $self->[2]->name($parse); |
| 202 |
} |
| 203 |
|
| 204 |
sub Marpa::R2::Internal::MetaAST_Nodes::lexer_name::name { |
| 205 |
my ( $self, $parse ) = @_; |
| 206 |
return $self->[2]->name($parse); |
| 207 |
} |
| 208 |
|
| 209 |
sub Marpa::R2::Internal::MetaAST_Nodes::array_descriptor::name { |
| 210 |
return $_[0]->[2]; |
| 211 |
} |
| 212 |
|
| 213 |
sub Marpa::R2::Internal::MetaAST_Nodes::reserved_blessing_name::name { |
| 214 |
return $_[0]->[2]; |
| 215 |
} |
| 216 |
|
| 217 |
sub Marpa::R2::Internal::MetaAST_Nodes::blessing_name::name { |
| 218 |
my ( $self, $parse ) = @_; |
| 219 |
return $self->[2]->name($parse); |
| 220 |
} |
| 221 |
|
| 222 |
sub Marpa::R2::Internal::MetaAST_Nodes::standard_name::name { |
| 223 |
return $_[0]->[2]; |
| 224 |
} |
| 225 |
|
| 226 |
sub Marpa::R2::Internal::MetaAST_Nodes::Perl_name::name { |
| 227 |
return $_[0]->[2]; |
| 228 |
} |
| 229 |
|
| 230 |
sub Marpa::R2::Internal::MetaAST_Nodes::lhs::name { |
| 231 |
my ( $values, $parse ) = @_; |
| 232 |
my ( undef, undef, $symbol ) = @{$values}; |
| 233 |
return $symbol->name($parse); |
| 234 |
} |
| 235 |
|
| 236 |
# After development, delete this |
| 237 |
sub Marpa::R2::Internal::MetaAST_Nodes::lhs::evaluate { |
| 238 |
my ( $values, $parse ) = @_; |
| 239 |
return $values->name($parse); |
| 240 |
} |
| 241 |
|
| 242 |
sub Marpa::R2::Internal::MetaAST_Nodes::quantifier::evaluate { |
| 243 |
my ($data) = @_; |
| 244 |
return $data->[2]; |
| 245 |
} |
| 246 |
|
| 247 |
sub Marpa::R2::Internal::MetaAST_Nodes::op_declare::op { |
| 248 |
my ($values) = @_; |
| 249 |
return $values->[2]->op(); |
| 250 |
} |
| 251 |
|
| 252 |
sub Marpa::R2::Internal::MetaAST_Nodes::op_declare_match::op { |
| 253 |
my ($values) = @_; |
| 254 |
return $values->[2]; |
| 255 |
} |
| 256 |
|
| 257 |
sub Marpa::R2::Internal::MetaAST_Nodes::op_declare_bnf::op { |
| 258 |
my ($values) = @_; |
| 259 |
return $values->[2]; |
| 260 |
} |
| 261 |
|
| 262 |
sub Marpa::R2::Internal::MetaAST_Nodes::bracketed_name::name { |
| 263 |
my ($values) = @_; |
| 264 |
my ( undef, undef, $bracketed_name ) = @{$values}; |
| 265 |
|
| 266 |
# normalize whitespace |
| 267 |
$bracketed_name =~ s/\A [<] \s*//xms; |
| 268 |
$bracketed_name =~ s/ \s* [>] \z//xms; |
| 269 |
$bracketed_name =~ s/ \s+ / /gxms; |
| 270 |
return $bracketed_name; |
| 271 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::bracketed_name::name |
| 272 |
|
| 273 |
sub Marpa::R2::Internal::MetaAST_Nodes::single_quoted_name::name { |
| 274 |
my ($values) = @_; |
| 275 |
my ( undef, undef, $single_quoted_name ) = @{$values}; |
| 276 |
|
| 277 |
# normalize whitespace |
| 278 |
$single_quoted_name =~ s/\A ['] \s*//xms; |
| 279 |
$single_quoted_name =~ s/ \s* ['] \z//xms; |
| 280 |
$single_quoted_name =~ s/ \s+ / /gxms; |
| 281 |
return $single_quoted_name; |
| 282 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::single_quoted_name::name |
| 283 |
|
| 284 |
sub Marpa::R2::Internal::MetaAST_Nodes::parenthesized_rhs_primary_list::evaluate |
| 285 |
{ |
| 286 |
my ( $data, $parse ) = @_; |
| 287 |
my ( undef, undef, @values ) = @{$data}; |
| 288 |
my @symbol_lists = map { $_->evaluate($parse); } @values; |
| 289 |
my $flattened_list = |
| 290 |
Marpa::R2::Internal::MetaAST::Symbol_List->combine(@symbol_lists); |
| 291 |
$flattened_list->mask_set(0); |
| 292 |
return $flattened_list; |
| 293 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::parenthesized_rhs_primary_list::evaluate |
| 294 |
|
| 295 |
sub Marpa::R2::Internal::MetaAST_Nodes::rhs::evaluate { |
| 296 |
my ( $data, $parse ) = @_; |
| 297 |
my ( $start, $length, @values ) = @{$data}; |
| 298 |
my $rhs = eval { |
| 299 |
my @symbol_lists = map { $_->evaluate($parse) } @values; |
| 300 |
my $flattened_list = |
| 301 |
Marpa::R2::Internal::MetaAST::Symbol_List->combine(@symbol_lists); |
| 302 |
bless { |
| 303 |
rhs => $flattened_list->names($parse), |
| 304 |
mask => $flattened_list->mask() |
| 305 |
}, |
| 306 |
$PROTO_ALTERNATIVE; |
| 307 |
}; |
| 308 |
if ( not $rhs ) { |
| 309 |
my $eval_error = $EVAL_ERROR; |
| 310 |
chomp $eval_error; |
| 311 |
Marpa::R2::exception( |
| 312 |
qq{$eval_error\n}, |
| 313 |
q{ RHS involved was }, |
| 314 |
$parse->substring( $start, $length ) |
| 315 |
); |
| 316 |
} ## end if ( not $rhs ) |
| 317 |
return $rhs; |
| 318 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::rhs::evaluate |
| 319 |
|
| 320 |
sub Marpa::R2::Internal::MetaAST_Nodes::rhs_primary::evaluate { |
| 321 |
my ( $data, $parse ) = @_; |
| 322 |
my ( undef, undef, @values ) = @{$data}; |
| 323 |
my @symbol_lists = map { $_->evaluate($parse) } @values; |
| 324 |
return Marpa::R2::Internal::MetaAST::Symbol_List->combine(@symbol_lists); |
| 325 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::rhs_primary::evaluate |
| 326 |
|
| 327 |
sub Marpa::R2::Internal::MetaAST_Nodes::rhs_primary_list::evaluate { |
| 328 |
my ( $data, $parse ) = @_; |
| 329 |
my ( undef, undef, @values ) = @{$data}; |
| 330 |
my @symbol_lists = map { $_->evaluate($parse) } @values; |
| 331 |
return Marpa::R2::Internal::MetaAST::Symbol_List->combine(@symbol_lists); |
| 332 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::rhs_primary_list::evaluate |
| 333 |
|
| 334 |
sub Marpa::R2::Internal::MetaAST_Nodes::action::evaluate { |
| 335 |
my ( $values, $parse ) = @_; |
| 336 |
my ( undef, undef, $child ) = @{$values}; |
| 337 |
return bless { action => $child->name($parse) }, $PROTO_ALTERNATIVE; |
| 338 |
} |
| 339 |
|
| 340 |
sub Marpa::R2::Internal::MetaAST_Nodes::blessing::evaluate { |
| 341 |
my ( $values, $parse ) = @_; |
| 342 |
my ( undef, undef, $child ) = @{$values}; |
| 343 |
return bless { bless => $child->name($parse) }, $PROTO_ALTERNATIVE; |
| 344 |
} |
| 345 |
|
| 346 |
sub Marpa::R2::Internal::MetaAST_Nodes::naming::evaluate { |
| 347 |
my ( $values, $parse ) = @_; |
| 348 |
my ( undef, undef, $child ) = @{$values}; |
| 349 |
return bless { name => $child->name($parse) }, $PROTO_ALTERNATIVE; |
| 350 |
} |
| 351 |
|
| 352 |
sub Marpa::R2::Internal::MetaAST_Nodes::right_association::evaluate { |
| 353 |
my ($values) = @_; |
| 354 |
return bless { assoc => 'R' }, $PROTO_ALTERNATIVE; |
| 355 |
} |
| 356 |
|
| 357 |
sub Marpa::R2::Internal::MetaAST_Nodes::left_association::evaluate { |
| 358 |
my ($values) = @_; |
| 359 |
return bless { assoc => 'L' }, $PROTO_ALTERNATIVE; |
| 360 |
} |
| 361 |
|
| 362 |
sub Marpa::R2::Internal::MetaAST_Nodes::group_association::evaluate { |
| 363 |
my ($values) = @_; |
| 364 |
return bless { assoc => 'G' }, $PROTO_ALTERNATIVE; |
| 365 |
} |
| 366 |
|
| 367 |
sub Marpa::R2::Internal::MetaAST_Nodes::event_specification::evaluate { |
| 368 |
my ($values) = @_; |
| 369 |
return bless { event => ( $values->[2]->event() ) }, $PROTO_ALTERNATIVE; |
| 370 |
} |
| 371 |
|
| 372 |
sub Marpa::R2::Internal::MetaAST_Nodes::event_initialization::event { |
| 373 |
my ($values) = @_; |
| 374 |
my $event_name = $values->[2]; |
| 375 |
my $event_initializer = $values->[3]; |
| 376 |
return [$event_name->name(), $event_initializer->on_or_off()], |
| 377 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::event_specification::evaluate |
| 378 |
|
| 379 |
sub Marpa::R2::Internal::MetaAST_Nodes::proper_specification::evaluate { |
| 380 |
my ($values) = @_; |
| 381 |
my $child = $values->[2]; |
| 382 |
return bless { proper => $child->value() }, $PROTO_ALTERNATIVE; |
| 383 |
} |
| 384 |
|
| 385 |
sub Marpa::R2::Internal::MetaAST_Nodes::latm_specification::evaluate { |
| 386 |
my ($values) = @_; |
| 387 |
my $child = $values->[2]; |
| 388 |
return bless { latm => $child->value() }, $PROTO_ALTERNATIVE; |
| 389 |
} |
| 390 |
|
| 391 |
sub Marpa::R2::Internal::MetaAST_Nodes::pause_specification::evaluate { |
| 392 |
my ($values) = @_; |
| 393 |
my $child = $values->[2]; |
| 394 |
return bless { pause => $child->value() }, $PROTO_ALTERNATIVE; |
| 395 |
} |
| 396 |
|
| 397 |
sub Marpa::R2::Internal::MetaAST_Nodes::priority_specification::evaluate { |
| 398 |
my ($values) = @_; |
| 399 |
my $child = $values->[2]; |
| 400 |
return bless { priority => $child->value() }, $PROTO_ALTERNATIVE; |
| 401 |
} |
| 402 |
|
| 403 |
sub Marpa::R2::Internal::MetaAST_Nodes::rank_specification::evaluate { |
| 404 |
my ($values) = @_; |
| 405 |
my $child = $values->[2]; |
| 406 |
return bless { rank => $child->value() }, $PROTO_ALTERNATIVE; |
| 407 |
} |
| 408 |
|
| 409 |
sub Marpa::R2::Internal::MetaAST_Nodes::null_ranking_specification::evaluate { |
| 410 |
my ($values) = @_; |
| 411 |
my $child = $values->[2]; |
| 412 |
return bless { null_ranking => $child->value() }, $PROTO_ALTERNATIVE; |
| 413 |
} |
| 414 |
|
| 415 |
sub Marpa::R2::Internal::MetaAST_Nodes::null_ranking_constant::value { |
| 416 |
return $_[0]->[2]; |
| 417 |
} |
| 418 |
|
| 419 |
sub Marpa::R2::Internal::MetaAST_Nodes::before_or_after::value { |
| 420 |
return $_[0]->[2]; |
| 421 |
} |
| 422 |
|
| 423 |
sub Marpa::R2::Internal::MetaAST_Nodes::event_initializer::on_or_off |
| 424 |
{ |
| 425 |
# die Data::Dumper::Dumper(\@_); |
| 426 |
my ($values) = @_; |
| 427 |
my $is_activated = $values->[2]; |
| 428 |
return 1 if not defined $is_activated; |
| 429 |
return $is_activated->value(); |
| 430 |
} |
| 431 |
|
| 432 |
sub Marpa::R2::Internal::MetaAST_Nodes::on_or_off::value { |
| 433 |
return $_[0]->[2] eq 'off' ? 0 : 1; |
| 434 |
} |
| 435 |
|
| 436 |
sub Marpa::R2::Internal::MetaAST_Nodes::boolean::value { |
| 437 |
return $_[0]->[2]; |
| 438 |
} |
| 439 |
|
| 440 |
sub Marpa::R2::Internal::MetaAST_Nodes::signed_integer::value { |
| 441 |
return $_[0]->[2]; |
| 442 |
} |
| 443 |
|
| 444 |
sub Marpa::R2::Internal::MetaAST_Nodes::separator_specification::evaluate { |
| 445 |
my ( $values, $parse ) = @_; |
| 446 |
my $child = $values->[2]; |
| 447 |
return bless { separator => $child->name($parse) }, $PROTO_ALTERNATIVE; |
| 448 |
} |
| 449 |
|
| 450 |
sub Marpa::R2::Internal::MetaAST_Nodes::adverb_item::evaluate { |
| 451 |
my ( $values, $parse ) = @_; |
| 452 |
my $child = $values->[2]->evaluate($parse); |
| 453 |
return bless $child, $PROTO_ALTERNATIVE; |
| 454 |
} |
| 455 |
|
| 456 |
sub Marpa::R2::Internal::MetaAST_Nodes::default_rule::evaluate { |
| 457 |
my ( $values, $parse ) = @_; |
| 458 |
my ( $start, $length, undef, $op_declare, $raw_adverb_list ) = @{$values}; |
| 459 |
my $subgrammar = $op_declare->op() eq q{::=} ? 'G1' : $parse->{current_lexer}; |
| 460 |
my $adverb_list = $raw_adverb_list->evaluate($parse); |
| 461 |
|
| 462 |
# A default rule clears the previous default |
| 463 |
my %default_adverbs = (); |
| 464 |
$parse->{default_adverbs}->{$subgrammar} = \%default_adverbs; |
| 465 |
|
| 466 |
ADVERB: for my $key ( keys %{$adverb_list} ) { |
| 467 |
my $value = $adverb_list->{$key}; |
| 468 |
if ( $key eq 'action' and $subgrammar eq 'G1' ) { |
| 469 |
$default_adverbs{$key} = $adverb_list->{$key}; |
| 470 |
next ADVERB; |
| 471 |
} |
| 472 |
if ( $key eq 'bless' and $subgrammar eq 'G1' ) { |
| 473 |
$default_adverbs{$key} = $adverb_list->{$key}; |
| 474 |
next ADVERB; |
| 475 |
} |
| 476 |
die qq{Adverb "$key" not allowed in $subgrammar default rule\n}, |
| 477 |
' Rule was ', $parse->substring( $start, $length ), "\n"; |
| 478 |
} ## end ADVERB: for my $key ( keys %{$adverb_list} ) |
| 479 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 480 |
return undef; |
| 481 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::default_rule::evaluate |
| 482 |
|
| 483 |
sub Marpa::R2::Internal::MetaAST_Nodes::discard_default_statement::evaluate { |
| 484 |
my ( $data, $parse ) = @_; |
| 485 |
my ( $start, $length, $raw_adverb_list ) = @{$data}; |
| 486 |
local $Marpa::R2::Internal::SUBGRAMMAR = 'G1'; |
| 487 |
|
| 488 |
my $adverb_list = $raw_adverb_list->evaluate($parse); |
| 489 |
if ( exists $parse->{discard_default_adverbs} ) { |
| 490 |
my $problem_rule = $parse->substring( $start, $length ); |
| 491 |
Marpa::R2::exception( |
| 492 |
qq{More than one discard default statement is not allowed\n}, |
| 493 |
qq{ This was the rule that caused the problem:\n}, |
| 494 |
qq{ $problem_rule\n} |
| 495 |
); |
| 496 |
} ## end if ( exists $parse->{discard_default_adverbs} ) |
| 497 |
$parse->{discard_default_adverbs} = {}; |
| 498 |
ADVERB: for my $key ( keys %{$adverb_list} ) { |
| 499 |
my $value = $adverb_list->{$key}; |
| 500 |
if ( $key eq 'event' and defined $value ) { |
| 501 |
$parse->{discard_default_adverbs}->{$key} = $value; |
| 502 |
next ADVERB; |
| 503 |
} |
| 504 |
Marpa::R2::exception( |
| 505 |
qq{"$key" adverb not allowed as discard default"}); |
| 506 |
} ## end ADVERB: for my $key ( keys %{$adverb_list} ) |
| 507 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 508 |
return undef; |
| 509 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::discard_default_statement::evaluate |
| 510 |
|
| 511 |
sub Marpa::R2::Internal::MetaAST_Nodes::lexeme_default_statement::evaluate { |
| 512 |
my ( $data, $parse ) = @_; |
| 513 |
my ( $start, $length, $raw_adverb_list ) = @{$data}; |
| 514 |
local $Marpa::R2::Internal::SUBGRAMMAR = 'G1'; |
| 515 |
|
| 516 |
my $adverb_list = $raw_adverb_list->evaluate($parse); |
| 517 |
if ( exists $parse->{lexeme_default_adverbs} ) { |
| 518 |
my $problem_rule = $parse->substring( $start, $length ); |
| 519 |
Marpa::R2::exception( |
| 520 |
qq{More than one lexeme default statement is not allowed\n}, |
| 521 |
qq{ This was the rule that caused the problem:\n}, |
| 522 |
qq{ $problem_rule\n} |
| 523 |
); |
| 524 |
} ## end if ( exists $parse->{lexeme_default_adverbs} ) |
| 525 |
$parse->{lexeme_default_adverbs} = {}; |
| 526 |
ADVERB: for my $key ( keys %{$adverb_list} ) { |
| 527 |
my $value = $adverb_list->{$key}; |
| 528 |
if ( $key eq 'action' ) { |
| 529 |
$parse->{lexeme_default_adverbs}->{$key} = $value; |
| 530 |
next ADVERB; |
| 531 |
} |
| 532 |
if ( $key eq 'bless' ) { |
| 533 |
$parse->{lexeme_default_adverbs}->{$key} = $value; |
| 534 |
next ADVERB; |
| 535 |
} |
| 536 |
if ( $key eq 'latm' ) { |
| 537 |
$parse->{lexeme_default_adverbs}->{$key} = $value; |
| 538 |
next ADVERB; |
| 539 |
} |
| 540 |
Marpa::R2::exception( |
| 541 |
qq{"$key" adverb not allowed as lexeme default"}); |
| 542 |
} ## end ADVERB: for my $key ( keys %{$adverb_list} ) |
| 543 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 544 |
return undef; |
| 545 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::lexeme_default_statement::evaluate |
| 546 |
|
| 547 |
sub Marpa::R2::Internal::MetaAST_Nodes::inaccessible_statement::evaluate { |
| 548 |
my ( $data, $parse ) = @_; |
| 549 |
my ( $start, $length, $inaccessible_treatment ) = @{$data}; |
| 550 |
local $Marpa::R2::Internal::SUBGRAMMAR = 'G1'; |
| 551 |
|
| 552 |
if ( exists $parse->{defaults}->{if_inaccessible} ) { |
| 553 |
my $problem_rule = $parse->substring( $start, $length ); |
| 554 |
Marpa::R2::exception( |
| 555 |
qq{More than one inaccessible default statement is not allowed\n}, |
| 556 |
qq{ This was the rule that caused the problem:\n}, |
| 557 |
qq{ $problem_rule\n} |
| 558 |
); |
| 559 |
} |
| 560 |
$parse->{defaults}->{if_inaccessible} = $inaccessible_treatment->value(); |
| 561 |
return undef; |
| 562 |
} |
| 563 |
|
| 564 |
sub Marpa::R2::Internal::MetaAST_Nodes::inaccessible_treatment::value { |
| 565 |
return $_[0]->[2]; |
| 566 |
} |
| 567 |
|
| 568 |
sub Marpa::R2::Internal::MetaAST_Nodes::priority_rule::evaluate { |
| 569 |
my ( $values, $parse ) = @_; |
| 570 |
my ( $start, $length, $raw_lhs, $op_declare, $raw_priorities ) = |
| 571 |
@{$values}; |
| 572 |
|
| 573 |
my $current_lexer = $parse->{current_lexer}; |
| 574 |
my $subgrammar; |
| 575 |
if ( $op_declare->op() eq q{::=} ) { |
| 576 |
if ( $current_lexer ne 'L0' ) { |
| 577 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 578 |
die "G1 rules currently allowed only when L0 is current lexer\n", |
| 579 |
qq{ A prioritized rule was found when "$current_lexer" was the current lexer\n"}, |
| 580 |
" Location was line $line, column $column\n", |
| 581 |
' Rule was ', $parse->substring( $start, $length ), "\n"; |
| 582 |
} ## end if ( $current_lexer ne 'L0' ) |
| 583 |
$subgrammar = 'G1'; |
| 584 |
} ## end if ( $op_declare->op() eq q{::=} ) |
| 585 |
else { |
| 586 |
$subgrammar = $current_lexer; |
| 587 |
} |
| 588 |
|
| 589 |
my $lhs = $raw_lhs->name($parse); |
| 590 |
$parse->{'first_lhs'} //= $lhs if $subgrammar eq 'G1'; |
| 591 |
local $Marpa::R2::Internal::SUBGRAMMAR = $subgrammar; |
| 592 |
|
| 593 |
my ( undef, undef, @priorities ) = @{$raw_priorities}; |
| 594 |
my $priority_count = scalar @priorities; |
| 595 |
my @working_rules = (); |
| 596 |
|
| 597 |
$parse->{rules}->{$subgrammar} //= []; |
| 598 |
my $rules = $parse->{rules}->{$subgrammar}; |
| 599 |
|
| 600 |
my $default_adverbs = $parse->{default_adverbs}->{$subgrammar}; |
| 601 |
|
| 602 |
if ( $priority_count <= 1 ) { |
| 603 |
## If there is only one priority |
| 604 |
my ( undef, undef, @alternatives ) = @{ $priorities[0] }; |
| 605 |
for my $alternative (@alternatives) { |
| 606 |
my ($alternative_start, $alternative_end, |
| 607 |
$raw_rhs, $raw_adverb_list |
| 608 |
) = @{$alternative}; |
| 609 |
my ( $proto_rule, $adverb_list ); |
| 610 |
my $eval_ok = eval { |
| 611 |
$proto_rule = $raw_rhs->evaluate($parse); |
| 612 |
$adverb_list = $raw_adverb_list->evaluate($parse); |
| 613 |
1; |
| 614 |
}; |
| 615 |
if ( not $eval_ok ) { |
| 616 |
my $eval_error = $EVAL_ERROR; |
| 617 |
chomp $eval_error; |
| 618 |
Marpa::R2::exception( |
| 619 |
qq{$eval_error\n}, |
| 620 |
qq{ The problem was in this RHS alternative:\n}, |
| 621 |
q{ }, |
| 622 |
$parse->substring( $alternative_start, $alternative_end ), |
| 623 |
"\n" |
| 624 |
); |
| 625 |
} ## end if ( not $eval_ok ) |
| 626 |
my @rhs_names = @{ $proto_rule->{rhs} }; |
| 627 |
my @mask = @{ $proto_rule->{mask} }; |
| 628 |
if ( ( substr $subgrammar, 0, 1 ) eq 'L' |
| 629 |
and grep { !$_ } @mask ) |
| 630 |
{ |
| 631 |
Marpa::R2::exception( |
| 632 |
qq{hidden symbols are not allowed in lexical rules (rule's LHS was "$lhs")} |
| 633 |
); |
| 634 |
} |
| 635 |
my %hash_rule = |
| 636 |
( lhs => $lhs, rhs => \@rhs_names, mask => \@mask ); |
| 637 |
|
| 638 |
my $action; |
| 639 |
my $blessing; |
| 640 |
my $naming; |
| 641 |
my $null_ranking; |
| 642 |
my $rank; |
| 643 |
ADVERB: for my $key ( keys %{$adverb_list} ) { |
| 644 |
my $value = $adverb_list->{$key}; |
| 645 |
if ( $key eq 'action' ) { |
| 646 |
$action = $adverb_list->{$key}; |
| 647 |
next ADVERB; |
| 648 |
} |
| 649 |
if ( $key eq 'assoc' ) { |
| 650 |
|
| 651 |
# OK, but ignored |
| 652 |
next ADVERB; |
| 653 |
} |
| 654 |
if ( $key eq 'bless' ) { |
| 655 |
$blessing = $adverb_list->{$key}; |
| 656 |
next ADVERB; |
| 657 |
} |
| 658 |
if ( $key eq 'name' ) { |
| 659 |
$naming = $adverb_list->{$key}; |
| 660 |
next ADVERB; |
| 661 |
} |
| 662 |
if ( $key eq 'null_ranking' ) { |
| 663 |
$null_ranking = $adverb_list->{$key}; |
| 664 |
next ADVERB; |
| 665 |
} |
| 666 |
if ( $key eq 'rank' ) { |
| 667 |
$rank = $adverb_list->{$key}; |
| 668 |
next ADVERB; |
| 669 |
} |
| 670 |
my ( $line, $column ) = |
| 671 |
$parse->{meta_recce}->line_column($start); |
| 672 |
die qq{Adverb "$key" not allowed in an prioritized rule\n}, |
| 673 |
' Rule was ', $parse->substring( $start, $length ), "\n"; |
| 674 |
} ## end ADVERB: for my $key ( keys %{$adverb_list} ) |
| 675 |
|
| 676 |
$action //= $default_adverbs->{action}; |
| 677 |
if ( defined $action ) { |
| 678 |
Marpa::R2::exception( |
| 679 |
qq{actions not allowed in lexical rules (rule's LHS was "$lhs")} |
| 680 |
) if ( substr $subgrammar, 0, 1 ) eq 'L'; |
| 681 |
$hash_rule{action} = $action; |
| 682 |
} ## end if ( defined $action ) |
| 683 |
|
| 684 |
$rank //= $default_adverbs->{rank}; |
| 685 |
if ( defined $rank ) { |
| 686 |
Marpa::R2::exception( |
| 687 |
qq{ranks not allowed in lexical rules (rule's LHS was "$lhs")} |
| 688 |
) if ( substr $subgrammar, 0, 1 ) eq 'L'; |
| 689 |
$hash_rule{rank} = $rank; |
| 690 |
} ## end if ( defined $rank ) |
| 691 |
|
| 692 |
$null_ranking //= $default_adverbs->{null_ranking}; |
| 693 |
if ( defined $null_ranking ) { |
| 694 |
Marpa::R2::exception( |
| 695 |
qq{null-ranking allowed in lexical rules (rule's LHS was "$lhs")} |
| 696 |
) if ( substr $subgrammar, 0, 1 ) eq 'L'; |
| 697 |
$hash_rule{null_ranking} = $null_ranking; |
| 698 |
} ## end if ( defined $rank ) |
| 699 |
|
| 700 |
$blessing //= $default_adverbs->{bless}; |
| 701 |
if (defined $blessing |
| 702 |
and |
| 703 |
( substr $subgrammar, 0, 1 ) eq 'L' |
| 704 |
) |
| 705 |
{ |
| 706 |
Marpa::R2::exception( |
| 707 |
'bless option not allowed in lexical rules (rules LHS was "', |
| 708 |
$lhs, '")' |
| 709 |
); |
| 710 |
} |
| 711 |
|
| 712 |
$parse->bless_hash_rule( \%hash_rule, $blessing, $naming, $lhs ); |
| 713 |
|
| 714 |
push @{$rules}, \%hash_rule; |
| 715 |
} ## end for my $alternative (@alternatives) |
| 716 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 717 |
return undef; |
| 718 |
} ## end if ( $priority_count <= 1 ) |
| 719 |
|
| 720 |
for my $priority_ix ( 0 .. $priority_count - 1 ) { |
| 721 |
my $priority = $priority_count - ( $priority_ix + 1 ); |
| 722 |
my ( undef, undef, @alternatives ) = @{ $priorities[$priority_ix] }; |
| 723 |
for my $alternative (@alternatives) { |
| 724 |
my ($alternative_start, $alternative_end, |
| 725 |
$raw_rhs, $raw_adverb_list |
| 726 |
) = @{$alternative}; |
| 727 |
my ( $adverb_list, $rhs ); |
| 728 |
my $eval_ok = eval { |
| 729 |
$adverb_list = $raw_adverb_list->evaluate($parse); |
| 730 |
$rhs = $raw_rhs->evaluate($parse); |
| 731 |
1; |
| 732 |
}; |
| 733 |
if ( not $eval_ok ) { |
| 734 |
my $eval_error = $EVAL_ERROR; |
| 735 |
chomp $eval_error; |
| 736 |
Marpa::R2::exception( |
| 737 |
qq{$eval_error\n}, |
| 738 |
qq{ The problem was in this RHS alternative:\n}, |
| 739 |
q{ }, |
| 740 |
$parse->substring( $alternative_start, $alternative_end ), |
| 741 |
"\n" |
| 742 |
); |
| 743 |
} ## end if ( not $eval_ok ) |
| 744 |
push @working_rules, [ $priority, $rhs, $adverb_list ]; |
| 745 |
} ## end for my $alternative (@alternatives) |
| 746 |
} ## end for my $priority_ix ( 0 .. $priority_count - 1 ) |
| 747 |
|
| 748 |
# Default mask (all ones) is OK for this rule |
| 749 |
my @arg0_action = (); |
| 750 |
@arg0_action = ( action => '::first' ) if $subgrammar eq 'G1'; |
| 751 |
push @{$rules}, |
| 752 |
{ |
| 753 |
lhs => $lhs, |
| 754 |
rhs => [ $parse->prioritized_symbol( $lhs, 0 ) ], |
| 755 |
@arg0_action, |
| 756 |
description => qq{Internal rule top priority rule for <$lhs>}, |
| 757 |
}, |
| 758 |
( |
| 759 |
map { |
| 760 |
; |
| 761 |
{ lhs => $parse->prioritized_symbol( $lhs, $_ - 1 ), |
| 762 |
rhs => [ $parse->prioritized_symbol( $lhs, $_ ) ], |
| 763 |
description => ( |
| 764 |
qq{Internal rule for symbol <$lhs> priority transition from } |
| 765 |
. ( $_ - 1 ) |
| 766 |
. qq{ to $_} |
| 767 |
), |
| 768 |
@arg0_action |
| 769 |
} |
| 770 |
} 1 .. $priority_count - 1 |
| 771 |
); |
| 772 |
RULE: for my $working_rule (@working_rules) { |
| 773 |
my ( $priority, $rhs, $adverb_list ) = @{$working_rule}; |
| 774 |
my @new_rhs = @{ $rhs->{rhs} }; |
| 775 |
my @arity = grep { $new_rhs[$_] eq $lhs } 0 .. $#new_rhs; |
| 776 |
my $rhs_length = scalar @new_rhs; |
| 777 |
|
| 778 |
my $current_exp = $parse->prioritized_symbol( $lhs, $priority ); |
| 779 |
my @mask = @{ $rhs->{mask} }; |
| 780 |
if ( ( substr $subgrammar, 0, 1 ) eq 'L' and grep { !$_ } @mask ) |
| 781 |
{ |
| 782 |
Marpa::R2::exception( |
| 783 |
'hidden symbols are not allowed in lexical rules (rules LHS was "', |
| 784 |
$lhs, '")' |
| 785 |
); |
| 786 |
} |
| 787 |
my %new_xs_rule = ( lhs => $current_exp ); |
| 788 |
$new_xs_rule{mask} = \@mask; |
| 789 |
|
| 790 |
my $action; |
| 791 |
my $assoc; |
| 792 |
my $blessing; |
| 793 |
my $naming; |
| 794 |
my $rank; |
| 795 |
my $null_ranking; |
| 796 |
ADVERB: for my $key ( keys %{$adverb_list} ) { |
| 797 |
my $value = $adverb_list->{$key}; |
| 798 |
if ( $key eq 'action' ) { |
| 799 |
$action = $adverb_list->{$key}; |
| 800 |
next ADVERB; |
| 801 |
} |
| 802 |
if ( $key eq 'assoc' ) { |
| 803 |
$assoc = $adverb_list->{$key}; |
| 804 |
next ADVERB; |
| 805 |
} |
| 806 |
if ( $key eq 'bless' ) { |
| 807 |
$blessing = $adverb_list->{$key}; |
| 808 |
next ADVERB; |
| 809 |
} |
| 810 |
if ( $key eq 'name' ) { |
| 811 |
$naming = $adverb_list->{$key}; |
| 812 |
next ADVERB; |
| 813 |
} |
| 814 |
if ( $key eq 'null_ranking' ) { |
| 815 |
$null_ranking = $adverb_list->{$key}; |
| 816 |
next ADVERB; |
| 817 |
} |
| 818 |
if ( $key eq 'rank' ) { |
| 819 |
$rank = $adverb_list->{$key}; |
| 820 |
next ADVERB; |
| 821 |
} |
| 822 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 823 |
die qq{Adverb "$key" not allowed in a prioritized rule\n}, |
| 824 |
' Rule was ', $parse->substring( $start, $length ), "\n"; |
| 825 |
} ## end ADVERB: for my $key ( keys %{$adverb_list} ) |
| 826 |
|
| 827 |
$assoc //= 'L'; |
| 828 |
|
| 829 |
$action //= $default_adverbs->{action}; |
| 830 |
if ( defined $action ) { |
| 831 |
Marpa::R2::exception( |
| 832 |
qq{actions not allowed in lexical rules (rule's LHS was "$lhs")} |
| 833 |
) if ( substr $subgrammar, 0, 1 ) eq 'L'; |
| 834 |
$new_xs_rule{action} = $action; |
| 835 |
} ## end if ( defined $action ) |
| 836 |
|
| 837 |
$null_ranking //= $default_adverbs->{null_ranking}; |
| 838 |
if ( defined $null_ranking ) { |
| 839 |
Marpa::R2::exception( |
| 840 |
qq{null-ranking not allowed in lexical rules (rule's LHS was "$lhs")} |
| 841 |
) if ( substr $subgrammar, 0, 1 ) eq 'L'; |
| 842 |
$new_xs_rule{null_ranking} = $null_ranking; |
| 843 |
} ## end if ( defined $rank ) |
| 844 |
|
| 845 |
$rank //= $default_adverbs->{rank}; |
| 846 |
if ( defined $rank ) { |
| 847 |
Marpa::R2::exception( |
| 848 |
qq{ranks not allowed in lexical rules (rule's LHS was "$lhs")} |
| 849 |
) if ( substr $subgrammar, 0, 1 ) eq 'L'; |
| 850 |
$new_xs_rule{rank} = $rank; |
| 851 |
} ## end if ( defined $rank ) |
| 852 |
|
| 853 |
$blessing //= $default_adverbs->{bless}; |
| 854 |
if ( defined $blessing |
| 855 |
and ( substr $subgrammar, 0, 1 ) eq 'L' ) |
| 856 |
{ |
| 857 |
Marpa::R2::exception( |
| 858 |
'bless option not allowed in lexical rules (rules LHS was "', |
| 859 |
$lhs, '")' |
| 860 |
); |
| 861 |
} |
| 862 |
|
| 863 |
$parse->bless_hash_rule( \%new_xs_rule, $blessing, $naming, $lhs ); |
| 864 |
|
| 865 |
my $next_priority = $priority + 1; |
| 866 |
|
| 867 |
# This is probably a mis-feature. It probably should be |
| 868 |
# $next_priority = $priority if $next_priority >= $priority_count; |
| 869 |
# However, I probably will not change this, because some apps |
| 870 |
# may be relying on this behavior. |
| 871 |
$next_priority = 0 if $next_priority >= $priority_count; |
| 872 |
|
| 873 |
my $next_exp = $parse->prioritized_symbol( $lhs, $next_priority); |
| 874 |
|
| 875 |
if ( not scalar @arity ) { |
| 876 |
$new_xs_rule{rhs} = \@new_rhs; |
| 877 |
push @{$rules}, \%new_xs_rule; |
| 878 |
next RULE; |
| 879 |
} |
| 880 |
|
| 881 |
if ( scalar @arity == 1 ) { |
| 882 |
die 'Unnecessary unit rule in priority rule' if $rhs_length == 1; |
| 883 |
$new_rhs[ $arity[0] ] = $current_exp; |
| 884 |
} |
| 885 |
DO_ASSOCIATION: { |
| 886 |
if ( $assoc eq 'L' ) { |
| 887 |
$new_rhs[ $arity[0] ] = $current_exp; |
| 888 |
for my $rhs_ix ( @arity[ 1 .. $#arity ] ) { |
| 889 |
$new_rhs[$rhs_ix] = $next_exp; |
| 890 |
} |
| 891 |
last DO_ASSOCIATION; |
| 892 |
} ## end if ( $assoc eq 'L' ) |
| 893 |
if ( $assoc eq 'R' ) { |
| 894 |
$new_rhs[ $arity[-1] ] = $current_exp; |
| 895 |
for my $rhs_ix ( @arity[ 0 .. $#arity - 1 ] ) { |
| 896 |
$new_rhs[$rhs_ix] = $next_exp; |
| 897 |
} |
| 898 |
last DO_ASSOCIATION; |
| 899 |
} ## end if ( $assoc eq 'R' ) |
| 900 |
if ( $assoc eq 'G' ) { |
| 901 |
for my $rhs_ix ( @arity[ 0 .. $#arity ] ) { |
| 902 |
$new_rhs[$rhs_ix] = $parse->prioritized_symbol( $lhs, 0 ); |
| 903 |
} |
| 904 |
last DO_ASSOCIATION; |
| 905 |
} ## end if ( $assoc eq 'G' ) |
| 906 |
die qq{Unknown association type: "$assoc"}; |
| 907 |
} ## end DO_ASSOCIATION: |
| 908 |
|
| 909 |
$new_xs_rule{rhs} = \@new_rhs; |
| 910 |
push @{$rules}, \%new_xs_rule; |
| 911 |
} ## end RULE: for my $working_rule (@working_rules) |
| 912 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 913 |
return undef; |
| 914 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::priority_rule::evaluate |
| 915 |
|
| 916 |
sub Marpa::R2::Internal::MetaAST_Nodes::empty_rule::evaluate { |
| 917 |
my ( $values, $parse ) = @_; |
| 918 |
my ( $start, $length, $raw_lhs, $op_declare, $raw_adverb_list ) = |
| 919 |
@{$values}; |
| 920 |
|
| 921 |
my $current_lexer = $parse->{current_lexer}; |
| 922 |
my $subgrammar; |
| 923 |
if ( $op_declare->op() eq q{::=} ) { |
| 924 |
if ( $current_lexer ne 'L0' ) { |
| 925 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 926 |
die "G1 rules currently allowed only when L0 is current lexer\n", |
| 927 |
qq{ An empty rule was found when "$current_lexer" was the current lexer\n"}, |
| 928 |
" Location was line $line, column $column\n", |
| 929 |
' Rule was ', $parse->substring( $start, $length ), "\n"; |
| 930 |
} ## end if ( $current_lexer ne 'L0' ) |
| 931 |
$subgrammar = 'G1'; |
| 932 |
} ## end if ( $op_declare->op() eq q{::=} ) |
| 933 |
else { |
| 934 |
$subgrammar = $current_lexer; |
| 935 |
} |
| 936 |
|
| 937 |
my $lhs = $raw_lhs->name($parse); |
| 938 |
$parse->{'first_lhs'} //= $lhs if $subgrammar eq 'G1'; |
| 939 |
local $Marpa::R2::Internal::SUBGRAMMAR = $subgrammar; |
| 940 |
|
| 941 |
my %rule = ( lhs => $lhs, |
| 942 |
description => qq{Empty rule for <$lhs>}, |
| 943 |
rhs => [] ); |
| 944 |
my $adverb_list = $raw_adverb_list->evaluate($parse); |
| 945 |
|
| 946 |
my $default_adverbs = $parse->{default_adverbs}->{$subgrammar}; |
| 947 |
|
| 948 |
my $action; |
| 949 |
my $blessing; |
| 950 |
my $naming; |
| 951 |
my $rank; |
| 952 |
my $null_ranking; |
| 953 |
ADVERB: for my $key ( keys %{$adverb_list} ) { |
| 954 |
my $value = $adverb_list->{$key}; |
| 955 |
if ( $key eq 'action' ) { |
| 956 |
$action = $adverb_list->{$key}; |
| 957 |
next ADVERB; |
| 958 |
} |
| 959 |
if ( $key eq 'bless' ) { |
| 960 |
$blessing = $adverb_list->{$key}; |
| 961 |
next ADVERB; |
| 962 |
} |
| 963 |
if ( $key eq 'name' ) { |
| 964 |
$naming = $adverb_list->{$key}; |
| 965 |
next ADVERB; |
| 966 |
} |
| 967 |
if ( $key eq 'null_ranking' ) { |
| 968 |
$null_ranking = $adverb_list->{$key}; |
| 969 |
next ADVERB; |
| 970 |
} |
| 971 |
if ( $key eq 'rank' ) { |
| 972 |
$rank = $adverb_list->{$key}; |
| 973 |
next ADVERB; |
| 974 |
} |
| 975 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 976 |
die qq{Adverb "$key" not allowed in an empty rule\n}, |
| 977 |
' Rule was ', $parse->substring( $start, $length ), "\n"; |
| 978 |
} ## end ADVERB: for my $key ( keys %{$adverb_list} ) |
| 979 |
|
| 980 |
$action //= $default_adverbs->{action}; |
| 981 |
if ( defined $action ) { |
| 982 |
Marpa::R2::exception( |
| 983 |
qq{actions not allowed in lexical rules (rule's LHS was "$lhs")} |
| 984 |
) if ( substr $subgrammar, 0, 1 ) eq 'L'; |
| 985 |
$rule{action} = $action; |
| 986 |
} ## end if ( defined $action ) |
| 987 |
|
| 988 |
$null_ranking //= $default_adverbs->{null_ranking}; |
| 989 |
if ( defined $null_ranking ) { |
| 990 |
Marpa::R2::exception( |
| 991 |
qq{null-ranking not allowed in lexical rules (rule's LHS was "$lhs")} |
| 992 |
) if ( substr $subgrammar, 0, 1 ) eq 'L'; |
| 993 |
$rule{null_ranking} = $null_ranking; |
| 994 |
} ## end if ( defined $null_ranking ) |
| 995 |
|
| 996 |
$rank //= $default_adverbs->{rank}; |
| 997 |
if ( defined $rank ) { |
| 998 |
Marpa::R2::exception( |
| 999 |
qq{ranks not allowed in lexical rules (rule's LHS was "$lhs")} |
| 1000 |
) if ( substr $subgrammar, 0, 1 ) eq 'L'; |
| 1001 |
$rule{rank} = $rank; |
| 1002 |
} ## end if ( defined $rank ) |
| 1003 |
|
| 1004 |
$blessing //= $default_adverbs->{bless}; |
| 1005 |
if ( defined $blessing |
| 1006 |
and ( substr $subgrammar, 0, 1 ) eq 'L' ) |
| 1007 |
{ |
| 1008 |
Marpa::R2::exception( |
| 1009 |
qq{bless option not allowed in lexical rules (rule's LHS was "$lhs")} |
| 1010 |
); |
| 1011 |
} |
| 1012 |
$parse->bless_hash_rule( \%rule, $blessing, $naming, $lhs ); |
| 1013 |
|
| 1014 |
# mask not needed |
| 1015 |
push @{ $parse->{rules}->{$subgrammar} }, \%rule; |
| 1016 |
|
| 1017 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 1018 |
return undef; |
| 1019 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::empty_rule::evaluate |
| 1020 |
|
| 1021 |
sub Marpa::R2::Internal::MetaAST_Nodes::lexeme_rule::evaluate { |
| 1022 |
my ( $values, $parse ) = @_; |
| 1023 |
my ( $start, $length, $symbol, $unevaluated_adverb_list ) = @{$values}; |
| 1024 |
|
| 1025 |
my $symbol_name = $symbol->name(); |
| 1026 |
my $declarations = $parse->{lexeme_declarations}->{$symbol_name}; |
| 1027 |
if ( defined $declarations ) { |
| 1028 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 1029 |
die "Duplicate lexeme rule for <$symbol_name>\n", |
| 1030 |
" Only one lexeme rule is allowed for each symbol\n", |
| 1031 |
" Location was line $line, column $column\n", |
| 1032 |
' Rule was ', $parse->substring( $start, $length ), "\n"; |
| 1033 |
} ## end if ( defined $declarations ) |
| 1034 |
|
| 1035 |
my $adverb_list = $unevaluated_adverb_list->evaluate(); |
| 1036 |
my %declarations; |
| 1037 |
ADVERB: for my $key ( keys %{$adverb_list} ) { |
| 1038 |
my $raw_value = $adverb_list->{$key}; |
| 1039 |
if ( $key eq 'priority' ) { |
| 1040 |
$declarations{$key} = $raw_value + 0; |
| 1041 |
next ADVERB; |
| 1042 |
} |
| 1043 |
if ( $key eq 'pause' ) { |
| 1044 |
if ( $raw_value eq 'before' ) { |
| 1045 |
$declarations{$key} = -1; |
| 1046 |
next ADVERB; |
| 1047 |
} |
| 1048 |
if ( $raw_value eq 'after' ) { |
| 1049 |
$declarations{$key} = 1; |
| 1050 |
next ADVERB; |
| 1051 |
} |
| 1052 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 1053 |
die qq{Bad value for "pause" adverb: "$raw_value"}, |
| 1054 |
" Location was line $line, column $column\n", |
| 1055 |
' Rule was ', $parse->substring( $start, $length ), "\n"; |
| 1056 |
} ## end if ( $key eq 'pause' ) |
| 1057 |
if ( $key eq 'event' ) { |
| 1058 |
$declarations{$key} = $raw_value; |
| 1059 |
next ADVERB; |
| 1060 |
} |
| 1061 |
if ( $key eq 'latm' ) { |
| 1062 |
$declarations{$key} = $raw_value; |
| 1063 |
next ADVERB; |
| 1064 |
} |
| 1065 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 1066 |
die qq{"$key" adverb not allowed in lexeme rule"\n}, |
| 1067 |
" Location was line $line, column $column\n", |
| 1068 |
' Rule was ', $parse->substring( $start, $length ), "\n"; |
| 1069 |
} ## end ADVERB: for my $key ( keys %{$adverb_list} ) |
| 1070 |
if ( exists $declarations{'event'} and not exists $declarations{'pause'} ) |
| 1071 |
{ |
| 1072 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 1073 |
die |
| 1074 |
qq{"event" adverb not allowed without "pause" adverb in lexeme rule"\n}, |
| 1075 |
" Location was line $line, column $column\n", |
| 1076 |
' Rule was ', $parse->substring( $start, $length ), "\n"; |
| 1077 |
} ## end if ( exists $declarations{'event'} and not exists $declarations...) |
| 1078 |
$parse->{lexeme_declarations}->{$symbol_name} = \%declarations; |
| 1079 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 1080 |
return undef; |
| 1081 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::lexeme_rule::evaluate |
| 1082 |
|
| 1083 |
sub Marpa::R2::Internal::MetaAST_Nodes::statements::evaluate { |
| 1084 |
my ( $data, $parse ) = @_; |
| 1085 |
my ( undef, undef, @statement_list ) = @{$data}; |
| 1086 |
map { $_->evaluate($parse) } @statement_list; |
| 1087 |
return undef; |
| 1088 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::statements::evaluate |
| 1089 |
|
| 1090 |
sub Marpa::R2::Internal::MetaAST_Nodes::statement::evaluate { |
| 1091 |
my ( $data, $parse ) = @_; |
| 1092 |
my ( undef, undef, $child ) = @{$data}; |
| 1093 |
$child->evaluate($parse); |
| 1094 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 1095 |
return undef; |
| 1096 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::statement::evaluate |
| 1097 |
|
| 1098 |
sub Marpa::R2::Internal::MetaAST_Nodes::null_statement::evaluate { |
| 1099 |
return undef; |
| 1100 |
} |
| 1101 |
|
| 1102 |
sub Marpa::R2::Internal::MetaAST_Nodes::statement_group::evaluate { |
| 1103 |
my ( $data, $parse ) = @_; |
| 1104 |
my ( undef, undef, $statements ) = @{$data}; |
| 1105 |
$statements->evaluate($parse); |
| 1106 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 1107 |
return undef; |
| 1108 |
} |
| 1109 |
|
| 1110 |
sub Marpa::R2::Internal::MetaAST::start_rule_create { |
| 1111 |
my ( $parse, $symbol_name ) = @_; |
| 1112 |
my $start_lhs = '[:start]'; |
| 1113 |
$parse->{'default_g1_start_action'} = |
| 1114 |
$parse->{'default_adverbs'}->{'G1'}->{'action'}; |
| 1115 |
$parse->{'symbols'}->{'G1'}->{$start_lhs} = { |
| 1116 |
display_form => ':start', |
| 1117 |
description => 'Internal G1 start symbol' |
| 1118 |
}; |
| 1119 |
push @{ $parse->{rules}->{G1} }, |
| 1120 |
{ |
| 1121 |
lhs => $start_lhs, |
| 1122 |
rhs => [$symbol_name], |
| 1123 |
action => '::first' |
| 1124 |
}; |
| 1125 |
} ## end sub Marpa::R2::Internal::MetaAST::start_rule_create |
| 1126 |
|
| 1127 |
sub Marpa::R2::Internal::MetaAST_Nodes::start_rule::evaluate { |
| 1128 |
my ( $values, $parse ) = @_; |
| 1129 |
my ( $start, $length, $symbol ) = @{$values}; |
| 1130 |
if ( defined $parse->{'start_lhs'} ) { |
| 1131 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 1132 |
die qq{There are two start rules\n}, |
| 1133 |
qq{ That is not allowed\n}, |
| 1134 |
' The second start rule is ', |
| 1135 |
$parse->substring( $start, $length ), |
| 1136 |
"\n", |
| 1137 |
" Problem occurred at line $line, column $column\n"; |
| 1138 |
} ## end if ( defined $parse->{'start_lhs'} ) |
| 1139 |
$parse->{'start_lhs'} = $symbol->name($parse); |
| 1140 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 1141 |
return undef; |
| 1142 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::start_rule::evaluate |
| 1143 |
|
| 1144 |
sub Marpa::R2::Internal::MetaAST_Nodes::discard_rule::evaluate { |
| 1145 |
my ( $values, $parse ) = @_; |
| 1146 |
my ( $start, $length, $symbol, $raw_adverb_list ) = @{$values}; |
| 1147 |
|
| 1148 |
my $lexer_name = $parse->{current_lexer}; |
| 1149 |
local $Marpa::R2::Internal::SUBGRAMMAR = $lexer_name; |
| 1150 |
my $discard_lhs = '[:discard]'; |
| 1151 |
$parse->symbol_names_set( |
| 1152 |
$discard_lhs, |
| 1153 |
'L', |
| 1154 |
{ display_form => ':discard', |
| 1155 |
description => qq{Internal LHS for lexer "$lexer_name" discard} |
| 1156 |
} |
| 1157 |
); |
| 1158 |
my $rhs = $symbol->names($parse); |
| 1159 |
my $rhs_as_event = $symbol->event_name($parse); |
| 1160 |
my $adverb_list = $raw_adverb_list->evaluate($parse); |
| 1161 |
my $event; |
| 1162 |
ADVERB: for my $key ( keys %{$adverb_list} ) { |
| 1163 |
my $value = $adverb_list->{$key}; |
| 1164 |
if ( $key eq 'event' ) { |
| 1165 |
$event = $value; |
| 1166 |
next ADVERB; |
| 1167 |
} |
| 1168 |
Marpa::R2::exception( |
| 1169 |
qq{"$key" adverb not allowed as discard default"}); |
| 1170 |
} ## end ADVERB: for my $key ( keys %{$adverb_list} ) |
| 1171 |
my %rule_hash = ( |
| 1172 |
description => ( |
| 1173 |
"Discard rule for " . join q{ }, |
| 1174 |
map { '<' . $_ . '>' } @{$rhs} |
| 1175 |
), |
| 1176 |
lhs => $discard_lhs, |
| 1177 |
rhs => $rhs, |
| 1178 |
symbol_as_event => $rhs_as_event |
| 1179 |
); |
| 1180 |
$rule_hash{event} = $event if defined $event; |
| 1181 |
push @{ $parse->{rules}->{$lexer_name} }, \%rule_hash; |
| 1182 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 1183 |
return undef; |
| 1184 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::discard_rule::evaluate |
| 1185 |
|
| 1186 |
sub Marpa::R2::Internal::MetaAST_Nodes::quantified_rule::evaluate { |
| 1187 |
my ( $values, $parse ) = @_; |
| 1188 |
my ( $start, $length, $lhs, $op_declare, $rhs, $quantifier, |
| 1189 |
$proto_adverb_list ) |
| 1190 |
= @{$values}; |
| 1191 |
|
| 1192 |
my $subgrammar; |
| 1193 |
my $current_lexer = $parse->{current_lexer}; |
| 1194 |
if ( $op_declare->op() eq q{::=} ) { |
| 1195 |
if ( $current_lexer ne 'L0' ) { |
| 1196 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 1197 |
die "G1 rules currently allowed only when L0 is current lexer\n", |
| 1198 |
qq{ A quantified rule was found when "$current_lexer" was the current lexer\n"}, |
| 1199 |
" Location was line $line, column $column\n", |
| 1200 |
' Rule was ', $parse->substring( $start, $length ), "\n"; |
| 1201 |
} ## end if ( $current_lexer ne 'L0' ) |
| 1202 |
$subgrammar = 'G1'; |
| 1203 |
} ## end if ( $op_declare->op() eq q{::=} ) |
| 1204 |
else { |
| 1205 |
$subgrammar = $current_lexer; |
| 1206 |
} |
| 1207 |
|
| 1208 |
my $lhs_name = $lhs->name($parse); |
| 1209 |
$parse->{'first_lhs'} //= $lhs_name if $subgrammar eq 'G1'; |
| 1210 |
local $Marpa::R2::Internal::SUBGRAMMAR = $subgrammar; |
| 1211 |
|
| 1212 |
my $adverb_list = $proto_adverb_list->evaluate($parse); |
| 1213 |
my $default_adverbs = $parse->{default_adverbs}->{$subgrammar}; |
| 1214 |
|
| 1215 |
# Some properties of the sequence rule will not be altered |
| 1216 |
# no matter how complicated this gets |
| 1217 |
my %sequence_rule = ( |
| 1218 |
rhs => [ $rhs->name($parse) ], |
| 1219 |
min => ( $quantifier->evaluate($parse) eq q{+} ? 1 : 0 ) |
| 1220 |
); |
| 1221 |
|
| 1222 |
my @rules = ( \%sequence_rule ); |
| 1223 |
|
| 1224 |
my $action; |
| 1225 |
my $blessing; |
| 1226 |
my $naming; |
| 1227 |
my $separator; |
| 1228 |
my $proper; |
| 1229 |
my $rank; |
| 1230 |
my $null_ranking; |
| 1231 |
ADVERB: for my $key ( keys %{$adverb_list} ) { |
| 1232 |
my $value = $adverb_list->{$key}; |
| 1233 |
if ( $key eq 'action' ) { |
| 1234 |
$action = $adverb_list->{$key}; |
| 1235 |
next ADVERB; |
| 1236 |
} |
| 1237 |
if ( $key eq 'bless' ) { |
| 1238 |
$blessing = $adverb_list->{$key}; |
| 1239 |
next ADVERB; |
| 1240 |
} |
| 1241 |
if ( $key eq 'name' ) { |
| 1242 |
$naming = $adverb_list->{$key}; |
| 1243 |
next ADVERB; |
| 1244 |
} |
| 1245 |
if ( $key eq 'proper' ) { |
| 1246 |
$proper = $adverb_list->{$key}; |
| 1247 |
next ADVERB; |
| 1248 |
} |
| 1249 |
if ( $key eq 'rank' ) { |
| 1250 |
$rank = $adverb_list->{$key}; |
| 1251 |
next ADVERB; |
| 1252 |
} |
| 1253 |
if ( $key eq 'null_ranking' ) { |
| 1254 |
$null_ranking = $adverb_list->{$key}; |
| 1255 |
next ADVERB; |
| 1256 |
} |
| 1257 |
if ( $key eq 'separator' ) { |
| 1258 |
$separator = $adverb_list->{$key}; |
| 1259 |
next ADVERB; |
| 1260 |
} |
| 1261 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 1262 |
die qq{Adverb "$key" not allowed in quantified rule\n}, |
| 1263 |
' Rule was ', $parse->substring( $start, $length ), "\n"; |
| 1264 |
} ## end ADVERB: for my $key ( keys %{$adverb_list} ) |
| 1265 |
|
| 1266 |
# mask not needed |
| 1267 |
$sequence_rule{lhs} = $lhs_name; |
| 1268 |
|
| 1269 |
$sequence_rule{separator} = $separator |
| 1270 |
if defined $separator; |
| 1271 |
$sequence_rule{proper} = $proper if defined $proper; |
| 1272 |
|
| 1273 |
$action //= $default_adverbs->{action}; |
| 1274 |
if ( defined $action ) { |
| 1275 |
Marpa::R2::exception( |
| 1276 |
qq{actions not allowed in lexical rules (rule's LHS was "$lhs")} |
| 1277 |
) if ( substr $subgrammar, 0, 1 ) eq 'L'; |
| 1278 |
$sequence_rule{action} = $action; |
| 1279 |
} ## end if ( defined $action ) |
| 1280 |
|
| 1281 |
$null_ranking //= $default_adverbs->{null_ranking}; |
| 1282 |
if ( defined $null_ranking ) { |
| 1283 |
Marpa::R2::exception( |
| 1284 |
qq{null-ranking not allowed in lexical rules (rule's LHS was "$lhs")} |
| 1285 |
) if ( substr $subgrammar, 0, 1 ) eq 'L'; |
| 1286 |
$sequence_rule{null_ranking} = $null_ranking; |
| 1287 |
} ## end if ( defined $null_ranking ) |
| 1288 |
|
| 1289 |
$rank //= $default_adverbs->{rank}; |
| 1290 |
if ( defined $rank ) { |
| 1291 |
Marpa::R2::exception( |
| 1292 |
qq{ranks not allowed in lexical rules (rule's LHS was "$lhs")} |
| 1293 |
) if ( substr $subgrammar, 0, 1 ) eq 'L'; |
| 1294 |
$sequence_rule{rank} = $rank; |
| 1295 |
} ## end if ( defined $rank ) |
| 1296 |
|
| 1297 |
$blessing //= $default_adverbs->{bless}; |
| 1298 |
if ( defined $blessing and ( substr $subgrammar, 0, 1 ) eq 'L' ) |
| 1299 |
{ |
| 1300 |
Marpa::R2::exception( |
| 1301 |
qq{bless option not allowed in lexical rules (rule's LHS was "$lhs")} |
| 1302 |
); |
| 1303 |
} |
| 1304 |
$parse->bless_hash_rule( \%sequence_rule, $blessing, $naming, $lhs_name ); |
| 1305 |
|
| 1306 |
push @{ $parse->{rules}->{$subgrammar} }, @rules; |
| 1307 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 1308 |
return undef; |
| 1309 |
|
| 1310 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::quantified_rule::evaluate |
| 1311 |
|
| 1312 |
sub Marpa::R2::Internal::MetaAST_Nodes::completion_event_declaration::evaluate |
| 1313 |
{ |
| 1314 |
my ( $values, $parse ) = @_; |
| 1315 |
my ( $start, $length, $raw_event, $raw_symbol_name ) = @{$values}; |
| 1316 |
my $symbol_name = $raw_symbol_name->name(); |
| 1317 |
my $completion_events = $parse->{completion_events} //= {}; |
| 1318 |
if ( defined $completion_events->{$symbol_name} ) { |
| 1319 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 1320 |
die qq{Completion event for symbol "$symbol_name" declared twice\n}, |
| 1321 |
qq{ That is not allowed\n}, |
| 1322 |
' Second declaration was ', $parse->substring( $start, $length ), |
| 1323 |
"\n", |
| 1324 |
" Problem occurred at line $line, column $column\n"; |
| 1325 |
} ## end if ( defined $completion_events->{$symbol_name} ) |
| 1326 |
$completion_events->{$symbol_name} = $raw_event->event(); |
| 1327 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 1328 |
return undef; |
| 1329 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::completion_event_declaration::evaluate |
| 1330 |
|
| 1331 |
sub Marpa::R2::Internal::MetaAST_Nodes::nulled_event_declaration::evaluate { |
| 1332 |
my ( $values, $parse ) = @_; |
| 1333 |
my ( $start, $length, $raw_event, $raw_symbol_name ) = @{$values}; |
| 1334 |
my $symbol_name = $raw_symbol_name->name(); |
| 1335 |
my $nulled_events = $parse->{nulled_events} //= {}; |
| 1336 |
if ( defined $nulled_events->{$symbol_name} ) { |
| 1337 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 1338 |
die qq{nulled event for symbol "$symbol_name" declared twice\n}, |
| 1339 |
qq{ That is not allowed\n}, |
| 1340 |
' Second declaration was ', $parse->substring( $start, $length ), |
| 1341 |
"\n", |
| 1342 |
" Problem occurred at line $line, column $column\n"; |
| 1343 |
} ## end if ( defined $nulled_events->{$symbol_name} ) |
| 1344 |
$nulled_events->{$symbol_name} = $raw_event->event(); |
| 1345 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 1346 |
return undef; |
| 1347 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::nulled_event_declaration::evaluate |
| 1348 |
|
| 1349 |
sub Marpa::R2::Internal::MetaAST_Nodes::prediction_event_declaration::evaluate |
| 1350 |
{ |
| 1351 |
my ( $values, $parse ) = @_; |
| 1352 |
my ( $start, $length, $raw_event, $raw_symbol_name ) = @{$values}; |
| 1353 |
my $symbol_name = $raw_symbol_name->name(); |
| 1354 |
my $prediction_events = $parse->{prediction_events} //= {}; |
| 1355 |
if ( defined $prediction_events->{$symbol_name} ) { |
| 1356 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 1357 |
die qq{prediction event for symbol "$symbol_name" declared twice\n}, |
| 1358 |
qq{ That is not allowed\n}, |
| 1359 |
' Second declaration was ', $parse->substring( $start, $length ), |
| 1360 |
"\n", |
| 1361 |
" Problem occurred at line $line, column $column\n"; |
| 1362 |
} ## end if ( defined $prediction_events->{$symbol_name} ) |
| 1363 |
$prediction_events->{$symbol_name} = $raw_event->event(); |
| 1364 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 1365 |
return undef; |
| 1366 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::prediction_event_declaration::evaluate |
| 1367 |
|
| 1368 |
sub Marpa::R2::Internal::MetaAST_Nodes::current_lexer_statement::evaluate |
| 1369 |
{ |
| 1370 |
my ( $values, $parse ) = @_; |
| 1371 |
my ( $start, $length, $lexer_name_object ) = @{$values}; |
| 1372 |
my $raw_lexer_name = $lexer_name_object->name(); |
| 1373 |
if ( $raw_lexer_name eq 'L0' ) { |
| 1374 |
$parse->{current_lexer} = $raw_lexer_name; |
| 1375 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 1376 |
return undef; |
| 1377 |
} |
| 1378 |
if ( $raw_lexer_name =~ m/\A [[:upper:]] [[:digit:]]+ \z/xms) { |
| 1379 |
my ( $line, $column ) = $parse->{meta_recce}->line_column($start); |
| 1380 |
die qq{Attempt to name a new lexer "$raw_lexer_name"\n}, |
| 1381 |
qq{ Lexer names of the form [A-Z][0-9]+ are reserved\n}, |
| 1382 |
qq{ Please choose another name\n}, |
| 1383 |
" Problem occurred at line $line, column $column\n"; |
| 1384 |
} ## end if ( defined $prediction_events->{$symbol_name} ) |
| 1385 |
my $lexer_name .= 'L-' . $raw_lexer_name; |
| 1386 |
$parse->{current_lexer} = $lexer_name; |
| 1387 |
## no critic(Subroutines::ProhibitExplicitReturnUndef) |
| 1388 |
return undef; |
| 1389 |
} |
| 1390 |
|
| 1391 |
sub Marpa::R2::Internal::MetaAST_Nodes::alternatives::evaluate { |
| 1392 |
my ( $values, $parse ) = @_; |
| 1393 |
return bless [ map { $_->evaluate( $_, $parse ) } @{$values} ], |
| 1394 |
ref $values; |
| 1395 |
} |
| 1396 |
|
| 1397 |
sub Marpa::R2::Internal::MetaAST_Nodes::alternative::evaluate { |
| 1398 |
my ( $values, $parse ) = @_; |
| 1399 |
my ( $start, $length, $rhs, $adverbs ) = @{$values}; |
| 1400 |
my $alternative = eval { |
| 1401 |
Marpa::R2::Internal::MetaAST::Proto_Alternative->combine( |
| 1402 |
map { $_->evaluate($parse) } $rhs, $adverbs ); |
| 1403 |
}; |
| 1404 |
if ( not $alternative ) { |
| 1405 |
Marpa::R2::exception( |
| 1406 |
$EVAL_ERROR, "\n", |
| 1407 |
q{ Alternative involved was }, |
| 1408 |
$parse->substring( $start, $length ) |
| 1409 |
); |
| 1410 |
} ## end if ( not $alternative ) |
| 1411 |
return $alternative; |
| 1412 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::alternative::evaluate |
| 1413 |
|
| 1414 |
sub Marpa::R2::Internal::MetaAST_Nodes::single_symbol::names { |
| 1415 |
my ( $values, $parse ) = @_; |
| 1416 |
my ( undef, undef, $symbol ) = @{$values}; |
| 1417 |
return $symbol->names($parse); |
| 1418 |
} |
| 1419 |
|
| 1420 |
sub Marpa::R2::Internal::MetaAST_Nodes::single_symbol::name { |
| 1421 |
my ( $values, $parse ) = @_; |
| 1422 |
my ( undef, undef, $symbol ) = @{$values}; |
| 1423 |
return $symbol->name($parse); |
| 1424 |
} |
| 1425 |
|
| 1426 |
sub Marpa::R2::Internal::MetaAST_Nodes::single_symbol::event_name { |
| 1427 |
my ( $values, $parse ) = @_; |
| 1428 |
my ( undef, undef, $symbol ) = @{$values}; |
| 1429 |
return $symbol->event_name($parse); |
| 1430 |
} |
| 1431 |
|
| 1432 |
sub Marpa::R2::Internal::MetaAST_Nodes::single_symbol::literal { |
| 1433 |
my ( $values, $parse ) = @_; |
| 1434 |
my ( $start, $length ) = @{$values}; |
| 1435 |
return $parse->substring($start, $length); |
| 1436 |
} |
| 1437 |
|
| 1438 |
sub Marpa::R2::Internal::MetaAST_Nodes::single_symbol::evaluate { |
| 1439 |
my ( $values, $parse ) = @_; |
| 1440 |
my ( undef, undef, $symbol ) = @{$values}; |
| 1441 |
return Marpa::R2::Internal::MetaAST::Symbol_List->new( |
| 1442 |
$symbol->name($parse) ); |
| 1443 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::single_symbol::evaluate |
| 1444 |
|
| 1445 |
sub Marpa::R2::Internal::MetaAST_Nodes::Symbol::evaluate { |
| 1446 |
my ( $values, $parse ) = @_; |
| 1447 |
my ( undef, undef, $symbol ) = @{$values}; |
| 1448 |
return $symbol->evaluate($parse); |
| 1449 |
} |
| 1450 |
|
| 1451 |
sub Marpa::R2::Internal::MetaAST_Nodes::symbol::name { |
| 1452 |
my ( $self, $parse ) = @_; |
| 1453 |
return $self->[2]->name($parse); |
| 1454 |
} |
| 1455 |
|
| 1456 |
sub Marpa::R2::Internal::MetaAST_Nodes::symbol::event_name { |
| 1457 |
my ( $self, $parse ) = @_; |
| 1458 |
return $self->[2]->name($parse); |
| 1459 |
} |
| 1460 |
|
| 1461 |
sub Marpa::R2::Internal::MetaAST_Nodes::symbol::names { |
| 1462 |
my ( $self, $parse ) = @_; |
| 1463 |
return $self->[2]->names($parse); |
| 1464 |
} |
| 1465 |
|
| 1466 |
sub Marpa::R2::Internal::MetaAST_Nodes::symbol_name::evaluate { |
| 1467 |
my ($self) = @_; |
| 1468 |
return $self->[2]; |
| 1469 |
} |
| 1470 |
|
| 1471 |
sub Marpa::R2::Internal::MetaAST_Nodes::symbol_name::name { |
| 1472 |
my ( $self, $parse ) = @_; |
| 1473 |
return $self->evaluate($parse)->name($parse); |
| 1474 |
} |
| 1475 |
|
| 1476 |
sub Marpa::R2::Internal::MetaAST_Nodes::symbol_name::names { |
| 1477 |
my ( $self, $parse ) = @_; |
| 1478 |
return [ $self->name($parse) ]; |
| 1479 |
} |
| 1480 |
|
| 1481 |
sub Marpa::R2::Internal::MetaAST_Nodes::adverb_list::evaluate { |
| 1482 |
my ( $data, $parse ) = @_; |
| 1483 |
my ( undef, undef, $adverb_list_items ) = @{$data}; |
| 1484 |
return undef if not defined $adverb_list_items; |
| 1485 |
return $adverb_list_items->evaluate($parse); |
| 1486 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::adverb_list::evaluate |
| 1487 |
|
| 1488 |
sub Marpa::R2::Internal::MetaAST_Nodes::null_adverb::evaluate { |
| 1489 |
return {}; |
| 1490 |
} |
| 1491 |
|
| 1492 |
sub Marpa::R2::Internal::MetaAST_Nodes::adverb_list_items::evaluate { |
| 1493 |
my ( $data, $parse ) = @_; |
| 1494 |
my ( undef, undef, @raw_items ) = @{$data}; |
| 1495 |
my (@adverb_items) = map { $_->evaluate($parse) } @raw_items; |
| 1496 |
return Marpa::R2::Internal::MetaAST::Proto_Alternative->combine( |
| 1497 |
@adverb_items); |
| 1498 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::adverb_list::evaluate |
| 1499 |
|
| 1500 |
sub Marpa::R2::Internal::MetaAST_Nodes::character_class::event_name { |
| 1501 |
my ( $data, $parse ) = @_; |
| 1502 |
my ( $start, $length ) = @{$data}; |
| 1503 |
return $parse->substring( $start, $length ); |
| 1504 |
} |
| 1505 |
|
| 1506 |
sub Marpa::R2::Internal::MetaAST_Nodes::character_class::names { |
| 1507 |
my ( $self, $parse ) = @_; |
| 1508 |
return [ $self->name($parse) ]; |
| 1509 |
} |
| 1510 |
|
| 1511 |
sub Marpa::R2::Internal::MetaAST_Nodes::character_class::name { |
| 1512 |
my ( $self, $parse ) = @_; |
| 1513 |
return $self->evaluate($parse)->name($parse); |
| 1514 |
} |
| 1515 |
|
| 1516 |
sub Marpa::R2::Internal::MetaAST_Nodes::character_class::evaluate { |
| 1517 |
my ( $values, $parse ) = @_; |
| 1518 |
my $character_class = $values->[2]; |
| 1519 |
my $subgrammar = $Marpa::R2::Internal::SUBGRAMMAR; |
| 1520 |
if (( substr $subgrammar, 0, 1 ) eq 'L') { |
| 1521 |
return Marpa::R2::Internal::MetaAST::Symbol_List->char_class_to_symbol( |
| 1522 |
$parse, $character_class ); |
| 1523 |
} |
| 1524 |
# If here, in G1 |
| 1525 |
# Character classes and strings always go into L0, for now |
| 1526 |
my $lexer_symbol = do { |
| 1527 |
local $Marpa::R2::Internal::SUBGRAMMAR = 'L0'; |
| 1528 |
Marpa::R2::Internal::MetaAST::Symbol_List->char_class_to_symbol( |
| 1529 |
$parse, $character_class ); |
| 1530 |
}; |
| 1531 |
my $lexical_lhs = $parse->internal_lexeme($character_class); |
| 1532 |
my $lexical_rhs = $lexer_symbol->names($parse); |
| 1533 |
my %lexical_rule = ( |
| 1534 |
lhs => $lexical_lhs, |
| 1535 |
rhs => $lexical_rhs, |
| 1536 |
mask => [1], |
| 1537 |
); |
| 1538 |
push @{ $parse->{rules}->{L0} }, \%lexical_rule; |
| 1539 |
my $g1_symbol = |
| 1540 |
Marpa::R2::Internal::MetaAST::Symbol_List->new($lexical_lhs); |
| 1541 |
return $g1_symbol; |
| 1542 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::character_class::evaluate |
| 1543 |
|
| 1544 |
sub Marpa::R2::Internal::MetaAST_Nodes::single_quoted_string::evaluate { |
| 1545 |
my ( $values, $parse ) = @_; |
| 1546 |
my ( undef, undef, $string ) = @{$values}; |
| 1547 |
my @symbols = (); |
| 1548 |
|
| 1549 |
my $end_of_string = rindex $string, q{'}; |
| 1550 |
my $unmodified_string = substr $string, 0, $end_of_string+1; |
| 1551 |
my $raw_flags = substr $string, $end_of_string+1; |
| 1552 |
my $flags = Marpa::R2::Internal::MetaAST::flag_string_to_flags($raw_flags); |
| 1553 |
my $subgrammar = $Marpa::R2::Internal::SUBGRAMMAR; |
| 1554 |
|
| 1555 |
# If we are currently in a lexical grammar, the strings go there |
| 1556 |
# If we are currently in G1, the strings always go into L0 |
| 1557 |
my $lexical_grammar = $subgrammar eq 'G1' ? 'L0' : $subgrammar; |
| 1558 |
|
| 1559 |
for my $char_class ( |
| 1560 |
map { '[' . ( quotemeta $_ ) . ']' . $flags } split //xms, |
| 1561 |
substr $unmodified_string, |
| 1562 |
1, -1 |
| 1563 |
) |
| 1564 |
{ |
| 1565 |
local $Marpa::R2::Internal::SUBGRAMMAR = $lexical_grammar; |
| 1566 |
my $symbol = |
| 1567 |
Marpa::R2::Internal::MetaAST::Symbol_List->char_class_to_symbol( |
| 1568 |
$parse, $char_class ); |
| 1569 |
push @symbols, $symbol; |
| 1570 |
} ## end for my $char_class ( map { '[' . ( quotemeta $_ ) . ']'...}) |
| 1571 |
my $list = Marpa::R2::Internal::MetaAST::Symbol_List->combine(@symbols); |
| 1572 |
return $list if $Marpa::R2::Internal::SUBGRAMMAR ne 'G1'; |
| 1573 |
my $lexical_lhs = $parse->internal_lexeme($string); |
| 1574 |
my $lexical_rhs = $list->names($parse); |
| 1575 |
my %lexical_rule = ( |
| 1576 |
lhs => $lexical_lhs, |
| 1577 |
rhs => $lexical_rhs, |
| 1578 |
description => "Internal rule for single-quoted string $string", |
| 1579 |
mask => [ map { ; 1 } @{$lexical_rhs} ], |
| 1580 |
); |
| 1581 |
push @{ $parse->{rules}->{$lexical_grammar} }, \%lexical_rule; |
| 1582 |
my $g1_symbol = |
| 1583 |
Marpa::R2::Internal::MetaAST::Symbol_List->new($lexical_lhs); |
| 1584 |
return $g1_symbol; |
| 1585 |
} ## end sub Marpa::R2::Internal::MetaAST_Nodes::single_quoted_string::evaluate |
| 1586 |
|
| 1587 |
package Marpa::R2::Internal::MetaAST::Symbol_List; |
| 1588 |
|
| 1589 |
use English qw( -no_match_vars ); |
| 1590 |
|
| 1591 |
sub new { |
| 1592 |
my ( $class, $name ) = @_; |
| 1593 |
return bless { names => [ q{} . $name ], mask => [1] }, $class; |
| 1594 |
} |
| 1595 |
|
| 1596 |
sub combine { |
| 1597 |
my ( $class, @lists ) = @_; |
| 1598 |
my $self = {}; |
| 1599 |
$self->{names} = [ map { @{ $_->names() } } @lists ]; |
| 1600 |
$self->{mask} = [ map { @{ $_->mask() } } @lists ]; |
| 1601 |
return bless $self, $class; |
| 1602 |
} ## end sub combine |
| 1603 |
|
| 1604 |
sub Marpa::R2::Internal::MetaAST::char_class_to_re { |
| 1605 |
my ($cc_components) = @_; |
| 1606 |
die if ref $cc_components ne 'ARRAY'; |
| 1607 |
my ( $char_class, $flags ) = @{$cc_components}; |
| 1608 |
$flags = $flags ? '(' . q{?} . $flags . ')' : q{}; |
| 1609 |
my $regex; |
| 1610 |
my $error; |
| 1611 |
if ( not defined eval { $regex = qr/$flags$char_class/xms; 1; } ) { |
| 1612 |
$error = qq{Problem in evaluating character class: "$char_class"\n}; |
| 1613 |
$error .= qq{ Flags were "$flags"\n} if $flags; |
| 1614 |
$error .= $EVAL_ERROR; |
| 1615 |
} |
| 1616 |
return $regex, $error; |
| 1617 |
} |
| 1618 |
|
| 1619 |
sub Marpa::R2::Internal::MetaAST::flag_string_to_flags { |
| 1620 |
my ($raw_flag_string) = @_; |
| 1621 |
return q{} if not $raw_flag_string; |
| 1622 |
my @raw_flags = split m/:/xms, $raw_flag_string; |
| 1623 |
my %flags = (); |
| 1624 |
RAW_FLAG: for my $raw_flag (@raw_flags) { |
| 1625 |
next RAW_FLAG if not $raw_flag; |
| 1626 |
if ( $raw_flag eq 'i' ) { |
| 1627 |
$flags{'i'} = 1; |
| 1628 |
next RAW_FLAG; |
| 1629 |
} |
| 1630 |
if ( $raw_flag eq 'ic' ) { |
| 1631 |
$flags{'i'} = 1; |
| 1632 |
next RAW_FLAG; |
| 1633 |
} |
| 1634 |
Carp::croak( |
| 1635 |
qq{Bad flag for character class\n}, |
| 1636 |
qq{ Flag string was $raw_flag_string\n}, |
| 1637 |
qq{ Bad flag was $raw_flag\n} |
| 1638 |
); |
| 1639 |
} ## end RAW_FLAG: for my $raw_flag (@raw_flags) |
| 1640 |
my $cooked_flags = join q{}, sort keys %flags; |
| 1641 |
return $cooked_flags; |
| 1642 |
} ## end sub flag_string_to_flags |
| 1643 |
|
| 1644 |
# Return the character class symbol name, |
| 1645 |
# after ensuring everything is set up properly |
| 1646 |
sub char_class_to_symbol { |
| 1647 |
my ( $class, $parse, $char_class ) = @_; |
| 1648 |
|
| 1649 |
my $end_of_char_class = rindex $char_class, q{]}; |
| 1650 |
my $unmodified_char_class = substr $char_class, 0, $end_of_char_class+1; |
| 1651 |
my $raw_flags = substr $char_class, $end_of_char_class+1; |
| 1652 |
my $flags = Marpa::R2::Internal::MetaAST::flag_string_to_flags($raw_flags); |
| 1653 |
my $subgrammar = $Marpa::R2::Internal::SUBGRAMMAR; |
| 1654 |
|
| 1655 |
# character class symbol name always start with TWO left square brackets |
| 1656 |
my $symbol_name = '[' . $unmodified_char_class . $flags . ']'; |
| 1657 |
$parse->{character_classes} //= {}; |
| 1658 |
my $cc_hash = $parse->{character_classes}; |
| 1659 |
my ( undef, $symbol ) = $cc_hash->{$symbol_name}; |
| 1660 |
if ( not defined $symbol ) { |
| 1661 |
|
| 1662 |
my $cc_components = [$unmodified_char_class, $flags]; |
| 1663 |
|
| 1664 |
# Fast fail on badly formed char_class -- we re-evaluate the regex just in time |
| 1665 |
# before we register characters. |
| 1666 |
my ( $regex, $eval_error ) = |
| 1667 |
Marpa::R2::Internal::MetaAST::char_class_to_re($cc_components); |
| 1668 |
Carp::croak( 'Bad Character class: ', |
| 1669 |
$char_class, "\n", 'Perl said ', $eval_error ) |
| 1670 |
if not $regex; |
| 1671 |
|
| 1672 |
$symbol = |
| 1673 |
Marpa::R2::Internal::MetaAST::Symbol_List->new($symbol_name); |
| 1674 |
$cc_hash->{$symbol_name} = [ $cc_components, $symbol ]; |
| 1675 |
$parse->symbol_names_set( |
| 1676 |
$symbol_name, |
| 1677 |
$subgrammar, |
| 1678 |
{ dsl_form => $char_class, |
| 1679 |
display_form => $char_class, |
| 1680 |
description => "Character class: $char_class" |
| 1681 |
} |
| 1682 |
); |
| 1683 |
} ## end if ( not defined $symbol ) |
| 1684 |
return $symbol; |
| 1685 |
} ## end sub char_class_to_symbol |
| 1686 |
|
| 1687 |
sub Marpa::R2::Internal::MetaAST::Parse::symbol_names_set { |
| 1688 |
my ( $parse, $symbol, $subgrammar, $args ) = @_; |
| 1689 |
my $symbol_type = $subgrammar eq 'G1' ? 'G1' : 'L'; |
| 1690 |
for my $arg_type (keys %{$args}) { |
| 1691 |
my $value = $args->{$arg_type}; |
| 1692 |
$parse->{symbols}->{$symbol_type}->{$symbol}->{$arg_type} = $value; |
| 1693 |
} |
| 1694 |
} |
| 1695 |
|
| 1696 |
# Return the priotized symbol name, |
| 1697 |
# after ensuring everything is set up properly |
| 1698 |
sub Marpa::R2::Internal::MetaAST::Parse::prioritized_symbol { |
| 1699 |
my ( $parse, $base_symbol, $priority ) = @_; |
| 1700 |
|
| 1701 |
# character class symbol name always start with TWO left square brackets |
| 1702 |
my $symbol_name = $base_symbol . '[' . $priority . ']'; |
| 1703 |
my $symbol_data = |
| 1704 |
$parse->{symbols}->{$Marpa::R2::Internal::SUBGRAMMAR eq 'G1' ? 'G1' : 'L'}->{$symbol_name}; |
| 1705 |
return $symbol_name if defined $symbol_data; |
| 1706 |
my $display_form = |
| 1707 |
( $base_symbol =~ m/\s/xms ) ? "<$base_symbol>" : $base_symbol; |
| 1708 |
$parse->symbol_names_set( |
| 1709 |
$symbol_name, |
| 1710 |
$Marpa::R2::Internal::SUBGRAMMAR, |
| 1711 |
{ legacy_name => $base_symbol, |
| 1712 |
dsl_form => $base_symbol, |
| 1713 |
display_form => $display_form, |
| 1714 |
description => "<$base_symbol> at priority $priority" |
| 1715 |
} |
| 1716 |
); |
| 1717 |
return $symbol_name; |
| 1718 |
} ## end sub Marpa::R2::Internal::MetaAST::Parse::prioritized_symbol |
| 1719 |
|
| 1720 |
# Return the prioritized symbol name, |
| 1721 |
# after ensuring everything is set up properly |
| 1722 |
sub Marpa::R2::Internal::MetaAST::Parse::internal_lexeme { |
| 1723 |
my ( $parse, $dsl_form, @grammars ) = @_; |
| 1724 |
|
| 1725 |
# character class symbol name always start with TWO left square brackets |
| 1726 |
my $lexical_lhs_index = $parse->{lexical_lhs_index}++; |
| 1727 |
my $lexical_symbol = "[Lex-$lexical_lhs_index]"; |
| 1728 |
my %names = ( |
| 1729 |
dsl_form => $dsl_form, |
| 1730 |
display_form => $dsl_form, |
| 1731 |
description => qq{Internal lexical symbol for "$dsl_form"} |
| 1732 |
); |
| 1733 |
$parse->symbol_names_set( $lexical_symbol, $_, \%names ) for qw(G1 L); |
| 1734 |
return $lexical_symbol; |
| 1735 |
} ## end sub Marpa::R2::Internal::MetaAST::Parse::internal_lexeme |
| 1736 |
|
| 1737 |
sub name { |
| 1738 |
my ($self) = @_; |
| 1739 |
my $names = $self->{names}; |
| 1740 |
Marpa::R2::exception( 'list->name() on symbol list of length ', |
| 1741 |
scalar @{$names} ) |
| 1742 |
if scalar @{$names} != 1; |
| 1743 |
return $self->{names}->[0]; |
| 1744 |
} ## end sub name |
| 1745 |
sub names { return shift->{names} } |
| 1746 |
sub mask { return shift->{mask} } |
| 1747 |
|
| 1748 |
sub mask_set { |
| 1749 |
my ( $self, $mask ) = @_; |
| 1750 |
return $self->{mask} = [ map {$mask} @{ $self->{mask} } ]; |
| 1751 |
} |
| 1752 |
|
| 1753 |
1; |
| 1754 |
|
| 1755 |
# vim: expandtab shiftwidth=4: |