Skip to content

Commit 0716b91

Browse files
authored
Just some typo and consistency fixes
1 parent 6329a01 commit 0716b91

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

doc/Language/grammar_tutorial.pod6

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ perfectly.
1313
1414
=SUBTITLE When Would I Use Grammars?
1515
16-
Grammars are great at taking stings, trying to make sense of them, and then saving them into a data structure you can actually work with. If you have strings of some kind that you need to bring order to, or interpret, grammars give you some great tools to make it easier.
16+
Grammars are great at taking strings, trying to make sense of them, and then saving them into a data structure you can actually work with. If you have strings of some kind that you need to bring order to, or interpret, grammars give you some great tools to make it easier.
1717
1818
Your string might be a whole file that you need to break into sections. Or
1919
perhaps line by line. Maybe you have a protocol like SMTP you're working with,
@@ -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 :skip-test
42-
grammar My::Gram { ..methods 'n stuff... }
41+
=begin code
42+
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 :skip-test
49+
=begin code
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 :skip-test
113+
=begin code
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 )
@@ -140,7 +140,7 @@ The REST grammar so far will handle retrieves, deletes and updates ok. However,
140140
# 「product」「create」
141141
=end code
142142
143-
Let's imagine, for the sake of demonstration, that we might want to allow these same URI's to be entered in by a user from the terminal. In that case, they might put spaces between the '/'s, since users are prone to break things. If we wanted to accommodate this possibility, we could replace the '/'s in TOP with another token that allowed for spaces on either side of it.
143+
Let's imagine, for the sake of demonstration, that we might want to allow these same URIs to be entered in by a user from the terminal. In that case, they might put spaces between the '/'s, since users are prone to break things. If we wanted to accommodate this possibility, we could replace the '/'s in TOP with another token that allowed for spaces on either side of it.
144144
145145
=begin code
146146
grammar REST {
@@ -155,13 +155,14 @@ 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-
# 「/ product / update /7 /notify」
159-
# slash => 「/ 」
160-
# subject => 「product」
161-
# slash => 「 / 」
162-
# command => 「update」
163-
# slash => 「 /」
164-
# data => 「7 /notify」
158+
###
159+
「/ product / update /7 /notify」
160+
slash => 「/ 」
161+
subject => 「product」
162+
slash => 「 / 」
163+
command => 「update」
164+
slash => 「 /」
165+
data => 「7 /notify」
165166
=end code
166167
167168
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.
@@ -172,10 +173,10 @@ We want our RESTful grammar to allow for CRUD operations only. Anything else we
172173
173174
There are several ways to accomplish this. For example, you could change the command method:
174175
175-
=begin code :skip-test
176+
=begin code
176177
token command { \w+ }
177178
178-
# …becomes
179+
..becomes..
179180
180181
token command { 'create'|'retrieve'|'update'|'delete' }
181182
=end code
@@ -184,7 +185,7 @@ This results in any URI coming in getting checked; where the second string betwe
184185
185186
There is another way, though, that can give you far greater flexibility to do interesting or unholy things with your regexes, and provide some better readability when options grow large. These are proto-regexes.
186187
187-
To utilize these multimethods (here called protoregexes) to constrain our command to the same values we had above, we'll replace "token command" with the following:
188+
To utilize these multi-methods (here called protoregexes) to constrain our command to the same values we had above, we'll replace "token command" with the following:
188189
189190
=begin code
190191
proto token command {*}
@@ -217,9 +218,9 @@ This is what we've come up for processing our RESTful URIs so far:
217218
}
218219
=end code
219220
220-
Let's look at various URI's and how they behave being passed through our grammar.
221+
Let's look at various URIs and how they behave being passed through our grammar.
221222
222-
=begin code :skip-test
223+
=begin code
223224
my @uris = ['/product/update/7/notify',
224225
'/product/create',
225226
'/item/delete/4'];
@@ -229,14 +230,15 @@ Let's look at various URI's and how they behave being passed through our grammar
229230
say "Sub: $m<subject> Cmd: $m<command> Dat: $m<data>";
230231
}
231232
232-
# Sub: product Cmd: update Dat: 7/notify
233-
# Sub: product Cmd: create Dat:
234-
# Sub: item Cmd: delete Dat: 4
233+
###
234+
Sub: product Cmd: update Dat: 7/notify
235+
Sub: product Cmd: create Dat:
236+
Sub: item Cmd: delete Dat: 4
235237
=end code
236238
237-
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.
239+
So with just this part of a grammar, we're getting almost everything we need. Our URIs get efficiently parsed and we're given a nice little data structure for the variables we need to work with.
238240
239-
But look at that first line returned -- the <em>data</em> token is returning the entire end of the URI as just one string. We need to be able to work with that 7 there. And that 4! Well, the 4 is easy... But the 7 had the extra /notify on the end, to signal the system to notify someone that a product was updated (perhaps).
241+
But look at that first line returned -- the I<data> token is returning the entire end of the URI as just one string. We need to be able to work with that 7 there. And that 4! Well, the 4 is easy... But the 7 had the extra /notify on the end, to signal the system to notify someone that a product was updated (perhaps).
240242
241243
So let's make sure we can do stuff with our regex tokens that were matched, such as that I<data> token that returned a "7/notify". And to do so, we'll take advantage of another characteristic of these Grammar classes -- a thing called actions.
242244
@@ -248,10 +250,10 @@ You can think of grammar actions as a kind of plug-in expansion module for gramm
248250
249251
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
250252
251-
=begin code :skip-test
253+
=begin code
252254
my $matchObj = REST.parse($uri, actions => REST-actions.new);
253255
254-
# or if you prefer
256+
...or if you prefer...
255257
256258
my $matchObj = REST.parse($uri, :actions(REST-actions.new));
257259
=end code
@@ -302,7 +304,7 @@ But not really.
302304
303305
=head2 Keeping grammars with actions tidy with "make" and "made"
304306
305-
If our grammar calls our action above on data, the data method will be called, but nothing will show up in the big TOP grammar match result returned to our program. In order to make our action results show up, we need to call "make" on that result, and that result can be many things, including strings, array or hash structures.
307+
If our grammar calls our action above on data, the "data" method will be called, but nothing will show up in the big TOP grammar match result returned to our program. In order to make our action results show up, we need to call "make" on that result, and that result can be many things, including strings, array or hash structures.
306308
307309
You can imagine that the "make" we put on our action results, places that result in a special, contained area in our whole grammar. Everything that we "make" for data structures, can be accessed later by "made".
308310
@@ -319,7 +321,7 @@ When we add "make" to our match split (which returns a list), our action will re
319321
320322
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":
321323
322-
=begin code :skip-test
324+
=begin code
323325
my $uri = '/product/update/7/notify';
324326
325327
my $match = REST.parse($uri, actions => REST-actions.new);
@@ -353,7 +355,7 @@ But what we want to be certain to do, is to use the "made" method on our $<data>
353355
354356
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
355357
356-
=begin code :skip-test
358+
=begin code
357359
my $uri = '/product/update/7/notify';
358360
359361
my $match = REST.parse($uri, actions => REST-actions.new);
@@ -366,7 +368,7 @@ After we "make" something in the TOP method of a grammar action, we can then acc
366368
367369
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.
368370
369-
=begin code :skip-test
371+
=begin code
370372
my $uri = '/product/update/7/notify';
371373
372374
my $rest = REST.parse($uri, actions => REST-actions.new).made;
@@ -394,7 +396,7 @@ Oh, did we forget to get rid of that ugly array element number? Hmm. Let's make
394396
395397
Now we can do this instead
396398
397-
=begin code :skip-test
399+
=begin code
398400
my $uri = '/product/update/7/notify';
399401
400402
my $rest = REST.parse($uri, actions => REST-actions.new).made;

0 commit comments

Comments
 (0)