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