Debian Codegraph @

pod/Scanless/G.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::Scanless::G - Scanless interface grammars
19
20 =head1 Synopsis
21
22 =for Marpa::R2::Display
23 name: Scanless grammar synopsis
24 partial: 1
25 normalize-whitespace: 1
26
27 my $grammar = Marpa::R2::Scanless::G->new(
28 {
29 source => \(<<'END_OF_SOURCE'),
30 :default ::= action => do_first_arg
31 :start ::= Script
32 Script ::= Expression+ separator => comma action => do_script
33 comma ~ [,]
34 Expression ::=
35 Number
36 | '(' Expression ')' action => do_parens assoc => group
37 || Expression '**' Expression action => do_pow assoc => right
38 || Expression '*' Expression action => do_multiply
39 | Expression '/' Expression action => do_divide
40 || Expression '+' Expression action => do_add
41 | Expression '-' Expression action => do_subtract
42 Number ~ [\d]+
43
44 :discard ~ whitespace
45 whitespace ~ [\s]+
46 # allow comments
47 :discard ~ <hash comment>
48 <hash comment> ~ <terminated hash comment> | <unterminated
49 final hash comment>
50 <terminated hash comment> ~ '#' <hash comment body> <vertical space char>
51 <unterminated final hash comment> ~ '#' <hash comment body>
52 <hash comment body> ~ <hash comment char>*
53 <vertical space char> ~ [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
54 <hash comment char> ~ [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
55 END_OF_SOURCE
56 }
57 );
58
59 =for Marpa::R2::Display::End
60
61 =head1 About this document
62
63 This page is the reference for the grammar objects
64 of Marpa's Scanless interface.
65
66 =head1 Constructor
67
68 The C<new()> method is the constructor for Scanless grammars.
69 An example of its use is L<above|/"Synopsis">.
70 The C<new()> constructor accepts a hash of named arguments.
71 The following named arguments are allowed:
72
73 =head2 bless_package
74
75 Specifies the name of a Perl package.
76 The package is used
77 for blessing node values into a Perl class,
78 in conjunction with the
79 L<C<bless> adverb|Marpa::R2::Scanless::DSL/"bless">.
80 C<bless_package> should not be confused with the
81 L<SLIF's
82 C<semantics_package> recognizer setting|Marpa::R2::Scanless::R/"semantics_package">.
83 The two are not closely related.
84
85 =head2 source
86
87 The value of the C<source> named argument must be a reference
88 to a string which contains a description of the grammar.
89 The string's format is a domain-specific language,
90 described L<in its own
91 document|Marpa::R2::Scanless::DSL>.
92
93 =head2 trace_file_handle
94
95 The value is a file handle.
96 Trace output and warning messages
97 go to the trace file handle.
98 By default the trace file handle is C<STDERR>.
99
100 =head2 Discouraged named arguments
101
102 =head3 action_object
103
104 Use of this argument is discouraged
105 in favor of L<the C<semantics_package> named argument of the SLIF
106 recognizer|Marpa::R2::Scanless::R/"semantics_package">.
107 Like the C<semantics_package> named argument, it sets the semantic
108 package.
109 Unlike the C<semantics_package> named argument, it is a fatal error if used
110 together with an explicit per-parse argument of the SLIF recognizer's C<value()> method.
111 It is also a fatal error to try to use the C<semantics_package>
112 and C<action_object> arguments together.
113
114 =head3 default_action
115
116 Use of this argument is deprecated in favor of using the
117 L<C<action> adverb|Marpa::R2::Scanless::DSL/"action">
118 in a
119 L<default pseudo-rule|Marpa::R2::Scanless::DSL/"Default pseudo-rule">.
120 Specifies the C<default_action> named argument that
121 will be used for the G1 grammar.
122 For details of on possible default action values and how
123 they are used, see
124 L<C<action> adverb|Marpa::R2::Scanless::DSL/"action">.
125
126 =head1 Mutators
127
128 =head2 parse()
129
130 =for Marpa::R2::Display
131 name: Landing page synopsis
132 normalize-whitespace: 1
133 partial: 1
134
135 my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } );
136 my $input = '42 * 1 + 7';
137 my $value_ref = $grammar->parse( \$input, 'My_Actions' );
138
139 =for Marpa::R2::Display::End
140
141 This very-high level method is a "one shot"
142 way of producing a parse value from a grammar and an input stream.
143 The features this method provides
144 are those most often wanted in
145 the "first cut" of a parser.
146
147 As the parser grows,
148 users are likely to find their application has
149 outgrown this method.
150 It is recommended, rather than spend a lot of time
151 exploring ways to adapt this method to expanding needs,
152 that users be quick to abandon it
153 in favor of the lower level calls.
154 As an example of how to make this transition,
155 the tutorial in L<Marpa::R2> is reimplemented
156 using low-level calls in L<Marpa::R2::Tutorial2>.
157
158 The C<parse()> method takes one or more arguments.
159 The first argument, which is required, is a ref to an input string.
160 Optionally, the second argument may be a string specifying the package name
161 for the semantics.
162 The remaining arguments
163 (including the second argument if it exists, but is not a string)
164 must be references to hashes of named arguments.
165 These hash references will be
166 passed, as is,
167 to the constructor for the recognizer.
168
169 This method returns a reference to the only parse value, if there is
170 exactly one parse value.
171 If there is no parse, or if the parse is ambiguous,
172 C<parse()> throws an exception.
173
174 =head2 set()
175
176 =for Marpa::R2::Display
177 name: SLIF grammar set() synopsis
178 normalize-whitespace: 1
179
180 $grammar->set( { trace_file_handle => $trace_fh } );
181
182 =for Marpa::R2::Display::End
183
184 This method allows the named arguments to be changed after an SLIF
185 grammar is created.
186 Currently, the only argument that may be changed in L<C<trace_file_handle>|/"trace_file_handle">.
187
188 =head1 Accessors
189
190 =head2 rule_expand()
191
192 =for Marpa::R2::Display
193 name: SLG rule_expand() synopsis
194
195 my ($lhs_id, @rhs_ids) = $grammar->rule_expand($rule_id);
196 $text .= "Rule #$rule_id: $lhs_id ::= " . (join q{ }, @rhs_ids) . "\n";
197
198 =for Marpa::R2::Display::End
199
200 =for Marpa::R2::Display
201 name: SLG rule_expand() 2 args synopsis
202
203 my ($lhs_id, @rhs_ids) = $grammar->rule_expand($rule_id, 'L0');
204 $text .= "L0 Rule #$rule_id: $lhs_id ::= " . (join q{ }, @rhs_ids) . "\n";
205
206 =for Marpa::R2::Display::End
207
208 "Expands" a rule ID into symbol ID's.
209 An array of symbol ID's is returned.
210 The ID of the LHS symbol is the first element,
211 and the remaining elements are the ID's of the RHS symbols,
212 in order.
213 Returns an empty array if the rule does not exist.
214
215 The first argument is the ID of the rule to be "expanded".
216 The second, optional, argument is the name of a subgrammar.
217 Currently there are L0 and G1 subgrammars.
218 The default subgrammar is G1.
219
220 =head2 rule_ids()
221
222 =for Marpa::R2::Display
223 name: SLG rule_ids() synopsis
224 normalize-whitespace: 1
225
226 do_something($_) for $grammar->rule_ids();
227
228 =for Marpa::R2::Display::End
229
230 =for Marpa::R2::Display
231 name: SLG rule_ids() 2 arg synopsis
232 normalize-whitespace: 1
233
234 do_something($_) for $grammar->rule_ids('L0');
235
236 =for Marpa::R2::Display::End
237
238 Returns a list of the rule ID's as an array.
239 Takes one, optional, argument: the name of a subgrammar.
240 Currently there are L0 and G1 subgrammars.
241 The default subgrammar is G1.
242
243 =head2 rule_name()
244
245 =for Marpa::R2::Display
246 name: $grammar->rule_name() example
247
248 push @rule_names, $grammar->rule_name($_) for $grammar->rule_ids();
249
250 =for Marpa::R2::Display::End
251
252 Given a rule ID, returns the rule name.
253 A rule name is as defined by
254 L<the C<name> adverb|Marpa::R2::Scanless::DSL/"name">.
255 If no rule name was defined, the rule name is the name of
256 the LHS symbol.
257
258 =head2 rule_show()
259
260 =for Marpa::R2::Display
261 name: SLG rule_show() synopsis
262
263 my $rule_description = $grammar->rule_show($rule_id);
264
265 =for Marpa::R2::Display::End
266
267 =for Marpa::R2::Display
268 name: SLG rule_show() 2 args synopsis
269
270 my $rule_description = $grammar->rule_show($rule_id, 'L0');
271
272 =for Marpa::R2::Display::End
273
274 For a rule ID,
275 returns a string describing that rule in a form which is useful for tracing and debugging,
276 but subject to change.
277 Returns a Perl undef if the rule does not exist.
278
279 The first argument is the ID of the rule to be displayed.
280 The second, optional, argument is the name of a subgrammar.
281 Currently there are L0 and G1 subgrammars.
282 The default subgrammar is G1.
283
284 =head2 start_symbol_id()
285
286 =for Marpa::R2::Display
287 name: $grammar->start_symbol_id() example
288
289 my $start_id = $grammar->start_symbol_id();
290
291 =for Marpa::R2::Display::End
292
293 Returns the ID of the start symbol.
294 Note that there is no method to return the ID of the start
295 rule, because there may be no unique start rule.
296
297 =head2 symbol_description()
298
299 =for Marpa::R2::Display
300 name: SLG symbol_description() synopsis
301
302 my $description = $grammar->symbol_description($symbol_id)
303 // '[No description]';
304 $text .= "symbol number: $symbol_id description $description\n";
305
306 =for Marpa::R2::Display::End
307
308 =for Marpa::R2::Display
309 name: SLG symbol_description() 2 arg synopsis
310
311 my $description = $grammar->symbol_description( $symbol_id, 'L0' )
312 // '[No description]';
313 $text .= "L0 symbol number: $symbol_id description $description\n";
314
315 =for Marpa::R2::Display::End
316
317 Given a symbol ID, returns a description of the symbol.
318 The description may not be defined.
319 Currently internal symbols tend to have descriptions,
320 while symbols explicitly specified by the user in the DSL are treated as self-explanatory.
321 The description is intended for humans to read, and is subject to change.
322
323 The first argument is the symbol ID.
324 A second, optional, argument is the subgrammar.
325 Currently there are L0 and G1 subgrammars.
326 The default subgrammar is G1.
327 Returns a Perl C<undef> if the symbol does not exist,
328 or if it has no description.
329
330 =head2 symbol_display_form()
331
332 =for Marpa::R2::Display
333 name: SLG symbol_display_form() synopsis
334
335 my $display_form = $grammar->symbol_display_form($symbol_id);
336 $text
337 .= "symbol number: $symbol_id name in display form: $display_form\n";
338
339 =for Marpa::R2::Display::End
340
341 =for Marpa::R2::Display
342 name: SLG symbol_display_form() 2 arg synopsis
343
344 my $display_form = $grammar->symbol_display_form( $symbol_id, 'L0' );
345 $text
346 .= "L0 symbol number: $symbol_id name in display form: $display_form\n";
347
348 =for Marpa::R2::Display::End
349
350 Given a symbol ID, returns the "display form" of the symbol.
351 This is the symbol in a form thought most suitable for display in messages, etc.
352 The display form is always defined.
353 The display form of a symbol is not useable as a name -- it is not necessarily unique,
354 and is subject to change.
355
356 The first argument is the symbol ID.
357 A second, optional, argument is the subgrammar.
358 Currently there are L0 and G1 subgrammars.
359 The default subgrammar is G1.
360 Returns a Perl C<undef> if the symbol does not exist.
361
362 =head2 symbol_dsl_form()
363
364 =for Marpa::R2::Display
365 name: SLG symbol_dsl_form() synopsis
366
367 my $dsl_form = $grammar->symbol_dsl_form($symbol_id)
368 // '[No name in DSL form]';
369 $text .= "symbol number: $symbol_id DSL form: $dsl_form\n";
370
371 =for Marpa::R2::Display::End
372
373 =for Marpa::R2::Display
374 name: SLG symbol_dsl_form() 2 arg synopsis
375
376 my $dsl_form = $grammar->symbol_dsl_form( $symbol_id, 'L0' )
377 // '[No name in DSL form]';
378 $text .= "L0 symbol number: $symbol_id DSL form: $dsl_form\n";
379
380 =for Marpa::R2::Display::End
381
382 Given a symbol ID, returns the "DSL form" of the symbol.
383 This is the name of the symbol in a form similar
384 to the way it is specified by the user in the DSL.
385 If the symbol has an explicit name,
386 the symbol's DSL form is the same as its explicit name.
387 If the symbol does not have an explicit name,
388 the method may return a Perl C<undef>,
389 or it may return a DSL name invented by Marpa
390 and intended to be suggestive.
391 The DSL form of a symbol is not intended for use as a symbol name
392 -- it is not necessarily unique,
393 is not always defined,
394 and it is subject to change.
395
396 The first argument is the symbol ID.
397 A second, optional, argument is the subgrammar.
398 Currently there are L0 and G1 subgrammars.
399 The default subgrammar is G1.
400 Returns a Perl C<undef> if the symbol does not exist,
401 or if it has no DSL form.
402
403 =head2 symbol_ids()
404
405 =for Marpa::R2::Display
406 name: SLG symbol_ids() synopsis
407 normalize-whitespace: 1
408
409 do_something($_) for $grammar->symbol_ids();
410
411 =for Marpa::R2::Display::End
412
413 =for Marpa::R2::Display
414 name: SLG symbol_ids() 2 arg synopsis
415 normalize-whitespace: 1
416
417 do_something($_) for $grammar->symbol_ids('L0');
418
419 =for Marpa::R2::Display::End
420
421 Returns a list of the symbol ID's as an array.
422 Takes one, optional, argument: the name of a subgrammar.
423 Currently there are L0 and G1 subgrammars.
424 The default subgrammar is G1.
425
426 =head2 symbol_name()
427
428 =for Marpa::R2::Display
429 name: SLG symbol_name() synopsis
430
431 my $name = $grammar->symbol_name($symbol_id);
432 $text .= "symbol number: $symbol_id name: $name\n";
433
434 =for Marpa::R2::Display::End
435
436 =for Marpa::R2::Display
437 name: SLG symbol_name() 2 arg synopsis
438
439 my $name = $grammar->symbol_name( $symbol_id, 'L0' );
440 $text .= "L0 symbol number: $symbol_id name: $name\n";
441
442 =for Marpa::R2::Display::End
443
444 Given a symbol ID, returns the name of the symbol.
445 For every symbol ID, this method's return value will be defined
446 and will be unique to that symbol ID,
447 so that it is suitable for use as a symbol name.
448 If a symbol has an explicit name, the return value will be
449 the symbol's explicit name.
450 If there is no explicit name, it will be an internal name.
451 Internal names are subject to change.
452
453 The first argument is the symbol ID.
454 A second, optional, argument is the subgrammar.
455 Currently there are L0 and G1 subgrammars.
456 The default subgrammar is G1.
457 Returns a Perl C<undef> if the symbol does not exist.
458
459 =head1 Trace methods
460
461 =head2 show_rules()
462
463 =for Marpa::R2::Display
464 name: SLG show_rules() synopsis
465 partial: 1
466 normalize-whitespace: 1
467
468 my $show_rules_output = $grammar->show_rules();
469
470 =for Marpa::R2::Display::End
471
472 =for Marpa::R2::Display
473 name: SLG show_rules() synopsis with 2 args
474 partial: 1
475 normalize-whitespace: 1
476
477 $show_rules_output .= $grammar->show_rules(3, 'L0');
478
479 =for Marpa::R2::Display::End
480
481 The C<show_rules()> method returns a description of
482 the rules for a subgrammar, by default G1.
483 It is useful for understanding the rules as they
484 appear in trace and debugging outputs.
485 To allow for improvements in Marpa::R2,
486 the output of C<show_rules()> is subject to change.
487
488 The first optional argument can be a numeric verbosity level.
489 The default verbosity is 1, which is adequate for
490 most purposes.
491 A verbosity of 2 prints additional information useful
492 for those new to SLIF tracing and debugging.
493 A verbosity of 3 prints additional information for
494 experts.
495
496 The second, optional, argument is the name of a subgrammar.
497 Currently there are L0 and G1 subgrammars.
498
499 =head2 show_symbols()
500
501 =for Marpa::R2::Display
502 name: SLIF show_symbols() synopsis
503 partial: 1
504 normalize-whitespace: 1
505
506 $show_symbols_output .= $grammar->show_symbols(3);
507
508 =for Marpa::R2::Display::End
509
510 =for Marpa::R2::Display
511 name: SLIF show_symbols() synopsis
512 partial: 1
513 normalize-whitespace: 1
514
515 $show_symbols_output .= $grammar->show_symbols(3, 'L0');
516
517 =for Marpa::R2::Display::End
518
519 The C<show_symbols()> method returns a description of
520 the symbols for a subgrammar,
521 by default G1.
522 It is useful for understanding the symbols as they
523 appear in trace and debugging outputs.
524 To allow for improvements in Marpa::R2,
525 the output of C<show_symbols()> is subject to change.
526
527 The first argument can be a numeric verbosity level.
528 The default verbosity is 1, which is adequate for
529 most purposes.
530 A verbosity of 2 prints additional information useful
531 for those new to SLIF tracing and debugging.
532 A verbosity of 3 prints additional information for
533 experts.
534
535 The second, optional, argument is the name of a subgrammar.
536 Currently there are L0 and G1 subgrammars.
537
538 =head1 Discouraged methods
539
540 Discouraged methods are those that
541 continue to be supported, but whose use is discouraged for one
542 reason or another.
543
544 =head2 g0_rule()
545
546 =for Marpa::R2::Display
547 name: Scanless g0_rule() synopsis
548 normalize-whitespace: 1
549
550 my @g0_rule_ids = $grammar->g0_rule_ids();
551 for my $g0_rule_id (@g0_rule_ids) {
552 $g0_rules_description .= "$g0_rule_id "
553 . ( join q{ }, map {"<$_>"} $grammar->g0_rule($g0_rule_id) ) . "\n";
554 }
555
556 =for Marpa::R2::Display::End
557
558 Please prefer L<"rule_expand()">, together with
559 L<"symbol_name()"> or
560 L<"symbol_display_form()">.
561 Given a L0 rule ID as its argument,
562 returns an array containing the
563 names of the symbols of that rule.
564 The C<g0_rule()> method
565 returns a Perl false if no L0 rule with that rule ID exists.
566 If the L0 rule ID exists,
567 C<g0_rule()> returns a list of one or more symbol names.
568 The first symbol name will be that of
569 the rule's LHS symbol.
570 The rest of the list will be the names of the rule's
571 RHS symbols, in order.
572
573 =head2 g0_rule_ids()
574
575 =for Marpa::R2::Display
576 name: Scanless g0_rule() synopsis
577 normalize-whitespace: 1
578
579 my @g0_rule_ids = $grammar->g0_rule_ids();
580 for my $g0_rule_id (@g0_rule_ids) {
581 $g0_rules_description .= "$g0_rule_id "
582 . ( join q{ }, map {"<$_>"} $grammar->g0_rule($g0_rule_id) ) . "\n";
583 }
584
585 =for Marpa::R2::Display::End
586
587 Please prefer L<"rule_expand()">.
588 Returns a list of the L0 rule ID's.
589
590 =head2 g1_rule_ids()
591
592 =for Marpa::R2::Display
593 name: Scanless rule() synopsis
594 normalize-whitespace: 1
595
596 my @g1_rule_ids = $grammar->g1_rule_ids();
597 for my $g1_rule_id (@g1_rule_ids) {
598 $g1_rules_description .= "$g1_rule_id "
599 . ( join q{ }, map {"<$_>"} $grammar->rule($g1_rule_id) ) . "\n";
600 }
601
602 =for Marpa::R2::Display::End
603
604 Please prefer L<"rule_expand()">.
605 Returns a list of the G1 rule ID's.
606
607 =head2 rule()
608
609 =for Marpa::R2::Display
610 name: Scanless rule() synopsis
611
612 my @g1_rule_ids = $grammar->g1_rule_ids();
613 for my $g1_rule_id (@g1_rule_ids) {
614 $g1_rules_description .= "$g1_rule_id "
615 . ( join q{ }, map {"<$_>"} $grammar->rule($g1_rule_id) ) . "\n";
616 }
617
618 =for Marpa::R2::Display::End
619
620 Please prefer L<"rule_expand()">, together with
621 L<"symbol_name()"> or
622 L<"symbol_display_form()">.
623 Given a G1 rule ID as its argument,
624 returns an array containing the
625 names of the symbols of that rule.
626 The C<rule()> method
627 returns a Perl false if no G1 rule with that rule ID exists.
628 If the rule ID exists,
629 C<rule()> returns a list of one or more symbol names.
630 The first symbol name will be that of
631 the rule's LHS symbol.
632 The rest of the list will be the names of the rule's
633 RHS symbols, in order.
634 The SLIF's C<rule()> method is useful in
635 combination with
636 the SLIF's
637 of L<the progress method|Marpa::R2::Scanless::R/progress()>,
638 whose output identifies rules by rule ID.
639
640 =head1 Copyright and License
641
642 =for Marpa::R2::Display
643 ignore: 1
644
645 Copyright 2022 Jeffrey Kegler
646 This file is part of Marpa::R2. Marpa::R2 is free software: you can
647 redistribute it and/or modify it under the terms of the GNU Lesser
648 General Public License as published by the Free Software Foundation,
649 either version 3 of the License, or (at your option) any later version.
650
651 Marpa::R2 is distributed in the hope that it will be useful,
652 but WITHOUT ANY WARRANTY; without even the implied warranty of
653 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
654 Lesser General Public License for more details.
655
656 You should have received a copy of the GNU Lesser
657 General Public License along with Marpa::R2. If not, see
658 http://www.gnu.org/licenses/.
659
660 =for Marpa::R2::Display::End
661
662 =cut
663
664 # Local Variables:
665 # mode: cperl
666 # cperl-indent-level: 4
667 # fill-column: 100
668 # End:
669 # vim: expandtab shiftwidth=4:
670