Debian Codegraph @

pod/Advanced/Thin.pod

raw
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::Advanced::Thin - Direct access to Libmarpa
19
20 =head1 About this document
21
22 Most Marpa users can ignore this document.
23 It describes Marpa's "thin" interface,
24 which
25 provides efficient access to Marpa's core
26 library, Libmarpa.
27
28 The "thin" interface is very low-level and
29 is not designed to be convenient to use.
30 User-friendliness is expected to be provided by
31 an upper layer.
32 The "thin" interface
33 is intended for use in writing
34 those upper layers.
35 Libmarpa is also intended for writing applications,
36 when the programmer wants
37 to eliminate the overhead of an upper layer,
38 or wants the flexibility provided by direct access to Libmarpa,
39 and is willing to go to some extra effort.
40
41 =head1 How this document is written
42
43 This document assumes that the reader is familiar with
44 the other Marpa::R2 documentation.
45 and very familiar wth the Libmarpa API document.
46 Must of the terminology in this document is adopted
47 from the Libmarpa API document.
48 The reader will also have to know some C language --
49 at least enough to understand a discussion
50 written in terms of C functions,
51 their parameters and return values,
52 and C language macros.
53
54 This document
55 avoids
56 duplicating the material in the Libmarpa
57 API document.
58 Most Marpa thin interface functions correspond
59 directly to a Libmarpa method,
60 and their implementation follows a "general pattern".
61 This document describes that "general pattern".
62
63 Methods that follow
64 the general pattern are usually
65 not mentioned specifically.
66 Methods that are
67 exceptions
68 to the general pattern
69 are always mentioned,
70 and behaviors which deviate from the general
71 pattern are always described in detail.
72 This document also identifies
73 those Libmarpa
74 methods to which
75 no Marpa thin interface method directly corresponds.
76
77 This style of documentation
78 is very efficient,
79 which is one reason that it is the
80 standard for C library interfaces to Perl.
81 Admittedly, however,
82 it is also very terse.
83 As an aid to
84 the reader,
85 an example of a script using
86 the Marpa thin interface is presented
87 L<below|"Example">.
88 While small, the example is non-trival.
89 It is also full,
90 in the sense it contains a complete
91 logic flow, starting with the definition
92 of the grammar
93 and continuing all the way to the iteration of
94 the values of an ambiguous parse.
95
96 =head1 Methods in the thin interface
97
98 While the thin interface has a few
99 methods of its own,
100 most of its methods are wrappers for a method
101 from the Libmarpa interface.
102 On the other hand,
103 many
104 Libmarpa methods do not have
105 Marpa thin interface wrappers.
106 No internal Libmarpa method is part of the Marpa thin interface.
107
108 Additionally,
109 many of the
110 external Libmarpa methods are omitted because their function is performed
111 by the Marpa thin interface.
112 No thin interface method corresponds to
113 the C<marpa_check_version()> static method,
114 because the Marpa thin interface
115 interface handles its own version matching.
116
117 No thin interface method corresponds to any
118 of the Libmarpa configuration class methods.
119 No Marpa thin interface object corresponds
120 to Libmarpa's configuration objects.
121 Configuration in the Marpa thin
122 interface is done using Perl variables.
123
124 No Marpa thin interface method corresponds to the
125 C<marpa_g_ref()> and C<marpa_g_unref()> methods
126 because the thin interface handles the
127 reference counting of the Libmarpa objects it creates.
128 The application can rely on Libmarpa
129 objects being cleaned up properly as part of Perl's
130 usual garbage collection.
131 For the same reason,
132 no Marpa thin interface method corresponds to the
133 "ref" and "unref" methods of the other Libmarpa time
134 classes.
135
136 Whenever an external Libmarpa method is not mentioned
137 in this document, the reader can assume that
138 it has a wrapper that is implemented according
139 to the general pattern, as
140 described L<below|"The general pattern">.
141 Where the implementation of an
142 external Libmarpa method
143 is an exception to the general pattern,
144 its implementation will be explicitly described
145 and any corresponding Marpa thin interface method
146 will have a section devoted to it
147 and specifying its differences
148 from the general pattern.
149
150 =head1 Libmarpa time objects and constructors
151
152 As a reminder,
153 Libmarpa's major classes are,
154 in sequence,
155 configuration,
156 grammar, recognizer, bocage,
157 ordering, tree and value.
158 The one-letter abbreviations for these
159 are, respectively,
160 C<c>, C<g>, C<r>, C<b>, C<o>, C<t> and C<v>.
161
162 =for Marpa::R2::Display
163 ignore: 1
164
165 Marpa_Config Marpa::R2::Thin::C
166 Marpa_Grammar Marpa::R2::Thin::G
167 Marpa_Recognizer Marpa::R2::Thin::R
168 Marpa_Bocage Marpa::R2::Thin::B
169 Marpa_Ordering Marpa::R2::Thin::O
170 Marpa_Tree Marpa::R2::Thin::T
171 Marpa_Value Marpa::R2::Thin::V
172
173 =for Marpa::R2::Display::End
174
175 The thin interface implements a Perl class
176 corresponding to
177 each of Libmarpa time classes.
178 The thin interface does not implement a Perl class
179 corresponding to Libmarpa's configuration class.
180
181 Objects in the thin Marpa classes
182 should be treated as opaque scalars.
183 Applications must not define new methods,
184 constants or variables in a
185 thin Marpa classes.
186 Similarly, applications
187 must not
188 redefine, overload or remove existing elements.
189 Thin Marpa classes must not be subclassed.
190 Applications should restrict their operation on
191 objects in the Marpa thin classes to assignments;
192 calling methods of the class; and,
193 in those cases where
194 this document states that it is appropriate,
195 passing them as arguments.
196
197 Constructors for the time objects may be called using
198 the C<new> method of the corresponding Perl class.
199 For example,
200
201 =for Marpa::R2::Display
202 name: Thin example
203 partial: 1
204 normalize-whitespace: 1
205
206 my $recce = Marpa::R2::Thin::R->new($grammar);
207
208 =for Marpa::R2::Display::End
209
210 Perl applications can destroy objects of the
211 Marpa thin classes
212 by undefining them,
213 or by letting them go out of scope.
214 Application programmers do not need
215 to concern themselves
216 with the reference counting of Libmarpa objects.
217 The Marpa thin interface handles this.
218
219 =head1 The throw setting
220
221 One of the most important functions of
222 the Marpa thin interface
223 is to adapt Libmarpa's
224 error reporting to Perl.
225 Perl allows two kinds of error reporting.
226 Perl methods can communicate failure via their return values,
227 or they can throw exceptions.
228
229 Each has its place.
230 Throwing failure as an exception is the default in
231 the Marpa thin interface because it is safer,
232 and because
233 it is convenient for prototyping and the first
234 cuts at a new application.
235 On the other hand, for Marpa thin
236 applications to have access
237 to the full flexibility and power of Libmarpa,
238 they must be able to set the Marpa thin
239 interface to return failure, instead of
240 throwing failure as an exception.
241
242 In choosing whether or not to throw a failure
243 as an exception,
244 almost every Marpa thin method obeys the throw
245 setting.
246 If the throw setting is 1, failure is thrown.
247 If the throw setting is 0, failure is returned.
248
249 The throw setting is 1 in newly created grammar
250 objects.
251 Once created, the throw setting of a grammar object
252 can be changed using the L<< C<throw_set()>
253 method|C<< $g->throw_set() >> >>
254 The throw setting of other objects is that
255 of their base grammar.
256
257 Nearly every Marpa thin method always obeys the throw setting.
258 Only three methods never obey the throw setting.
259 They are exceptions because their logic
260 controls the throw setting, so that a problem with them
261 suggests the throw setting itself may not be correct.
262 The methods which never obey the throw setting,
263 are
264 L<< C<< Marpa::R2::Thin::G->new() >>|C<< Marpa::R2::Thin::G->new() >> >>,
265 L<< C<throw_set()>|C<< $g->throw_set() >> >>
266 and L<< C<ruby_slippers_set()>|C<< $r->ruby_slippers_set() >> >>.
267 These methods throw all failures.
268
269 One method allows the throw setting to be overriden.
270 For convenience in using the Ruby Slippers technique,
271 the behavior of the L<< C<alternative()>
272 method|C<< $r->alternative() >> >>
273 is also affected by a separate "Ruby Slippers" flag.
274 For details, see
275 L<< the description of that
276 method|C<< $r->alternative() >> >>.
277
278 =head1 Error codes, names and descriptions
279
280 Errors in the Marpa thin interface come from
281 two sources:
282 Libmarpa, and the Marpa thin interface itself.
283 These will be called Libmarpa errors
284 and thin interface errors, respectively.
285
286 Internally, Marpa maintains two variables to
287 track recent errors.
288 These are the error code and the error description.
289 For every defined error code,
290 there is an error name.
291 Together, the error code, the error name
292 and the error descriptions are called the
293 "error variables".
294
295 When the most recent error was a Libmarpa error,
296 the error code is the Libmarpa error code, as described
297 in the Libmarpa API document.
298 A Libmarpa error code is a non-negative integer.
299 When the most recent error was a thin interface
300 error, the error code is
301 a Perl C<undef>.
302
303 Libmarpa's integral error codes are rarely used directly,
304 either in C or in Perl.
305 In C, the error codes are referred to using macros.
306 The macro names are available through the thin interface
307 as error names.
308 For details see L<the section on error methods|"Error methods">.
309 Thin interface errors do not have error names.
310
311 In addition to error names, there are error descriptions.
312
313 =over 4
314
315 =item *
316
317 Error names are short mnemonics.
318 Error descriptions are typically longer.
319
320 =item *
321
322 Error names and error codes have a one to one correspondence (bijection).
323 For a given error code, the error name will always
324 be the same, and vice versa.
325 Error descriptions
326 may contain text relating, not just to the error code,
327 but to the specific error instance.
328
329 =item *
330
331 The error name is defined if and
332 only if
333 the error code is defined.
334 Error descriptions always exist,
335 whether or not there is an error code defined.
336
337 =item *
338
339 A thin interface error
340 will always have an error description.
341 A thin interface error
342 will never have an error name.
343
344 =item *
345
346 The programmer may expect error codes and error names
347 to remain stable and may write code that relies
348 on the numeric value of the error codes
349 and the text of the error name.
350 Applications should treat the text of an error description
351 as suitable for the purpose of passing it on to a human user,
352 and should otherwise regard it as opaque.
353
354 =back
355
356 Error descriptions, while typically longer than error names,
357 are intended for situations where
358 it is most convenient if they fit into a single line,
359 or at most two.
360 The Libmarpa API document contains a section on the Libmarpa
361 error codes.
362 and there the descriptions
363 are often longer and more detailed.
364
365 Error codes and error descriptions should be considered
366 valid
367 only if the most recently called Marpa thin method
368 indicated that it set the error code.
369 An application should assume that the
370 error codes and error descriptions will be overwritten by the
371 next call to
372 any thin interface method other than the L<< C<error()>
373 method|C<< $g->error() >> >>.
374
375 =head2 Failure and the error variables
376
377 A method indicates failure
378 either by throwing an exception
379 or by returning a value that indicates failure.
380 If a method follows the general pattern,
381 it indicates failure if and only if
382 its return value is less than or equal to -2.
383 Other methods indicate failure as stated in
384 their descriptions in this document.
385
386 Whenever a method indicates failure, that also
387 indicates that it has set the error variables.
388 On Libmarpa failures, the error code is set to the
389 Libmarpa error code.
390 On thin interface failure, the error code is
391 set to a Perl C<undef>.
392 For both Libmarpa and thin interface failures,
393 the error description is set to a text
394 that describes the error.
395
396 =head2 Success and the error variables
397
398 On success,
399 a method will take one of the following
400 three actions
401 with respect to the error variables:
402
403 =over 4
404
405 =item Reset the error code
406
407 A successful method
408 may set the error code to C<MARPA_ERR_NONE>,
409 together with an error description that indicates
410 there is no error.
411
412 =item Leave the error code as is
413
414 A successful method
415 may leave the error code and error
416 description as is.
417
418 =item Set an informational error code
419
420 A successful method may set a
421 Libmarpa error code to
422 a value other than C<MARPA_ERR_NONE>.
423 Error codes of this kind are called
424 informational error codes.
425 The phrase "error code" in this context
426 is something of a misnomer.
427 Informational error codes exist
428 as a convenience for some applications,
429 but typically are ignored.
430
431 =back
432
433 The Libmarpa API document sometimes specifies
434 what a Libmarpa method does with the error code
435 on success.
436 The Libmarpa API document always specifies
437 if and when a method sets an informational error code.
438 If Libmarpa API document is silent,
439 the application should regard it as unspecified
440 whether the error code is reset or left as is.
441
442 =head1 The general pattern
443
444 Most Marpa thin interface methods correspond directly
445 to a Libmarpa method,
446 and their behaviors are in most cases exactly the same.
447 These behaviors are called the "general pattern"
448 in this document.
449 To avoid repetition,
450 Marpa thin interface methods that follow
451 the general pattern exactly are
452 usually not
453 described explicitly in this document.
454
455 =head2 Method names
456
457 The name of a general pattern method is that of
458 its corresponding Libmarpa method,
459 minus the 8-letter prefix which indicates
460 its Libmarpa class.
461 For example,
462 the Marpa thin interface method that
463 corresponds to C<marpa_g_start_symbol_set>
464 is C<< $g->start_symbol_set() >>.
465 The class of the general pattern method
466 will be the Marpa thin class corresponding
467 to the time class
468 of the Libmarpa method.
469 For example, C<< $g->start_symbol_set() >>
470 will be in
471 the C<Marpa::R2::Thin:G> Perl class.
472
473 =head2 Arguments
474
475 Libmarpa's class instance methods
476 take an object of the their class
477 as their first ("self") argument.
478 Zero or more non-self arguments follow the self argument.
479 The arguments of the corresponding general pattern
480 Marpa thin method will be
481 the same, converted from C types to Perl as described next.
482 (This discussion follows
483 the convention used in L<perlobj>,
484 and considers the "self" object to
485 be a Perl method's first argument.)
486
487 In the general pattern,
488 every argument whose type is one
489 of Libmarpa's time classes is converted
490 to the corresponding Marpa thin interface class.
491 Arguments which belong to Libmarpa's numbered
492 classes (C<Marpa_Earley_Set_ID>,
493 C<Marpa_Rank>,
494 C<Marpa_Rule_ID> and C<Marpa_Symbol_ID>)
495 are converted to Perl scalar integers.
496 C language C<int>'s are also
497 converted to Perl scalar integers.
498
499 The Marpa thin interface does
500 not recognize booleans, either in
501 C or in Perl.
502 For example,
503 if a Perl true value is not a numeric
504 1, it will not be converted to a numeric
505 1 in C, even in a situation where
506 the Libmarpa method is clearly
507 looking for a boolean.
508 The intent is to allow for future extensions
509 to the Libmarpa interface that
510 accept and interpret other numeric values.
511
512 =head2 Return values
513
514 In the general pattern,
515 the return value from a Libmarpa method
516 will always either
517 belong to
518 one of Libmarpa's numbered classes,
519 or be a C language C<int>.
520 If the Libmarpa return value is a non-negative
521 integer,
522 the corresponding general pattern Marpa thin method
523 will return a numeric Perl scalar.
524 If the Libmarpa method returns -1,
525 its corresponding general pattern Marpa thin method
526 will return a Perl C<undef>.
527
528 =head2 General pattern failures
529
530 General pattern methods consider failure to be
531 a Libmarpa return value of -2 or less.
532 Failure is thrown if the throw setting is 1.
533 On unthrown failure, the return value
534 of the Libmarpa method will
535 be returned by the Marpa thin
536 method as a numeric Perl scalar.
537
538 =head2 An example of a general pattern method
539
540 Here is an example of a Libmarpa function
541 whose corresponding Marpa thin method follows the
542 general pattern.
543
544 =for Marpa::R2::Display
545 ignore: 1
546
547 marpa_g_start_symbol_set (grammar, symbol_S);
548
549 =for Marpa::R2::Display::End
550
551 and here is the corresponding thin Marpa call:
552
553 =for Marpa::R2::Display
554 name: Thin example
555 normalize-whitespace: 1
556 partial: 1
557
558 $grammar->start_symbol_set($symbol_S);
559
560 =for Marpa::R2::Display::End
561
562 =head1 Error recovery
563
564 Error recovery is as in the Libmarpa API,
565 with changes appropriate to adopt it to a Perl module.
566 We recall the concepts of application behavior and diagnostic
567 behavior from the Libmarpa API:
568 Application behaviors are those that normal applications may
569 expect, as specified in this and the Libmarpa API document.
570 Diagnostic behaviors are a subset of the application behaviors.
571 Diagnostic behaviors are those
572 that diagnostic or testing code may reasonably
573 attempt after an irrecoverable error.
574 Informally, application behaviors are promised,
575 while diagnostic behaviors are hoped for.
576 For a full discussion of application and diagnostic behaviors,
577 refer back to the Libmarpa API document.
578
579 Analogously to the Libmarpa API,
580 we call the object which caused a failure,
581 the failure object.
582 If the failure object is a time object,
583 its base grammar object is the grammar object it was
584 created from, whether directly, indirectly
585 or trivially.
586 A grammar object is considered to be created trivially from
587 itself.
588 If the failure object is a time object,
589 the failure grammar is the base grammar object of
590 the failure object.
591
592 In the following sections,
593 we adopted the definitions of library-recoverable,
594 ancestry-recoverable and fully recoverable failure
595 to make them
596 analogous to the
597 Libmarpa API's definitions of these terms.
598
599 =head2 Library-recoverable failure
600
601 In the thin interface, "library-recoverable failure" might be more properly be called
602 "module-recoverable failure".
603 We retain the term "library-recoverable failure"
604 for convenience in referring back to the Libmarpa API document.
605
606 When a thin wrapper corresponding to a Libmarpa method
607 has a library-recoverable failure the following application behaviors are no longer
608 available:
609
610 =over 4
611
612 =item *
613
614 Thin interface mutator and constructor method calls
615 on objects whose base grammar is the failure grammar.
616
617 =back
618
619 The following application behaviors remain available:
620
621 =over 4
622
623 =item *
624
625 All behaviors not in the thin interface,
626 including the Perl environment itself,
627 and all modules other than the thin interface.
628
629 =item *
630
631 All thin interface static method calls.
632
633 =item *
634
635 All thin interface accessor method calls,
636 and thin interface object destruction.
637
638 =item *
639
640 All thin interface constructor and mutator method calls
641 for objects whose base grammar is not the failure grammar.
642
643 =back
644
645 For more details on library-recoverability, refer to
646 the Libmarpa API document.
647
648 =head2 Ancestry-recoverable failure
649
650 When a thin wrapper corresponding to a Libmarpa method
651 has a ancestry-recoverable failure the following application behaviors are no longer
652 available:
653
654 =over 4
655
656 =item *
657
658 Thin interface mutator and constructor method calls
659 on on the failure object,
660 or on one of the descendants
661 of the failure object.
662
663 =back
664
665 The following application behaviors remain available:
666
667 =over 4
668
669 =item *
670
671 All behaviors not in the thin interface,
672 including the Perl environment itself,
673 and all packages other than the thin interface.
674
675 =item *
676
677 All thin interface static method calls.
678
679 =item *
680
681 All thin interface accessor method calls,
682 and thin interface object destruction.
683
684 =item *
685
686 All thin interface constructor and mutator method calls
687 for objects that are not the failure object,
688 or one of its descendants.
689
690 =back
691
692 For more details on ancestry-recoverability, refer to
693 the Libmarpa API document.
694
695 =head2 Fully recoverable failure
696
697 After a fully recoverable failure,
698 all application behaviors remain available.
699 For more details on fully recoverable failure, refer to
700 the Libmarpa API document.
701
702 =head1 Error methods
703
704 The thin interface to Libmarpa provides
705 error methods
706 more appropriate to the Perl
707 environment
708 than Libmarpa's own.
709 Calls to these methods are diagnostic behaviors.
710
711 =head2 C<< $g->error() >>
712
713 =for Marpa::R2::Display
714 name: Thin grammar error methods
715 normalize-whitespace: 1
716
717 my ( $error_code, $error_description ) = $grammar->error();
718 my @error_names = Marpa::R2::Thin::error_names();
719 my $error_name = $error_names[$error_code];
720
721 =for Marpa::R2::Display::End
722
723 In scalar context,
724 the C<error()> method returns the error description.
725 In array context, it returns a 2-element array.
726 The first element of the array is the error code,
727 and the second element is the error description.
728 Applications should assume that
729 a call to any other Marpa thin method
730 will overwrite the error code and error description.
731 For C<error()> to successfully query
732 the error code or error description of a method,
733 C<error()> should be
734 the next Marpa thin interface method called.
735
736 =head2 C<< $g->error_clear() >>
737
738 The C<error_clear()> method follows the general pattern.
739
740 =head2 C<< $g->error_names() >>
741
742 For a synopsis, see L<< the section on
743 the C<< $g->error() >>
744 method|C<< $g->error() >> >>.
745 The C<error_names()> method returns a
746 B<reference> to an array of error names,
747 indexed by Libmarpa error code.
748
749 =head2 C<< $g->throw_set() >>
750
751 =for Marpa::R2::Display
752 name: Thin throw_set() example
753 normalize-whitespace: 1
754
755 $grammar->throw_set(0);
756
757 =for Marpa::R2::Display::End
758
759 The C<throw_set()> method turns the throw flag
760 for the grammar on or off,
761 according to whether its argument is 1 or 0.
762 C<throw_set()> fails if its argument is
763 not a numeric 0 or 1.
764 C<throw_set()> itself never returns failure --
765 it always throws an exception.
766
767 =head2 Omitted configuration methods
768
769 All of the methods of Libmarpa's configuration
770 class are omitted in the Marpa thin interface.
771 The functions performed by Libmarpa's configuration
772 methods are handled in a more Perl-centric way
773 by the Marpa thin interface.
774
775 =head1 Grammar methods
776
777 =head2 C<< Marpa::R2::Thin::G->new() >>
778
779 =for Marpa::R2::Display
780 name: Thin example
781 partial: 1
782 normalize-whitespace: 1
783
784 my $grammar = Marpa::R2::Thin::G->new( { if => 1 } );
785
786 =for Marpa::R2::Display::End
787
788 The one argument to the Marpa thin interface's
789 grammar constructor,
790 is a reference to a hash of named arguments.
791 On success,
792 the return value is a thin interface grammar object.
793 C<new()> does not obey the throw setting --
794 errors are always thrown.
795
796 Most applications should call the
797 C<force_valued()> method immediately after the call
798 of the C<new()> constructor.
799 Not doing so enables unvalued terminals,
800 which is a deprecated feature both in the thin
801 interface and the Libmarpa API.
802
803 At present the only named argument allowed is C<if>,
804 the interface number.
805 This argument is required and currently is required
806 to have a value of 1,
807 which specifies interface 1.
808 The intent of the C<if> argument is to provide
809 for backward compatibility in the future.
810
811 Although there is no error message or warning if the hash
812 ref argument
813 is omitted, new code should treat
814 the hash ref argument as a required argument.
815 Calling C<new()> without an argument is deprecated.
816 If the hash ref argument is omitted, the thin
817 layer uses interface 0.
818 Interface 0 cannot be specified directly,
819 and is deprecated.
820 The difference between interface 0 and interface 1
821 is that, in interface 0, the default throw setting
822 of the newly created grammar object
823 is unspecified.
824 (In fact, the interface 0 throw setting depends on an
825 undocumented and deprecated global variable.)
826
827 =head2 C<< $g->force_valued() >>
828
829 =for Marpa::R2::Display
830 name: Thin example
831 partial: 1
832 normalize-whitespace: 1
833
834 $grammar->force_valued();
835
836 =for Marpa::R2::Display::End
837
838 The C<force_valued()> follows the general pattern.
839 Most applications should call the
840 C<force_valued()> method immediately after the
841 grammar's C<new()> constructor.
842 Not doing so enables unvalued terminals,
843 which is a deprecated feature both in the thin
844 interface and the Libmarpa API.
845
846 =head2 C<< $g->event() >>
847
848 =for Marpa::R2::Display
849 name: Thin event() example
850 normalize-whitespace: 1
851
852 my ( $event_type, $value ) = $grammar->event( $event_ix++ );
853
854 =for Marpa::R2::Display::End
855
856 The C<event()> method returns a two-element array on success.
857 The first element is a string naming the event type,
858 and the second is a scalar representing its value.
859 The string for an event type is its macro name,
860 as given in the Libmarpa API document.
861
862 Some event types have an event "value".
863 All event values are numeric Perl scalars.
864 The number is either a symbol ID or a count,
865 as described in the Libmarpa API document.
866
867 The permissible range of event indexes can be
868 found with the Marpa thin interface's C<event_count()>
869 grammar method,
870 which corresponds to Libmarpa's
871 C<marpa_g_event_count()> method.
872 The thin interface's C<event_count()> method
873 follows the general pattern.
874
875 Since C<event()> returns the event value whenever it
876 exists,
877 the Libmarpa C<marpa_g_event_value()> method is
878 unneeded.
879 The Libmarpa C<marpa_g_event_value()> method has
880 no corresponding Marpa thin interface method.
881
882 C<event()> obeys the throw setting.
883 On unthrown failure, C<event()> returns a Perl C<undef>.
884
885 =head2 C<< $g->rule_new() >>
886
887 =for Marpa::R2::Display
888 name: Thin example
889 partial: 1
890 normalize-whitespace: 1
891
892 my $start_rule_id = $grammar->rule_new( $symbol_S, [$symbol_E] );
893
894 =for Marpa::R2::Display::End
895
896 The C<rule_new()> grammar method is
897 the Libmarpa thin interface method corresponding
898 to the C<marpa_g_rule_new()> method.
899 It takes two arguments, both required.
900 The first argument is a symbol ID representing the
901 rule's LHS,
902 and the second argument is a reference to an
903 array of symbol ID's.
904 The symbol ID's in the array represent the RHS.
905 On success,
906 the return value is the ID of the new rule.
907
908 C<rule_new()> obeys the throw setting.
909 On unthrown failure, it returns -2.
910
911 =head2 C<< $g->sequence_new() >>
912
913 =for Marpa::R2::Display
914 name: Thin sequence_new() example
915 normalize-whitespace: 1
916
917 my $sequence_rule_id = $grammar->sequence_new(
918 $symbol_S,
919 $symbol_a,
920 { separator => $symbol_sep,
921 proper => 0,
922 min => 1
923 }
924 );
925
926 =for Marpa::R2::Display::End
927
928 The C<sequence_new()> grammar method is
929 the Libmarpa thin interface method corresponding
930 to the C<marpa_g_sequence_new()> method.
931 It takes three arguments, all required.
932 The first argument is a symbol ID representing the
933 sequence's LHS.
934 The second argument is a symbol ID representing the
935 sequence's RHS.
936 The third argument is a reference to a hash of named
937 arguments.
938
939 The hash of named arguments may be empty.
940 If not empty, its keys, and their values,
941 must be one of the following:
942
943 =over
944
945 =item C<separator>
946
947 The value of the C<separator> named argument
948 will be treated as an integer,
949 and passed as the separator ID argument
950 to the C<marpa_g_sequence_new()> method.
951 It defaults to -1.
952
953 =item C<proper>
954
955 If the value of C<proper> named argument
956 is a Perl true value,
957 the C<MARPA_PROPER_SEPARATION> flag
958 will be set in the flags passed
959 to the C<marpa_g_sequence_new()> method.
960 Otherwise,
961 the C<MARPA_PROPER_SEPARATION> flag
962 will not be set.
963
964 =item C<min>
965
966 The value of the C<min> named argument
967 will be treated as an integer,
968 and passed as the C<min> argument
969 to the C<marpa_g_sequence_new()> method.
970 The C<min> argument indicates the minimum number of
971 repetitions of the sequence that are required.
972 It defaults to 1.
973
974 =back
975
976 On success,
977 the return value is
978 the rule ID of the new sequence.
979
980 Users should be aware that all sequences at
981 the Marpa thin interface level are "keep
982 separation".
983 This differs from the higher-level interface,
984 which discards separators by default.
985 At the
986 Marpa thin interface level, it is up
987 to the programmer to discard separators,
988 if that is what is wanted.
989
990 C<sequence_new()> obeys the throw setting.
991 On unthrown failure, it returns -2.
992
993 =head2 C<< $g->precompute() >>
994
995 =for Marpa::R2::Display
996 name: Thin example
997 partial: 1
998 normalize-whitespace: 1
999
1000 $grammar->precompute();
1001
1002 =for Marpa::R2::Display::End
1003
1004 The C<precompute()> method follows the general pattern.
1005 In addition to errors,
1006 C<precompute()> also reports events.
1007 Events are queried using the grammar's
1008 C<event()> method.
1009
1010 As in the Libmarpa API, parsing with cycles
1011 is deprecated.
1012 This mean that treating C<MARPA_ERR_GRAMMAR_HAS_CYCLE> as a
1013 fully recoverable failure is deprecated.
1014 Instead,
1015 C<MARPA_ERR_GRAMMAR_HAS_CYCLE> should be treated as
1016 as a library-recoverable failure.
1017
1018 On success, C<precompute()> returns an event count.
1019 But, even when there is an error,
1020 C<precompute()> often reports
1021 one or more events.
1022 It is not safe to assume that no events occurred
1023 unless C<precompute()> succeeds and reports
1024 an event count of zero.
1025
1026 =head2 C<< $g->rule_rank() >>
1027
1028 The C<rule_rank()> method is based on
1029 Libmarpa's C<marpa_g_rule_rank()> method.
1030 Its argument is the rule ID,
1031 and its return value is the rank,
1032 or a -2 to indicate an unthrown error.
1033
1034 Note a return value of -2 is ambiguous -- it can
1035 indicate that the rank was -2, or that a failure
1036 occurred.
1037 To distinguish the cases, the application can
1038 look at the error code.
1039 The error code
1040 will be C<MARPA_ERR_NONE> if and only
1041 if the call was successful.
1042 The error code can be found using the L<< C<error()>
1043 method|C<< $g->error() >> >>.
1044 Applications may find it more convenient to have
1045 C<rule_rank()> always throw its errors.
1046
1047 =head2 C<< $g->rule_rank_set() >>
1048
1049 The C<rule_rank_set()> method is based on
1050 Libmarpa's C<marpa_g_rule_rank_set()> method.
1051 Its two arguments are the rule ID and a rule rank.
1052 Its return value is the new value of the rank,
1053 or a -2 to indicate an unthrown error.
1054
1055 Note a return value of -2 is ambiguous -- it can
1056 indicate that the rank was -2, or that a failure
1057 occurred.
1058 To distinguish the cases, the application can
1059 look at the error code.
1060 The error code
1061 will be C<MARPA_ERR_NONE> if and only
1062 if the call was successful.
1063 The error code can be found using the L<< C<error()>
1064 method|C<< $g->error() >> >>.
1065 Applications may find it more convenient to have
1066 C<rule_rank_set()> always throw its errors.
1067
1068 =head2 C<< $g->symbol_is_terminal_set() >>
1069
1070 =for Marpa::R2::Display
1071 name: Thin symbol_is_terminal_set() example
1072 normalize-whitespace: 1
1073
1074 $grammar->symbol_is_terminal_set( $symbol_seq, 1 );
1075
1076 =for Marpa::R2::Display::End
1077
1078 The C<symbol_is_terminal_set()> method is deprecated.
1079 It enables the LHS terminals feature, which is deprecated
1080 in both the thin interface and the Libmarpa API.
1081 The C<symbol_is_terminal_set()> method follows the general pattern.
1082
1083 =head2 Omitted grammar methods
1084
1085 The C<marpa_g_ref()>
1086 and C<marpa_g_unref()> methods are omitted
1087 because the Marpa thin interface performs
1088 their function.
1089 The C<marpa_g_event_value()> method is omitted
1090 because its function is absorbed into
1091 the thin interface's C<event()> grammar method.
1092
1093 =head2 General pattern methods
1094
1095 All grammar methods that are part of the Libmarpa external interface,
1096 but that are not mentioned explicitly in this document,
1097 are implemented
1098 following the general pattern, as described
1099 L<above|"The general pattern">.
1100
1101 =head1 Recognizer methods
1102
1103 =head2 C<< Marpa::R2::Thin::R->new() >>
1104
1105 =for Marpa::R2::Display
1106 name: Thin example
1107 partial: 1
1108 normalize-whitespace: 1
1109
1110 my $recce = Marpa::R2::Thin::R->new($grammar);
1111
1112 =for Marpa::R2::Display::End
1113
1114 The C<new()> method takes a Marpa thin grammar object
1115 as its one argument.
1116 On success, it returns a Marpa thin recognizer object.
1117 C<new()> obeys the throw setting.
1118 On unthrown failure, it returns a Perl C<undef>.
1119
1120 =head2 C<< $r->ruby_slippers_set() >>
1121
1122 =for Marpa::R2::Display
1123 name: Thin ruby_slippers_set() example
1124 normalize-whitespace: 1
1125
1126 $recce->ruby_slippers_set(1);
1127
1128 =for Marpa::R2::Display::End
1129
1130 With an argument of 1,
1131 the C<ruby_slippers_set()> method enables "Ruby Slippers" mode.
1132 An argument of 0 disables "Ruby Slippers" mode.
1133 By default,
1134 Ruby Slippers mode is disabled.
1135 Note that this default (disabled) is
1136 the opposite of that in the higher level L<Marpa::R2> interface.
1137
1138 The L<< C<alternative()>
1139 method|C<< $r->alternative() >> >>
1140 will only throw exceptions when
1141 "Ruby Slippers" mode is disabled and the throw flag is on.
1142 One way of describing Ruby Slippers mode is as
1143 an override of the throw setting, one which only applies
1144 to the C<alternative()> method.
1145
1146 The C<ruby_slippers_set()> method itself does B<not>
1147 obey the throw setting.
1148 All failures by C<ruby_slippers_set()> are thrown
1149 as exceptions.
1150
1151 =head2 C<< $r->alternative() >>
1152
1153 =for Marpa::R2::Display
1154 name: Thin example
1155 partial: 1
1156 normalize-whitespace: 1
1157
1158 $recce->alternative( $symbol_number, 2, 1 );
1159
1160 =for Marpa::R2::Display::End
1161
1162 In the Libmarpa API
1163 the
1164 C<alternative()> method returns an error
1165 code,
1166 with
1167 C<MARPA_ERR_NONE> being the code returned if there
1168 was no error.
1169 The C<alternative()> method will
1170 throw the error code as an
1171 exception if and only if all three
1172 of the following are true:
1173
1174 =over
1175
1176 =item * The base grammar's throw flag is on.
1177
1178 =item * The Ruby Slippers flag is off.
1179
1180 =item * The error code is not C<MARPA_ERR_NONE>.
1181
1182 =back
1183
1184 Of major interest is the error code
1185 C<MARPA_ERR_UNEXPECTED_TOKEN_ID>,
1186 which indicates that a token was not accepted because
1187 its token ID was not one of those expected.
1188 Catching and recovering
1189 from this error is the basis of the Ruby Slippers parsing
1190 technique.
1191 For more on the Ruby Slippers flag,
1192 see L<< C<ruby_slippers_set()>|C<< $r->ruby_slippers_set() >> >>.
1193
1194 =head2 C<< $r->terminals_expected() >>
1195
1196 =for Marpa::R2::Display
1197 name: Thin terminals_expected() example
1198 partial: 1
1199 normalize-whitespace: 1
1200
1201 my @terminals = $recce->terminals_expected();
1202
1203 =for Marpa::R2::Display::End
1204
1205 The C<terminals_expected()> method takes no arguments.
1206 On success, it returns an array containing the
1207 symbol ID's of the expected terminals.
1208 Note that the array of expected terminal ID's may
1209 be empty, so that an empty array is NOT a failure
1210 indicator.
1211 C<terminals_expected()> obeys the throw setting.
1212 On unthrown failure,
1213 C<terminals_expected()> returns a Perl C<undef>.
1214
1215 =head2 C<< $r->progress_item() >>
1216
1217 =for Marpa::R2::Display
1218 name: Thin progress_item() example
1219
1220 my $ordinal = $recce->latest_earley_set();
1221 $recce->progress_report_start($ordinal);
1222 ITEM: while (1) {
1223 my ($rule_id, $dot_position, $origin) = $recce->progress_item();
1224 last ITEM if not defined $rule_id;
1225 push @{$report}, [$rule_id, $dot_position, $origin];
1226 }
1227 $recce->progress_report_finish();
1228
1229 =for Marpa::R2::Display::End
1230
1231 The C<progress_item()> method takes no arguments.
1232 On success, it returns an array of 3 elements:
1233 the rule ID, the dot position, and the earley set ID
1234 of the origin.
1235 If there are no more items,
1236 C<progress_item()> returns a Perl C<undef>.
1237
1238 C<progress_item()> obeys the throw setting.
1239 On unthrown failure,
1240 the rule ID element in the array returned by
1241 C<progress_item()> will have a value of -2.
1242
1243 =head2 Omitted recognizer methods
1244
1245 Because the Marpa thin interface
1246 handles reference counting internally,
1247 it does not implement methods
1248 directly corresponding to
1249 Libmarpa's C<marpa_r_ref()>
1250 and C<marpa_r_unref()> methods.
1251
1252 There are Marpa thin methods
1253 corresponding to
1254 Libmarpa's C<marpa_r_earley_set_value()>
1255 and C<marpa_r_earley_set_value_set()> methods,
1256 but not to
1257 Libmarpa's C<marpa_r_earley_set_values()>
1258 and C<marpa_r_earley_set_values_set()> methods.
1259 The difference between these is that the "values" form allows an
1260 integer and a pointer value to be set,
1261 while
1262 the "value" form allows only an integer to be set.
1263 Perl applications which want to associate non-integer data with an Earley set
1264 should create an array, and use the integer to index the array.
1265 The elements of the array can contain arbitrary data.
1266
1267 The thin interface does not implement a way to set the Earley set pointer
1268 value, because to do so would not add value.
1269 The thin interface would have to track the reference
1270 count of a pointer,
1271 and this can done as easily and efficiently,
1272 and with more flexibility, at the Perl level.
1273
1274 =head2 Methods not mentioned
1275
1276 All recognizer methods that are part of the Libmarpa external interface,
1277 but that are not mentioned explicitly in this document,
1278 are implemented
1279 following the general pattern, as described
1280 L<above|"The general pattern">.
1281
1282 =head1 Bocage methods
1283
1284 =head2 C<< Marpa::R2::Thin::B->new() >>
1285
1286 =for Marpa::R2::Display
1287 name: Thin example
1288 partial: 1
1289 normalize-whitespace: 1
1290
1291 my $latest_earley_set_ID = $recce->latest_earley_set();
1292 my $bocage = Marpa::R2::Thin::B->new( $recce, $latest_earley_set_ID );
1293
1294 =for Marpa::R2::Display::End
1295
1296 The C<new()> method takes a Marpa thin recognizer object
1297 as its one argument.
1298 On success, it returns a Marpa thin bocage object.
1299 C<new()> obeys the throw setting.
1300 On unthrown failure, it returns a Perl C<undef>.
1301
1302 =head2 Omitted bocage methods
1303
1304 Because the Marpa thin interface
1305 handles reference counting internally,
1306 it does not implement methods
1307 directly corresponding to
1308 Libmarpa's C<marpa_b_ref()>
1309 and C<marpa_b_unref()> methods.
1310
1311 =head2 Methods not mentioned
1312
1313 All bocage methods that are part of the Libmarpa external interface,
1314 but that are not mentioned explicitly in this document,
1315 are implemented
1316 following the general pattern, as described
1317 L<above|"The general pattern">.
1318
1319 =head1 Ordering methods
1320
1321 =head2 C<< Marpa::R2::Thin::O->new() >>
1322
1323 =for Marpa::R2::Display
1324 name: Thin example
1325 partial: 1
1326 normalize-whitespace: 1
1327
1328 my $order = Marpa::R2::Thin::O->new($bocage);
1329
1330 =for Marpa::R2::Display::End
1331
1332 The C<new()> method takes a Marpa thin bocage object
1333 as its one argument.
1334 On success, it returns a Marpa thin ordering object.
1335 C<new()> obeys the throw setting.
1336 On unthrown failure, it returns a Perl C<undef>.
1337
1338 =head2 Omitted ordering methods
1339
1340 Because the Marpa thin interface
1341 handles reference counting internally,
1342 it does not implement methods
1343 directly corresponding to
1344 Libmarpa's C<marpa_o_ref()>
1345 and C<marpa_o_unref()> methods.
1346
1347 =head2 Methods not mentioned
1348
1349 All ordering methods that are part of the Libmarpa external interface,
1350 but that are not mentioned explicitly in this document,
1351 are implemented
1352 following the general pattern, as described
1353 L<above|"The general pattern">.
1354
1355 =head1 Tree methods
1356
1357 =head2 C<< Marpa::R2::Thin::T->new() >>
1358
1359 =for Marpa::R2::Display
1360 name: Thin example
1361 partial: 1
1362 normalize-whitespace: 1
1363
1364 my $tree = Marpa::R2::Thin::T->new($order);
1365
1366 =for Marpa::R2::Display::End
1367
1368 The C<new()> method takes a Marpa thin ordering object
1369 as its one argument.
1370 On success, it returns a Marpa thin tree object.
1371 C<new()> obeys the throw setting.
1372 On unthrown failure, it returns a Perl C<undef>.
1373
1374 =head2 Omitted tree methods
1375
1376 Because the Marpa thin interface
1377 handles reference counting internally,
1378 it does not implement methods
1379 directly corresponding to
1380 Libmarpa's C<marpa_t_ref()>
1381 and C<marpa_t_unref()> methods.
1382
1383 =head2 Methods not mentioned
1384
1385 All tree methods that are part of the Libmarpa external interface,
1386 but that are not mentioned explicitly in this document,
1387 are implemented
1388 following the general pattern, as described
1389 L<above|"The general pattern">.
1390
1391 =head1 Value methods
1392
1393 =head2 C<< Marpa::R2::Thin::V->new() >>
1394
1395 =for Marpa::R2::Display
1396 name: Thin example
1397 partial: 1
1398 normalize-whitespace: 1
1399
1400 my $valuator = Marpa::R2::Thin::V->new($tree);
1401
1402 =for Marpa::R2::Display::End
1403
1404 The C<new()> method takes a Marpa thin tree object
1405 as its one argument.
1406 On success, it returns a Marpa thin value object.
1407 C<new()> obeys the throw setting.
1408 On unthrown failure, it returns a Perl C<undef>.
1409
1410 =head2 C<< $v->location() >>
1411
1412 =for Marpa::R2::Display
1413 name: Thin location() example
1414 normalize-whitespace: 1
1415
1416 $type = $valuator->step_type();
1417 my ( $start, $end ) = $valuator->location();
1418 if ( $type eq 'MARPA_STEP_RULE' ) {
1419 my ($rule_id) = @step_data;
1420 $locations_report .= "Rule $rule_id is from $start to $end\n";
1421 }
1422 if ( $type eq 'MARPA_STEP_TOKEN' ) {
1423 my ($token_id) = @step_data;
1424 $locations_report .= "Token $token_id is from $start to $end\n";
1425 }
1426 if ( $type eq 'MARPA_STEP_NULLING_SYMBOL' ) {
1427 my ($symbol_id) = @step_data;
1428 $locations_report
1429 .= "Nulling symbol $symbol_id is from $start to $end\n";
1430 }
1431
1432
1433 =for Marpa::R2::Display::End
1434
1435 The C<location()> method takes no arguments.
1436 The C<location()> method always succeeds,
1437 returning either an empty array
1438 or an array of two elements.
1439
1440 =over 4
1441
1442 =item *
1443
1444 If the last step was C<MARPA_STEP_RULE>,
1445 the array contains the locations where the rule starts
1446 and ends,
1447 as returned
1448 by the Libmarpa methods C<marpa_v_rule_start_es_id()>
1449 and C<marpa_v_es_id()>.
1450
1451 =item *
1452
1453 It the last step was C<MARPA_STEP_TOKEN>,
1454 the array contains the locations where the token starts
1455 and ends,
1456 as returned
1457 by the Libmarpa methods C<marpa_v_token_start_es_id()>
1458 and C<marpa_v_es_id()>.
1459
1460 =item *
1461
1462 It the last step was C<MARPA_STEP_NULLING_SYMBOL>,
1463 the array contains the locations where the token starts
1464 and ends,
1465 as returned
1466 by the Libmarpa methods C<marpa_v_token_start_es_id()>
1467 and C<marpa_v_es_id()>.
1468
1469 =item *
1470
1471 In any other case,
1472 the array is empty.
1473
1474 =back
1475
1476 =head2 C<< $v->step() >>
1477
1478 =for Marpa::R2::Display
1479 name: Thin example
1480 partial: 1
1481 normalize-whitespace: 1
1482
1483 my ( $type, @step_data ) = $valuator->step();
1484
1485 =for Marpa::R2::Display::End
1486
1487 The C<step()> method takes no arguments.
1488 On success, C<step()>
1489 returns an array, whose contents are as follows:
1490
1491 =over 4
1492
1493 =item C<MARPA_STEP_RULE>
1494
1495 If the step type is C<MARPA_STEP_RULE>, C<step()>
1496 returns an array of 4 elements. These will be,
1497 in order:
1498
1499 =over 4
1500
1501 =item *
1502
1503 The string "C<MARPA_STEP_RULE>".
1504
1505 =item *
1506
1507 The rule id, as returned
1508 by the Libmarpa method C<marpa_v_rule_id()>.
1509
1510 =item *
1511
1512 The stack location where the child values
1513 of the rule begin,
1514 as returned
1515 by the Libmarpa method C<marpa_v_token_arg_0()>.
1516 This is also the stack location to which the result
1517 should be written.
1518
1519 =item *
1520
1521 The stack location where the child values
1522 of the rule end,
1523 as returned
1524 by the Libmarpa method C<marpa_v_token_arg_n()>.
1525
1526 =back
1527
1528 =item C<MARPA_STEP_TOKEN>
1529
1530 If the step type is C<MARPA_STEP_TOKEN>, C<step()>
1531 returns an array of 4 elements. These will be,
1532 in order:
1533
1534 =over 4
1535
1536 =item *
1537
1538 The string "C<MARPA_STEP_TOKEN>".
1539
1540 =item *
1541
1542 The token id, as returned
1543 by the Libmarpa method C<marpa_v_token()>.
1544
1545 =item *
1546
1547 The token value, as returned
1548 by the Libmarpa method C<marpa_v_token_value()>.
1549
1550 =item *
1551
1552 The stack location to which the token's
1553 value should be written,
1554 as returned
1555 by the Libmarpa method C<marpa_v_result()>.
1556
1557 =back
1558
1559 As a reminder, Libmarpa's token values are always integers.
1560 Applications will often have a richer or different
1561 semantics for token values.
1562 One approach such applications can take is
1563 to use
1564 Libmarpa's token values as indexes into an array.
1565
1566 =item C<MARPA_STEP_NULLING_SYMBOL>
1567
1568 If the step type is C<MARPA_STEP_NULLING_SYMBOL>, C<step()>
1569 returns an array of 3 elements. These will be,
1570 in order:
1571
1572 =over 4
1573
1574 =item *
1575
1576 The string "C<MARPA_STEP_NULLING_SYMBOL>".
1577
1578 =item *
1579
1580 The ID of the nulling symbol, as returned
1581 by the Libmarpa method C<marpa_v_symbol()>.
1582
1583 =item *
1584
1585 The stack location to which the nulling
1586 symbol's value should be written,
1587 as returned
1588 by the Libmarpa method C<marpa_v_result()>.
1589
1590 =back
1591
1592 =item C<MARPA_STEP_INACTIVE>
1593
1594 If the step type is C<MARPA_STEP_INACTIVE>, C<step()> returns
1595 an empty array.
1596
1597 =back
1598
1599 C<step()> obeys the throw setting.
1600 On unthrown failure,
1601 C<step()> returns an array whose only
1602 element is a
1603 string not reserved by Libmarpa.
1604 A string is not reserved by Libmarpa if it does
1605 not begin with "C<MARPA_>" in one of its
1606 capitalization variants.
1607 The string will usually be a description of the error.
1608
1609 =head2 C<< $v->step_type() >>
1610
1611 =for Marpa::R2::Display
1612 name: Thin location() example
1613 partial: 1
1614 normalize-whitespace: 1
1615
1616 $type = $valuator->step_type();
1617
1618 =for Marpa::R2::Display::End
1619
1620 The C<step_type()> method takes no arguments.
1621 On success, C<step_type()>
1622 returns the string indicating the type of the last
1623 Libmarpa valuator step.
1624 If the last call of the
1625 L<< C<step()>
1626 method|C<< $v->step() >> >> succeeded,
1627 the string returned by C<step_type()> will
1628 be the same as the one that was the first
1629 element of the array returned by C<step()>.
1630
1631 C<step_type()> obeys the throw setting.
1632 On unthrown failure,
1633 C<step()> returns an array whose only
1634 element is a
1635 string not reserved by Libmarpa.
1636 A string is not reserved by Libmarpa if it does
1637 not begin with "C<MARPA_>" in one of its
1638 capitalization variants.
1639 The string will usually be a description of the error.
1640
1641 =head2 Omitted value methods
1642
1643 Because the Marpa thin interface
1644 handles reference counting internally,
1645 it does not implement methods
1646 directly corresponding to
1647 Libmarpa's C<marpa_v_ref()>
1648 and C<marpa_v_unref()> methods.
1649 The step accessor macros are folded into the thin interface's
1650 C<< $v->step() >>
1651 and C<< $v->location() >>
1652 methods.
1653 For this reason,
1654 no thin interface macro corresponds directly to most
1655 of the individual step accessors.
1656
1657 =head2 Methods not mentioned
1658
1659 All value methods that are part of the Libmarpa external interface,
1660 but that are not mentioned explicitly in this document,
1661 are implemented
1662 following the general pattern, as described
1663 L<above|"The general pattern">.
1664
1665 =head1 Example
1666
1667 =for Marpa::R2::Display
1668 name: Thin example
1669 normalize-whitespace: 1
1670
1671 my $grammar = Marpa::R2::Thin::G->new( { if => 1 } );
1672 $grammar->force_valued();
1673 my $symbol_S = $grammar->symbol_new();
1674 my $symbol_E = $grammar->symbol_new();
1675 $grammar->start_symbol_set($symbol_S);
1676 my $symbol_op = $grammar->symbol_new();
1677 my $symbol_number = $grammar->symbol_new();
1678 my $start_rule_id = $grammar->rule_new( $symbol_S, [$symbol_E] );
1679 my $op_rule_id =
1680 $grammar->rule_new( $symbol_E, [ $symbol_E, $symbol_op, $symbol_E ] );
1681 my $number_rule_id = $grammar->rule_new( $symbol_E, [$symbol_number] );
1682 $grammar->precompute();
1683
1684 my $recce = Marpa::R2::Thin::R->new($grammar);
1685 $recce->start_input();
1686
1687 # The numbers from 1 to 3 are themselves --
1688 # that is, they index their own token value.
1689 # Important: zero cannot be itself!
1690
1691 my @token_values = ( 0 .. 3 );
1692 my $zero = -1 + push @token_values, 0;
1693 my $minus_token_value = -1 + push @token_values, q{-};
1694 my $plus_token_value = -1 + push @token_values, q{+};
1695 my $multiply_token_value = -1 + push @token_values, q{*};
1696
1697 $recce->alternative( $symbol_number, 2, 1 );
1698 $recce->earleme_complete();
1699 $recce->alternative( $symbol_op, $minus_token_value, 1 );
1700 $recce->earleme_complete();
1701 $recce->alternative( $symbol_number, $zero, 1 );
1702 $recce->earleme_complete();
1703 $recce->alternative( $symbol_op, $multiply_token_value, 1 );
1704 $recce->earleme_complete();
1705 $recce->alternative( $symbol_number, 3, 1 );
1706 $recce->earleme_complete();
1707 $recce->alternative( $symbol_op, $plus_token_value, 1 );
1708 $recce->earleme_complete();
1709 $recce->alternative( $symbol_number, 1, 1 );
1710 $recce->earleme_complete();
1711
1712 my $latest_earley_set_ID = $recce->latest_earley_set();
1713 my $bocage = Marpa::R2::Thin::B->new( $recce, $latest_earley_set_ID );
1714 my $order = Marpa::R2::Thin::O->new($bocage);
1715 my $tree = Marpa::R2::Thin::T->new($order);
1716 my @actual_values = ();
1717 while ( $tree->next() ) {
1718 my $valuator = Marpa::R2::Thin::V->new($tree);
1719 my @stack = ();
1720 STEP: while ( 1 ) {
1721 my ( $type, @step_data ) = $valuator->step();
1722 last STEP if not defined $type;
1723 if ( $type eq 'MARPA_STEP_TOKEN' ) {
1724 my ( undef, $token_value_ix, $arg_n ) = @step_data;
1725 $stack[$arg_n] = $token_values[$token_value_ix];
1726 next STEP;
1727 }
1728 if ( $type eq 'MARPA_STEP_RULE' ) {
1729 my ( $rule_id, $arg_0, $arg_n ) = @step_data;
1730 if ( $rule_id == $start_rule_id ) {
1731 my ( $string, $value ) = @{ $stack[$arg_n] };
1732 $stack[$arg_0] = "$string == $value";
1733 next STEP;
1734 }
1735 if ( $rule_id == $number_rule_id ) {
1736 my $number = $stack[$arg_0];
1737 $stack[$arg_0] = [ $number, $number ];
1738 next STEP;
1739 }
1740 if ( $rule_id == $op_rule_id ) {
1741 my $op = $stack[ $arg_0 + 1 ];
1742 my ( $right_string, $right_value ) = @{ $stack[$arg_n] };
1743 my ( $left_string, $left_value ) = @{ $stack[$arg_0] };
1744 my $value;
1745 my $text = '(' . $left_string . $op . $right_string . ')';
1746 if ( $op eq q{+} ) {
1747 $stack[$arg_0] = [ $text, $left_value + $right_value ];
1748 next STEP;
1749 }
1750 if ( $op eq q{-} ) {
1751 $stack[$arg_0] = [ $text, $left_value - $right_value ];
1752 next STEP;
1753 }
1754 if ( $op eq q{*} ) {
1755 $stack[$arg_0] = [ $text, $left_value * $right_value ];
1756 next STEP;
1757 }
1758 die "Unknown op: $op";
1759 } ## end if ( $rule_id == $op_rule_id )
1760 die "Unknown rule $rule_id";
1761 } ## end if ( $type eq 'MARPA_STEP_RULE' )
1762 die "Unexpected step type: $type";
1763 } ## end while ( my ( $type, @step_data ) = $valuator->step() )
1764 push @actual_values, $stack[0];
1765 } ## end while ( $tree->next() )
1766
1767 =for Marpa::R2::Display::End
1768
1769 =head1 Copyright and License
1770
1771 =for Marpa::R2::Display
1772 ignore: 1
1773
1774 Copyright 2022 Jeffrey Kegler
1775 This file is part of Marpa::R2. Marpa::R2 is free software: you can
1776 redistribute it and/or modify it under the terms of the GNU Lesser
1777 General Public License as published by the Free Software Foundation,
1778 either version 3 of the License, or (at your option) any later version.
1779
1780 Marpa::R2 is distributed in the hope that it will be useful,
1781 but WITHOUT ANY WARRANTY; without even the implied warranty of
1782 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1783 Lesser General Public License for more details.
1784
1785 You should have received a copy of the GNU Lesser
1786 General Public License along with Marpa::R2. If not, see
1787 http://www.gnu.org/licenses/.
1788
1789 =for Marpa::R2::Display::End
1790
1791 =cut
1792
1793 # Local Variables:
1794 # mode: cperl
1795 # cperl-indent-level: 4
1796 # fill-column: 100
1797 # End:
1798 # vim: expandtab shiftwidth=4: