| 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 |
=head1 NAME |
| 17 |
|
| 18 |
Marpa::R2::Deprecated::NAIF::Semantics - How the NAIF evaluates parses |
| 19 |
|
| 20 |
=head1 THE NAIF INTERFACE IS DEPRECATED |
| 21 |
|
| 22 |
This document describes the NAIF interface, |
| 23 |
which is deprecated. |
| 24 |
PLEASE DO NOT USE IT FOR NEW DEVELOPMENT. |
| 25 |
|
| 26 |
=head1 Synopsis |
| 27 |
|
| 28 |
=for Marpa::R2::Display |
| 29 |
name: Engine Synopsis Unambiguous Parse |
| 30 |
partial: 1 |
| 31 |
normalize-whitespace: 1 |
| 32 |
|
| 33 |
my $grammar = Marpa::R2::Grammar->new( |
| 34 |
{ start => 'Expression', |
| 35 |
actions => 'My_Actions', |
| 36 |
default_action => 'first_arg', |
| 37 |
rules => [ |
| 38 |
{ lhs => 'Expression', rhs => [qw/Term/] }, |
| 39 |
{ lhs => 'Term', rhs => [qw/Factor/] }, |
| 40 |
{ lhs => 'Factor', rhs => [qw/Number/] }, |
| 41 |
{ lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' }, |
| 42 |
{ lhs => 'Factor', |
| 43 |
rhs => [qw/Factor Multiply Factor/], |
| 44 |
action => 'do_multiply' |
| 45 |
}, |
| 46 |
], |
| 47 |
} |
| 48 |
); |
| 49 |
|
| 50 |
=for Marpa::R2::Display::End |
| 51 |
|
| 52 |
=for Marpa::R2::Display |
| 53 |
name: Engine Synopsis Unambiguous Parse |
| 54 |
partial: 1 |
| 55 |
normalize-whitespace: 1 |
| 56 |
|
| 57 |
sub My_Actions::do_add { |
| 58 |
my ( undef, $t1, undef, $t2 ) = @_; |
| 59 |
return $t1 + $t2; |
| 60 |
} |
| 61 |
|
| 62 |
sub My_Actions::do_multiply { |
| 63 |
my ( undef, $t1, undef, $t2 ) = @_; |
| 64 |
return $t1 * $t2; |
| 65 |
} |
| 66 |
|
| 67 |
sub My_Actions::first_arg { shift; return shift; } |
| 68 |
|
| 69 |
my $value_ref = $recce->value; |
| 70 |
my $value = $value_ref ? ${$value_ref} : 'No Parse'; |
| 71 |
|
| 72 |
=for Marpa::R2::Display::End |
| 73 |
|
| 74 |
=head1 Overview |
| 75 |
|
| 76 |
This document deals with Marpa's low-level NAIF interface. |
| 77 |
If you are new to Marpa, |
| 78 |
or are not sure which interface you are interested in, |
| 79 |
or do not know what the Named Argument InterFace (NAIF) is, |
| 80 |
you probably want to look instead at |
| 81 |
L<the document on semantics for the SLIF |
| 82 |
interface|Marpa::R2::Semantics>. |
| 83 |
|
| 84 |
The NAIF's semantics |
| 85 |
will be |
| 86 |
familiar |
| 87 |
to those who have used traditional |
| 88 |
methods to evaluate parses. |
| 89 |
A parse is seen as a parse tree. |
| 90 |
Nodes on the tree are evaluated recursively, bottom-up. |
| 91 |
Once the values of all its child nodes are known, |
| 92 |
a parent node is ready to be evaluated. |
| 93 |
The value of a parse is the value of the top node |
| 94 |
of the parse tree. |
| 95 |
|
| 96 |
When a Marpa grammar is created, |
| 97 |
its semantics |
| 98 |
is |
| 99 |
specified indirectly, as B<action names>. |
| 100 |
To produce the values used in the semantics, |
| 101 |
Marpa must do three things: |
| 102 |
|
| 103 |
=over |
| 104 |
|
| 105 |
=item * Determine the action name. |
| 106 |
|
| 107 |
=item * Resolve the action name to an action. |
| 108 |
|
| 109 |
=item * If the action is a rule evaluation closure, |
| 110 |
call it to produce the actual result. |
| 111 |
|
| 112 |
=back |
| 113 |
|
| 114 |
An action name is either reserved, |
| 115 |
or resolves to a Perl object. |
| 116 |
When it resolves to a Perl object, |
| 117 |
that object is usually a Perl closure -- code. |
| 118 |
If the Perl object action is code, |
| 119 |
it is a rule evaluation closure, and will be |
| 120 |
called to produce the result. |
| 121 |
When a Perl object action is some other kind of object, |
| 122 |
its treatment is |
| 123 |
L<as described |
| 124 |
below|/"Types of Perl actions"> |
| 125 |
|
| 126 |
An action name |
| 127 |
and action is also used |
| 128 |
to create the per-parse-tree variable, |
| 129 |
as L<described |
| 130 |
below|/"The per-parse-tree variable">. |
| 131 |
|
| 132 |
=head1 Nodes |
| 133 |
|
| 134 |
=head2 Token nodes |
| 135 |
|
| 136 |
For every input token, there is an associated B<token node>. |
| 137 |
Token nodes are leaf nodes |
| 138 |
in the parse tree. |
| 139 |
Tokens always have a B<token symbol>. |
| 140 |
At lexing time, |
| 141 |
they can be assigned a B<token value>. |
| 142 |
If no token value is assigned at lex time, |
| 143 |
the value of the token is a Perl C<undef>. |
| 144 |
|
| 145 |
=head2 Rule nodes |
| 146 |
|
| 147 |
If a node is not a token node, |
| 148 |
then it is a B<rule node>. |
| 149 |
Rule nodes are always |
| 150 |
associated with a rule. |
| 151 |
It a rule's action is a rule evaluation closure, |
| 152 |
it is called at |
| 153 |
L<Node Evaluation Time|Marpa::R2::Deprecated::NAIF::Semantics::Phases/"Tree Traversal Phase">. |
| 154 |
|
| 155 |
The rule evaluation closures's |
| 156 |
arguments will be a |
| 157 |
per-parse-tree variable followed, if the rule is not nulled, |
| 158 |
by the values of its child nodes in lexical order. |
| 159 |
If the rule is nulled, the child node values will be omitted. |
| 160 |
A rule evaluation closure action is always called in scalar context. |
| 161 |
|
| 162 |
If the action is a constant, |
| 163 |
it becomes the value of the rule node. |
| 164 |
If the action is a rule evaluation closure, |
| 165 |
its return value becomes the value of the node. |
| 166 |
If there is no action for a |
| 167 |
rule node, |
| 168 |
the value of the rule node is a Perl C<undef>. |
| 169 |
|
| 170 |
=head2 Sequence rule nodes |
| 171 |
|
| 172 |
Some rules are L<sequence rules|Marpa::R2::Deprecated::NAIF::Grammar/"Sequence rules">. |
| 173 |
Sequence rule nodes are also rule nodes. |
| 174 |
Everything said above about rule nodes |
| 175 |
applies to sequence rule nodes. |
| 176 |
Specifically, |
| 177 |
the arguments to the value actions for sequence rules |
| 178 |
are the |
| 179 |
per-parse-tree variable followed by |
| 180 |
the values of the child nodes in lexical order. |
| 181 |
|
| 182 |
The difference (and it is a big one) |
| 183 |
is that in an ordinary rule, the right hand side |
| 184 |
is fixed in length, and that length is known |
| 185 |
when you are writing the code for the value action. |
| 186 |
In a sequence rule, |
| 187 |
the number of right hand side symbols is not known |
| 188 |
until node evaluation time. |
| 189 |
The rule evaluation closure |
| 190 |
of a sequence rule |
| 191 |
must be capable of |
| 192 |
dealing with |
| 193 |
a variable number of arguments. |
| 194 |
|
| 195 |
Sequence semantics work best when |
| 196 |
every child node |
| 197 |
in the sequence has the same semantics. |
| 198 |
When that is not the case, |
| 199 |
writing the sequence using |
| 200 |
ordinary non-sequence rules should be considered as |
| 201 |
an alternative. |
| 202 |
|
| 203 |
By default, if a sequence rule has separators, |
| 204 |
the separators are thrown away before |
| 205 |
the value action is called. |
| 206 |
(Separators are described in |
| 207 |
L<the section introducing sequence rules|Marpa::R2::Deprecated::NAIF::Grammar/"Sequence rules">.) |
| 208 |
This means that separators do not appear in the C<@_> array |
| 209 |
of the rule evaluation closure which is the value action. |
| 210 |
If the value of the C<keep> rule property |
| 211 |
is a Perl true value, separators are kept, |
| 212 |
and do appear in the |
| 213 |
value action's |
| 214 |
C<@_> array. |
| 215 |
|
| 216 |
=head2 Null nodes |
| 217 |
|
| 218 |
A null node is a special case of a rule node, |
| 219 |
one where the rule derives the zero-length, |
| 220 |
or empty string. |
| 221 |
When the rule node is a null node, |
| 222 |
the rule evaluation closure will be called with |
| 223 |
no child value arguments. |
| 224 |
|
| 225 |
When a node is nulled, |
| 226 |
it must be as a result of a nullable rule, |
| 227 |
and the action name and action are those |
| 228 |
associated with that rule. |
| 229 |
An ambiguity can arise if there is more |
| 230 |
than one nullable rule with the same LHS, |
| 231 |
but a different action name. |
| 232 |
In that case the action name for the null nodes |
| 233 |
is that of the empty rule. |
| 234 |
|
| 235 |
The remaining case is that of |
| 236 |
a set of nullable rules with the same LHS, |
| 237 |
where two or more of the rules have different action names, |
| 238 |
but none of the rules in the set is an empty rule. |
| 239 |
When this happens, Marpa throws an exception. |
| 240 |
To fix the issue, |
| 241 |
the user can add an empty rule. |
| 242 |
For more details, |
| 243 |
see L<the document on |
| 244 |
null semantics|Marpa::R2::Deprecated::NAIF::Semantics::Null>. |
| 245 |
|
| 246 |
=head1 Action context |
| 247 |
|
| 248 |
=for Marpa::R2::Display |
| 249 |
name: Action context synopsis |
| 250 |
normalize-whitespace: 1 |
| 251 |
|
| 252 |
sub do_S { |
| 253 |
my ($action_object) = @_; |
| 254 |
my $rule_id = $Marpa::R2::Context::rule; |
| 255 |
my $grammar = $Marpa::R2::Context::grammar; |
| 256 |
my ( $lhs, @rhs ) = $grammar->rule($rule_id); |
| 257 |
$action_object->{text} = |
| 258 |
"rule $rule_id: $lhs ::= " |
| 259 |
. ( join q{ }, @rhs ) . "\n" |
| 260 |
. "locations: " |
| 261 |
. ( join q{-}, Marpa::R2::Context::location() ) . "\n"; |
| 262 |
return $action_object; |
| 263 |
} ## end sub do_S |
| 264 |
|
| 265 |
=for Marpa::R2::Display::End |
| 266 |
|
| 267 |
In addition to the per-parse-tree variable |
| 268 |
and their child arguments, |
| 269 |
rule evaluation closures also have access |
| 270 |
to B<context variables>. |
| 271 |
|
| 272 |
=over |
| 273 |
|
| 274 |
=item * C<$Marpa::R2::Context::grammar> is set to |
| 275 |
the grammar being parsed. |
| 276 |
|
| 277 |
=item * C<$Marpa::R2::Context::rule> is the ID of the |
| 278 |
current rule. |
| 279 |
Given the rule ID, an application can find |
| 280 |
its LHS and RHS symbols using |
| 281 |
L<the grammar's C<rule()> method|Marpa::R2::Deprecated::NAIF::Grammar/"rule()">. |
| 282 |
|
| 283 |
=item * C<Marpa::R2::Context::location()> returns the start |
| 284 |
and end locations of the current rule. |
| 285 |
|
| 286 |
=back |
| 287 |
|
| 288 |
=head1 Bailing out of parse evaluation |
| 289 |
|
| 290 |
=for Marpa::R2::Display |
| 291 |
name: Semantics bail synopsis |
| 292 |
normalize-whitespace: 1 |
| 293 |
|
| 294 |
my $bail_message = "This is a bail out message!"; |
| 295 |
|
| 296 |
sub do_bail_with_message_if_A { |
| 297 |
my ($action_object, $terminal) = @_; |
| 298 |
Marpa::R2::Context::bail($bail_message) if $terminal eq 'A'; |
| 299 |
} |
| 300 |
|
| 301 |
sub do_bail_with_object_if_A { |
| 302 |
my ($action_object, $terminal) = @_; |
| 303 |
Marpa::R2::Context::bail([$bail_message]) if $terminal eq 'A'; |
| 304 |
} |
| 305 |
|
| 306 |
=for Marpa::R2::Display::End |
| 307 |
|
| 308 |
The C<Marpa::R2::Context::bail()> static method is used to |
| 309 |
"bail out" of the evaluation of a parse tree. |
| 310 |
It will cause an exception to be thrown. |
| 311 |
If its first and only argument is a reference, |
| 312 |
that reference is the exception object. |
| 313 |
Otherwise, an exception message is created |
| 314 |
by converting the method's arguments to strings, |
| 315 |
concatenating them, |
| 316 |
and prepending them with a message indicating |
| 317 |
the file and line number at which the |
| 318 |
C<Marpa::R2::Context::bail()> method was called. |
| 319 |
|
| 320 |
=head1 Parse trees, parse results and parse series |
| 321 |
|
| 322 |
When the semantics are applied to a parse tree, |
| 323 |
it produces a value called a B<parse result>. |
| 324 |
Because Marpa allows ambiguous parsing, |
| 325 |
each parse can produce a B<parse series> -- |
| 326 |
a series of zero or more parse trees, |
| 327 |
each with its own parse result. |
| 328 |
The first call to the |
| 329 |
L<the recognizer's C<value> |
| 330 |
method|Marpa::R2::Deprecated::NAIF::Recognizer/"value()"> |
| 331 |
after the recognizer is created is the |
| 332 |
start of the first parse series. |
| 333 |
The first parse series continues until there is |
| 334 |
a call to the |
| 335 |
L<the C<reset_evaluation> |
| 336 |
method|Marpa::R2::Deprecated::NAIF::Recognizer/"reset_evaluation()"> |
| 337 |
or until the recognizer is destroyed. |
| 338 |
Usually, an application is only interested in a single |
| 339 |
parse series. |
| 340 |
|
| 341 |
When the |
| 342 |
L<C<reset_evaluation>|Marpa::R2::Deprecated::NAIF::Recognizer/"reset_evaluation()"> |
| 343 |
method |
| 344 |
is called |
| 345 |
for a recognizer, it begins a new parse series. |
| 346 |
The new parse series continues until |
| 347 |
there is another |
| 348 |
call to the |
| 349 |
L<the C<reset_evaluation> |
| 350 |
method|Marpa::R2::Deprecated::NAIF::Recognizer/"reset_evaluation()">, |
| 351 |
or until the recognizer is destroyed. |
| 352 |
|
| 353 |
Most applications will find that the order in which |
| 354 |
Marpa executes its semantics "just works". |
| 355 |
L<A separate |
| 356 |
document|Marpa::R2::Deprecated::NAIF::Semantics::Phases> |
| 357 |
describes that order |
| 358 |
in detail. |
| 359 |
The details can matter in some applications, |
| 360 |
for example, those which exploit side effects. |
| 361 |
|
| 362 |
=head1 Finding the action for a rule |
| 363 |
|
| 364 |
Marpa finds the action for each rule based on |
| 365 |
rule and symbol properties and on the grammar named arguments. |
| 366 |
Specifically, Marpa attempts the following, |
| 367 |
in order: |
| 368 |
|
| 369 |
=over |
| 370 |
|
| 371 |
=item * Resolving an action based on the C<action> property of the rule, |
| 372 |
if one is defined. |
| 373 |
|
| 374 |
=item * If the rule is empty, |
| 375 |
and the C<default_empty_action> named argument of the grammar |
| 376 |
is defined, |
| 377 |
resolving an action based on that named argument. |
| 378 |
|
| 379 |
=item * Resolving an action based on |
| 380 |
the C<default_action> named argument of the grammar, |
| 381 |
if one is defined. |
| 382 |
|
| 383 |
=item * Defaulting to a Perl C<undef> value. |
| 384 |
|
| 385 |
=back |
| 386 |
|
| 387 |
Resolution of action names is L<described |
| 388 |
below|/"Resolving action names">. |
| 389 |
If the C<action> property, |
| 390 |
the C<default_action> named argument, |
| 391 |
or the C<default_empty_action> named argument |
| 392 |
is defined, |
| 393 |
but does not resolve successfully, Marpa |
| 394 |
throws an exception. |
| 395 |
Marpa prefers to "fast fail" in these cases, |
| 396 |
because they usually indicate a mistake |
| 397 |
that the application's author |
| 398 |
will want to correct. |
| 399 |
|
| 400 |
=head1 Resolving action names |
| 401 |
|
| 402 |
Action names come from these sources: |
| 403 |
|
| 404 |
=over |
| 405 |
|
| 406 |
=item * The C<default_action> named argument of Marpa's grammar. |
| 407 |
|
| 408 |
=item * The C<default_empty_action> named argument of Marpa's grammar. |
| 409 |
|
| 410 |
=item * The C<action> property of Marpa's rules. |
| 411 |
|
| 412 |
=item * The C<new> constructor in the package specified by the |
| 413 |
C<action_object> named argument of the Marpa grammar. |
| 414 |
|
| 415 |
=back |
| 416 |
|
| 417 |
=head2 Reserved action names |
| 418 |
|
| 419 |
Action names that begin with a double colon ("C<::>") are reserved. |
| 420 |
At present only the C<::undef> reserved action is documented for |
| 421 |
use outside of the DSL-based interfaces. |
| 422 |
|
| 423 |
=head3 ::undef |
| 424 |
|
| 425 |
A constant whose value is a Perl C<undef>. |
| 426 |
Perl is unable to distinguish reliably between a |
| 427 |
non-existent value and scalars with an C<undef> value. |
| 428 |
This makes it impossible to reliably distinguish |
| 429 |
resolutions |
| 430 |
to a Perl C<undef> from resolution problems. |
| 431 |
The "C<::undef>" reserved action name should |
| 432 |
be preferred for |
| 433 |
indicating a constant whose value is a Perl C<undef>. |
| 434 |
|
| 435 |
=head2 Explicit resolution |
| 436 |
|
| 437 |
The recognizer's C<closures> named argument |
| 438 |
allows the user to directly control the mapping from action names |
| 439 |
to actions. |
| 440 |
The value of the C<closures> named argument |
| 441 |
is a reference to a hash whose keys are |
| 442 |
action names and whose hash values are references. |
| 443 |
Typically (but not always) these will be C<CODE> refs. |
| 444 |
|
| 445 |
If an action name is the key of an entry in the C<closures> hash, |
| 446 |
it resolves to the closure referenced by the value part of that hash entry. |
| 447 |
Resolution via the C<closures> named argument is |
| 448 |
called B<explicit resolution>. |
| 449 |
|
| 450 |
When explicit resolution is the only kind of resolution that is wanted, |
| 451 |
it is best to pick a name that is very unlikely to be the name |
| 452 |
of a Perl object. |
| 453 |
Many of |
| 454 |
L<Marpa::HTML>'s action names |
| 455 |
are intended for explicit resolution only. |
| 456 |
In L<Marpa::HTML> those action names |
| 457 |
begin with |
| 458 |
an exclamation mark ("!"), |
| 459 |
and that convention is recommended. |
| 460 |
|
| 461 |
=head2 Fully qualified action names |
| 462 |
|
| 463 |
If explicit resolution fails, |
| 464 |
Marpa transforms the action name into a |
| 465 |
B<fully qualified> Perl name. |
| 466 |
An action name that |
| 467 |
contains a double colon ("C<::>") or a single quote ("C<'>") |
| 468 |
is considered to be a fully qualified name. |
| 469 |
Any other action name is considered to be a B<bare action name>. |
| 470 |
|
| 471 |
If the action name to be resolved is already a fully qualified name, |
| 472 |
it is not further transformed. |
| 473 |
It will be resolved in the form it was received, |
| 474 |
or not at all. |
| 475 |
|
| 476 |
For bare action names, |
| 477 |
Marpa tries to qualify them by adding a package name. |
| 478 |
If the C<actions> grammar named argument is defined, |
| 479 |
Marpa uses it as the package name. |
| 480 |
Otherwise, |
| 481 |
if the |
| 482 |
C<action_object> grammar named argument is defined, |
| 483 |
Marpa uses it as the package name. |
| 484 |
Once Marpa has fully qualified the action name, |
| 485 |
Marpa looks for a Perl object with that name. |
| 486 |
|
| 487 |
Marpa will not attempt to resolve an action name |
| 488 |
that it cannot fully qualify. |
| 489 |
This implies that, |
| 490 |
for an action name to resolve successfully, |
| 491 |
one of these five things must be the case: |
| 492 |
|
| 493 |
=over |
| 494 |
|
| 495 |
=item * The action name is one of the reserved action names. |
| 496 |
|
| 497 |
=item * The action name resolves explicitly. |
| 498 |
|
| 499 |
=item * The action name is fully qualified to begin with. |
| 500 |
|
| 501 |
=item * The C<actions> named argument is defined. |
| 502 |
|
| 503 |
=item * The C<action_object> named argument is defined. |
| 504 |
|
| 505 |
=back |
| 506 |
|
| 507 |
Marpa's philosophy |
| 508 |
is to require that the programmer be specific about action names. |
| 509 |
This can be an inconvenience, but Marpa prefers this to |
| 510 |
silently executing unintended code. |
| 511 |
|
| 512 |
If the user wants to leave the |
| 513 |
rule evaluation closures in the C<main> namespace, |
| 514 |
she can specify |
| 515 |
C<"main"> |
| 516 |
as the value of the C<actions> named argument. |
| 517 |
But |
| 518 |
it can be good practice to keep |
| 519 |
the rule evaluation closures |
| 520 |
in their own namespace, |
| 521 |
particularly if the application is not small. |
| 522 |
|
| 523 |
=head2 Types of Perl actions |
| 524 |
|
| 525 |
Actions resolve in three ways: |
| 526 |
to reserved actions, to Perl rule evaluation closures and to Perl variable actions. |
| 527 |
The following are tried, in order. |
| 528 |
|
| 529 |
=over 4 |
| 530 |
|
| 531 |
=item * |
| 532 |
If an action name begins with a double colon ("C<::>"), |
| 533 |
it will resolve to a reserved action, or not at all. |
| 534 |
|
| 535 |
=item * |
| 536 |
If the fully qualified form of an action name is the name of a Perl |
| 537 |
subroutine in the symbol table, |
| 538 |
the action name resolves to the Perl subroutine. |
| 539 |
That subroutine then becomes a Perl rule evaluation closure. |
| 540 |
|
| 541 |
=item * |
| 542 |
If the fully qualified form of an action name is the name of a Perl |
| 543 |
scalar variable in the symbol table, |
| 544 |
the action name resolves to the Perl variable. |
| 545 |
Note that, for this purpose, a Perl reference variable is considered as one type of Perl scalar. |
| 546 |
|
| 547 |
=item * |
| 548 |
Other kinds of Perl objects in the symbol table that match the fully qualified name, |
| 549 |
such as arrays and hashes, are ignored. |
| 550 |
Note that, while resolution to arrays and hashes is not allowed, |
| 551 |
resolution to references to arrays and hashes is permitted. |
| 552 |
|
| 553 |
=back |
| 554 |
|
| 555 |
Resolution to a |
| 556 |
Perl rule evaluation closure |
| 557 |
or to a Perl variable |
| 558 |
may came from explicit resolution. |
| 559 |
Explicit resolution always takes place via a reference, |
| 560 |
and requires an extra level of indirection. |
| 561 |
For resolution to a rule evaluation closure, |
| 562 |
the closure must be provided in the form of a reference to the closure. |
| 563 |
For resolution to a Perl variable, |
| 564 |
the variable has to be provided in the form of a reference to the variable. |
| 565 |
If the Perl variable is a reference, that means adding another level of |
| 566 |
indirection. |
| 567 |
|
| 568 |
=head2 Modifying Perl variable actions |
| 569 |
|
| 570 |
When resolution is to a |
| 571 |
Perl variable, |
| 572 |
it is possible to modify the value of the variable. |
| 573 |
In practice, this will usually be a bad idea. |
| 574 |
The Perl variable reference actions should be treated |
| 575 |
as read-only constants, and never modified. |
| 576 |
|
| 577 |
This is because multiple resolutions |
| 578 |
to a Perl variable will |
| 579 |
always point to the same contents. |
| 580 |
Any modification to those contents will be |
| 581 |
seen by other users of that Perl variable. |
| 582 |
In other words, the modification will have global |
| 583 |
effect. |
| 584 |
For this reason modifying the referents of |
| 585 |
reference actions is almost always bad practice |
| 586 |
at the least, and is often an error. |
| 587 |
|
| 588 |
For example, |
| 589 |
assume that actions are in a package named C<My_Nodes>, |
| 590 |
which contains a hash reference named C<empty_hash>, |
| 591 |
|
| 592 |
=for Marpa::R2::Display |
| 593 |
ignore: 1 |
| 594 |
|
| 595 |
package My_Nodes; |
| 596 |
our $empty_hash = {}; |
| 597 |
|
| 598 |
=for Marpa::R2::Display::End |
| 599 |
|
| 600 |
It can be tempting, in building objects which are hashes, |
| 601 |
to start with a leaf node whose action is C<empty_hash> |
| 602 |
and to add contents to it as the object is passed up the evaluation |
| 603 |
tree. |
| 604 |
But C<$empty_hash> points to a single hash object. |
| 605 |
This single hash object will shared by all nodes, |
| 606 |
with all nodes seeing each other's changes. |
| 607 |
Worse, all Marpa parsers which use the same C<My_Nodes> |
| 608 |
namespace will share the same hash object. |
| 609 |
An application which needs an action which produces |
| 610 |
an empty hash should have the action resolve to a Perl rule |
| 611 |
evaluation closure |
| 612 |
that returns C<{}>. |
| 613 |
|
| 614 |
=head2 Visibility and resolution |
| 615 |
|
| 616 |
When Perl closures and variables are used for the semantics, |
| 617 |
they must be visible in the scope where the semantics are |
| 618 |
B<resolved>. |
| 619 |
The action names are usually B<specified> with the grammar, |
| 620 |
but action B<resolution> takes place |
| 621 |
in the recognizer's |
| 622 |
L<C<value>|Marpa::R2::Deprecated::NAIF::Recognizer/"value()"> method. |
| 623 |
This can sometimes be a source of confusion. |
| 624 |
For example, if a Perl closure is visible when the |
| 625 |
action is specified, |
| 626 |
but goes out of scope before the action name is resolved, |
| 627 |
resolution will fail. |
| 628 |
|
| 629 |
=head1 The per-parse-tree variable |
| 630 |
|
| 631 |
In the Tree Setup Phase, |
| 632 |
Marpa creates a per-parse-tree variable. |
| 633 |
This becomes the first argument of the rule evaluation closures for |
| 634 |
the rule nodes. |
| 635 |
If the grammar's C<action_object> named argument is not defined, |
| 636 |
the per-parse-tree variable is initialized to an empty hash ref. |
| 637 |
|
| 638 |
Most data for |
| 639 |
the value actions of the rules |
| 640 |
will be passed up the parse tree. |
| 641 |
The actions will see the values of the rule node's child nodes |
| 642 |
as arguments, |
| 643 |
and will return their own value to be seen as an argument |
| 644 |
by their parent node. |
| 645 |
The per-parse-tree variable can be used for data which does not |
| 646 |
conveniently fit this model. |
| 647 |
|
| 648 |
The lifetime of the per-parse-tree variable |
| 649 |
extends into the Tree Traversal Phase. |
| 650 |
During the Tree Traversal Phase, |
| 651 |
Marpa's internals never alter the per-parse-tree variable -- |
| 652 |
it is reserved for use by the application. |
| 653 |
|
| 654 |
=head2 Action object constructor |
| 655 |
|
| 656 |
If the grammar's C<action_object> named argument has a defined value, |
| 657 |
that value is treated as the name of a class. |
| 658 |
The B<action object constructor> is |
| 659 |
the C<new> method |
| 660 |
in the C<action_object> class. |
| 661 |
|
| 662 |
The action object constructor is called |
| 663 |
in the Tree Setup Phase. |
| 664 |
The return value of the |
| 665 |
action object constructor becomes the per-parse-tree variable. |
| 666 |
It is a fatal error if the |
| 667 |
grammar's C<action_object> named argument is defined, |
| 668 |
but does not name a class with a C<new> method. |
| 669 |
|
| 670 |
Resolution of the action object constructor is |
| 671 |
resolution of the B<action object constructor name>. |
| 672 |
The action object constructor name is |
| 673 |
formed by concatenating |
| 674 |
the literal string "C<::new>" to |
| 675 |
the value of the C<action_object> named argument. |
| 676 |
|
| 677 |
All standard rules apply when resolving the action |
| 678 |
object constructor name. |
| 679 |
In particular, bypass via |
| 680 |
explicit resolution applies to |
| 681 |
the action object constructor. |
| 682 |
If the action object constructor name is |
| 683 |
a hash key in the |
| 684 |
evaluator's C<closures> named argument, |
| 685 |
then |
| 686 |
the value referred to by |
| 687 |
that hash entry becomes the |
| 688 |
action object constructor. |
| 689 |
|
| 690 |
If a grammar has both the C<actions> and the |
| 691 |
C<action_object> named arguments defined, |
| 692 |
all action names B<except> |
| 693 |
for the action object constructor will be |
| 694 |
resolved in the C<actions> package or not at all. |
| 695 |
The action object constructor is always in |
| 696 |
the C<action_object> class, if it is anywhere. |
| 697 |
|
| 698 |
=head1 Parse order |
| 699 |
|
| 700 |
If a parse is ambiguous, all parses are returned, |
| 701 |
with no duplication. |
| 702 |
By default, the order is arbitrary, but |
| 703 |
it is also possible to control the order. |
| 704 |
Details are in L<the document |
| 705 |
on parse order|Marpa::R2::Deprecated::NAIF::Semantics::Order>. |
| 706 |
|
| 707 |
=head1 Infinite loops |
| 708 |
|
| 709 |
Grammars with infinite loops (cycles) |
| 710 |
are generally regarded as useless in practical applications, |
| 711 |
but Marpa allows them. |
| 712 |
Marpa can accurately |
| 713 |
claim to support B<every grammar> that can be written in BNF. |
| 714 |
|
| 715 |
If a grammar with cycles is ambiguous, |
| 716 |
it can produce cycle-free parses |
| 717 |
and parses with finite-length cycles, |
| 718 |
as well as parses with infinite length cycles. |
| 719 |
Marpa will parse with grammars that contain cycles, |
| 720 |
and Marpa's evaluator will iterate through |
| 721 |
the values from the grammar's |
| 722 |
cycle-free parses. |
| 723 |
For those who want to know more, |
| 724 |
the details are in a L<separate |
| 725 |
document|Marpa::R2::Deprecated::NAIF::Semantics::Infinite>. |
| 726 |
|
| 727 |
=head1 Copyright and License |
| 728 |
|
| 729 |
=for Marpa::R2::Display |
| 730 |
ignore: 1 |
| 731 |
|
| 732 |
Copyright 2022 Jeffrey Kegler |
| 733 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 734 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 735 |
General Public License as published by the Free Software Foundation, |
| 736 |
either version 3 of the License, or (at your option) any later version. |
| 737 |
|
| 738 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 739 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 740 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 741 |
Lesser General Public License for more details. |
| 742 |
|
| 743 |
You should have received a copy of the GNU Lesser |
| 744 |
General Public License along with Marpa::R2. If not, see |
| 745 |
http://www.gnu.org/licenses/. |
| 746 |
|
| 747 |
=for Marpa::R2::Display::End |
| 748 |
|
| 749 |
=cut |
| 750 |
|
| 751 |
# Local Variables: |
| 752 |
# mode: cperl |
| 753 |
# cperl-indent-level: 4 |
| 754 |
# fill-column: 100 |
| 755 |
# End: |
| 756 |
# vim: expandtab shiftwidth=4: |