| 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::Scanless::R - Scanless interface recognizers |
| 19 |
|
| 20 |
=head1 Synopsis |
| 21 |
|
| 22 |
=for Marpa::R2::Display |
| 23 |
name: Scanless recognizer synopsis |
| 24 |
partial: 1 |
| 25 |
normalize-whitespace: 1 |
| 26 |
|
| 27 |
my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } ); |
| 28 |
my $self = bless { grammar => $grammar }, 'My_Actions'; |
| 29 |
$self->{recce} = $recce; |
| 30 |
|
| 31 |
if ( not defined eval { $recce->read($p_input_string); 1 } |
| 32 |
) |
| 33 |
{ |
| 34 |
## Add last expression found, and rethrow |
| 35 |
my $eval_error = $EVAL_ERROR; |
| 36 |
chomp $eval_error; |
| 37 |
die $self->show_last_expression(), "\n", $eval_error, "\n"; |
| 38 |
} ## end if ( not defined eval { $event_count = $recce->read...}) |
| 39 |
|
| 40 |
my $value_ref = $recce->value( $self ); |
| 41 |
if ( not defined $value_ref ) { |
| 42 |
die $self->show_last_expression(), "\n", |
| 43 |
"No parse was found, after reading the entire input\n"; |
| 44 |
} |
| 45 |
|
| 46 |
=for Marpa::R2::Display::End |
| 47 |
|
| 48 |
=for Marpa::R2::Display |
| 49 |
name: Scanless recognizer semantics |
| 50 |
partial: 1 |
| 51 |
normalize-whitespace: 1 |
| 52 |
|
| 53 |
package My_Actions; |
| 54 |
sub do_parens { shift; return $_[1] } |
| 55 |
sub do_add { shift; return $_[0] + $_[2] } |
| 56 |
sub do_subtract { shift; return $_[0] - $_[2] } |
| 57 |
sub do_multiply { shift; return $_[0] * $_[2] } |
| 58 |
sub do_divide { shift; return $_[0] / $_[2] } |
| 59 |
sub do_pow { shift; return $_[0]**$_[2] } |
| 60 |
sub do_first_arg { shift; return shift; } |
| 61 |
sub do_script { shift; return join q{ }, @_ } |
| 62 |
|
| 63 |
=for Marpa::R2::Display::End |
| 64 |
|
| 65 |
=head1 About this document |
| 66 |
|
| 67 |
This page is the reference document for the recognizer objects |
| 68 |
of Marpa's SLIF (Scanless interface). |
| 69 |
|
| 70 |
The Scanless interface is so-called because |
| 71 |
it does not require the application to supply a scanner (lexer). |
| 72 |
The SLIF contains its own lexer, |
| 73 |
one whose use is integrated into |
| 74 |
its syntax. |
| 75 |
In this document, use of the SLIF's |
| 76 |
internal scanner is called |
| 77 |
B<internal scanning>. |
| 78 |
|
| 79 |
The SLIF allows applications that find it useful to |
| 80 |
do their own scanning. |
| 81 |
When an application |
| 82 |
bypasses the SLIF's internal scanner |
| 83 |
and does its own scanning, |
| 84 |
this document calls it |
| 85 |
B<external scanning>. |
| 86 |
An application can use |
| 87 |
external scanning to supplement internal |
| 88 |
scanning, |
| 89 |
or to replace the SLIF's internal scanner entirely. |
| 90 |
|
| 91 |
=head1 The input stream |
| 92 |
|
| 93 |
The recognizer reads a virtual input stream. |
| 94 |
By default, this |
| 95 |
is identical to a physical input stream. |
| 96 |
The physical input stream is |
| 97 |
a Perl string passed as the first argument |
| 98 |
to the L<C<< $recce->read() >> method|/read()> method. |
| 99 |
Once set by the C<read()> method, the physical input |
| 100 |
stream cannot be changed. |
| 101 |
|
| 102 |
Physical input stream location is simply |
| 103 |
the Perl C<pos()> location |
| 104 |
in the physical input string. |
| 105 |
Physical input stream location may be zero, |
| 106 |
but is never negative. |
| 107 |
|
| 108 |
In this document, |
| 109 |
the phrase "input stream" and |
| 110 |
the word "stream", |
| 111 |
unless otherwise specified, |
| 112 |
refer to the physical input stream. |
| 113 |
The phrase "input stream location" and |
| 114 |
the word "location", |
| 115 |
unless otherwise specified, |
| 116 |
refer |
| 117 |
to physical input stream location. |
| 118 |
|
| 119 |
Virtual input streams complicate the idea of parse |
| 120 |
location, |
| 121 |
but they are essential for some applications. |
| 122 |
Implementing the C language's pre-processor directives requires |
| 123 |
either two passes, or |
| 124 |
a virtual approach to the input. |
| 125 |
And Perl here-documents cannot be parsed correctly |
| 126 |
by an application which insists on moving forward |
| 127 |
serially in the input. |
| 128 |
The |
| 129 |
SLIF allows applications to skip |
| 130 |
backward and forward in the physical input stream, |
| 131 |
and to read sections of the stream repeatedly. |
| 132 |
|
| 133 |
Input streams are ordered sets of characters, |
| 134 |
and the locations in them are |
| 135 |
represented as the integers from 0 to I<N>, |
| 136 |
where I<N+1> is the size of the set. |
| 137 |
In this document, we will refer to ordered subsets of |
| 138 |
contiguous locations as either B<ranges> or B<spans>. |
| 139 |
|
| 140 |
=head1 Ranges |
| 141 |
|
| 142 |
A range is an ordered set of contiguous locations |
| 143 |
specified by start location and end location: |
| 144 |
[I<S ... E>]. |
| 145 |
A range is a subset of a "universe" -- |
| 146 |
some larger ordered set of locations |
| 147 |
0 to I<N>. |
| 148 |
In this document the larger sets, |
| 149 |
or universes, will be |
| 150 |
either physical input streams |
| 151 |
or L<G1 location streams|"G1 locations">. |
| 152 |
|
| 153 |
The start and end locations of the range |
| 154 |
refer to locations |
| 155 |
in its universe. |
| 156 |
Negative locations |
| 157 |
refer to a locations relative |
| 158 |
to the end of the range's universe, so that |
| 159 |
-1 refers to the last location of the universe, |
| 160 |
-2 refers to the second-to-last location of the universe, |
| 161 |
etc. |
| 162 |
|
| 163 |
=head1 Spans |
| 164 |
|
| 165 |
A span is an ordered set of contiguous locations |
| 166 |
specified by start location and length: |
| 167 |
[I<S, L>]. |
| 168 |
A span is a subset of a universe of locations, |
| 169 |
as was |
| 170 |
described above for ranges. |
| 171 |
|
| 172 |
The range corresponding to the span |
| 173 |
[I<S, L>] is [I<S ... (S+L)-1>]. |
| 174 |
The span corresponding to the range |
| 175 |
[I<S ... E>] is [I<S, (E-S)+1>]. |
| 176 |
A span with |
| 177 |
a negative length |
| 178 |
is interpreted as if it was the range |
| 179 |
with that same pair of values. |
| 180 |
|
| 181 |
In general, spans are more convenient for programming. |
| 182 |
But when fencepost issues are important, |
| 183 |
spans require a lot of mental arithmetic, |
| 184 |
and a discussion that uses ranges is easier to follow. |
| 185 |
|
| 186 |
As examples, |
| 187 |
|
| 188 |
=over |
| 189 |
|
| 190 |
=item * |
| 191 |
|
| 192 |
The entire input stream is the range C<[0 ... -1]> |
| 193 |
and the span C<[0, -1]>. |
| 194 |
|
| 195 |
=item * |
| 196 |
|
| 197 |
The first 42 characters of the input stream are the range C<[0 ... 41]> |
| 198 |
and the span C<[0, 42]>. |
| 199 |
|
| 200 |
=item * |
| 201 |
|
| 202 |
The entire input stream, except for the last character, |
| 203 |
is the range C<[0 ... -2]> |
| 204 |
and the span C<[0, -2]>. |
| 205 |
|
| 206 |
=item * |
| 207 |
|
| 208 |
The substring consisting only of the last character is |
| 209 |
the range C<[-1 ... -1]> and the span C<[-1, 1]>. |
| 210 |
|
| 211 |
=item * |
| 212 |
|
| 213 |
The substring which consists of the last 3 characters is |
| 214 |
the range C<[-3 ... -1]> |
| 215 |
and the span C<[-3, 3]>. |
| 216 |
|
| 217 |
=item * |
| 218 |
|
| 219 |
The substring which consists |
| 220 |
of only the third-to-last character |
| 221 |
is the range C<[-3 ... -3]> |
| 222 |
and the span C<[-3, 1]>. |
| 223 |
|
| 224 |
=back |
| 225 |
|
| 226 |
=head1 Internal scanning |
| 227 |
|
| 228 |
The virtual input stream is a series of input strings. |
| 229 |
An input string is a substring |
| 230 |
of the physical input stream. |
| 231 |
By default the virtual input stream consists of |
| 232 |
exactly one input string, |
| 233 |
one which begins at location 0 in the physical input stream |
| 234 |
and whose length is the length of the physical input stream. |
| 235 |
|
| 236 |
The SLIF always starts scanning using the L<C<read()>|/"read()"> |
| 237 |
method, and |
| 238 |
the first input string is specified, implicitly or explicitly, |
| 239 |
by the L<C<read()>|/"read()"> method. |
| 240 |
When not specified, the |
| 241 |
input string |
| 242 |
for C<read()> defaults to the range [I<0 ... -1>]. |
| 243 |
|
| 244 |
L<C<read()>|/"read()"> will return success when it reaches the end of its |
| 245 |
input string, or when a SLIF parse event triggers. |
| 246 |
(Parse events are described in |
| 247 |
L<a separate |
| 248 |
document|Marpa::R2::Event>.) |
| 249 |
In many cases there are no parse events declared, |
| 250 |
or none trigger. |
| 251 |
If no parse event triggers |
| 252 |
and the parse does not fail, |
| 253 |
then C<read()> will read to the end of string. |
| 254 |
|
| 255 |
The SLIF tracks a C<current location> in the physical input stream. |
| 256 |
On return from |
| 257 |
the C<read()> method, |
| 258 |
current location will depend on the reason for the return. |
| 259 |
If a |
| 260 |
L<SLIF parse event|Marpa::R2::Event> |
| 261 |
triggered, the current location will be |
| 262 |
the trigger location; |
| 263 |
otherwise the current location will be at the end of the |
| 264 |
input string. |
| 265 |
|
| 266 |
The C<read()> method may only be called once for a recognizer, |
| 267 |
but internal scanning can be resumed with the L<C<resume()>|/"resume()"> method. |
| 268 |
The C<resume()> method, as the name suggests, |
| 269 |
resumes the internal scanning with a new input string. |
| 270 |
This input string must always be a substring |
| 271 |
of the physical input stream |
| 272 |
that was specified to the C<read()> method. |
| 273 |
By default, the new input string runs from the current location |
| 274 |
to the end of the physical input stream. |
| 275 |
|
| 276 |
On successful return from the |
| 277 |
L<C<resume()>|/"resume()"> method, |
| 278 |
the current location is set in the same way as it for the |
| 279 |
L<C<read()>|/"read()"> method: |
| 280 |
the trigger location, if an event triggered; |
| 281 |
otherwise, the end of string. |
| 282 |
The L<C<resume()>|/"resume()"> method may be called repeatedly, |
| 283 |
until the application considers the virtual input stream |
| 284 |
complete. |
| 285 |
More details are in the reference descriptions |
| 286 |
of the |
| 287 |
L<C<read()>|/"read()"> and |
| 288 |
L<C<resume()>|/"resume()"> methods, below. |
| 289 |
|
| 290 |
When the application considers input complete, |
| 291 |
and is ready to produce a parse value, |
| 292 |
the L<C<< $recce->value() >> method|/"value()"> method |
| 293 |
is used. |
| 294 |
In most cases, this is all that is needed. |
| 295 |
But Marpa also allows repeated passes over the same input |
| 296 |
with different settings. |
| 297 |
More details on the semantics are provided in |
| 298 |
L<a separate document|Marpa::R2::Semantics>. |
| 299 |
|
| 300 |
=head1 External scanning |
| 301 |
|
| 302 |
External scanning is usually performed by reading lexemes using the |
| 303 |
L<C<< $recce->lexeme_read() >> method|/"lexeme_read()">, which |
| 304 |
allows the reading of unambiguous lexemes. |
| 305 |
If ambiguous lexemes are needed, then |
| 306 |
the L<C<< $recce->lexeme_alternative() >>|/"lexeme_alternative()"> and |
| 307 |
L<C<< $recce->lexeme_complete() >>|/"lexeme_complete()"> methods can be used. |
| 308 |
|
| 309 |
Scanning must always begin with a call |
| 310 |
to the L<C<read()>|/"read()"> method, |
| 311 |
so that, in a pedantic sense, scanning always begins with internal scanning. |
| 312 |
But the first input string may be zero length: |
| 313 |
|
| 314 |
=for Marpa::R2::Display |
| 315 |
name: SLIF external read example |
| 316 |
partial: 1 |
| 317 |
normalize-whitespace: 1 |
| 318 |
|
| 319 |
$recce->read( \$string, 0, 0 ); |
| 320 |
|
| 321 |
=for Marpa::R2::Display::End |
| 322 |
|
| 323 |
and there is no requirement that internal scanning ever be resumed. |
| 324 |
|
| 325 |
=head2 External lexemes and the input stream |
| 326 |
|
| 327 |
For error message and other purposes, |
| 328 |
even externally scanned lexemes |
| 329 |
are required to correspond to a span of the input stream. |
| 330 |
An external scanner |
| 331 |
must set up a relationship to the input stream, |
| 332 |
even if that relationship is completely artificial. |
| 333 |
|
| 334 |
Here is |
| 335 |
one very general way to deal with external lexemes |
| 336 |
which have no natural mapping into |
| 337 |
the physical input stream. |
| 338 |
We will call what would ordinarily be the input string, the "natural input". |
| 339 |
To form the physical input stream, we append these 7 characters: |
| 340 |
"C<NO TEXT>". |
| 341 |
For example, if the natural input is "C<Hi! I am the real input>", |
| 342 |
then the physical input stream will be |
| 343 |
|
| 344 |
=for Marpa::R2::Display |
| 345 |
ignore: 1 |
| 346 |
|
| 347 |
"Hi! I am the real inputNO TEXT" |
| 348 |
|
| 349 |
=for Marpa::R2::Display::End |
| 350 |
|
| 351 |
To read the natural input, |
| 352 |
we will use |
| 353 |
an initial call to the L<C<read()>|/"read()"> method |
| 354 |
of the form |
| 355 |
C<< $recce->read($input_string, -8) >>. |
| 356 |
If we want to read a lexeme |
| 357 |
which has no real relationship |
| 358 |
to the natural input, |
| 359 |
we can read it externally, |
| 360 |
using a method call similar to |
| 361 |
C<< $recce->lexeme_read($symbol_name, -7, -1, $value) >>. |
| 362 |
|
| 363 |
The above approach allows the application, essentially, |
| 364 |
to ignore the natural input. |
| 365 |
External |
| 366 |
scanning also allows a wide variety of alternative input models. |
| 367 |
Alternative input models are an advanced topic and are |
| 368 |
discussed in L<a separate |
| 369 |
document|Marpa::R2::Advanced::Input_Models>. |
| 370 |
|
| 371 |
=head1 G1 locations |
| 372 |
|
| 373 |
In addition to input stream location, |
| 374 |
the SLIF also tracks G1 location. |
| 375 |
G1 locations run from 0 to I<N>, |
| 376 |
where I<N+1> is the length of the input stream. |
| 377 |
The conventions |
| 378 |
and notation |
| 379 |
for numbering G1 locations |
| 380 |
and for describing G1 spans and ranges |
| 381 |
are the same as for input stream locations. |
| 382 |
|
| 383 |
G1 location can be ignored most of the time, |
| 384 |
but it does become relevant |
| 385 |
when tracing the G1 grammar, |
| 386 |
and when dealing with ambiguous terminals. |
| 387 |
(For those familiar with Marpa's internals, |
| 388 |
the G1 location is the G1 Earley set index.) |
| 389 |
|
| 390 |
Because lexemes may be ambiguous, |
| 391 |
more than one lexeme may be read |
| 392 |
at a single G1 location. |
| 393 |
We can think of the lexemes read at a single |
| 394 |
G1 location as a set -- call it the B<G1 lexeme set>, |
| 395 |
or, for brevity, the B<G1 set>. |
| 396 |
If a lexeme is unambiguous, |
| 397 |
its G1 set will contain exactly one lexeme. |
| 398 |
|
| 399 |
G1 location can be thought of |
| 400 |
as location in terms of |
| 401 |
boundaries of G1 sets, |
| 402 |
so that the |
| 403 |
the first G1 set starts at G1 location 0 |
| 404 |
and ends at G1 location 1. |
| 405 |
When we speak of a G1 set B<at> G1 location I<L>, |
| 406 |
we refer to the G1 set ending at G1 location I<L>. |
| 407 |
That means that there is no G1 set at |
| 408 |
G1 location 0. |
| 409 |
|
| 410 |
As each G1 set is read, |
| 411 |
G1 location |
| 412 |
increases by one. |
| 413 |
B<G1 length> |
| 414 |
is length calculated in terms of G1 locations. |
| 415 |
For example, if a span of G1 locations which begin at G1 location |
| 416 |
42 and has length 2, |
| 417 |
it will contain a pair of G1 locations: G1 location 42 and G1 location 43. |
| 418 |
|
| 419 |
Sometimes it is convenient to think of |
| 420 |
a G1 location as corresponding to a single input stream location. |
| 421 |
When this is the case, |
| 422 |
what is meant is the location |
| 423 |
at the end of physical input stream span: |
| 424 |
C<$span_start+$span_length>. |
| 425 |
|
| 426 |
=head2 Literals and G1 spans |
| 427 |
|
| 428 |
It is sometimes |
| 429 |
useful to find the literal substring |
| 430 |
of the physical input stream which corresponds to a span |
| 431 |
of G1 locations. |
| 432 |
If an application reads the physical stream in sequence within the G1 span, |
| 433 |
Marpa "does what you mean". |
| 434 |
For more complicated cases, the exact rules are described in |
| 435 |
this section. |
| 436 |
|
| 437 |
Except for G1 location zero, |
| 438 |
every G1 location I<X> corresponds |
| 439 |
to one or more characters |
| 440 |
in the physical input stream. |
| 441 |
Let [I<s(X) ... e(X)>] be |
| 442 |
the physical input stream range that corresponds to |
| 443 |
G1 location I<X>. |
| 444 |
Only two things are guaranteed about I<s(X)> and I<e(X)> as |
| 445 |
a function of I<X>: |
| 446 |
|
| 447 |
=over 4 |
| 448 |
|
| 449 |
=item * |
| 450 |
|
| 451 |
I<s(X)> and I<e(X)> are not defined when I<X> is zero. |
| 452 |
|
| 453 |
=item * |
| 454 |
It will always be the case that |
| 455 |
I<< s(X) <= e(X) >>. |
| 456 |
|
| 457 |
=back |
| 458 |
|
| 459 |
In mapping ranges of G1 locations to ranges of physical |
| 460 |
input stream locations, there are several complications: |
| 461 |
|
| 462 |
=over 4 |
| 463 |
|
| 464 |
=item * |
| 465 |
|
| 466 |
There is a fencepost versus interval issue: |
| 467 |
physical input stream locations correspond to characters, |
| 468 |
but G1 locations are locations before and after characters. |
| 469 |
|
| 470 |
=item * |
| 471 |
|
| 472 |
Both kinds of locations are zero-based, |
| 473 |
but G1 location 0 does not corresponds to a range in |
| 474 |
the physical input stream. |
| 475 |
|
| 476 |
=item * |
| 477 |
|
| 478 |
Scanning is allowed to skip backward and forward, |
| 479 |
so the mapping of G1 location to physical stream locations |
| 480 |
is not necessarily monotonic. |
| 481 |
For example, if I<X> and I<Y> are G1 locations |
| 482 |
such that I<< X < Y >>, |
| 483 |
it is possible that |
| 484 |
I<< s(X) > e(Y) >>. |
| 485 |
|
| 486 |
=item * |
| 487 |
|
| 488 |
Repeated scanning of the same physical input stream locations |
| 489 |
is allowed, as well as overlaps. |
| 490 |
For example, if I<X> and I<Y> are G1 locations, |
| 491 |
it is possible that |
| 492 |
I<< s(X) < s(Y) < e(X) < e(Y) >>. |
| 493 |
|
| 494 |
=item * |
| 495 |
|
| 496 |
Even when there is a monotonic function from G1 location to physical |
| 497 |
input stream span, |
| 498 |
there will usually be gaps. |
| 499 |
For example, applications typically discard whitespace. |
| 500 |
This means that if I<W> is the physical input stream location of |
| 501 |
a whitespace character, |
| 502 |
there will be no G1 location I<X> such that |
| 503 |
I<< s(X) <= W <= e(X) >>. |
| 504 |
|
| 505 |
=back |
| 506 |
|
| 507 |
To cope with these situations, |
| 508 |
the following rules are used when |
| 509 |
translating G1 locations into literal substrings |
| 510 |
of the physical input stream. |
| 511 |
|
| 512 |
=over 4 |
| 513 |
|
| 514 |
=item * |
| 515 |
|
| 516 |
If [I<X ... Y>] is a G1 range, |
| 517 |
and |
| 518 |
I<< s(X) < e(Y) >>, |
| 519 |
the literal will be |
| 520 |
substring made of the characters in the |
| 521 |
physical input stream range [I<s(X) ... e(Y)>]. |
| 522 |
|
| 523 |
=item * |
| 524 |
|
| 525 |
If I<< s(X) >= e(Y) >>, |
| 526 |
the literal will be the empty string. |
| 527 |
|
| 528 |
=back |
| 529 |
|
| 530 |
For applications which read the physical input stream in lexical order, |
| 531 |
without skipping forward, |
| 532 |
the above rules will work as expected. |
| 533 |
For other applications, |
| 534 |
the above may be "close enough". |
| 535 |
But some applications |
| 536 |
may want to use custom logic to reassemble the input |
| 537 |
from the physical input stream. |
| 538 |
The L</"literal()"> method can assist in this process. |
| 539 |
|
| 540 |
=head1 The life cycle of a recognizer |
| 541 |
|
| 542 |
This describes the life cycle of a recognizer |
| 543 |
which has only one parse series. |
| 544 |
Your recognizer has only one parse series |
| 545 |
unless it |
| 546 |
calls the L<C<series_restart()>|"series_restart()"> method. |
| 547 |
Use of multiple parse series is an advanced technique, |
| 548 |
one which most applications will not need. |
| 549 |
Full details about parse series are in |
| 550 |
L<a separate document|Marpa::R2::Semantics::Phases>. |
| 551 |
|
| 552 |
=head2 The Initial Phase |
| 553 |
|
| 554 |
The B<Initial Phase> |
| 555 |
begins when the recognizer is created with the |
| 556 |
calls the L<C<new()>|/"Constructor"> method. |
| 557 |
It ends when |
| 558 |
the L<C<read()>|"read()"> method |
| 559 |
is called. |
| 560 |
It will also end, of course, if |
| 561 |
the recognizer is destroyed, |
| 562 |
but most applications will want to |
| 563 |
continue into the next phase. |
| 564 |
Very little can happen in this phase. |
| 565 |
It is possible to change some recognizer settings |
| 566 |
using the L<C<set()>|/"set()"> method. |
| 567 |
|
| 568 |
=head2 The Reading Phase |
| 569 |
|
| 570 |
The B<Reading Phase> of a recognizer |
| 571 |
begins when |
| 572 |
it calls the L<C<read()>|"read()"> method. |
| 573 |
It ends when it first calls |
| 574 |
the L<C<value()>|"value()"> method. |
| 575 |
The Reading Phase will also end, of course, if |
| 576 |
the recognizer is destroyed, |
| 577 |
but most applications will want to |
| 578 |
continue into the next phase. |
| 579 |
During this phase, it is possible to add other |
| 580 |
input strings to the virtual input, |
| 581 |
by calling |
| 582 |
the L<C<resume()>|"resume()"> method. |
| 583 |
|
| 584 |
=head2 The Evaluation Phase |
| 585 |
|
| 586 |
The B<Evaluation Phase> of a SLIF recognizer |
| 587 |
begins when |
| 588 |
it first calls the L<C<value()>|"value()"> method, |
| 589 |
which returns the result of the first parse tree. |
| 590 |
If there were no parses, |
| 591 |
the L<C<value()>|"value()"> method will return |
| 592 |
a Perl C<undef>. |
| 593 |
|
| 594 |
The L<C<value()>|"value()"> method |
| 595 |
may be called more than once during the Evaluation Phase. |
| 596 |
The second and later calls of |
| 597 |
the L<C<value()>|"value()"> method will return |
| 598 |
the result of the next parse tree. |
| 599 |
When there are no more parse trees, |
| 600 |
the L<C<value()>|"value()"> method will return |
| 601 |
a Perl C<undef> |
| 602 |
The L<C<resume()>|"resume()"> method should |
| 603 |
B<not> be called during Evaluation Phase. |
| 604 |
|
| 605 |
=head2 For more details |
| 606 |
|
| 607 |
In the above, we have described the life cycle for |
| 608 |
recognizers which have |
| 609 |
only one parse series. |
| 610 |
A recognizer will have only one parse series, |
| 611 |
unless it calls |
| 612 |
the L<C<series_restart()>|"series_restart()"> method. |
| 613 |
|
| 614 |
Using multiple parse series, |
| 615 |
an application can run |
| 616 |
the SLIF recognizer several times on the same |
| 617 |
virtual input stream. |
| 618 |
More detail |
| 619 |
about the recognizer's life cycle, |
| 620 |
including a full treatment of parse series, |
| 621 |
is in |
| 622 |
L<a separate document|Marpa::R2::Semantics::Phases>. |
| 623 |
|
| 624 |
=head1 Recognizer settings |
| 625 |
|
| 626 |
The B<recognizer settings> are the named arguments |
| 627 |
accepted by |
| 628 |
the recognizer setting-aware methods. |
| 629 |
The B<recognizer setting-aware methods> |
| 630 |
are the L<C<new()>|/"Constructor">, |
| 631 |
L<C<set()>|/"set()"> and |
| 632 |
L<C<series_restart()>|"series_restart()"> methods. |
| 633 |
Not every recognizer setting-aware method accepts all of the settings. |
| 634 |
The details are given below, by setting. |
| 635 |
|
| 636 |
=head2 end |
| 637 |
|
| 638 |
Most users will not need this setting. |
| 639 |
The L<C<end>|/"end"> setting |
| 640 |
specifies the parse end, as a G1 location. |
| 641 |
The default is for the parse to end where the input did, |
| 642 |
so that the parse returned is of the entire virtual input stream. |
| 643 |
The L<C<end>|/"end"> setting is only allowed in |
| 644 |
the L<C<new()>|/"Constructor"> |
| 645 |
and L<C<series_restart()>|"series_restart()"> methods. |
| 646 |
|
| 647 |
=head2 event_is_active |
| 648 |
|
| 649 |
=for Marpa::R2::Display |
| 650 |
name: SLIF recce event_is_active named arg example |
| 651 |
normalize-whitespace: 1 |
| 652 |
|
| 653 |
$slr = Marpa::R2::Scanless::R->new( |
| 654 |
{ grammar => $grammar, |
| 655 |
semantics_package => 'My_Actions', |
| 656 |
event_is_active => { 'before c' => 1, 'after b' => 0 } |
| 657 |
} |
| 658 |
); |
| 659 |
|
| 660 |
=for Marpa::R2::Display::End |
| 661 |
|
| 662 |
The C<event_is_active> recognizer setting |
| 663 |
changes the activation setting of events. |
| 664 |
Its value should be a reference to a hash, |
| 665 |
in which the key of every entry is an event name, |
| 666 |
and its value is either 0 or 1. |
| 667 |
If the value is 1, |
| 668 |
the event named in the hash key will be activated |
| 669 |
when the recognizer starts. |
| 670 |
If the value is 0, |
| 671 |
the event named in the hash key will be inactive |
| 672 |
when the recognizer starts. |
| 673 |
The C<event_is_active> setting is only allowed |
| 674 |
with the L<recognizer's C<new() method>|/"Constructor">. |
| 675 |
|
| 676 |
The setting in the |
| 677 |
C<event_is_active> hash |
| 678 |
overrides the activation setting in the grammar. |
| 679 |
The setting will be in effect |
| 680 |
before events at earleme 0 are triggered, |
| 681 |
and before any of the input stream is read. |
| 682 |
The L<C<activate()> method|/"activate()"> |
| 683 |
can also be used to change an event's activation |
| 684 |
setting for events that trigger after earleme 0. |
| 685 |
But |
| 686 |
events at earleme 0 |
| 687 |
trigger during |
| 688 |
the L<recognizer's C<new() method>|/"Constructor"> -- |
| 689 |
they can not be affected |
| 690 |
by calls of the C<activate()> method. |
| 691 |
|
| 692 |
If an event is initialized to inactive |
| 693 |
in the grammar, |
| 694 |
the C<event_is_active> recognizer setting |
| 695 |
is the only way |
| 696 |
for a recognizer |
| 697 |
to allow that event to be active |
| 698 |
at earleme 0. |
| 699 |
Similarly, |
| 700 |
if an event is initialized to active |
| 701 |
in the grammar, |
| 702 |
the C<event_is_active> recognizer setting |
| 703 |
is the only way |
| 704 |
for a recognizer |
| 705 |
to set that event |
| 706 |
to be inactive |
| 707 |
at earleme 0. |
| 708 |
|
| 709 |
=head2 exhaustion |
| 710 |
|
| 711 |
The C<exhaustion> recognizer setting |
| 712 |
determines what happens when asynchronous parse exhaustion occurs. |
| 713 |
Intuitively, "asynchronous" parse exhaustion is parse |
| 714 |
exhaustion at a point when control would not normally |
| 715 |
return to the application. |
| 716 |
The C<exhaustion> setting is allowed |
| 717 |
in any call of any of the recognizer setting-aware methods. |
| 718 |
For details |
| 719 |
see the |
| 720 |
L<description of exhaustion parse events|Marpa::R2::Event/"Exhaustion events">. |
| 721 |
|
| 722 |
The value |
| 723 |
of the C<exhaustion> recognizer setting |
| 724 |
must be either "C<fatal>" |
| 725 |
or "C<event>". |
| 726 |
"C<fatal>" is the default. |
| 727 |
If the value is "C<fatal>", |
| 728 |
asynchronous parse exhaustion is treated as an error, |
| 729 |
and an exception is thrown. |
| 730 |
If the value is "C<event>", |
| 731 |
an event occurs |
| 732 |
as described in the |
| 733 |
L<section on exhaustion parse events|Marpa::R2::Event/"Exhaustion events">. |
| 734 |
|
| 735 |
=head2 grammar |
| 736 |
|
| 737 |
The value of the C<grammar> setting must be |
| 738 |
a SLIF grammar object. |
| 739 |
The C<new()> method is required to have |
| 740 |
a C<grammar> setting. |
| 741 |
The C<grammar> setting is only allowed |
| 742 |
by the L<C<new() method>|/"Constructor">. |
| 743 |
Once the recognizer is created, the grammar cannot be |
| 744 |
changed. |
| 745 |
|
| 746 |
=head2 max_parses |
| 747 |
|
| 748 |
If non-zero, causes a fatal error when that number |
| 749 |
of parse results is exceeded. |
| 750 |
C<max_parses> is useful to |
| 751 |
limit CPU usage and output length when testing |
| 752 |
and debugging. |
| 753 |
Stable and production applications may |
| 754 |
prefer to count the number of parses, |
| 755 |
and take a less Draconian response when the |
| 756 |
count is exceeded. |
| 757 |
|
| 758 |
The value must be an integer. |
| 759 |
If it is zero, there will be no |
| 760 |
limit on the number of parse results returned. |
| 761 |
The default is for |
| 762 |
there to be no limit. |
| 763 |
The C<max_parses> setting is valid in all |
| 764 |
calls of the recognizer setting-aware methods. |
| 765 |
|
| 766 |
=head2 ranking_method |
| 767 |
|
| 768 |
The C<ranking_method> |
| 769 |
is only allowed in |
| 770 |
calls of the L<C<new()>|/"Constructor"> method. |
| 771 |
The value must be a string: |
| 772 |
one of "C<none>", |
| 773 |
"C<rule>", |
| 774 |
or "C<high_rule_only>". |
| 775 |
When the value is "C<none>", Marpa returns the parse results |
| 776 |
in arbitrary order. |
| 777 |
This is the default. |
| 778 |
|
| 779 |
The "C<rule>" |
| 780 |
and "C<high_rule_only>" ranking methods |
| 781 |
allows the user |
| 782 |
to control the order |
| 783 |
in which parse results are returned by |
| 784 |
the C<value> method, |
| 785 |
and to exclude some parse results from the parse series. |
| 786 |
For details, see L<the document |
| 787 |
on parse order|Marpa::R2::Semantics::Order>. |
| 788 |
|
| 789 |
|
| 790 |
=head2 rejection |
| 791 |
|
| 792 |
The C<rejection> recognizer setting |
| 793 |
determines what happens when all tokens are rejected by the |
| 794 |
G1 parser. |
| 795 |
The C<rejection> setting is allowed |
| 796 |
in any call of any of the recognizer setting-aware methods. |
| 797 |
The value must be either "C<fatal>" |
| 798 |
or "C<event>". |
| 799 |
"C<fatal>" is the default. |
| 800 |
|
| 801 |
If the value is "C<fatal>", |
| 802 |
rejection of all tokens is treated as an error, |
| 803 |
and an exception is thrown. |
| 804 |
If the value is "C<event>", |
| 805 |
an event occurs |
| 806 |
as described in the |
| 807 |
L<section on rejection parse events|Marpa::R2::Event/"Rejection events">. |
| 808 |
|
| 809 |
=head2 semantics_package |
| 810 |
|
| 811 |
Sets the semantic package for the recognizer. |
| 812 |
This setting takes precedence |
| 813 |
over any package implied by the blessing of the per-parse arguments to |
| 814 |
the SLIF recognizer's C<value()> method. |
| 815 |
The C<semantics_package> recognizer setting |
| 816 |
is used when resolving action names to |
| 817 |
fully qualified Perl names. |
| 818 |
For more details on the SLIF semantics, |
| 819 |
see the L<document on SLIF |
| 820 |
semantics|Marpa::R2::Semantics>. |
| 821 |
|
| 822 |
The L<C<semantics_package>|/"semantics_package"> setting is only allowed |
| 823 |
when a new parse series is begun. |
| 824 |
That is, it is only allowed in calls |
| 825 |
of the L<C<new()>|/"Constructor"> |
| 826 |
and L<C<series_restart()>|"series_restart()"> methods. |
| 827 |
The C<semantics_package> recognizer setting |
| 828 |
should not be confused with the |
| 829 |
L<SLIF's |
| 830 |
C<bless_package> grammar setting|Marpa::R2::Scanless::G/"bless_package">. |
| 831 |
The two are not closely related. |
| 832 |
|
| 833 |
=head2 too_many_earley_items |
| 834 |
|
| 835 |
The C<too_many_earley_items> setting is optional, |
| 836 |
and very few applications will need it. |
| 837 |
If specified, it sets the B<Earley item warning threshold> to |
| 838 |
a value other than its default. |
| 839 |
If an Earley set becomes larger than the |
| 840 |
Earley item warning threshold, |
| 841 |
a recognizer event is generated, |
| 842 |
and |
| 843 |
a warning is printed to the trace file handle. |
| 844 |
|
| 845 |
Marpa parses from any BNF, |
| 846 |
and can handle grammars and inputs which produce very large |
| 847 |
Earley sets. |
| 848 |
But parsing that involves very large Earley sets can be slow. |
| 849 |
|
| 850 |
By default, Marpa calculates |
| 851 |
an Earley item warning threshold |
| 852 |
for the G1 recognizer |
| 853 |
based on the size of the |
| 854 |
G1 grammar, |
| 855 |
and for each L0 recognizer based on the size |
| 856 |
of the L0 grammar. |
| 857 |
The default thresholds will never be less than 100. |
| 858 |
The default is the result of considerable experience |
| 859 |
and almost all users will be happy with it. |
| 860 |
|
| 861 |
If the |
| 862 |
Earley item warning threshold is changed from its default, |
| 863 |
the change applies to both L0 and G1 -- currently |
| 864 |
there is no way to set them separately. |
| 865 |
If the Earley item warning threshold is set to 0, |
| 866 |
no recognizer event is generated, |
| 867 |
and |
| 868 |
warnings about large Earley sets are turned off. |
| 869 |
An Earley item threshold warning almost always |
| 870 |
indicates a serious issue, |
| 871 |
and turning these warnings off will |
| 872 |
rarely be something |
| 873 |
that an application wants to do. |
| 874 |
|
| 875 |
The C<too_many_earley_items> setting is allowed |
| 876 |
in any call of any of the recognizer setting-aware methods. |
| 877 |
|
| 878 |
=head2 trace_terminals |
| 879 |
|
| 880 |
If non-zero, traces the lexemes -- |
| 881 |
those tokens passed from the L0 parser to |
| 882 |
the G1 parser. |
| 883 |
This recognizer setting is the best way to follow |
| 884 |
what the L0 parser is doing, |
| 885 |
and it is also very helpful for tracing the G1 parser. |
| 886 |
The C<trace_terminals> setting is allowed |
| 887 |
in any call of any of the recognizer setting-aware methods. |
| 888 |
|
| 889 |
=head2 trace_values |
| 890 |
|
| 891 |
The value of the C<trace_values> setting is a numeric trace level. |
| 892 |
If the |
| 893 |
numeric trace level is 1, Marpa prints tracing information as values |
| 894 |
are computed in the evaluation stack. A trace level of 0 turns |
| 895 |
value tracing off, which is the default. Traces are written to the |
| 896 |
trace file handle. |
| 897 |
The C<trace_values> setting is allowed |
| 898 |
in any call of any of the recognizer setting-aware methods. |
| 899 |
|
| 900 |
=head2 trace_file_handle |
| 901 |
|
| 902 |
The value is a file handle. |
| 903 |
Trace output and warning messages |
| 904 |
go to the trace file handle. |
| 905 |
By default, the trace file handle is inherited from the |
| 906 |
grammar. |
| 907 |
The C<trace_file_handle> setting is allowed |
| 908 |
in any call of any of the recognizer setting-aware methods. |
| 909 |
|
| 910 |
=head1 Constructor |
| 911 |
|
| 912 |
=for Marpa::R2::Display |
| 913 |
name: Scanless recognizer synopsis |
| 914 |
partial: 1 |
| 915 |
normalize-whitespace: 1 |
| 916 |
|
| 917 |
my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } ); |
| 918 |
|
| 919 |
=for Marpa::R2::Display::End |
| 920 |
|
| 921 |
The C<new()> method is the constructor for SLIF recognizers. |
| 922 |
The arguments |
| 923 |
to the C<new()> constructor must be one or more hashes of named arguments, |
| 924 |
where each hash key is a recognizer setting. |
| 925 |
The L<C<grammar>|/"grammar"> recognizer setting is required. |
| 926 |
All other recognizer settings are optional. |
| 927 |
For more on recognizer settings, |
| 928 |
see |
| 929 |
L<the section describing them|/"Recognizer settings">. |
| 930 |
|
| 931 |
=head1 Basic mutators |
| 932 |
|
| 933 |
=head2 ambiguous() |
| 934 |
|
| 935 |
=for Marpa::R2::Display |
| 936 |
name: Tutorial 2 synopsis |
| 937 |
partial: 1 |
| 938 |
normalize-whitespace: 1 |
| 939 |
|
| 940 |
if ( my $ambiguous_status = $recce->ambiguous() ) { |
| 941 |
chomp $ambiguous_status; |
| 942 |
die "Parse is ambiguous\n", $ambiguous_status; |
| 943 |
} |
| 944 |
|
| 945 |
=for Marpa::R2::Display::End |
| 946 |
|
| 947 |
This method should be called after the C<read()> method. |
| 948 |
If there is exactly one parse, it returns the empty string. |
| 949 |
If there is no parse, it returns a non-empty string indicating that fact. |
| 950 |
If there are two or more parses, |
| 951 |
it returns a non-empty string describing the ambiguity. |
| 952 |
|
| 953 |
Applications should only test the returned string to see if it is |
| 954 |
empty or non-empty. |
| 955 |
The non-empty strings are intended only for reading by |
| 956 |
humans -- their exact format is subject to change. |
| 957 |
|
| 958 |
When C<ambiguous()> detects an ambiguous parse, |
| 959 |
it puts |
| 960 |
the recognizer into "forest mode", |
| 961 |
so that it can examine the parse. |
| 962 |
As long as the recognizer is in forest mode, |
| 963 |
calls to the L<C<value()>|"value()"> method |
| 964 |
will produce fatal errors. |
| 965 |
Forest mode can be cleared using the |
| 966 |
L<C<series_restart()>|"series_restart()"> method. |
| 967 |
This will start a new parse series in "tree mode", |
| 968 |
which will allow calls to the |
| 969 |
L<C<value()>|"value()"> method |
| 970 |
to succeed. |
| 971 |
|
| 972 |
=head2 read() |
| 973 |
|
| 974 |
=for Marpa::R2::Display |
| 975 |
name: Scanless recognizer synopsis |
| 976 |
partial: 1 |
| 977 |
normalize-whitespace: 1 |
| 978 |
|
| 979 |
$recce->read($p_input_string); |
| 980 |
|
| 981 |
=for Marpa::R2::Display::End |
| 982 |
|
| 983 |
=for Marpa::R2::Display |
| 984 |
name: SLIF external read example |
| 985 |
partial: 1 |
| 986 |
normalize-whitespace: 1 |
| 987 |
|
| 988 |
$recce->read( \$string, 0, 0 ); |
| 989 |
|
| 990 |
=for Marpa::R2::Display::End |
| 991 |
|
| 992 |
Given a pointer to a physical input stream and, |
| 993 |
optionally, a span specifying an input string within it, |
| 994 |
C<read()> parses the input string according to the grammar. |
| 995 |
C<read()> returns success if it parses to the end |
| 996 |
of the input string, |
| 997 |
or if it triggers a SLIF parse event. |
| 998 |
Only a single call to C<read()> |
| 999 |
is allowed for a SLIF |
| 1000 |
recognizer. |
| 1001 |
|
| 1002 |
The first argument of C<read()> is a pointer to the physical |
| 1003 |
input stream which, |
| 1004 |
by default, |
| 1005 |
will be |
| 1006 |
exactly the same as the virtual input stream. |
| 1007 |
To specify the input string, |
| 1008 |
C<read()> recognizes |
| 1009 |
optional second and third arguments and treats them |
| 1010 |
as the start and length |
| 1011 |
of a L<span|/"Spans"> of the physical input stream. |
| 1012 |
The default start location is zero. |
| 1013 |
The default length is -1. |
| 1014 |
Negative locations and lengths are |
| 1015 |
interpreted as L<described above|/"Spans">. |
| 1016 |
|
| 1017 |
If a SLIF parse event occurs during the C<read()> method, |
| 1018 |
the current location is set to the trigger location. |
| 1019 |
SLIF parse events are described in detail in |
| 1020 |
L<a separate document|Marpa::R2::Event>. |
| 1021 |
If no SLIF parse event triggers, |
| 1022 |
and the parse reaches the end of the input string without a |
| 1023 |
failure, |
| 1024 |
the current location is set to the end of the input string. |
| 1025 |
|
| 1026 |
On success, C<read()> returns the current |
| 1027 |
physical input stream location. |
| 1028 |
This value may be zero. |
| 1029 |
The call is considered successful |
| 1030 |
if it reaches the end of input string, |
| 1031 |
or if a SLIF parse event triggers. |
| 1032 |
On failure, C<read()> throws an exception. |
| 1033 |
|
| 1034 |
=head2 series_restart() |
| 1035 |
|
| 1036 |
=for Marpa::R2::Display |
| 1037 |
name: SLIF recognizer series_restart() synopsis |
| 1038 |
normalize-whitespace: 1 |
| 1039 |
|
| 1040 |
$recce->series_restart( { end => $i } ); |
| 1041 |
|
| 1042 |
=for Marpa::R2::Display::End |
| 1043 |
|
| 1044 |
The C<series_restart()> method ends the current parse series, |
| 1045 |
and starts another. |
| 1046 |
Parse series are described L<in another document|Marpa::R2::Semantics::Phases>. |
| 1047 |
The C<series_restart()> method allows, |
| 1048 |
as optional arguments, hashes whose key-value pairs |
| 1049 |
are L<recognizer settings|/"Recognizer settings">. |
| 1050 |
|
| 1051 |
The C<series_restart()> method |
| 1052 |
cannot change the C<grammar> recognizer setting. |
| 1053 |
If any other recognizer setting is not specified explicitly, |
| 1054 |
it is reset to its default. |
| 1055 |
If an application wants an explicit recognizer setting to persist |
| 1056 |
into a new parse series, |
| 1057 |
it must specify that setting explicitly in the new parse series. |
| 1058 |
C<series_restart()> is particularly useful with |
| 1059 |
the |
| 1060 |
C<end> and C<semantics_package> named arguments. |
| 1061 |
|
| 1062 |
The C<series_restart()> method |
| 1063 |
must be called before L<C<value()>|"value()"> |
| 1064 |
when L<C<ambiguous()>|"ambiguous()"> |
| 1065 |
detects an ambiguous parse and the application needs to get the parse values. |
| 1066 |
|
| 1067 |
=head2 set() |
| 1068 |
|
| 1069 |
=for Marpa::R2::Display |
| 1070 |
name: SLIF recognizer set() synopsis |
| 1071 |
normalize-whitespace: 1 |
| 1072 |
|
| 1073 |
$recce->set( { max_parses => 42 } ); |
| 1074 |
|
| 1075 |
=for Marpa::R2::Display::End |
| 1076 |
|
| 1077 |
This method allows recognizer settings to be changed after a SLIF |
| 1078 |
grammar is created. |
| 1079 |
The arguments to |
| 1080 |
C<set()> must be one or more hashes whose key-value pairs |
| 1081 |
are recognizer settings and their values. |
| 1082 |
The allowed recognizer settings are |
| 1083 |
L<described above|/"Recognizer settings">. |
| 1084 |
|
| 1085 |
=head2 value() |
| 1086 |
|
| 1087 |
=for Marpa::R2::Display |
| 1088 |
name: Scanless recognizer synopsis |
| 1089 |
partial: 1 |
| 1090 |
normalize-whitespace: 1 |
| 1091 |
|
| 1092 |
my $value_ref = $recce->value( $self ); |
| 1093 |
|
| 1094 |
=for Marpa::R2::Display::End |
| 1095 |
|
| 1096 |
The C<value> method call evaluates the next parse tree |
| 1097 |
in the parse series, |
| 1098 |
and returns a reference to the parse result for that parse tree. |
| 1099 |
If there are no more parse trees, |
| 1100 |
the C<value> method returns C<undef>. |
| 1101 |
There are zero parse trees if there was no valid parse |
| 1102 |
of the input according to the grammar. |
| 1103 |
There will be more than one parse tree if the parse |
| 1104 |
was ambiguous. |
| 1105 |
|
| 1106 |
The C<value()> method allows one optional argument. |
| 1107 |
If provided, the argument |
| 1108 |
explicitly specifies the per-parse argument for the |
| 1109 |
parse tree. |
| 1110 |
This per-parse argument can be a Perl scalar of any type, |
| 1111 |
but the most useful |
| 1112 |
type for a per-parse argument is a reference |
| 1113 |
(blessed or unblessed) to a hash or to an array. |
| 1114 |
The per-parse argument, |
| 1115 |
if provided, |
| 1116 |
will be the first argument of all |
| 1117 |
Perl semantics closures. |
| 1118 |
When data does not conveniently fit into the bottom-up |
| 1119 |
flow of parse tree evaluation, |
| 1120 |
the per-parse argument |
| 1121 |
is useful for sharing it within |
| 1122 |
the tree. |
| 1123 |
Symbol tables are one example of the kind of data which parses often |
| 1124 |
require, but which it is not convenient to accumulate bottom-up. |
| 1125 |
|
| 1126 |
If the L<C<semantics_package>|/"semantics_package"> |
| 1127 |
setting of the SLIF |
| 1128 |
recognizer was not specified, |
| 1129 |
Marpa will use the package into which the per-parse argument was blessed |
| 1130 |
as the semantics package. |
| 1131 |
(As a reminder, the semantics package is the package |
| 1132 |
in which Marpa looks for the parse's Perl semantic closures.) |
| 1133 |
|
| 1134 |
When the per-parse argument of the C<value()> method is the |
| 1135 |
source of the semantics package, |
| 1136 |
all calls to the C<value()> method in the same |
| 1137 |
parse series must have a per-parse |
| 1138 |
argument that |
| 1139 |
specifies the same semantics package. |
| 1140 |
More precisely, |
| 1141 |
if the per-parse argument of the first |
| 1142 |
call of the C<value()> method |
| 1143 |
in a parse series |
| 1144 |
is |
| 1145 |
the source of the semantics package, |
| 1146 |
it will be a fatal error if any subsequent |
| 1147 |
C<value()> call in that parse series |
| 1148 |
|
| 1149 |
=over 4 |
| 1150 |
|
| 1151 |
=item * |
| 1152 |
|
| 1153 |
does not have a per-parse argument; |
| 1154 |
|
| 1155 |
=item * |
| 1156 |
|
| 1157 |
if that per-parse argument is not blessed; or |
| 1158 |
|
| 1159 |
=item * |
| 1160 |
|
| 1161 |
if that per-parse argument is blessed into a different package. |
| 1162 |
|
| 1163 |
=back |
| 1164 |
|
| 1165 |
=head1 Mutators for external scanning |
| 1166 |
|
| 1167 |
=head2 activate() |
| 1168 |
|
| 1169 |
=for Marpa::R2::Display |
| 1170 |
name: SLIF activate() method synopsis |
| 1171 |
partial: 1 |
| 1172 |
normalize-whitespace: 1 |
| 1173 |
|
| 1174 |
$recce->activate($_, 0) for @events; |
| 1175 |
|
| 1176 |
=for Marpa::R2::Display::End |
| 1177 |
|
| 1178 |
The C<activate()> method allows the recognizer to deactivate and reactivate |
| 1179 |
SLIF parse events. |
| 1180 |
SLIF parse events are described in |
| 1181 |
L<a separate document|Marpa::R2::Event>. |
| 1182 |
|
| 1183 |
The C<activate()> method takes two arguments. |
| 1184 |
The first is the name of an event, and the second (optional) argument is |
| 1185 |
0 or 1. |
| 1186 |
If the argument is 0, the event is deactivated. |
| 1187 |
If the argument is 1, the event is activated. |
| 1188 |
An argument of 1 is the default. |
| 1189 |
Since an SLIF recognizer always starts with all defined events |
| 1190 |
activated, |
| 1191 |
0 will probably be more common as the second argument to |
| 1192 |
C<activate()> |
| 1193 |
|
| 1194 |
Though they are not reported until the call of the |
| 1195 |
C<read()> method, |
| 1196 |
location 0 events are triggered in the SLIF recognizer's |
| 1197 |
constructor, |
| 1198 |
before the C<activate()> method can be called. |
| 1199 |
Currently there is no way to deactivate |
| 1200 |
location zero events. |
| 1201 |
|
| 1202 |
The overhead imposed by events |
| 1203 |
can be reduced by using the C<activate()> method. |
| 1204 |
But making many calls to |
| 1205 |
the C<activate()> method purely for efficiency |
| 1206 |
purposes will be counter-productive. |
| 1207 |
Also, deactivated events still impose |
| 1208 |
some overhead, so if an event is never used, |
| 1209 |
it should be commented out in the SLIF DSL. |
| 1210 |
|
| 1211 |
=head2 lexeme_alternative() |
| 1212 |
|
| 1213 |
=for Marpa::R2::Display |
| 1214 |
name: SLIF lexeme_alternative() example |
| 1215 |
partial: 1 |
| 1216 |
normalize-whitespace: 1 |
| 1217 |
|
| 1218 |
if ( not defined $recce->lexeme_alternative($token_name) ) { |
| 1219 |
die |
| 1220 |
qq{Parser rejected token "$long_name" at position $start_of_lexeme, before "}, |
| 1221 |
substr( $string, $start_of_lexeme, 40 ), q{"}; |
| 1222 |
} |
| 1223 |
|
| 1224 |
=for Marpa::R2::Display::End |
| 1225 |
|
| 1226 |
The C<lexeme_alternative()> method |
| 1227 |
allows an external scanner to read |
| 1228 |
ambiguous tokens. |
| 1229 |
Most applications |
| 1230 |
will prefer the simpler L<C<lexeme_read()>|/"lexeme_read()">. |
| 1231 |
|
| 1232 |
C<lexeme_alternative()> takes one or two arguments. |
| 1233 |
The first argument, |
| 1234 |
which is required, |
| 1235 |
is the name of a symbol to be read |
| 1236 |
at the current location. |
| 1237 |
The second argument, |
| 1238 |
which is optional, |
| 1239 |
is the value of the symbol. |
| 1240 |
The value argument is interpreted as described for C<lexeme_read()>. |
| 1241 |
|
| 1242 |
Any number of tokens may be read using C<lexeme_alternative()> |
| 1243 |
without advancing the current location. |
| 1244 |
This allows an application to use ambiguous tokens. |
| 1245 |
To complete reading at a G1 location, |
| 1246 |
and advance the current G1 location to the next G1 location, |
| 1247 |
use the L<C<lexeme_complete()>|/"lexeme_complete()"> method. |
| 1248 |
|
| 1249 |
On success, returns a non-negative number, |
| 1250 |
which may be zero. |
| 1251 |
Returns C<undef> if the token was rejected. |
| 1252 |
Failures are thrown as exceptions. |
| 1253 |
|
| 1254 |
=head2 lexeme_complete() |
| 1255 |
|
| 1256 |
=for Marpa::R2::Display |
| 1257 |
name: SLIF lexeme_alternative() example |
| 1258 |
partial: 1 |
| 1259 |
normalize-whitespace: 1 |
| 1260 |
|
| 1261 |
next TOKEN |
| 1262 |
if $recce->lexeme_complete( $start_of_lexeme, |
| 1263 |
( length $lexeme ) ); |
| 1264 |
|
| 1265 |
=for Marpa::R2::Display::End |
| 1266 |
|
| 1267 |
The C<lexeme_complete()> method allows an external scanner to read |
| 1268 |
ambiguous tokens. |
| 1269 |
It completes the reading of a set of tokens specified by |
| 1270 |
one or more calls of the L<C<lexeme_alternative()>|/"lexeme_alternative()"> |
| 1271 |
method |
| 1272 |
at a G1 location. |
| 1273 |
Most applications |
| 1274 |
will prefer the simpler L<C<lexeme_read()>|/"lexeme_read()"> method. |
| 1275 |
|
| 1276 |
The C<lexeme_complete()> method |
| 1277 |
requires two arguments, |
| 1278 |
which represent the start and length parameters of |
| 1279 |
a span in the physical input stream. |
| 1280 |
The span is interpreted, |
| 1281 |
and G1 location and current input stream location |
| 1282 |
are adjusted, |
| 1283 |
as described for |
| 1284 |
L<the C<lexeme_read()> method|/"lexeme_read()">. |
| 1285 |
|
| 1286 |
SLIF parse events may occur during |
| 1287 |
the L<C<lexeme_complete()>|/"lexeme_complete()"> method, |
| 1288 |
as described for |
| 1289 |
L<the C<lexeme_read()> method|/"lexeme_read()">. |
| 1290 |
|
| 1291 |
B<Return value:> |
| 1292 |
On success, C<lexeme_complete()> |
| 1293 |
returns the new current location. |
| 1294 |
This will never be location zero, because a succesful |
| 1295 |
call of C<lexeme_complete()> always advances the location. |
| 1296 |
Failure is thrown as an exception. |
| 1297 |
|
| 1298 |
=head2 lexeme_priority_set() |
| 1299 |
|
| 1300 |
=for Marpa::R2::Display |
| 1301 |
name: SLIF recognizer lexeme_priority_set() synopsis |
| 1302 |
normalize-whitespace: 1 |
| 1303 |
|
| 1304 |
$recce->lexeme_priority_set( 'prefix lexeme', -1 ); |
| 1305 |
|
| 1306 |
=for Marpa::R2::Display::End |
| 1307 |
|
| 1308 |
Takes as its first argument the name of a lexeme |
| 1309 |
and changes the priority of that lexeme to the value |
| 1310 |
of its second argument. |
| 1311 |
Both arguments are required. |
| 1312 |
|
| 1313 |
Changing the lexeme priority is a very flexible |
| 1314 |
technique. |
| 1315 |
It can, in effect, allow an application |
| 1316 |
to switch lexers. |
| 1317 |
|
| 1318 |
On success, returns the old priority value. |
| 1319 |
Failure is thrown. |
| 1320 |
|
| 1321 |
=head2 lexeme_read() |
| 1322 |
|
| 1323 |
=for Marpa::R2::Display |
| 1324 |
name: SLIF read/resume example |
| 1325 |
partial: 1 |
| 1326 |
normalize-whitespace: 1 |
| 1327 |
|
| 1328 |
$re->lexeme_read( 'lstring', $start, $length, $value ) // die; |
| 1329 |
|
| 1330 |
=for Marpa::R2::Display::End |
| 1331 |
|
| 1332 |
The C<lexeme_read()> method reads a single, unambiguous, lexeme. |
| 1333 |
It takes four arguments, only the first of which is required. |
| 1334 |
The first argument is the lexeme's symbol name. |
| 1335 |
The second and third arguments specify the span in |
| 1336 |
the physical input stream. |
| 1337 |
The last argument specifies the value of the lexeme. |
| 1338 |
|
| 1339 |
In the span specified by |
| 1340 |
the second and third arguments, |
| 1341 |
the start location defaults to the current location. |
| 1342 |
If the |
| 1343 |
L<pause span|Marpa::R2::Event/"Pause span and pause lexeme"> |
| 1344 |
is defined, |
| 1345 |
and the start of the pause lexeme is |
| 1346 |
the same as the current location, |
| 1347 |
length defaults to the length of the pause span. |
| 1348 |
Otherwise length defaults to -1. |
| 1349 |
Negative values are allowed and are interpreted |
| 1350 |
as L<described above|/"Spans">. |
| 1351 |
|
| 1352 |
The span will be interpreted as the section |
| 1353 |
of the physical input stream |
| 1354 |
that corresponds to the current G1 set. |
| 1355 |
(As a reminder, the G1 set consists |
| 1356 |
of the tokens read at single G1 location.) |
| 1357 |
This correspondence between the span and the token |
| 1358 |
may be artificial, but a span is defined for every token, |
| 1359 |
even if only by default. |
| 1360 |
|
| 1361 |
The fourth argument specifies the lexeme value. |
| 1362 |
The lexeme value plays an important role in the SLIF's semantics. |
| 1363 |
More details on the SLIF's semantics are in |
| 1364 |
L<a document dedicated to them|Marpa::R2::Semantics>. |
| 1365 |
If the fourth argument |
| 1366 |
is omitted, |
| 1367 |
the lexeme value will be a string |
| 1368 |
containing the corresponding substring |
| 1369 |
of the input stream. |
| 1370 |
Omitting the value argument does not have the same |
| 1371 |
effect as passing an explicit Perl C<undef>. |
| 1372 |
If the value argument is an explicit Perl C<undef>, |
| 1373 |
the lexeme value will be a Perl C<undef>. |
| 1374 |
|
| 1375 |
=for Marpa::R2::Display |
| 1376 |
ignore: 1 |
| 1377 |
|
| 1378 |
$recce->lexeme_read($symbol, $start, $length, $value) |
| 1379 |
|
| 1380 |
=for Marpa::R2::Display::End |
| 1381 |
|
| 1382 |
is roughly equivalent to |
| 1383 |
|
| 1384 |
=for Marpa::R2::Display |
| 1385 |
ignore: 1 |
| 1386 |
|
| 1387 |
$recce->lexeme_alternative($symbol, $value) |
| 1388 |
$recce->lexeme_complete($start, $length) |
| 1389 |
|
| 1390 |
=for Marpa::R2::Display::End |
| 1391 |
|
| 1392 |
Non-lexeme |
| 1393 |
SLIF parse events may trigger during the C<lexeme_read()> method. |
| 1394 |
Lexeme SLIF parse events are ignored because they are designed |
| 1395 |
to allow switching over to external scanning, and |
| 1396 |
make no sense when external scanning is already in progress. |
| 1397 |
SLIF parse events are described in detail in |
| 1398 |
L<a separate document|Marpa::R2::Event>. |
| 1399 |
|
| 1400 |
Current input stream location will be set |
| 1401 |
to C<$start+$length>. |
| 1402 |
If a SLIF parse event triggers, current input stream location |
| 1403 |
will be set to the trigger location. |
| 1404 |
Currently the trigger location and |
| 1405 |
C<$start+$length> will always be the same, |
| 1406 |
but that may change. |
| 1407 |
|
| 1408 |
When successful, |
| 1409 |
C<lexeme_read()> advances |
| 1410 |
the current G1 location by one. |
| 1411 |
The token read by C<lexeme_read()> |
| 1412 |
will start at the previous G1 location |
| 1413 |
and end at the new current G1 location. |
| 1414 |
The new current location in the input stream will |
| 1415 |
be at the end location of the new lexeme. |
| 1416 |
|
| 1417 |
On success, C<lexeme_read()> returns the new current physical |
| 1418 |
input stream location. |
| 1419 |
This will never be location zero, because lexemes cannot be zero length. |
| 1420 |
If the token was rejected, |
| 1421 |
C<lexeme_read()> returns a Perl C<undef>. |
| 1422 |
Failure is thrown as an exception. |
| 1423 |
|
| 1424 |
=head2 resume() |
| 1425 |
|
| 1426 |
=for Marpa::R2::Display |
| 1427 |
name: SLIF read/resume example |
| 1428 |
partial: 1 |
| 1429 |
normalize-whitespace: 1 |
| 1430 |
|
| 1431 |
my $re = Marpa::R2::Scanless::R->new( |
| 1432 |
{ grammar => $parser->{grammar}, |
| 1433 |
semantics_package => 'MarpaX::JSON::Actions' |
| 1434 |
} |
| 1435 |
); |
| 1436 |
my $length = length $string; |
| 1437 |
for ( |
| 1438 |
my $pos = $re->read( \$string ); |
| 1439 |
$pos < $length; |
| 1440 |
$pos = $re->resume() |
| 1441 |
) |
| 1442 |
{ |
| 1443 |
my ( $start, $length ) = $re->pause_span(); |
| 1444 |
my $value = substr $string, $start + 1, $length - 2; |
| 1445 |
$value = decode_string($value) if -1 != index $value, '\\'; |
| 1446 |
$re->lexeme_read( 'lstring', $start, $length, $value ) // die; |
| 1447 |
} ## end for ( my $pos = $re->read( \$string ); $pos < $length...) |
| 1448 |
my $per_parse_arg = bless {}, 'MarpaX::JSON::Actions'; |
| 1449 |
my $value_ref = $re->value($per_parse_arg); |
| 1450 |
return ${$value_ref}; |
| 1451 |
|
| 1452 |
=for Marpa::R2::Display::End |
| 1453 |
|
| 1454 |
The C<resume()> method resumes |
| 1455 |
the SLIF's internal scanning, |
| 1456 |
L<as described |
| 1457 |
above|/"Internal scanning">. |
| 1458 |
A physical input stream must already have |
| 1459 |
been specified using the |
| 1460 |
L<C<< $recce->read() >> method|/"read()">. |
| 1461 |
The C<resume()> method should only be called during the |
| 1462 |
Reading Phase. |
| 1463 |
|
| 1464 |
The C<resume()> method takes two optional arguments, |
| 1465 |
which represent the start and length parameters of |
| 1466 |
a span in the physical input stream. |
| 1467 |
The default start location is the current location. |
| 1468 |
The default length is -1. |
| 1469 |
Negative arguments are interpreted |
| 1470 |
as L<described above|/"Spans">. |
| 1471 |
|
| 1472 |
If a SLIF parse event occurs during the C<read()> method, |
| 1473 |
the current location is set to the trigger location. |
| 1474 |
SLIF parse events are described in detail in |
| 1475 |
L<a separate document|Marpa::R2::Event>. |
| 1476 |
If no SLIF parse event triggers, |
| 1477 |
and the parse reaches the end of the input string without a |
| 1478 |
failure, |
| 1479 |
the current location is set to the end of the input string. |
| 1480 |
|
| 1481 |
C<resume()> is considered successful |
| 1482 |
if it reads input to the end of input string, |
| 1483 |
or if a SLIF parse event triggers. |
| 1484 |
On success, C<resume()> returns |
| 1485 |
the new current location. |
| 1486 |
On unthrown failure, |
| 1487 |
C<resume()> returns a Perl C<undef>. |
| 1488 |
Currently, all failures are thrown. |
| 1489 |
|
| 1490 |
=head1 Accessors |
| 1491 |
|
| 1492 |
=head2 ambiguity_metric() |
| 1493 |
|
| 1494 |
=for Marpa::R2::Display |
| 1495 |
name: Scanless ambiguity_metric() synopsis |
| 1496 |
|
| 1497 |
my $ambiguity_metric = $recce->ambiguity_metric(); |
| 1498 |
|
| 1499 |
=for Marpa::R2::Display::End |
| 1500 |
|
| 1501 |
Succeeds and |
| 1502 |
returns 1 if there was an unambiguous parse, |
| 1503 |
in other words if there was exactly one parse tree. |
| 1504 |
Succeeds and |
| 1505 |
returns 2 or greater if the parse was ambiguous, |
| 1506 |
in other words if there was more than one parse tree. |
| 1507 |
Succeeds and returns 0 if there are no parse trees, |
| 1508 |
because parsing failed. |
| 1509 |
Currently, all other failures are thrown. |
| 1510 |
|
| 1511 |
When the return value is 2 or greater, |
| 1512 |
the return value is B<not> necessarily the parse count. |
| 1513 |
Instead, it is a value which is subject to change. |
| 1514 |
and on which an application should not rely. |
| 1515 |
The intent was that, some day, |
| 1516 |
return values of 2 or greater would represent a "metric" which |
| 1517 |
was cheap to compute, |
| 1518 |
but which |
| 1519 |
estimated the degree of ambiguity in some useful way. |
| 1520 |
The best metric is, of course, |
| 1521 |
would be the exact parse count, |
| 1522 |
but determining that is expensive. |
| 1523 |
|
| 1524 |
=head2 current_g1_location() |
| 1525 |
|
| 1526 |
=for Marpa::R2::Display |
| 1527 |
name: Scanless current_g1_location() synopsis |
| 1528 |
|
| 1529 |
my $current_g1_location = $recce->current_g1_location(); |
| 1530 |
|
| 1531 |
=for Marpa::R2::Display::End |
| 1532 |
|
| 1533 |
Returns the current G1 location. |
| 1534 |
|
| 1535 |
=head2 events() |
| 1536 |
|
| 1537 |
=for Marpa::R2::Display |
| 1538 |
name: SLIF events() method synopsis |
| 1539 |
normalize-whitespace: 1 |
| 1540 |
partial: 1 |
| 1541 |
|
| 1542 |
EVENT: |
| 1543 |
for my $event ( @{ $recce->events() } ) { |
| 1544 |
my ($name) = @{$event}; |
| 1545 |
push @actual_events, $name; |
| 1546 |
} |
| 1547 |
|
| 1548 |
=for Marpa::R2::Display::End |
| 1549 |
|
| 1550 |
The C<events()> method takes no arguments, |
| 1551 |
and returns an array of SLIF parse event descriptors. |
| 1552 |
It returns the empty array |
| 1553 |
if there were no event. |
| 1554 |
|
| 1555 |
SLIF parse events are described in detail in |
| 1556 |
L<a separate |
| 1557 |
document|Marpa::R2::Event>. |
| 1558 |
Each SLIF parse event descriptor is a reference to an array of one |
| 1559 |
or more elements. |
| 1560 |
The first element of every named event descriptor is a string |
| 1561 |
containing the name of the event. |
| 1562 |
Typically the name of the event is only element. |
| 1563 |
Other elements |
| 1564 |
will be as |
| 1565 |
L<described for each type of parse |
| 1566 |
event|Marpa::R2::Event/"Types of parse event">. |
| 1567 |
|
| 1568 |
Any other SLIF recognizer mutator |
| 1569 |
may clear the events. |
| 1570 |
It is expected that |
| 1571 |
an application interested in events |
| 1572 |
will call |
| 1573 |
the C<events()> method immediately after the |
| 1574 |
event-triggering event. |
| 1575 |
|
| 1576 |
Named events are returned in order by type: |
| 1577 |
|
| 1578 |
=over 4 |
| 1579 |
|
| 1580 |
=item * Lexeme events |
| 1581 |
|
| 1582 |
=item * Completion events |
| 1583 |
|
| 1584 |
=item * Nulling events |
| 1585 |
|
| 1586 |
=item * Prediction events |
| 1587 |
|
| 1588 |
=back |
| 1589 |
|
| 1590 |
Within each type, |
| 1591 |
the order of events is arbitrary. |
| 1592 |
|
| 1593 |
=head2 exhausted() |
| 1594 |
|
| 1595 |
=for Marpa::R2::Display |
| 1596 |
name: $recce->exhausted example |
| 1597 |
|
| 1598 |
my $exhausted_status = $recce->exhausted(); |
| 1599 |
|
| 1600 |
=for Marpa::R2::Display::End |
| 1601 |
|
| 1602 |
The exhausted method returns a Perl true if parsing in a SLIF |
| 1603 |
recognizer is |
| 1604 |
exhausted, and a Perl false otherwise. Parsing is exhausted when the |
| 1605 |
recognizer will not accept any further input. |
| 1606 |
|
| 1607 |
Marpa usually "does what you mean" in case of parse exhaustion, |
| 1608 |
but this method |
| 1609 |
allows the recognizer's exhaustion status to be discovered directly. |
| 1610 |
Parse exhaustion is discussed in detail in |
| 1611 |
L<a separate document|Marpa::R2::Exhaustion>. |
| 1612 |
|
| 1613 |
=head2 g1_location_to_span() |
| 1614 |
|
| 1615 |
=for Marpa::R2::Display |
| 1616 |
name: Scanless g1_location_to_span() synopsis |
| 1617 |
|
| 1618 |
my ( $span_start, $span_length ) = |
| 1619 |
$recce->g1_location_to_span($g1_location); |
| 1620 |
|
| 1621 |
=for Marpa::R2::Display::End |
| 1622 |
|
| 1623 |
G1 locations do not correspond to a single input stream |
| 1624 |
location, but to a span of them. |
| 1625 |
The C<g1_location_to_span()> method |
| 1626 |
returns an B<array> of two elements, representing a |
| 1627 |
L<span|/"Spans"> in the physical input stream. |
| 1628 |
G1 location 0 does not correspond to a input stream span so, |
| 1629 |
as a special case, |
| 1630 |
the input stream span for G1 location 0 is returned as (0,0). |
| 1631 |
|
| 1632 |
=head2 input_length() |
| 1633 |
|
| 1634 |
=for Marpa::R2::Display |
| 1635 |
name: SLIF input_length() example |
| 1636 |
partial: 1 |
| 1637 |
normalize-whitespace: 1 |
| 1638 |
|
| 1639 |
my $input_length = $recce->input_length(); |
| 1640 |
|
| 1641 |
=for Marpa::R2::Display::End |
| 1642 |
|
| 1643 |
The C<input_length()> method accepts no arguments, |
| 1644 |
and returns the length of the physical input stream. |
| 1645 |
|
| 1646 |
=head2 last_completed() |
| 1647 |
|
| 1648 |
=for Marpa::R2::Display |
| 1649 |
name: Scanless recognizer diagnostics |
| 1650 |
partial: 1 |
| 1651 |
normalize-whitespace: 1 |
| 1652 |
|
| 1653 |
sub show_last_expression { |
| 1654 |
my ($self) = @_; |
| 1655 |
my $recce = $self->{recce}; |
| 1656 |
my ( $g1_start, $g1_len ) = $recce->last_completed('Expression'); |
| 1657 |
return 'No expression was successfully parsed' if not defined $g1_start; |
| 1658 |
my $last_expression = $recce->substring( $g1_start, $g1_len ); |
| 1659 |
return "Last expression successfully parsed was: $last_expression"; |
| 1660 |
} ## end sub show_last_expression |
| 1661 |
|
| 1662 |
=for Marpa::R2::Display::End |
| 1663 |
|
| 1664 |
=for Marpa::R2::Display |
| 1665 |
name: Scanless recognizer diagnostics |
| 1666 |
partial: 1 |
| 1667 |
normalize-whitespace: 1 |
| 1668 |
|
| 1669 |
my ( $g1_start, $g1_len ) = $recce->last_completed('Expression'); |
| 1670 |
|
| 1671 |
=for Marpa::R2::Display::End |
| 1672 |
|
| 1673 |
Given the name of a symbol, |
| 1674 |
C<last_completed()> returns the 2-element array |
| 1675 |
that is the G1 location |
| 1676 |
L<span|/"Spans"> of the most recent match. |
| 1677 |
If there was more than one most recent match, it returns |
| 1678 |
the longest. |
| 1679 |
If there was no match, |
| 1680 |
C<last_completed()> returns the empty array in array context |
| 1681 |
and a Perl false in scalar context. |
| 1682 |
|
| 1683 |
=head2 last_completed_span() |
| 1684 |
|
| 1685 |
=for Marpa::R2::Display |
| 1686 |
name: SLIF recognizer last_completed_span() synopsis |
| 1687 |
normalize-whitespace: 1 |
| 1688 |
|
| 1689 |
my @longest_span = $recce->last_completed_span('target'); |
| 1690 |
diag( "Actual target at $pos: ", $recce->literal(@longest_span) ) if $verbose; |
| 1691 |
|
| 1692 |
=for Marpa::R2::Display::End |
| 1693 |
|
| 1694 |
Returns the most recent input stream span for a completed |
| 1695 |
instance of the symbol name |
| 1696 |
that is its first and only argument. |
| 1697 |
That argument is required. |
| 1698 |
The search for a completed instance of a symbol |
| 1699 |
can only succeed if the first argument |
| 1700 |
is the name |
| 1701 |
of the LHS symbol of some rule in the grammar. |
| 1702 |
For details on how the input stream span is determined, |
| 1703 |
see L<"Literals and G1 spans">. |
| 1704 |
|
| 1705 |
If more than one instance of the symbol |
| 1706 |
ends at the same location, |
| 1707 |
C<last_completed_span()> returns the longest span. |
| 1708 |
If there is no symbol instance for the argument symbol, |
| 1709 |
C<last_completed_span()> returns the empty array. |
| 1710 |
Other failures are thrown. |
| 1711 |
|
| 1712 |
=head2 line_column() |
| 1713 |
|
| 1714 |
=for Marpa::R2::Display |
| 1715 |
name: SLIF trace example |
| 1716 |
partial: 1 |
| 1717 |
normalize-whitespace: 1 |
| 1718 |
|
| 1719 |
my ( $start, $span_length ) = $re->pause_span(); |
| 1720 |
my ( $line, $column ) = $re->line_column($start); |
| 1721 |
|
| 1722 |
=for Marpa::R2::Display::End |
| 1723 |
|
| 1724 |
The C<line_column()> method accepts one, optional, argument: |
| 1725 |
a location in the input stream. |
| 1726 |
The location defaults to the current location. |
| 1727 |
C<line_column()> returns the corresponding line and column position, |
| 1728 |
as a 2-element array. |
| 1729 |
The first element of the array is the line position, |
| 1730 |
and the second element is the column position. |
| 1731 |
|
| 1732 |
Numbering of lines and columns is 1-based, |
| 1733 |
following UNIX editor tradition. |
| 1734 |
Except at B<EOVS> |
| 1735 |
(the B<end of the virtual input stream>), |
| 1736 |
the line and column will be that of an |
| 1737 |
actual character. |
| 1738 |
At EOVS the line number |
| 1739 |
will be that of the last line, |
| 1740 |
and the column number will be that of the last column |
| 1741 |
plus one. |
| 1742 |
Applications which want to treat EOVS as a special case |
| 1743 |
can test for it using the L<C<pos()> method|/"pos()"> |
| 1744 |
and the L<C<input_length()> method|/"input_length()">. |
| 1745 |
|
| 1746 |
A line is considered to end with any newline sequence |
| 1747 |
as defined in the |
| 1748 |
Unicode Specification 4.0.0, Section 5.8. |
| 1749 |
Specifically, a line ends with one of the following: |
| 1750 |
|
| 1751 |
=over 4 |
| 1752 |
|
| 1753 |
=item * |
| 1754 |
|
| 1755 |
a LF (line feed U+000A); |
| 1756 |
|
| 1757 |
=item * |
| 1758 |
|
| 1759 |
a CR (carriage return, U+000D), when it is not followed by a LF; |
| 1760 |
|
| 1761 |
=item * |
| 1762 |
|
| 1763 |
a CRLF sequence (U+000D,U+000A); |
| 1764 |
|
| 1765 |
=item * |
| 1766 |
|
| 1767 |
a NEL (next line, U+0085); |
| 1768 |
|
| 1769 |
=item * |
| 1770 |
|
| 1771 |
a VT (vertical tab, U+000B); |
| 1772 |
|
| 1773 |
=item * |
| 1774 |
|
| 1775 |
a FF (form feed, U+000C); |
| 1776 |
|
| 1777 |
=item * |
| 1778 |
|
| 1779 |
a LS (line separator, U+2028) or |
| 1780 |
|
| 1781 |
=item * |
| 1782 |
|
| 1783 |
a PS (paragraph separator, U+2029). |
| 1784 |
|
| 1785 |
=back |
| 1786 |
|
| 1787 |
=head2 literal() |
| 1788 |
|
| 1789 |
=for Marpa::R2::Display |
| 1790 |
name: SLIF trace example |
| 1791 |
partial: 1 |
| 1792 |
normalize-whitespace: 1 |
| 1793 |
|
| 1794 |
my $literal_string = $re->literal( $start, $span_length ); |
| 1795 |
|
| 1796 |
=for Marpa::R2::Display::End |
| 1797 |
|
| 1798 |
The C<literal()> method accepts two arguments, |
| 1799 |
the start location and length of a span in the |
| 1800 |
physical input stream. |
| 1801 |
It returns the substring of the input stream |
| 1802 |
corresponding to that span. |
| 1803 |
|
| 1804 |
=head2 pause_lexeme() |
| 1805 |
|
| 1806 |
=for Marpa::R2::Display |
| 1807 |
name: SLIF trace example |
| 1808 |
partial: 1 |
| 1809 |
normalize-whitespace: 1 |
| 1810 |
|
| 1811 |
my $lexeme = $re->pause_lexeme(); |
| 1812 |
|
| 1813 |
=for Marpa::R2::Display::End |
| 1814 |
|
| 1815 |
Use of this method is discouraged. |
| 1816 |
New applications should avoid it. |
| 1817 |
Instead the lexeme event should be declared as |
| 1818 |
a named event. |
| 1819 |
The named lexeme event can be set up in such |
| 1820 |
a way that |
| 1821 |
it uniquely identifies |
| 1822 |
the lexeme that triggered it. |
| 1823 |
SLIF parse events are described in detail in |
| 1824 |
L<a separate document|Marpa::R2::Event>. |
| 1825 |
|
| 1826 |
The C<pause_lexeme()> method accepts no arguments. |
| 1827 |
It returns the current |
| 1828 |
L<pause lexeme|Marpa::R2::Event/"Pause span and pause lexeme">. |
| 1829 |
More than one lexeme may trigger at the same location, |
| 1830 |
in which case the choice of pause lexeme |
| 1831 |
is made arbitrarily. |
| 1832 |
This is one reason that the use of |
| 1833 |
C<pause_lexeme()> is discouraged. |
| 1834 |
C<pause_lexeme()> returns a Perl C<undef> when |
| 1835 |
the pause lexeme is undefined. |
| 1836 |
|
| 1837 |
=head2 pause_span() |
| 1838 |
|
| 1839 |
=for Marpa::R2::Display |
| 1840 |
name: SLIF read/resume example |
| 1841 |
partial: 1 |
| 1842 |
normalize-whitespace: 1 |
| 1843 |
|
| 1844 |
my ( $start, $length ) = $re->pause_span(); |
| 1845 |
|
| 1846 |
=for Marpa::R2::Display::End |
| 1847 |
|
| 1848 |
The C<pause_span()> method accepts no arguments, |
| 1849 |
and returns |
| 1850 |
the pause span |
| 1851 |
as a 2-element array: start and length. |
| 1852 |
The "pause span" is |
| 1853 |
described in detail |
| 1854 |
L<in another document|Marpa::R2::Event/"Pause span and pause lexeme">. |
| 1855 |
C<pause_span()> returns a Perl C<undef> if |
| 1856 |
the pause span is undefined. |
| 1857 |
|
| 1858 |
=head2 pos() |
| 1859 |
|
| 1860 |
=for Marpa::R2::Display |
| 1861 |
name: SLIF pos() example |
| 1862 |
partial: 1 |
| 1863 |
normalize-whitespace: 1 |
| 1864 |
|
| 1865 |
my $pos = $recce->pos(); |
| 1866 |
|
| 1867 |
=for Marpa::R2::Display::End |
| 1868 |
|
| 1869 |
The C<pos()> method accepts no arguments, |
| 1870 |
and returns the current physical input stream location. |
| 1871 |
|
| 1872 |
=head2 progress() |
| 1873 |
|
| 1874 |
=for Marpa::R2::Display |
| 1875 |
name: Scanless progress() synopsis |
| 1876 |
|
| 1877 |
my $progress_output = $recce->progress(); |
| 1878 |
|
| 1879 |
=for Marpa::R2::Display::End |
| 1880 |
|
| 1881 |
Returns a reference to an array |
| 1882 |
that describes the progress |
| 1883 |
of a parse |
| 1884 |
at a location. |
| 1885 |
With no argument, C<progress()> reports progress at |
| 1886 |
the current location. |
| 1887 |
If a G1 location is |
| 1888 |
given as its argument, |
| 1889 |
C<progress()> reports progress at that G1 location. |
| 1890 |
Negative G1 locations are interpreted as |
| 1891 |
L<described above|/"Ranges">. |
| 1892 |
|
| 1893 |
The progress reports returned by |
| 1894 |
the C<progress()> method |
| 1895 |
identify rules by their G1 rule ID. |
| 1896 |
G1 rule IDs can be converted to a list of the rule's |
| 1897 |
symbols using the L<C<rule()> method |
| 1898 |
of the SLIF grammar|Marpa::R2::Scanless::G/"rule()">. |
| 1899 |
Details on progress reports can be found in |
| 1900 |
L<their own document|Marpa::R2::Progress>. |
| 1901 |
|
| 1902 |
=head2 show_progress() |
| 1903 |
|
| 1904 |
=for Marpa::R2::Display |
| 1905 |
name: Scanless show_progress() synopsis |
| 1906 |
partial: 1 |
| 1907 |
normalize-whitespace: 1 |
| 1908 |
|
| 1909 |
my $show_progress_output = $recce->show_progress(); |
| 1910 |
|
| 1911 |
=for Marpa::R2::Display::End |
| 1912 |
|
| 1913 |
Returns a string showing |
| 1914 |
the progress of the G1 parse. |
| 1915 |
For a description of its output, |
| 1916 |
see L<Marpa::R2::Progress>. |
| 1917 |
With no arguments, |
| 1918 |
the string contains reports for |
| 1919 |
the current location. |
| 1920 |
|
| 1921 |
Locations can be specified as arguments to |
| 1922 |
C<show_progress()>. |
| 1923 |
With a single integer argument I<N>, |
| 1924 |
the string contains reports for G1 location I<N>. |
| 1925 |
Two numeric arguments are interpreted as a |
| 1926 |
L<span|/"Spans"> of G1 locations, |
| 1927 |
and the returned string contains |
| 1928 |
reports for all locations in the span. |
| 1929 |
For example, |
| 1930 |
the method call C<< $recce->show_progress(0, -1) >> |
| 1931 |
will print progress reports for the entire parse. |
| 1932 |
|
| 1933 |
The arguments are G1 locations instead of physical input stream locations, |
| 1934 |
because G1 locations represent a unique point in the parse. |
| 1935 |
By contrast, |
| 1936 |
a single physical input stream location might be visited many times |
| 1937 |
by a SLIF recognizer. |
| 1938 |
|
| 1939 |
The output is intended only for reading by humans. |
| 1940 |
The exact format is subject to change |
| 1941 |
and should not be relied on by applications. |
| 1942 |
|
| 1943 |
=head2 substring() |
| 1944 |
|
| 1945 |
=for Marpa::R2::Display |
| 1946 |
name: Scanless recognizer diagnostics |
| 1947 |
partial: 1 |
| 1948 |
normalize-whitespace: 1 |
| 1949 |
|
| 1950 |
my $last_expression = $recce->substring( $g1_start, $g1_len ); |
| 1951 |
|
| 1952 |
=for Marpa::R2::Display::End |
| 1953 |
|
| 1954 |
Given a G1 span -- that is, a G1 start location and a length in G1 locations -- |
| 1955 |
the C<substring()> method |
| 1956 |
returns a substring of the input |
| 1957 |
stream. |
| 1958 |
A G1 length of zero will produce the zero-length string. |
| 1959 |
|
| 1960 |
The substring of the input stream is determined on the assumption |
| 1961 |
that the application reads the input in lexical order and |
| 1962 |
without gaps |
| 1963 |
except for whitespace and other normal discards. |
| 1964 |
When this is not the case, the substring is determined as |
| 1965 |
L<described above|/"Literals and G1 spans">. |
| 1966 |
|
| 1967 |
=head2 terminals_expected() |
| 1968 |
|
| 1969 |
=for Marpa::R2::Display |
| 1970 |
name: Scanless terminals_expected() synopsis |
| 1971 |
normalize-whitespace: 1 |
| 1972 |
|
| 1973 |
my @terminals_expected = @{$recce->terminals_expected()}; |
| 1974 |
|
| 1975 |
=for Marpa::R2::Display::End |
| 1976 |
|
| 1977 |
Returns a reference to a list of strings, where the strings are the |
| 1978 |
names of the lexemes acceptable at the current location. |
| 1979 |
The presence of a lexeme in this list means |
| 1980 |
that lexeme will be acceptable in the next call of the L<C<resume()>|/"resume()"> method. |
| 1981 |
This is highly useful for Ruby Slippers parsing. |
| 1982 |
A more fine-tuned approach is to identify the lexemes of interest |
| 1983 |
and create "predicted symbol" events for them. |
| 1984 |
|
| 1985 |
Some lexemes are specified in the G1 rules |
| 1986 |
of the DSL as quoted strings |
| 1987 |
or as character classes, |
| 1988 |
This is convenient, |
| 1989 |
but the lexemes created in this way |
| 1990 |
do not have real names. |
| 1991 |
Instead, internal names, like |
| 1992 |
C<[Lex-1]> are created for them, |
| 1993 |
and these are what appear in the |
| 1994 |
list of strings |
| 1995 |
returned by C<terminals_expected()>. |
| 1996 |
If an application wants a quoted string |
| 1997 |
or a character class to have a |
| 1998 |
mnemonic name, |
| 1999 |
the application must provide that name explicitly, |
| 2000 |
by specifying the character class |
| 2001 |
or quoted string in an L0 rule. |
| 2002 |
|
| 2003 |
=head1 Discouraged methods |
| 2004 |
|
| 2005 |
Methods in this section continue to be supported, but their use is |
| 2006 |
discouraged in favor of other, better solutions. |
| 2007 |
New applications should avoid using discouraged methods. |
| 2008 |
|
| 2009 |
=head2 event() |
| 2010 |
|
| 2011 |
=for Marpa::R2::Display |
| 2012 |
name: SLR event() method synopsis |
| 2013 |
normalize-whitespace: 1 |
| 2014 |
partial: 1 |
| 2015 |
|
| 2016 |
my $event = $recce->event($event_ix); |
| 2017 |
|
| 2018 |
=for Marpa::R2::Display::End |
| 2019 |
|
| 2020 |
Use of this method is discouraged in favor of the more efficient |
| 2021 |
L<events() method|/"events()">. |
| 2022 |
The C<event()> method requires one argument, |
| 2023 |
an event index. |
| 2024 |
It returns a descriptor of |
| 2025 |
the named event with that index, or a Perl C<undef> |
| 2026 |
if there is no such event. |
| 2027 |
For more details on events, see the |
| 2028 |
L<description of the events() method|/"events()">. |
| 2029 |
|
| 2030 |
=head2 last_completed_range() |
| 2031 |
|
| 2032 |
Use of this method is discouraged in favor of |
| 2033 |
L</"last_completed()">. |
| 2034 |
Given the name of a symbol, |
| 2035 |
C<last_completed_range()> |
| 2036 |
returns the G1 start and G1 end locations of the most recent match. |
| 2037 |
If there was more than one most recent match, |
| 2038 |
C<last_completed_range()> |
| 2039 |
returns the longest. |
| 2040 |
If there was no match, |
| 2041 |
C<last_completed_range()> |
| 2042 |
returns the empty array in array context |
| 2043 |
and a Perl false in scalar context. |
| 2044 |
|
| 2045 |
=head2 range_to_string() |
| 2046 |
|
| 2047 |
Use of this method is discouraged in favor of |
| 2048 |
L</"substring()">. |
| 2049 |
Given a G1 start and a G1 end location, |
| 2050 |
C<range_to_string()> |
| 2051 |
returns the substring of the input |
| 2052 |
stream that is between the two. |
| 2053 |
The C<range_to_string()> method |
| 2054 |
assumes that |
| 2055 |
the application read |
| 2056 |
the physical input stream in |
| 2057 |
lexical order and |
| 2058 |
without gaps |
| 2059 |
except for whitespace and other normal discards. |
| 2060 |
When that is not the case, |
| 2061 |
C<range_to_string()> behaves in |
| 2062 |
much the same way as described above |
| 2063 |
for L</"substring()">. |
| 2064 |
|
| 2065 |
=head1 Copyright and License |
| 2066 |
|
| 2067 |
=for Marpa::R2::Display |
| 2068 |
ignore: 1 |
| 2069 |
|
| 2070 |
Copyright 2022 Jeffrey Kegler |
| 2071 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 2072 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 2073 |
General Public License as published by the Free Software Foundation, |
| 2074 |
either version 3 of the License, or (at your option) any later version. |
| 2075 |
|
| 2076 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 2077 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 2078 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 2079 |
Lesser General Public License for more details. |
| 2080 |
|
| 2081 |
You should have received a copy of the GNU Lesser |
| 2082 |
General Public License along with Marpa::R2. If not, see |
| 2083 |
http://www.gnu.org/licenses/. |
| 2084 |
|
| 2085 |
=for Marpa::R2::Display::End |
| 2086 |
|
| 2087 |
=cut |
| 2088 |
|
| 2089 |
# Local Variables: |
| 2090 |
# mode: cperl |
| 2091 |
# cperl-indent-level: 4 |
| 2092 |
# fill-column: 100 |
| 2093 |
# End: |
| 2094 |
# vim: expandtab shiftwidth=4: |
| 2095 |
|