Skip to content

Commit e84a5ea

Browse files
authored
Update grammar_tutorial.pod6
1 parent 97da18d commit e84a5ea

File tree

1 file changed

+75
-73
lines changed

1 file changed

+75
-73
lines changed

doc/Language/grammar_tutorial.pod6

Lines changed: 75 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ between '/'s must be one of those CRUD values, otherwise the parsing fails.
248248
Exactly what we want.
249249
250250
There's another technique that provides greater flexibility and improved
251-
readability when options grow large: proto-regexes.
251+
readability when options grow large: I<proto-regexes>.
252252
253-
To utilize these proto-regexes (multimethods, in fact) to limit ourselves to the
254-
valid CRUD options, we'll replace "token command" with the following:
253+
To utilize these proto-regexes (multimethods, in fact) to limit ourselves to
254+
the valid CRUD options, we'll replace C<token command> with the following:
255255
256256
=begin code
257257
proto token command {*}
@@ -261,26 +261,27 @@ valid CRUD options, we'll replace "token command" with the following:
261261
token command:sym<delete> { <sym> }
262262
=end code
263263
264-
The 'sym' keyword is used to create the various proto-regex options. Each option
265-
is named (e.g., sym<update>), and for that option's use, a special <sym> token is
266-
auto-generated with the same name.
267-
268-
The <sym> token, as well as other user-defined tokens, may be used in the proto-
269-
regex option block to define the specific 'match condition'. Regex tokens are
270-
compiled forms and, once defined, cannot subsequently be modified by adverb
271-
actions (e.g., :i). Therefore, as it's auto-generated, the special <sym> token
272-
is useful only where an exact match of the option name is required.
273-
274-
If, for one of the proto-regex options, a match condition occurs, then the whole
275-
proto's search terminates. The matching data, in the form of a match object, is
276-
assigned to the parent proto token. If the special <sym> token was employed and
277-
formed all or part of the actual match, then it's preserved as a sub-level in
278-
the match object, otherwise it's absent.
279-
280-
Using proto-regexes like this gives us a lot of flexibility. For example,
281-
instead of returning <sym>, which in this case is the entire string that was
264+
The C<sym> keyword is used to create the various proto-regex options.
265+
Each option is named (e.g., C«sym<update>»), and for that option's use,
266+
a special C«<sym>» token is auto-generated with the same name.
267+
268+
The C«<sym>» token, as well as other user-defined tokens, may be used in the
269+
proto-regex option block to define the specific I<match condition>.
270+
Regex tokens are compiled forms and, once defined, cannot subsequently be
271+
modified by adverb actions (e.g., C<:i>). Therefore, as it's auto-generated,
272+
the special C«<sym>» token is useful only where an exact match of the option
273+
name is required.
274+
275+
If, for one of the proto-regex options, a match condition occurs, then
276+
the whole proto's search terminates. The matching data, in the form of a match
277+
object, is assigned to the parent proto token. If the special C«<sym>» token
278+
was employed and formed all or part of the actual match, then it's preserved as
279+
a sub-level in the match object, otherwise it's absent.
280+
281+
Using proto-regex like this gives us a lot of flexibility. For example,
282+
instead of returning C«<sym>», which in this case is the entire string that was
282283
matched, we could instead enter our own string, or do other funny stuff. We
283-
could do the same with the "token subject" method and limit it also to only
284+
could do the same with the C<token subject> method and limit it also to only
284285
parsing correctly on valid subjects (like 'part' or 'people', etc.).
285286
286287
=head2 Putting our RESTful grammar together
@@ -306,7 +307,7 @@ This is what we have for processing our RESTful URIs, so far:
306307
307308
Let's look at various URIs and see how they work with our grammar.
308309
309-
=begin code :skip-test
310+
=begin code :preamble<grammar REST{};>
310311
my @uris = ['/product/update/7/notify',
311312
'/product/create',
312313
'/item/delete/4'];
@@ -317,16 +318,19 @@ Let's look at various URIs and see how they work with our grammar.
317318
}
318319
319320
# OUTPUT: «Sub: product Cmd: update Dat: 7/notify␤
320-
# Sub: product Cmd: create Dat:
321-
# Sub: item Cmd: delete Dat: 4»
321+
# Sub: product Cmd: create Dat:
322+
# Sub: item Cmd: delete Dat: 4»
322323
=end code
323324
325+
Note that since C«<data>» matches nothing on the second string, C«$m<data>»
326+
will be C<Nil>, then using it in string context in the C<say> function warns.
327+
324328
With just this part of a grammar, we're getting almost everything we're
325329
looking for. The URIs get parsed and we get a data structure with the data.
326330
327331
The I<data> token returns the entire end of the URI as one string. The 4 is
328-
fine. However from the '7/notify', we only want the 7. To get just the 7, we'll use
329-
another feature of grammar classes: actions.
332+
fine. However from the '7/notify', we only want the 7. To get just the 7,
333+
we'll use another feature of grammar classes: I<actions>.
330334
331335
=head1 Grammar Actions
332336
@@ -338,10 +342,9 @@ grammars. A lot of the time you'll be happy using grammars all by their own.
338342
But when you need to further process some of those strings, you can plug in the
339343
Actions expansion module.
340344
341-
To work with actions, you use a named parameter called
342-
"actions" which should contain an instance of your actions class. With the
343-
code above, if our actions class called REST-actions, we would parse the
344-
URI string like this:
345+
To work with actions, you use a named parameter called C<actions> which
346+
should contain an instance of your actions class. With the code above, if our
347+
actions class called REST-actions, we would parse the URI string like this:
345348
346349
=begin code :skip-test
347350
my $matchObject = REST.parse($uri, actions => REST-actions.new);
@@ -353,12 +356,12 @@ URI string like this:
353356
354357
If you I<name your action methods with the same name as your grammar methods>
355358
(tokens, regexes, rules), then when your grammar methods match, your action
356-
method with the same name will get called automatically. The method will also be
357-
passed the corresponding match object (represented by the $/ variable).
359+
method with the same name will get called automatically. The method will also
360+
be passed the corresponding match object (represented by the C<$/> variable).
358361
359362
Let's turn to an example.
360363
361-
=head1 Grammars by Example with Actions
364+
=head2 Grammars by example with actions
362365
363366
Here we are back to our grammar.
364367
@@ -380,9 +383,9 @@ Here we are back to our grammar.
380383
=end code
381384
382385
Recall that we want to further process the data token "7/notify", to get the 7.
383-
To do this, we'll create an action class that has a method with the same name as
384-
the named token. In this case, our token is named "data" so our method is also
385-
named "data".
386+
To do this, we'll create an action class that has a method with the same name
387+
as the named token. In this case, our token is named C<data> so our method
388+
is also named C<data>.
386389
387390
=begin code
388391
class REST-actions
@@ -391,25 +394,25 @@ named "data".
391394
}
392395
=end code
393396
394-
Now when we pass the URI string through the grammar, the "data" token match
395-
will be passed to the REST-actions' "data" method. The action method will split
396-
the string by the '/' character and the first element of the returned
397+
Now when we pass the URI string through the grammar, the I<data token match>
398+
will be passed to the I<REST-actions' data method>. The action method will
399+
split the string by the '/' character and the first element of the returned
397400
list will be the ID number (7 in the case of "7/notify").
398401
399402
But not really; there's a little more.
400403
401-
=head2 Keeping grammars with actions tidy with "make" and "made"
404+
=head2 Keeping grammars with actions tidy with C<make> and C<made>
402405
403406
If the grammar calls the action above on data, the data method will be called,
404-
but nothing will show up in the big TOP grammar match result returned to our
405-
program. In order to make the action results show up, we need to call "make" on
406-
that result. The result can be many things, including strings, array or
407+
but nothing will show up in the big C<TOP> grammar match result returned to our
408+
program. In order to make the action results show up, we need to call L<make>
409+
on that result. The result can be many things, including strings, array or
407410
hash structures.
408411
409-
You can imagine that the "make" places the result in a special contained area
410-
for a grammar. Everything that we "make" can be accessed later by "made".
412+
You can imagine that the C<make> places the result in a special contained area
413+
for a grammar. Everything that we C<make> can be accessed later by L<made>.
411414
412-
So instead of the REST-actions class above, we should write
415+
So instead of the REST-actions class above, we should write:
413416
414417
=begin code
415418
class REST-actions
@@ -418,13 +421,13 @@ So instead of the REST-actions class above, we should write
418421
}
419422
=end code
420423
421-
When we add "make" to the match split (which returns a list), the action will
424+
When we add C<make> to the match split (which returns a list), the action will
422425
return a data structure to the grammar that will be stored separately from
423-
the "data" token of the original grammar. This way, we can work with both
426+
the C<data> token of the original grammar. This way, we can work with both
424427
if we need to.
425428
426429
If we want to access just the ID of 7 from that long URI, we access the first
427-
element of the list returned from the "data" action that we "made" with "make":
430+
element of the list returned from the C<data> action that we C<made>:
428431
429432
=begin code :skip-test
430433
my $uri = '/product/update/7/notify';
@@ -435,21 +438,20 @@ element of the list returned from the "data" action that we "made" with "make":
435438
say $match<command>.Str; # OUTPUT: «update␤»
436439
=end code
437440
438-
Here we call "made" on data, because we want the result of the action that
439-
we "made" (with "make") to get the split array. That's lovely! But, wouldn't
440-
it be lovelier if we could "make" a friendlier data structure that contained
441+
Here we call C<made> on data, because we want the result of the action that
442+
we C<made> (with C<make>) to get the split array. That's lovely! But, wouldn't
443+
it be lovelier if we could C<make> a friendlier data structure that contained
441444
all of the stuff we want, rather than having to coerce types and remember
442445
arrays?
443446
444-
Just like TOP, which over-arches and matches the entire
445-
string, actions have a TOP method as well. We can "make" all of the
446-
individual match components, like "data" or "subject" or "command", and then we
447-
can place them in a data structure that we will "make" in
448-
TOP. When we return the final match object, we can
449-
then access this data structure.
447+
Just like C<TOP>, which overarches and matches the entire string, actions
448+
have a TOP method as well. We can C<make> all of the individual match
449+
components, like C<data> or C<subject> or C<command>, and then we can
450+
place them in a data structure that we will C<make> in TOP. When we return
451+
the final match object, we can then access this data structure.
450452
451-
To do this, we add the method TOP to the action class and "make" whatever data
452-
structure we like from the component pieces.
453+
To do this, we add the method C<TOP> to the action class and C<make>
454+
whatever data structure we like from the component pieces.
453455
454456
So, our action class becomes:
455457
@@ -466,17 +468,17 @@ So, our action class becomes:
466468
}
467469
=end code
468470
469-
Here in the TOP method, the "subject" remains the same as the subject we
470-
matched in the grammar. Also, "command" returns the valid <sym> that was
471-
matched (create, update, retrieve, or delete). We coerce each into .Str, as
471+
Here in the C<TOP> method, the C<subject> remains the same as the subject we
472+
matched in the grammar. Also, C<command> returns the valid C«<sym>» that was
473+
matched (create, update, retrieve, or delete). We coerce each into C<.Str>, as
472474
well, since we don't need the full match object.
473475
474-
We want to make sure to use the "made" method on the
475-
$<data> object, since we want to access the split one that we "made" with
476-
"make" in our action, rather than the proper $<data> object.
476+
We want to make sure to use the C<made> method on the C«$<data>» object,
477+
since we want to access the split one that we C<made> with C<make> in our
478+
action, rather than the proper C«$<data>» object.
477479
478-
After we "make" something in the TOP method of a grammar action, we can then
479-
access all the custom values by calling the "made" method on the grammar
480+
After we C<make> something in the C<TOP> method of a grammar action, we can then
481+
access all the custom values by calling the C<made> method on the grammar
480482
result object. The code now becomes
481483
482484
=begin code :skip-test
@@ -491,7 +493,7 @@ result object. The code now becomes
491493
=end code
492494
493495
If the complete return match object is not needed, you could return only the made
494-
data from your action's TOP.
496+
data from your action's C<TOP>.
495497
496498
=begin code :skip-test
497499
my $uri = '/product/update/7/notify';
@@ -504,8 +506,8 @@ data from your action's TOP.
504506
=end code
505507
506508
Oh, did we forget to get rid of that ugly array element number? Hmm. Let's make
507-
something new in the grammar's custom return in TOP... how about we call it
508-
"subject-id" and have it set to element 0 of <data>.
509+
something new in the grammar's custom return in C<TOP>... how about we call it
510+
C<subject-id> and have it set to element 0 of C«<data>».
509511
510512
=begin code
511513
class REST-actions
@@ -521,7 +523,7 @@ something new in the grammar's custom return in TOP... how about we call it
521523
}
522524
=end code
523525
524-
Now we can do this instead
526+
Now we can do this instead:
525527
526528
=begin code :skip-test
527529
my $uri = '/product/update/7/notify';

0 commit comments

Comments
 (0)