Skip to content

Commit 6329a01

Browse files
committed
make examples compile
1 parent 9479b4a commit 6329a01

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

doc/Language/grammar_tutorial.pod6

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ For example, in the case of HTML, you could define a grammar that would recogniz
3838
3939
Grammars are defined as an object, just like everything else in Perl. Technically, they are normal classes with a little extra magic thrown in, which we'll get to later -- and a few limitations. You name and define a grammar exactly as you would a class, except using the "grammar" keyword instead of "class".
4040
41-
=begin code
41+
=begin code :skip-test
4242
grammar My::Gram { ..methods 'n stuff... }
4343
=end code
4444
4545
Grammars contain elements like methods, called I<regex, token or rule>. These elements are named, just like methods are named. And each one defines a regex, token or rule (which are mostly the same thing (not really) but we'll get to that later).
4646
4747
Once you have your grammar defined, in your program call it by name and pass in the string you want to parse, and that string will be run through the rules you defined based upon your regex, token and rule "methods". When done, you get back a I<Match> object that has been populated with data structured and stored by the names you used to define your methods.
4848
49-
=begin code
49+
=begin code :skip-test
5050
my $matchObject = My::Gram.parse($what-a-big-string-you-have);
5151
=end code
5252
@@ -110,14 +110,14 @@ But in our big string we get, we don't know what order these regex matches will
110110
111111
You could actually use this to extract your data from the URI for basic CRUD that has all 3 parameters included:
112112
113-
=begin code
113+
=begin code :skip-test
114114
my $match = REST.parse('/product/update/7/notify');
115115
say $match;
116116
117-
「/product/update/7/notify」
118-
subject => 「product」
119-
command => 「update」
120-
data => 「7/notify」
117+
# 「/product/update/7/notify」
118+
# subject => 「product」
119+
# command => 「update」
120+
# data => 「7/notify」
121121
=end code
122122
123123
Of course, the data can be accessed directly by using $match<subject> or $match<command> or $match<data> to return the values parsed. They each contain match objects you can work further with, or coerce into a string ( $match<command>.Str )
@@ -155,14 +155,13 @@ Let's imagine, for the sake of demonstration, that we might want to allow these
155155
my $m = REST.parse('/ product / update /7 /notify');
156156
say $m;
157157
158-
###
159-
「/ product / update /7 /notify」
160-
slash => 「/ 」
161-
subject => 「product」
162-
slash => 「 / 」
163-
command => 「update」
164-
slash => 「 /」
165-
data => 「7 /notify」
158+
# 「/ product / update /7 /notify」
159+
# slash => 「/ 」
160+
# subject => 「product」
161+
# slash => 「 / 」
162+
# command => 「update」
163+
# slash => 「 /」
164+
# data => 「7 /notify」
166165
=end code
167166
168167
We're getting some extra junk in our match object now, with those slashes, but there are some very nice ways to make a tidy return value that we'll get to.
@@ -173,10 +172,10 @@ We want our RESTful grammar to allow for CRUD operations only. Anything else we
173172
174173
There are several ways to accomplish this. For example, you could change the command method:
175174
176-
=begin code
175+
=begin code :skip-test
177176
token command { \w+ }
178177
179-
..becomes..
178+
# …becomes
180179
181180
token command { 'create'|'retrieve'|'update'|'delete' }
182181
=end code
@@ -220,7 +219,7 @@ This is what we've come up for processing our RESTful URIs so far:
220219
221220
Let's look at various URI's and how they behave being passed through our grammar.
222221
223-
=begin code
222+
=begin code :skip-test
224223
my @uris = ['/product/update/7/notify',
225224
'/product/create',
226225
'/item/delete/4'];
@@ -230,10 +229,9 @@ Let's look at various URI's and how they behave being passed through our grammar
230229
say "Sub: $m<subject> Cmd: $m<command> Dat: $m<data>";
231230
}
232231
233-
###
234-
Sub: product Cmd: update Dat: 7/notify
235-
Sub: product Cmd: create Dat:
236-
Sub: item Cmd: delete Dat: 4
232+
# Sub: product Cmd: update Dat: 7/notify
233+
# Sub: product Cmd: create Dat:
234+
# Sub: item Cmd: delete Dat: 4
237235
=end code
238236
239237
So with just this part of a grammar, we're getting almost everything we need. Our URI's get efficiently parsed and we're given a nice little data structure for the variables we need to work with.
@@ -250,10 +248,10 @@ You can think of grammar actions as a kind of plug-in expansion module for gramm
250248
251249
You do this when you first create your grammar. In addition to passing in the actual string you want to parse, you can pass in a named parameter called "actions" which should contain an instance of your actions class. From our example above, if our actions class were called REST-actions we would parse our grammar string like this
252250
253-
=begin code
251+
=begin code :skip-test
254252
my $matchObj = REST.parse($uri, actions => REST-actions.new);
255253
256-
...or if you prefer...
254+
# or if you prefer
257255
258256
my $matchObj = REST.parse($uri, :actions(REST-actions.new));
259257
=end code
@@ -321,7 +319,7 @@ When we add "make" to our match split (which returns a list), our action will re
321319
322320
Now if we want to access just our id of 7 from that long URL, we can access the first element of the list returned from the "data" action we "made" with "make":
323321
324-
=begin code
322+
=begin code :skip-test
325323
my $uri = '/product/update/7/notify';
326324
327325
my $match = REST.parse($uri, actions => REST-actions.new);
@@ -355,7 +353,7 @@ But what we want to be certain to do, is to use the "made" method on our $<data>
355353
356354
After we "make" something in the TOP method of a grammar action, we can then access all of our custom-made stuff by calling the "made" method on our grammar result object. From our earlier example, it now becomes
357355
358-
=begin code
356+
=begin code :skip-test
359357
my $uri = '/product/update/7/notify';
360358
361359
my $match = REST.parse($uri, actions => REST-actions.new);
@@ -368,7 +366,7 @@ After we "make" something in the TOP method of a grammar action, we can then acc
368366
369367
Of course you could also shorten it if you knew you wouldn't need the full grammar return match object, and only wanted your custom-made data from your action's TOP.
370368
371-
=begin code
369+
=begin code :skip-test
372370
my $uri = '/product/update/7/notify';
373371
374372
my $rest = REST.parse($uri, actions => REST-actions.new).made;
@@ -396,7 +394,7 @@ Oh, did we forget to get rid of that ugly array element number? Hmm. Let's make
396394
397395
Now we can do this instead
398396
399-
=begin code
397+
=begin code :skip-test
400398
my $uri = '/product/update/7/notify';
401399
402400
my $rest = REST.parse($uri, actions => REST-actions.new).made;

0 commit comments

Comments
 (0)