| 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::Event - SLIF parse events |
| 19 |
|
| 20 |
=head1 Synopsis |
| 21 |
|
| 22 |
=for Marpa::R2::Display |
| 23 |
name: SLIF Event synopsis |
| 24 |
normalize-whitespace: 1 |
| 25 |
partial: 1 |
| 26 |
|
| 27 |
my $input = q{a b c "insert d here" e e f h}; |
| 28 |
my $length = length $input; |
| 29 |
my $pos = $slr->read( \$input ); |
| 30 |
|
| 31 |
my $actual_events = q{}; |
| 32 |
|
| 33 |
READ: while (1) { |
| 34 |
|
| 35 |
my @actual_events = (); |
| 36 |
|
| 37 |
my $next_lexeme; |
| 38 |
EVENT: |
| 39 |
for my $event ( @{ $slr->events() } ) { |
| 40 |
my ($name) = @{$event}; |
| 41 |
|
| 42 |
=for Marpa::R2::Display::End |
| 43 |
|
| 44 |
=for Marpa::R2::Display |
| 45 |
name: SLIF Event synopsis |
| 46 |
normalize-whitespace: 1 |
| 47 |
partial: 1 |
| 48 |
|
| 49 |
push @actual_events, $name; |
| 50 |
} |
| 51 |
|
| 52 |
if (@actual_events) { |
| 53 |
$actual_events .= join q{ }, "Events at position $pos:", @actual_events; |
| 54 |
$actual_events .= "\n"; |
| 55 |
} |
| 56 |
|
| 57 |
=for Marpa::R2::Display::End |
| 58 |
|
| 59 |
=for Marpa::R2::Display |
| 60 |
name: SLIF Event synopsis |
| 61 |
normalize-whitespace: 1 |
| 62 |
partial: 1 |
| 63 |
|
| 64 |
if ($pos < $length) { |
| 65 |
$pos = $slr->resume(); |
| 66 |
next READ; |
| 67 |
} |
| 68 |
last READ; |
| 69 |
} ## end READ: while (1) |
| 70 |
|
| 71 |
=for Marpa::R2::Display::End |
| 72 |
|
| 73 |
The synopsis is extracted from an example given in full |
| 74 |
L<below|/"An example">. |
| 75 |
|
| 76 |
=head1 About this document |
| 77 |
|
| 78 |
This document is an overview of B<SLIF parse events>. |
| 79 |
SLIF parse events |
| 80 |
B<trigger> based on conditions declared in the DSL. |
| 81 |
Typical events are the prediction or recognition |
| 82 |
of a symbol. |
| 83 |
|
| 84 |
SLIF parse events are often used to allow an application to |
| 85 |
switch over to its own custom procedural logic. |
| 86 |
Among other things, |
| 87 |
an application can do its own "external" scanning of lexemes. |
| 88 |
An application may ask Marpa to resume internal scanning at |
| 89 |
any point. |
| 90 |
|
| 91 |
SLIF parse events may be named or unnamed. |
| 92 |
Use of unnamed events is discouraged, and should be reserved for legacy |
| 93 |
code. |
| 94 |
New applications should only use named events. |
| 95 |
When not otherwise specified, |
| 96 |
this document is talking about named events. |
| 97 |
Unnamed events are described |
| 98 |
L<below, |
| 99 |
in a section dedicated to them|/"Unnamed events">. |
| 100 |
|
| 101 |
=head1 Terminology |
| 102 |
|
| 103 |
SLIF parse events are called B<parse events> |
| 104 |
or simply B<events>, |
| 105 |
in contexts where the meaning is clear. |
| 106 |
|
| 107 |
SLIF parse events evolved over time from simpler |
| 108 |
mechanisms, |
| 109 |
and the term SLIF parse event was introduced late |
| 110 |
in the development of Marpa::R2. |
| 111 |
In previous versions of Marpa::R2, |
| 112 |
SLIF parse events and their precursors are called "pauses" or simply "events". |
| 113 |
For historical reasons, |
| 114 |
some of the method names dealing with SLIF parse events |
| 115 |
still have the word "pause" as part of their name. |
| 116 |
|
| 117 |
In this document, an B<instance> of a symbol |
| 118 |
in a parse |
| 119 |
means an occurrence |
| 120 |
of the symbol in that parse |
| 121 |
with a specific start location and length. |
| 122 |
An instance of a symbol is also called |
| 123 |
a B<symbol instance>. |
| 124 |
A consequence of this definition |
| 125 |
is that every symbol instance |
| 126 |
has exactly one end location. |
| 127 |
|
| 128 |
For some string and grammar, |
| 129 |
we say that a parse is B<valid> |
| 130 |
if the parse of the string |
| 131 |
is valid according to the grammar. |
| 132 |
In a parse, |
| 133 |
a nulled symbol instance, |
| 134 |
or B<nulled symbol>, is a symbol instance with a length of zero. |
| 135 |
A non-nulled symbol instance, non-nulled instance, |
| 136 |
or B<non-nulled symbol>, is a symbol instance which is not |
| 137 |
nulled. |
| 138 |
A symbol in a grammar is |
| 139 |
a B<nullable symbol> if it has |
| 140 |
at least one nulled instance |
| 141 |
in at least one valid parse of at least one string. |
| 142 |
A symbol in a grammar is |
| 143 |
a B<nulling symbol> if, |
| 144 |
for all strings, |
| 145 |
all of its instances in valid parses are nulled instances. |
| 146 |
A symbol is B<non-nulling> if it is not nulling. |
| 147 |
|
| 148 |
Intuitively, a string is actual to I<L> if we |
| 149 |
know the actual input as far as I<L>, |
| 150 |
and the string is consistent with the input. |
| 151 |
More formally, |
| 152 |
let C<A> be the actual input to a parse. |
| 153 |
Let C<S> be an arbitrary string in the same alphabet. |
| 154 |
Let C<prefix(A, L)> be the prefix of length I<L> |
| 155 |
that is of length C<L>, |
| 156 |
and let C<prefix(S, L)> be defined in the same way. |
| 157 |
If C<prefix(A, L) = prefix(S, L)>, |
| 158 |
then we say that |
| 159 |
string C<S> is B<consistent with |
| 160 |
the actual input up to location L>. |
| 161 |
For brevity, |
| 162 |
we will often say |
| 163 |
that C<S> is B<actual to L>. |
| 164 |
|
| 165 |
If an instance of symbol I<S> |
| 166 |
that starts at location I<L> is in a valid parse |
| 167 |
of some string actual to I<L>, |
| 168 |
we say that a symbol I<S> is B<acceptable> at |
| 169 |
a location I<L>. |
| 170 |
We say that a symbol |
| 171 |
is B<acceptable> at |
| 172 |
a location I<L> if it is the symbol of |
| 173 |
a symbol instance acceptable at location I<L>. |
| 174 |
|
| 175 |
If an instance of symbol I<S> |
| 176 |
that ends at location I<L> is in a valid parse |
| 177 |
of some string actual to I<L>, |
| 178 |
we say that a symbol I<S> is B<recognized> at |
| 179 |
a location I<L>. |
| 180 |
We say that a symbol |
| 181 |
is B<recognized> at |
| 182 |
a location I<L> if it is the symbol of |
| 183 |
a symbol instance recognized at location I<L>. |
| 184 |
|
| 185 |
=head1 The life cycle of events |
| 186 |
|
| 187 |
=over 4 |
| 188 |
|
| 189 |
=item * An SLIF parse event must be B<declared>. |
| 190 |
|
| 191 |
=item * A declared event may B<trigger>. |
| 192 |
|
| 193 |
=item * Once an event triggers, it may be B<accessed>. |
| 194 |
|
| 195 |
=back |
| 196 |
|
| 197 |
Parse events are declared in the SLIF DSL. |
| 198 |
A parse event is either a lexeme event |
| 199 |
or a non-lexeme event. |
| 200 |
A lexeme event is declared using a |
| 201 |
L<C<:lexeme> pseudo-rule|Marpa::R2::Scanless::DSL/"Lexeme pseudo-rule">. |
| 202 |
A non-lexeme event is declared using a |
| 203 |
L<named event statement|Marpa::R2::Scanless::DSL/"Named event statement">. |
| 204 |
The various types of parse events are described in detail |
| 205 |
L<below|/"Types of parse event">. |
| 206 |
The description of |
| 207 |
each type of parse event |
| 208 |
will state whether it is a lexeme or a non-lexeme event. |
| 209 |
|
| 210 |
Once declared, a parse event |
| 211 |
may trigger during any event-triggering SLIF |
| 212 |
recognizer method. |
| 213 |
The event-triggering SLIF recognizer methods are |
| 214 |
L<C<read()>|Marpa::R2::Scanless::R/"read()">, |
| 215 |
L<C<resume()>|Marpa::R2::Scanless::R/"resume()">, |
| 216 |
L<C<lexeme_read()>|Marpa::R2::Scanless::R/"lexeme_read()"> and |
| 217 |
L<C<lexeme_complete()>|Marpa::R2::Scanless::R/"lexeme_complete()">. |
| 218 |
|
| 219 |
The location at which a parse event triggers is the B<event location>. |
| 220 |
An event may trigger at any location, including location 0. |
| 221 |
When an event triggers, it causes the event-triggering |
| 222 |
method to return immediately, with the current location |
| 223 |
at the B<trigger location>. |
| 224 |
The trigger location is the same as the event location, |
| 225 |
except in the case of |
| 226 |
L<pre-lexeme events|/"Pre-lexeme events">. |
| 227 |
|
| 228 |
A non-lexeme event may trigger during any of the event-triggering |
| 229 |
methods. |
| 230 |
A lexeme event will only trigger during calls of the |
| 231 |
C<< $slr->read() >> and C<< $slr->resume() >> methods. |
| 232 |
|
| 233 |
The triggering of events may be controlled with |
| 234 |
the L<activate() method|Marpa::R2::Scanless::R/"activate()">. |
| 235 |
An event will only trigger if activated. |
| 236 |
All events are automatically activated when declared. |
| 237 |
|
| 238 |
Events may be accessed using |
| 239 |
L<the Scanless recognizer's events() |
| 240 |
method|Marpa::R2::Scanless::R/"events()">. |
| 241 |
The beginning and end of the lexeme triggering a lexeme event |
| 242 |
may be found using |
| 243 |
L<the Scanless recognizer's pause_span() |
| 244 |
method|Marpa::R2::Scanless::R/"pause_span()">. |
| 245 |
|
| 246 |
=head1 Types of parse event |
| 247 |
|
| 248 |
=head2 Completion events |
| 249 |
|
| 250 |
Completion events are declared in the SLIF DSL |
| 251 |
using the |
| 252 |
L<named event statement|Marpa::R2::Scanless::DSL/"Named event statement">: |
| 253 |
|
| 254 |
=for Marpa::R2::Display |
| 255 |
name: SLIF completed event statement synopsis |
| 256 |
partial: 1 |
| 257 |
normalize-whitespace: 1 |
| 258 |
|
| 259 |
event 'a' = completed A |
| 260 |
event 'b'=off = completed B |
| 261 |
event 'c'=on = completed C |
| 262 |
event 'd' = completed D |
| 263 |
|
| 264 |
=for Marpa::R2::Display::End |
| 265 |
|
| 266 |
A completion SLIF parse event can be specified for any |
| 267 |
symbol that is not a lexeme. |
| 268 |
Completion events are non-lexeme events. |
| 269 |
A completion event triggers |
| 270 |
whenever a non-nulled instance of its symbol |
| 271 |
is recognized at the current location. |
| 272 |
|
| 273 |
When a completion event triggers, |
| 274 |
its trigger location and its event location |
| 275 |
are set to the current location, |
| 276 |
which will be the end location |
| 277 |
of the instance that triggered the event. |
| 278 |
The event is called a "completion" |
| 279 |
because, at the trigger location, |
| 280 |
the recognition of its symbol |
| 281 |
is "complete". |
| 282 |
|
| 283 |
In the SLIF parse event descriptor returned |
| 284 |
by the |
| 285 |
L<the C<< $slr->events() >> |
| 286 |
method|Marpa::R2::Scanless::R/"events()">, |
| 287 |
the name of completed event is the only element. |
| 288 |
|
| 289 |
=head2 Discard events |
| 290 |
|
| 291 |
=for Marpa::R2::Display |
| 292 |
name: SLIF discard event statement synopsis 2 |
| 293 |
partial: 1 |
| 294 |
normalize-whitespace: 1 |
| 295 |
|
| 296 |
:discard ~ ws event => ws |
| 297 |
ws ~ [\s]+ |
| 298 |
:discard ~ [,] event => comma=off |
| 299 |
:discard ~ [;] event => 'semicolon'=on |
| 300 |
:discard ~ [.] event => period |
| 301 |
|
| 302 |
=for Marpa::R2::Display::End |
| 303 |
|
| 304 |
Discard events are specified in |
| 305 |
L<discard pseudo-rules|Marpa::R2::Scanless::DSL/"Discard pseudo-rule">. |
| 306 |
They are non-lexeme events. |
| 307 |
This may seem counter-intuitive, |
| 308 |
but a lexeme must be a symbol visible to the G1 |
| 309 |
grammar and discarded symbols |
| 310 |
are discarded |
| 311 |
before the G1 grammar can see them. |
| 312 |
|
| 313 |
When a discard event triggers, its trigger location and its event location are set to the current location. |
| 314 |
This will be the end location of the discarded text. |
| 315 |
|
| 316 |
In the SLIF parse event descriptor returned |
| 317 |
by the |
| 318 |
L<the C<< $slr->events() >> |
| 319 |
method|Marpa::R2::Scanless::R/"events()">, |
| 320 |
there will be 4 elements: |
| 321 |
|
| 322 |
=over 4 |
| 323 |
|
| 324 |
=item * |
| 325 |
|
| 326 |
The name of the discard event. |
| 327 |
|
| 328 |
=item * |
| 329 |
|
| 330 |
The start location of the discarded text, |
| 331 |
as an offset in the physical input stream. |
| 332 |
|
| 333 |
=item * |
| 334 |
|
| 335 |
The end location of the discarded text, |
| 336 |
as an offset in the physical input stream. |
| 337 |
|
| 338 |
=item * |
| 339 |
|
| 340 |
The G1 location of the last lexeme read. |
| 341 |
|
| 342 |
=back |
| 343 |
|
| 344 |
An intended purpose of the G1 location is to allow |
| 345 |
the synchronization of data taken from a series of discard events, |
| 346 |
with data taken from a parse tree. |
| 347 |
Physical input stream locations can often |
| 348 |
be used for this purpose, |
| 349 |
but an application is allowed to move around |
| 350 |
in the physical input stream. |
| 351 |
If an application does not move monotonically |
| 352 |
through the physical input stream, |
| 353 |
physical input stream locations will not |
| 354 |
necessarily indicate the order from the point |
| 355 |
of view of the parse tree, |
| 356 |
and of the virtual input stream. |
| 357 |
G1 locations are always in left-to-right order |
| 358 |
from the point of view of parse tree, |
| 359 |
and of the virtual input stream on which it is based. |
| 360 |
|
| 361 |
Since discarded text is not seen by G1, |
| 362 |
it does not really have a G1 location, so the G1 location |
| 363 |
reported with the event |
| 364 |
is that of the last lexeme read. |
| 365 |
All lexemes have G1 locations. |
| 366 |
If the discarded text is at the beginning of the parse, |
| 367 |
before any lexemes have been read, |
| 368 |
the G1 location is reported as zero. |
| 369 |
|
| 370 |
=head2 Nulling events |
| 371 |
|
| 372 |
A nulling event is declared in the SLIF DSL |
| 373 |
using the |
| 374 |
L<named event statement|Marpa::R2::Scanless::DSL/"Named event statement">: |
| 375 |
|
| 376 |
=for Marpa::R2::Display |
| 377 |
name: SLIF nulled event statement synopsis |
| 378 |
partial: 1 |
| 379 |
normalize-whitespace: 1 |
| 380 |
|
| 381 |
event '!a' = nulled A |
| 382 |
event '!b'=off = nulled B |
| 383 |
event '!c'=on = nulled C |
| 384 |
event '!d' = nulled D |
| 385 |
|
| 386 |
=for Marpa::R2::Display::End |
| 387 |
|
| 388 |
A nulling SLIF parse event occurs whenever a nulled instance |
| 389 |
of its symbol is recognized at the current location. |
| 390 |
When a nulled event triggers, |
| 391 |
its trigger location and its event location |
| 392 |
are set to the current location, |
| 393 |
which will be |
| 394 |
the location where the triggering instance both begins and ends. |
| 395 |
|
| 396 |
A nulling event is a non-lexeme event. |
| 397 |
A nulling SLIF parse event can be specifed for any |
| 398 |
symbol that is not a lexeme. |
| 399 |
A nulled symbol may derive other null symbols, |
| 400 |
producing one or more nulled trees; |
| 401 |
because a null derivation may be ambiguous, |
| 402 |
a nulled symbol may derive more than one nulled |
| 403 |
tree. |
| 404 |
A set of one or more nulled trees |
| 405 |
is called a nulled forest. |
| 406 |
|
| 407 |
When a nulling event triggers for a symbol instance, |
| 408 |
all activated nulling events declared |
| 409 |
for symbols derived |
| 410 |
from the triggered symbol instance will |
| 411 |
also trigger. |
| 412 |
The triggering of nulling events is recursive, |
| 413 |
so that when a nulled symbol instance |
| 414 |
triggers an event, it triggers all the events |
| 415 |
in the nulled forest derived |
| 416 |
from the triggering symbol instance. |
| 417 |
Nulled forests are described in more detail |
| 418 |
in L<a separate |
| 419 |
section|"Nulled forests">. |
| 420 |
|
| 421 |
In the SLIF parse event descriptor returned |
| 422 |
by the |
| 423 |
L<the C<< $slr->events() >> |
| 424 |
method|Marpa::R2::Scanless::R/"events()">, |
| 425 |
the name of nulling event is the only element. |
| 426 |
|
| 427 |
=head2 Prediction events |
| 428 |
|
| 429 |
A prediction event is declared in the SLIF DSL |
| 430 |
using the |
| 431 |
L<named event statement|Marpa::R2::Scanless::DSL/"Named event statement">: |
| 432 |
|
| 433 |
=for Marpa::R2::Display |
| 434 |
name: SLIF predicted event statement synopsis |
| 435 |
partial: 1 |
| 436 |
normalize-whitespace: 1 |
| 437 |
|
| 438 |
event '^a' = predicted A |
| 439 |
|
| 440 |
=for Marpa::R2::Display::End |
| 441 |
|
| 442 |
A prediction event triggers whenever |
| 443 |
a non-nulling symbol is acceptable at the current location. |
| 444 |
When a prediction event triggers, |
| 445 |
its trigger location and its event location |
| 446 |
are set to the current location. |
| 447 |
A prediction may not result in an actual instance of the symbol, |
| 448 |
but no actual symbol instance can start |
| 449 |
at the event location unless a prediction, |
| 450 |
if properly declared and activated, |
| 451 |
would trigger at that location. |
| 452 |
|
| 453 |
Prediction SLIF parse events may be defined for any symbol, |
| 454 |
whether it is a lexeme or not. |
| 455 |
But prediction events are non-lexeme events, |
| 456 |
even when their symbol is a lexeme. |
| 457 |
|
| 458 |
In the SLIF parse event descriptor returned |
| 459 |
by the |
| 460 |
L<the C<< $slr->events() >> |
| 461 |
method|Marpa::R2::Scanless::R/"events()">, |
| 462 |
the name of prediction event is the only element. |
| 463 |
|
| 464 |
=head2 Post-lexeme events |
| 465 |
|
| 466 |
=for Marpa::R2::Display |
| 467 |
name: SLIF Event synopsis |
| 468 |
partial: 1 |
| 469 |
normalize-whitespace: 1 |
| 470 |
|
| 471 |
:lexeme ~ <a> pause => after event => '"a"' |
| 472 |
|
| 473 |
=for Marpa::R2::Display::End |
| 474 |
|
| 475 |
A post-lexeme event is a lexeme event. |
| 476 |
It triggers if the lexeme is scanned at the current location. |
| 477 |
The SLIF recognizer will have |
| 478 |
already read the lexeme |
| 479 |
when its post-lexeme event triggers. |
| 480 |
|
| 481 |
When a post-lexeme event triggers, |
| 482 |
its trigger location and its event location |
| 483 |
are set to the current location, |
| 484 |
which will also be the location where the lexeme ends. |
| 485 |
A post-lexeme event also sets the |
| 486 |
L<C<pause span> and |
| 487 |
C<pause lexeme>|"Pause span and pause lexeme">. |
| 488 |
Post-lexeme events which trigger during |
| 489 |
C<< $slr->lexeme_complete() >> and |
| 490 |
C<< $slr->lexeme_read() >> calls are discarded. |
| 491 |
|
| 492 |
In the SLIF parse event descriptor returned |
| 493 |
by the |
| 494 |
L<the C<< $slr->events() >> |
| 495 |
method|Marpa::R2::Scanless::R/"events()">, |
| 496 |
the name of post-lexeme event is the only element. |
| 497 |
|
| 498 |
=head2 Pre-lexeme events |
| 499 |
|
| 500 |
=for Marpa::R2::Display |
| 501 |
name: SLIF Event synopsis |
| 502 |
partial: 1 |
| 503 |
normalize-whitespace: 1 |
| 504 |
|
| 505 |
:lexeme ~ <insert d> pause => before event => 'insert d' |
| 506 |
|
| 507 |
=for Marpa::R2::Display::End |
| 508 |
|
| 509 |
A pre-lexeme event is a lexeme event. |
| 510 |
It triggers if the lexeme is scanned at the current location. |
| 511 |
When a pre-lexeme event triggers, |
| 512 |
its event location |
| 513 |
is set to the current location. |
| 514 |
Its trigger location is set to the location where the lexeme starts, |
| 515 |
which will be before the event location. |
| 516 |
A pre-lexeme event also sets the |
| 517 |
L<C<pause span> and |
| 518 |
C<pause lexeme>|"Pause span and pause lexeme">. |
| 519 |
|
| 520 |
The SLIF recognizer will B<not> have |
| 521 |
read the lexeme |
| 522 |
when its pre-lexeme event triggers. |
| 523 |
In effect, it "rewinds" the scanning. |
| 524 |
|
| 525 |
For most events, the trigger location is the current location, |
| 526 |
but pre-lexeme events are the exception. |
| 527 |
Its setting of the trigger location to the start of the lexeme |
| 528 |
is consistent with the pre-lexeme event's behavior as a "rewind". |
| 529 |
An intended use of pre-lexeme events |
| 530 |
is catching a lexeme which |
| 531 |
is about to be read, and giving it special treatment. |
| 532 |
For more on this, see |
| 533 |
L<below|"External scanning">. |
| 534 |
Pre-lexeme events which trigger during |
| 535 |
C<< $slr->lexeme_complete() >> and |
| 536 |
C<< $slr->lexeme_read() >> calls are discarded. |
| 537 |
|
| 538 |
There is a lot of similarity |
| 539 |
between pre-lexeme events and predictions, |
| 540 |
but there are also very important differences. |
| 541 |
|
| 542 |
=over 4 |
| 543 |
|
| 544 |
=item * |
| 545 |
|
| 546 |
A pre-lexeme event does not occur |
| 547 |
unless triggering lexeme is actually |
| 548 |
found in the input. |
| 549 |
On the other hand, |
| 550 |
a prediction event is, |
| 551 |
as the name suggests, only a prediction -- |
| 552 |
the triggering lexeme may never |
| 553 |
actually be found in the input. |
| 554 |
|
| 555 |
=item * |
| 556 |
|
| 557 |
Even though they have the same |
| 558 |
trigger location, |
| 559 |
pre-lexeme and prediction events |
| 560 |
do not occur at the same time, |
| 561 |
because pre-lexeme events |
| 562 |
require a scan of the lexeme, |
| 563 |
while prediction events do not. |
| 564 |
If both |
| 565 |
are defined for a symbol, |
| 566 |
the prediction event will trigger first, |
| 567 |
B<before> the lexeme is scanned. |
| 568 |
The pre-lexeme event will trigger next, |
| 569 |
B<after> the lexeme is scanned. |
| 570 |
|
| 571 |
=item * |
| 572 |
|
| 573 |
Pre-lexeme events can be defined only |
| 574 |
for lexemes. |
| 575 |
Prediction events can be defined for any |
| 576 |
symbol. |
| 577 |
|
| 578 |
=item * |
| 579 |
|
| 580 |
Prediction events will occur together |
| 581 |
with the other events expected at the trigger location. |
| 582 |
Pre-lexeme events will not, |
| 583 |
because they do not happen at the trigger location -- |
| 584 |
the trigger location is moved back to the |
| 585 |
start of the lexeme as a convenience. |
| 586 |
|
| 587 |
=back |
| 588 |
|
| 589 |
In the SLIF parse event descriptor returned |
| 590 |
by the |
| 591 |
L<the C<< $slr->events() >> |
| 592 |
method|Marpa::R2::Scanless::R/"events()">, |
| 593 |
the name of pre-lexeme event is the only element. |
| 594 |
|
| 595 |
=head2 Exhaustion events |
| 596 |
|
| 597 |
=for Marpa::R2::Display |
| 598 |
name: SLIF exhaustion recognizer setting synopsis |
| 599 |
partial: 1 |
| 600 |
normalize-whitespace: 1 |
| 601 |
|
| 602 |
my @shortest_span = (); |
| 603 |
my $recce = Marpa::R2::Scanless::R->new( |
| 604 |
{ grammar => $g, |
| 605 |
exhaustion => 'event', |
| 606 |
}, |
| 607 |
$recce_debug_args |
| 608 |
); |
| 609 |
my $pos = $recce->read( \$string, $target_start ); |
| 610 |
|
| 611 |
EVENT: |
| 612 |
for my $event ( @{ $recce->events() } ) { |
| 613 |
my ($name) = @{$event}; |
| 614 |
if ( $name eq 'target' ) { |
| 615 |
@shortest_span = $recce->last_completed_span('target'); |
| 616 |
diag( |
| 617 |
"Preliminary target at $pos: ", |
| 618 |
$recce->literal(@shortest_span) |
| 619 |
) if $verbose; |
| 620 |
next EVENT; |
| 621 |
} ## end if ( $name eq 'target' ) |
| 622 |
# Not all exhaustion has an exhaustion event, |
| 623 |
# so we look for exhaustion explicitly below. |
| 624 |
next EVENT if $name eq q('exhausted); |
| 625 |
die join q{ }, "Spurious event at position $pos: '$name'"; |
| 626 |
} ## end EVENT: for my $event ( @{ $recce->events() } ) |
| 627 |
|
| 628 |
=for Marpa::R2::Display::End |
| 629 |
|
| 630 |
An exhaustion parse event triggers on asynchronous parse exhaustion, |
| 631 |
if the recognizer's C<exhaustion> setting is "C<event>". |
| 632 |
The name of the event is |
| 633 |
"C<'exhausted>" |
| 634 |
(The initial single quote is part of the event's name, |
| 635 |
and indicates it is a reserved name, |
| 636 |
which will not conflict with |
| 637 |
the name of any user-named event.) |
| 638 |
|
| 639 |
Intuitively, parse exhaustion events are created only when |
| 640 |
needed for control to return to the application. |
| 641 |
More precisely, |
| 642 |
a parse exhaustion event is called B<synchronous> if it |
| 643 |
occurs at a G1 location where control would return to the application |
| 644 |
in any case, either due to end of string or another event. |
| 645 |
A parse exhaustion event is called B<asynchronous> if it is not |
| 646 |
synchronous. |
| 647 |
|
| 648 |
The event itself is often simply discarded, |
| 649 |
because an application typically |
| 650 |
does not care whether |
| 651 |
exhaustion is synchronous or asynchronous. |
| 652 |
The L<C<exhausted()> method|Marpa::R2::Scanless::R/"exhausted()"> |
| 653 |
can be relied on to report |
| 654 |
both asynchronous and synchronous parse exhaustion, |
| 655 |
and it is usually used instead. |
| 656 |
Exhaustion is discussed further in |
| 657 |
L<a separate document|Marpa::R2::Exhaustion>. |
| 658 |
|
| 659 |
=head2 Rejection events |
| 660 |
|
| 661 |
=for Marpa::R2::Display |
| 662 |
name: SLIF rejection recognizer setting synopsis |
| 663 |
partial: 1 |
| 664 |
normalize-whitespace: 1 |
| 665 |
|
| 666 |
my $recce = Marpa::R2::Scanless::R->new( |
| 667 |
{ grammar => $g, |
| 668 |
rejection => 'event', |
| 669 |
}, |
| 670 |
$recce_debug_args |
| 671 |
); |
| 672 |
my $pos = $recce->read( \$suffixed_string, 0, $original_length ); |
| 673 |
|
| 674 |
READ_LOOP: while (1) { |
| 675 |
my $rejection = 0; |
| 676 |
my $pos = $recce->pos(); |
| 677 |
EVENT: |
| 678 |
for my $event ( @{ $recce->events() } ) { |
| 679 |
my ($name) = @{$event}; |
| 680 |
if ( $name eq q('rejected) ) { |
| 681 |
$rejection = 1; |
| 682 |
diag("You fool! you forget the semi-colon at location $pos!") |
| 683 |
if $verbose; |
| 684 |
next EVENT; |
| 685 |
|
| 686 |
} ## end if ( $name eq q('rejected) ) |
| 687 |
die join q{ }, "Spurious event at position $pos: '$name'"; |
| 688 |
} ## end EVENT: for my $event ( @{ $recce->events() } ) |
| 689 |
|
| 690 |
last READ_LOOP if not $rejection; |
| 691 |
|
| 692 |
$recce->resume( $original_length, 1 ); |
| 693 |
diag("I fixed it for you. Now you owe me.") if $verbose; |
| 694 |
$recce->resume( $pos, $original_length - $pos ); |
| 695 |
} ## end READ_LOOP: while (1) |
| 696 |
|
| 697 |
=for Marpa::R2::Display::End |
| 698 |
|
| 699 |
A rejection event triggers if |
| 700 |
all lexemes at a G1 location are rejected, |
| 701 |
and the recognizer's C<rejection> setting is "C<event>". |
| 702 |
The name of the event is |
| 703 |
"C<'rejected"> |
| 704 |
(The initial single quote is part of the event's name, |
| 705 |
and indicates it is a reserved name, |
| 706 |
which will not conflict with the name |
| 707 |
of any user-named event.) |
| 708 |
|
| 709 |
=head1 Lexeme events |
| 710 |
|
| 711 |
SLIF parse events are divided |
| 712 |
into lexeme and non-lexeme events, |
| 713 |
based on their type. |
| 714 |
The lexeme events are the pre-lexeme event |
| 715 |
and post-lexeme event. |
| 716 |
|
| 717 |
A lexeme event will trigger at the current location |
| 718 |
if all of the following criteria, |
| 719 |
applied in order, are true: |
| 720 |
|
| 721 |
=over 4 |
| 722 |
|
| 723 |
=item * |
| 724 |
|
| 725 |
It is declared in a |
| 726 |
L<C<:lexeme> pseudo-rule|Marpa::R2::Scanless::DSL/"Lexeme pseudo-rule">. |
| 727 |
|
| 728 |
=item * |
| 729 |
|
| 730 |
Its lexeme has been scanned by the L0 grammar at that location. |
| 731 |
|
| 732 |
=item * |
| 733 |
|
| 734 |
The G1 grammar would accept its lexeme at that location. |
| 735 |
|
| 736 |
=item * |
| 737 |
|
| 738 |
The lexeme is not scanned externally, |
| 739 |
that is, |
| 740 |
it is not scanned |
| 741 |
by a call of the |
| 742 |
L<C<< $slr->lexeme_complete() >> method|Marpa::R2::Scanless::R/"lexeme_complete()"> |
| 743 |
or of the |
| 744 |
L<C<< $slr->lexeme_read() >> method|Marpa::R2::Scanless::R/"lexeme_read()"> |
| 745 |
method. |
| 746 |
|
| 747 |
=item * |
| 748 |
|
| 749 |
The event is activated. |
| 750 |
An event is activated by default when it |
| 751 |
is declared. |
| 752 |
Deactivation and reactivation of events is |
| 753 |
done with the SLIF recognizer's |
| 754 |
L<activate() method|Marpa::R2::Scanless::R/"activate()">. |
| 755 |
|
| 756 |
=item * |
| 757 |
|
| 758 |
Its lexeme priority is higher than, or equal to, |
| 759 |
that of any other lexeme |
| 760 |
remaining after the previous criteria |
| 761 |
have been applied. |
| 762 |
|
| 763 |
=item * |
| 764 |
|
| 765 |
If it is a post-lexeme event, |
| 766 |
none of other remaining events are pre-lexeme events. |
| 767 |
(In other words, a pre-lexeme event prevents |
| 768 |
post-lexeme events from triggering at the same location.) |
| 769 |
|
| 770 |
=back |
| 771 |
|
| 772 |
Marpa allows ambiguous lexemes and, |
| 773 |
even after all the above criteria have been applied, |
| 774 |
there may be more than one lexeme event at a G1 location. |
| 775 |
|
| 776 |
=head2 Pause span and pause lexeme |
| 777 |
|
| 778 |
When a lexeme event triggers, it will set |
| 779 |
the B<pause lexeme> to the lexeme symbol. |
| 780 |
It will also set |
| 781 |
the B<pause span> to the start physical input stream location |
| 782 |
and length of the triggering lexeme. |
| 783 |
The pause span and pause lexeme are |
| 784 |
originally undefined. |
| 785 |
Every call |
| 786 |
to the L<C<read()>|Marpa::R2::Scanless::R/"read()"> |
| 787 |
or the L<C<resume()>|Marpa::R2::Scanless::R/"resume()"> |
| 788 |
methods resets the pause lexeme |
| 789 |
and the pause span to undefined. |
| 790 |
|
| 791 |
The pause span may be accessed directly |
| 792 |
with the |
| 793 |
L<C<< $slr->pause_span() >> |
| 794 |
method|Marpa::R2::Scanless::R/"pause_span()">. |
| 795 |
L<Accessing the pause lexeme |
| 796 |
directly|Marpa::R2::Scanless::R/"pause_lexeme()"> |
| 797 |
is discouraged, |
| 798 |
because multiple lexeme events may occur at the |
| 799 |
same G1 location, |
| 800 |
but only one pause lexeme, arbitrarily chosen, is recorded. |
| 801 |
This is not a problem with the pause span, because |
| 802 |
all pause spans at a G1 location will be identical. |
| 803 |
|
| 804 |
=head1 Non-lexeme events |
| 805 |
|
| 806 |
Prediction, completion and nulling events are non-lexeme events. |
| 807 |
The conditions for a non-lexeme event are simpler than those for |
| 808 |
a lexeme event, because they do not involve lexical processing. |
| 809 |
|
| 810 |
A non-lexeme event will trigger at the current location |
| 811 |
if all of the following are true: |
| 812 |
|
| 813 |
=over 4 |
| 814 |
|
| 815 |
=item * |
| 816 |
|
| 817 |
It is declared in a |
| 818 |
L<named event statement|Marpa::R2::Scanless::DSL/"Named event statement">. |
| 819 |
|
| 820 |
=item * |
| 821 |
|
| 822 |
Its B<triggering condition> is true. Specifically, |
| 823 |
|
| 824 |
=over 4 |
| 825 |
|
| 826 |
=item * |
| 827 |
|
| 828 |
It is a prediction and its symbol is acceptable at the current location; or |
| 829 |
|
| 830 |
=item * |
| 831 |
|
| 832 |
it is a completion or a nulling event and its symbol is recognized |
| 833 |
at the current location; or |
| 834 |
|
| 835 |
=item * |
| 836 |
|
| 837 |
it is an exhaustion event, and asynchronous parse exhaustion, |
| 838 |
L<as defined above|/"Exhaustion events">, |
| 839 |
occurs at the current location; or |
| 840 |
|
| 841 |
=item * |
| 842 |
|
| 843 |
it is an rejection event, and all lexeme alternatives are rejected |
| 844 |
at the current location. |
| 845 |
|
| 846 |
=back |
| 847 |
|
| 848 |
=item * |
| 849 |
|
| 850 |
The event is activated. |
| 851 |
An event is activated by default when it |
| 852 |
is declared. |
| 853 |
Deactivation and reactivation of events is |
| 854 |
done with the SLIF recognizer's |
| 855 |
L<activate() method|Marpa::R2::Scanless::R/"activate()">. |
| 856 |
|
| 857 |
=back |
| 858 |
|
| 859 |
=head1 Techniques |
| 860 |
|
| 861 |
=head2 External scanning |
| 862 |
|
| 863 |
Switching to external scanning is an intended use case |
| 864 |
for all events, other than exhaustion events. |
| 865 |
In particular, |
| 866 |
the behavior of pre-lexeme events |
| 867 |
is most intuitive when thought about with |
| 868 |
external scanning in mind. |
| 869 |
|
| 870 |
L<The example code for this document|/"An example"> |
| 871 |
contains an artificially simple example of external |
| 872 |
scanning. |
| 873 |
The symbol C<< <insert d> >> has a pre-lexeme event |
| 874 |
declared: |
| 875 |
|
| 876 |
=for Marpa::R2::Display |
| 877 |
name: SLIF Event synopsis |
| 878 |
partial: 1 |
| 879 |
normalize-whitespace: 1 |
| 880 |
|
| 881 |
:lexeme ~ <insert d> pause => before event => 'insert d' |
| 882 |
|
| 883 |
=for Marpa::R2::Display::End |
| 884 |
|
| 885 |
When this triggers, the code in the example switches to |
| 886 |
external scanning: |
| 887 |
It reads a C<< <d> >> symbol externally, |
| 888 |
skips over the lexeme actually found |
| 889 |
in the physical input, |
| 890 |
and resumes internal scanning. |
| 891 |
|
| 892 |
=head2 Markers |
| 893 |
|
| 894 |
It is quite reasonable to create "markers" -- |
| 895 |
nulling symbols |
| 896 |
whose primary (or sole) purpose |
| 897 |
is to have nulling events declared for them. |
| 898 |
Markers are the only way to declare events that trigger in |
| 899 |
the middle of a rule. |
| 900 |
|
| 901 |
=head2 Rules |
| 902 |
|
| 903 |
There are no events explicitly defined in terms of rules, |
| 904 |
but every rule event that is wanted can be achieved in |
| 905 |
one or more ways. |
| 906 |
The most flexible of these, and the best for many purposes, |
| 907 |
is to use L<markers|/"Markers">. |
| 908 |
|
| 909 |
Another method is to use the LHS of a rule to track rule |
| 910 |
predictions and completions. |
| 911 |
This requires that the LHS symbol of the rule be unique to that |
| 912 |
rule. |
| 913 |
|
| 914 |
=head1 Implications |
| 915 |
|
| 916 |
This section describes |
| 917 |
some implications of the SLIF parse events mechanism |
| 918 |
that may be unexpected at first. |
| 919 |
These implications are Marpa working as designed and, |
| 920 |
I hope the reader will agree, |
| 921 |
as is desirable. |
| 922 |
|
| 923 |
=head2 Ambiguity |
| 924 |
|
| 925 |
If a parse is ambiguous, events trigger for |
| 926 |
B<all> the possible symbols. |
| 927 |
A user thinking in terms of one of the parses, |
| 928 |
and unaware of the ambiguity, may find this unexpected. |
| 929 |
In L<the example|/"An example">, |
| 930 |
events for both the symbols C<< <ambig1> >> |
| 931 |
and C<< <ambig2> >>, as well as all their |
| 932 |
derived symbols, trigger. |
| 933 |
|
| 934 |
=head2 Tentative events |
| 935 |
|
| 936 |
Marpa's events are left-eidetic but right-blind. |
| 937 |
Left of the event location, Marpa's events are 100% accurate. |
| 938 |
Right of the event location, they are totally unaware of |
| 939 |
what the actual input will be -- |
| 940 |
there is no "lookahead". |
| 941 |
Because events trigger based on input action |
| 942 |
only up to the event location, |
| 943 |
events are B<tentative>. |
| 944 |
|
| 945 |
Once the parse is complete, |
| 946 |
and the actual input to the right of the event |
| 947 |
location is taken into account, |
| 948 |
it is quite possible that |
| 949 |
none of the parse trees |
| 950 |
will actually contain the symbol instance |
| 951 |
that triggered an event. |
| 952 |
|
| 953 |
In L<the example|/"An example">, |
| 954 |
prediction and completion |
| 955 |
events are reported for the symbols |
| 956 |
C<< <start1> >>, |
| 957 |
C<< <start2> >>, |
| 958 |
C<< <mid1> >> and |
| 959 |
C<< <mid2> >>, |
| 960 |
but none of these symbols |
| 961 |
winds up in |
| 962 |
any of the parse tress. |
| 963 |
This is because they are derived from |
| 964 |
C<< <ambig1> >> or |
| 965 |
C<< <ambig2> >>, |
| 966 |
and C<< <ambig1> >> or |
| 967 |
C<< <ambig2> >> will never be fully recognized |
| 968 |
in any of the parse trees. |
| 969 |
The reason that |
| 970 |
C<< <ambig1> >> and |
| 971 |
C<< <ambig2> >> will not be fully recognized |
| 972 |
is that their full recognition requires |
| 973 |
that there be a |
| 974 |
C<< <z> >> symbol in the input and the |
| 975 |
input stream in the example does not contain a |
| 976 |
C<< <z> >> symbol. |
| 977 |
|
| 978 |
Exhaustion events are not tentative. |
| 979 |
All other SLIF parse events are tentative. |
| 980 |
|
| 981 |
In the example, the predictions |
| 982 |
for |
| 983 |
C<< <mid1> >> and |
| 984 |
C<< <mid2> >> do not match anything in |
| 985 |
the final parse tree, |
| 986 |
because the locations where |
| 987 |
C<< <mid1> >> and |
| 988 |
C<< <mid2> >> would be predicted are not reached in |
| 989 |
those trees. |
| 990 |
For similar reasons, nulling events are tentative. |
| 991 |
|
| 992 |
Lexemes can be ambiguous and |
| 993 |
when they are ambiguous |
| 994 |
one or more of the lexeme alternatives |
| 995 |
may not be used in any final parse tree. |
| 996 |
Because of this, |
| 997 |
lexeme events are also tentative. |
| 998 |
|
| 999 |
After rejection events, |
| 1000 |
input can be, |
| 1001 |
and typically is, |
| 1002 |
retried at the same G1 location. |
| 1003 |
This is what happens when the Ruby Slippers technique |
| 1004 |
is used. |
| 1005 |
Often, on the second or later attempt, one or |
| 1006 |
more lexemes are found that are acceptable |
| 1007 |
to the grammar. |
| 1008 |
For this reason, |
| 1009 |
rejection events are tentative. |
| 1010 |
|
| 1011 |
=head2 Nulled forests |
| 1012 |
|
| 1013 |
When a symbol is nulled, any symbol which can be null-derived |
| 1014 |
from it |
| 1015 |
is also nulled. |
| 1016 |
In L<the example|/"An example">, |
| 1017 |
when the |
| 1018 |
symbol C<< <g> >> is nulled, |
| 1019 |
derived symbols |
| 1020 |
C<< <g1> >>, |
| 1021 |
C<< <g2> >>, |
| 1022 |
C<< <g3> >>, |
| 1023 |
C<< <g4> >> |
| 1024 |
are also nulled. |
| 1025 |
|
| 1026 |
Note that what was said about |
| 1027 |
L<ambiguity|"Ambiguity"> |
| 1028 |
applies here. |
| 1029 |
In the example, the symbols |
| 1030 |
C<< <g1> >> and |
| 1031 |
C<< <g2> >> are in one derivation, |
| 1032 |
while C<< <g3> >> and |
| 1033 |
C<< <g4> >> are in another, |
| 1034 |
so that not just a parse tree, |
| 1035 |
but an entire parse forest |
| 1036 |
is nulled. |
| 1037 |
(Pedantically, a nulled tree is a forest |
| 1038 |
of a single tree.) |
| 1039 |
|
| 1040 |
More precisely, |
| 1041 |
|
| 1042 |
=over 4 |
| 1043 |
|
| 1044 |
=item * |
| 1045 |
|
| 1046 |
If the grammar allows |
| 1047 |
any derivation of the symbol |
| 1048 |
I<Y> from I<X> in which I<X> and I<Y> are both |
| 1049 |
nulled; and |
| 1050 |
|
| 1051 |
=item * |
| 1052 |
|
| 1053 |
a nulling SLIF parse event |
| 1054 |
is declared for I<Y> and activated; and |
| 1055 |
|
| 1056 |
=item * |
| 1057 |
|
| 1058 |
a nulled instance of I<X> is encountered |
| 1059 |
in the parse at location I<L>; then |
| 1060 |
|
| 1061 |
=item * |
| 1062 |
|
| 1063 |
a nulling SLIF parse event for I<Y> |
| 1064 |
will trigger at location I<L>. |
| 1065 |
|
| 1066 |
=back |
| 1067 |
|
| 1068 |
=head2 Events and instances |
| 1069 |
|
| 1070 |
As stated above, only nulling instances generate nulling events, |
| 1071 |
and only non-nulled symbols generate prediction events |
| 1072 |
and completion events. |
| 1073 |
Since lexemes cannot be zero length, this means that, |
| 1074 |
for a given symbol instance, |
| 1075 |
nulling events and all other events, |
| 1076 |
are mutually exclusive. |
| 1077 |
In other words, if a nulling event occurs for an |
| 1078 |
instance, no other event will trigger for that instance. |
| 1079 |
|
| 1080 |
Some cases may seem to violate this rule. |
| 1081 |
For example |
| 1082 |
at position 23 |
| 1083 |
in the parse in |
| 1084 |
L<the code below|/"An example">, |
| 1085 |
we have four events |
| 1086 |
of four different types, |
| 1087 |
all for the symbol C<< <e> >>. |
| 1088 |
In addition to |
| 1089 |
a nulling event, there is |
| 1090 |
a post-lexeme event, |
| 1091 |
a prediction event |
| 1092 |
and a completion event: |
| 1093 |
|
| 1094 |
=for Marpa::R2::Display |
| 1095 |
name: SLIF Event synopsis |
| 1096 |
normalize-whitespace: 1 |
| 1097 |
partial: 1 |
| 1098 |
|
| 1099 |
Events at position 23: "e" e$ e[] ^e ^f |
| 1100 |
|
| 1101 |
=for Marpa::R2::Display::End |
| 1102 |
|
| 1103 |
The reason for this is that these events are |
| 1104 |
for three different symbol instances, all of which |
| 1105 |
share the same trigger location: |
| 1106 |
|
| 1107 |
=over 4 |
| 1108 |
|
| 1109 |
=item 1 |
| 1110 |
|
| 1111 |
A nulled instance at location 23. |
| 1112 |
|
| 1113 |
=item 2 |
| 1114 |
|
| 1115 |
A potential non-nulled instance, which may begin |
| 1116 |
at location 23. |
| 1117 |
|
| 1118 |
=item 3 |
| 1119 |
|
| 1120 |
A non-nulled instance, which begins at location 22 |
| 1121 |
and ends at location 23. |
| 1122 |
|
| 1123 |
=back |
| 1124 |
|
| 1125 |
The prediction of the second instance is, in fact, |
| 1126 |
fulfilled, as reported at location 25: |
| 1127 |
|
| 1128 |
=for Marpa::R2::Display |
| 1129 |
name: SLIF Event synopsis |
| 1130 |
normalize-whitespace: 1 |
| 1131 |
partial: 1 |
| 1132 |
|
| 1133 |
Events at position 25: "e" e$ ^f |
| 1134 |
|
| 1135 |
=for Marpa::R2::Display::End |
| 1136 |
|
| 1137 |
The second instance is length 1 and predicted at location |
| 1138 |
23, but its completion is reported at location 25. |
| 1139 |
This is because whitespace delayed its start by one position. |
| 1140 |
|
| 1141 |
=for Marpa::R2::Display |
| 1142 |
name: SLIF Event synopsis |
| 1143 |
normalize-whitespace: 1 |
| 1144 |
partial: 1 |
| 1145 |
|
| 1146 |
Events at position 21: d$ mid1$ mid2$ e[] ^e ^f |
| 1147 |
|
| 1148 |
=for Marpa::R2::Display::End |
| 1149 |
|
| 1150 |
The third instance is reported as predicted at position 21, |
| 1151 |
even though it actually begins at position 22. |
| 1152 |
The delayed start is |
| 1153 |
because of whitespace. |
| 1154 |
|
| 1155 |
Prediction and completion events exclude |
| 1156 |
nulled symbols, |
| 1157 |
because there is no practical distinction between predicting |
| 1158 |
a nulled symbol, and actually seeing one. |
| 1159 |
This means that the prediction and completion of a nulled symbol |
| 1160 |
would always occur together. |
| 1161 |
This very special nature of nulled symbols motivates their |
| 1162 |
treatment as a special case. |
| 1163 |
|
| 1164 |
=head2 Hidden events |
| 1165 |
|
| 1166 |
An important aspect of the event mechanism is that |
| 1167 |
it triggers a return from the event-triggering |
| 1168 |
method at the trigger location. |
| 1169 |
It may happen, however, that the method would return |
| 1170 |
at that location in any case, |
| 1171 |
and in this circumstance |
| 1172 |
the triggering |
| 1173 |
can be said to be B<hidden>. |
| 1174 |
A event which causes hidden triggering is called |
| 1175 |
a B<hidden event>. |
| 1176 |
|
| 1177 |
As one example, |
| 1178 |
the |
| 1179 |
L<C<lexeme_complete()>|Marpa::R2::Scanless::R/"lexeme_complete()"> and |
| 1180 |
L<C<lexeme_read()>|Marpa::R2::Scanless::R/"lexeme_read()"> |
| 1181 |
methods return at every lexeme at which a lexeme is read, so |
| 1182 |
all triggering in those methods is hidden triggering. |
| 1183 |
In L<the example code in this |
| 1184 |
document|/"An example">, |
| 1185 |
the events at this location were all caused by hidden |
| 1186 |
triggering inside a call to C<< $slr->lexeme_complete() >>: |
| 1187 |
|
| 1188 |
=for Marpa::R2::Display |
| 1189 |
name: SLIF Event synopsis |
| 1190 |
normalize-whitespace: 1 |
| 1191 |
partial: 1 |
| 1192 |
|
| 1193 |
Events at position 21: d$ mid1$ mid2$ e[] ^e ^f |
| 1194 |
|
| 1195 |
=for Marpa::R2::Display::End |
| 1196 |
|
| 1197 |
As another example, |
| 1198 |
the C<< $slr->read() >> and C<< $slr->resume() >> methods |
| 1199 |
return at end of string, |
| 1200 |
but events may also trigger at end of string. |
| 1201 |
The events at this location were caused by hidden |
| 1202 |
triggering inside C<< $slr->resume() >> |
| 1203 |
at end of string: |
| 1204 |
|
| 1205 |
=for Marpa::R2::Display |
| 1206 |
name: SLIF Event synopsis |
| 1207 |
normalize-whitespace: 1 |
| 1208 |
partial: 1 |
| 1209 |
|
| 1210 |
Events at position 29: "h" test$ |
| 1211 |
|
| 1212 |
=for Marpa::R2::Display::End |
| 1213 |
|
| 1214 |
The L<example code for this document|/"An example"> |
| 1215 |
is programmed with the possibility of hidden |
| 1216 |
triggering in mind. |
| 1217 |
To do this, it is careful to access events after its |
| 1218 |
calls to the |
| 1219 |
C<< $slr->lexeme_read() >> |
| 1220 |
as well as to make an additional pass through the event-accessing loop |
| 1221 |
after an end of string is encountered. |
| 1222 |
|
| 1223 |
=head2 Lexeme events and external scanning |
| 1224 |
|
| 1225 |
During external scanning, lexemes are read using the |
| 1226 |
L<C<< $slr->lexeme_complete() >>|Marpa::R2::Scanless::R/"lexeme_complete()"> and |
| 1227 |
L<C<< $slr->lexeme_read() >>|Marpa::R2::Scanless::R/"lexeme_read()"> methods. |
| 1228 |
Non-lexeme events may trigger during these methods, as was discussed |
| 1229 |
in L</"Hidden events">. |
| 1230 |
However, lexeme events that would occur during the |
| 1231 |
C<< $slr->lexeme_complete() >> and |
| 1232 |
C<< $slr->lexeme_read() >> methods are ignored, |
| 1233 |
and will never trigger. |
| 1234 |
|
| 1235 |
This behavior may seem non-orthogonal, |
| 1236 |
but in fact it is the most consistent course of action. |
| 1237 |
A pre-lexeme event occuring during |
| 1238 |
a C<< $slr->lexeme_complete() >> and |
| 1239 |
C<< $slr->lexeme_read() >> method call would reverse its effect, |
| 1240 |
a behavior which is at best pointless. |
| 1241 |
A post-lexeme event would be less dangerous, |
| 1242 |
but it would be completely redundant -- |
| 1243 |
its presence or absence |
| 1244 |
would tell the application only what |
| 1245 |
the application already knows from the |
| 1246 |
return of success or failure by the |
| 1247 |
C<< $slr->lexeme_complete() >> or |
| 1248 |
C<< $slr->lexeme_read() >> methods. |
| 1249 |
|
| 1250 |
=head1 An example |
| 1251 |
|
| 1252 |
The SLIF DSL in this example |
| 1253 |
is designed to |
| 1254 |
include the unusual and "corner" |
| 1255 |
cases described in this document. |
| 1256 |
It is not like any grammar that you are likely |
| 1257 |
to encounter in normal practice. |
| 1258 |
|
| 1259 |
=for Marpa::R2::Display |
| 1260 |
name: SLIF Event synopsis |
| 1261 |
normalize-whitespace: 1 |
| 1262 |
|
| 1263 |
sub forty_two { return 42; }; |
| 1264 |
|
| 1265 |
use Marpa::R2; |
| 1266 |
|
| 1267 |
my $dsl = <<'END_OF_DSL'; |
| 1268 |
:default ::= action => [name,values] |
| 1269 |
lexeme default = latm => 1 |
| 1270 |
|
| 1271 |
test ::= a b c d e e f g h action => main::forty_two |
| 1272 |
| a ambig1 | a ambig2 |
| 1273 |
e ::= <real e> | <null e> |
| 1274 |
<null e> ::= |
| 1275 |
g ::= g1 | g3 |
| 1276 |
g1 ::= g2 |
| 1277 |
g2 ::= |
| 1278 |
g3 ::= g4 |
| 1279 |
g4 ::= |
| 1280 |
d ::= <real d> | <insert d> |
| 1281 |
ambig1 ::= start1 mid1 z |
| 1282 |
ambig2 ::= start2 mid2 z |
| 1283 |
start1 ::= b mid1 ::= c d |
| 1284 |
start2 ::= b c mid2 ::= d |
| 1285 |
|
| 1286 |
a ~ 'a' b ~ 'b' c ~ 'c' |
| 1287 |
<real d> ~ 'd' |
| 1288 |
<insert d> ~ ["] 'insert d here' ["] |
| 1289 |
<real e> ~ 'e' |
| 1290 |
f ~ 'f' |
| 1291 |
h ~ 'h' |
| 1292 |
z ~ 'z' |
| 1293 |
|
| 1294 |
:lexeme ~ <a> pause => after event => '"a"' |
| 1295 |
:lexeme ~ <b> pause => after event => '"b"' |
| 1296 |
:lexeme ~ <c> pause => after event => '"c"' |
| 1297 |
:lexeme ~ <real d> pause => after event => '"d"' |
| 1298 |
:lexeme ~ <insert d> pause => before event => 'insert d' |
| 1299 |
:lexeme ~ <real e> pause => after event => '"e"' |
| 1300 |
:lexeme ~ <f> pause => after event => '"f"' |
| 1301 |
:lexeme ~ <h> pause => after event => '"h"' |
| 1302 |
|
| 1303 |
event '^test' = predicted test |
| 1304 |
event 'test$' = completed test |
| 1305 |
event '^start1' = predicted start1 |
| 1306 |
event 'start1$' = completed start1 |
| 1307 |
event '^start2' = predicted start2 |
| 1308 |
event 'start2$' = completed start2 |
| 1309 |
event '^mid1' = predicted mid1 |
| 1310 |
event 'mid1$' = completed mid1 |
| 1311 |
event '^mid2' = predicted mid2 |
| 1312 |
event 'mid2$' = completed mid2 |
| 1313 |
|
| 1314 |
event '^a' = predicted a |
| 1315 |
event '^b' = predicted b |
| 1316 |
event '^c' = predicted c |
| 1317 |
event 'd[]' = nulled d |
| 1318 |
event 'd$' = completed d |
| 1319 |
event '^d' = predicted d |
| 1320 |
event '^e' = predicted e |
| 1321 |
event 'e[]' = nulled e |
| 1322 |
event 'e$' = completed e |
| 1323 |
event '^f' = predicted f |
| 1324 |
event 'g[]' = nulled g |
| 1325 |
event '^g' = predicted g |
| 1326 |
event 'g$' = completed g |
| 1327 |
event 'g1[]' = nulled g1 |
| 1328 |
event 'g2[]' = nulled g2 |
| 1329 |
event 'g3[]' = nulled g3 |
| 1330 |
event 'g4[]' = nulled g4 |
| 1331 |
event '^h' = predicted h |
| 1332 |
|
| 1333 |
:discard ~ whitespace |
| 1334 |
whitespace ~ [\s]+ |
| 1335 |
END_OF_DSL |
| 1336 |
|
| 1337 |
my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } ); |
| 1338 |
my $slr = Marpa::R2::Scanless::R->new( |
| 1339 |
{ grammar => $grammar, semantics_package => 'My_Actions' } ); |
| 1340 |
|
| 1341 |
my $input = q{a b c "insert d here" e e f h}; |
| 1342 |
my $length = length $input; |
| 1343 |
my $pos = $slr->read( \$input ); |
| 1344 |
|
| 1345 |
my $actual_events = q{}; |
| 1346 |
|
| 1347 |
READ: while (1) { |
| 1348 |
|
| 1349 |
my @actual_events = (); |
| 1350 |
|
| 1351 |
my $next_lexeme; |
| 1352 |
EVENT: |
| 1353 |
for my $event ( @{ $slr->events() } ) { |
| 1354 |
my ($name) = @{$event}; |
| 1355 |
if ($name eq 'insert d') { |
| 1356 |
my (undef, $length) = $slr->pause_span(); |
| 1357 |
$next_lexeme = ['real d', undef, $length]; |
| 1358 |
} |
| 1359 |
push @actual_events, $name; |
| 1360 |
} |
| 1361 |
|
| 1362 |
if (@actual_events) { |
| 1363 |
$actual_events .= join q{ }, "Events at position $pos:", @actual_events; |
| 1364 |
$actual_events .= "\n"; |
| 1365 |
} |
| 1366 |
|
| 1367 |
if ($next_lexeme) { |
| 1368 |
$slr->lexeme_read(@{$next_lexeme}); |
| 1369 |
$pos = $slr->pos(); |
| 1370 |
next READ; |
| 1371 |
} |
| 1372 |
if ($pos < $length) { |
| 1373 |
$pos = $slr->resume(); |
| 1374 |
next READ; |
| 1375 |
} |
| 1376 |
last READ; |
| 1377 |
} ## end READ: while (1) |
| 1378 |
|
| 1379 |
my $expected_events = <<'=== EOS ==='; |
| 1380 |
Events at position 0: ^test ^a |
| 1381 |
Events at position 1: "a" ^b ^start1 ^start2 |
| 1382 |
Events at position 3: "b" start1$ ^c ^mid1 |
| 1383 |
Events at position 5: "c" start2$ ^d ^mid2 |
| 1384 |
Events at position 6: insert d |
| 1385 |
Events at position 21: d$ mid1$ mid2$ e[] ^e ^f |
| 1386 |
Events at position 23: "e" e$ e[] ^e ^f |
| 1387 |
Events at position 25: "e" e$ ^f |
| 1388 |
Events at position 27: "f" g[] g1[] g3[] g2[] g4[] ^h |
| 1389 |
Events at position 29: "h" test$ |
| 1390 |
=== EOS === |
| 1391 |
|
| 1392 |
=for Marpa::R2::Display::End |
| 1393 |
|
| 1394 |
=head1 Unnamed events |
| 1395 |
|
| 1396 |
Use of unnamed events is strongly discouraged. |
| 1397 |
However, to support legacy code, unnamed events are still supported. |
| 1398 |
|
| 1399 |
Unnamed events are declared by |
| 1400 |
L<C<:lexeme> pseudo-rules|Marpa::R2::Scanless::DSL/"Lexeme pseudo-rule">, |
| 1401 |
when |
| 1402 |
L<the C<pause> adverb|Marpa::R2::Scanless::DSL/"pause"> |
| 1403 |
is used without |
| 1404 |
L<the C<event> adverb|Marpa::R2::Scanless::DSL/"event">. |
| 1405 |
Since the |
| 1406 |
L<the C<pause> adverb|Marpa::R2::Scanless::DSL/"pause"> |
| 1407 |
creates a SLIF parse event, but |
| 1408 |
L<the C<event> adverb|Marpa::R2::Scanless::DSL/"event"> |
| 1409 |
provides the event's name, |
| 1410 |
this results in a SLIF parse event without a name -- |
| 1411 |
an unnamed event. |
| 1412 |
|
| 1413 |
Unnamed events cannot be accessed using |
| 1414 |
L<the C<< $slr->events() >> |
| 1415 |
method|Marpa::R2::Scanless::R/"events()">. |
| 1416 |
The only accessors for unnamed events are |
| 1417 |
L<the C<< $slr->pause_lexeme() >> |
| 1418 |
method|Marpa::R2::Scanless::R/"pause_lexeme()"> |
| 1419 |
and L<the C<< $slr->pause_span() >> |
| 1420 |
method|Marpa::R2::Scanless::R/"pause_span()">. |
| 1421 |
|
| 1422 |
=head1 Copyright and License |
| 1423 |
|
| 1424 |
=for Marpa::R2::Display |
| 1425 |
ignore: 1 |
| 1426 |
|
| 1427 |
Copyright 2022 Jeffrey Kegler |
| 1428 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 1429 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 1430 |
General Public License as published by the Free Software Foundation, |
| 1431 |
either version 3 of the License, or (at your option) any later version. |
| 1432 |
|
| 1433 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 1434 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 1435 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 1436 |
Lesser General Public License for more details. |
| 1437 |
|
| 1438 |
You should have received a copy of the GNU Lesser |
| 1439 |
General Public License along with Marpa::R2. If not, see |
| 1440 |
http://www.gnu.org/licenses/. |
| 1441 |
|
| 1442 |
=for Marpa::R2::Display::End |
| 1443 |
|
| 1444 |
=cut |
| 1445 |
|
| 1446 |
# vim: expandtab shiftwidth=4: |