@@ -90,19 +90,20 @@ they are declared with the C<grammar> keyword instead of C<class>. Grammars
90
90
should only be used to parse text; if you wish to extract complex data, you can
91
91
add actions within the grammar, or an
92
92
L < action object|/language/grammars#Action_Objects > is recommended to be used in
93
- conjunction with the grammar.
93
+ conjunction with the grammar. If action objects are not used, C < .parse > returns a L < Match > object and sets, by default, the
94
+ L < default match object C < $/ > |/syntax/$$SOLIDUS > , to the same value.
94
95
95
96
96
97
= head2 X « Proto regexes| :sym<>; proto regex; declarator,grammar »
97
98
98
- L < Grammar|/type/Grammar > s are composed of rules, tokens and regexes;
99
- they are actually methods, since grammars are classes. These methods
100
- can share a name and functionality in common, and thus can use L < proto|/syntax/proto > .
99
+ L < Grammar|/type/Grammar > s are composed of rules, tokens and regexes; these are
100
+ actually methods, since grammars are classes. These methods can share a name and
101
+ functionality in common, and thus can use L < proto|/syntax/proto > .
101
102
102
- For instance, if you have a lot of alternations, it may become difficult to produce
103
- readable code or subclass your grammar. In the Actions class below, the
104
- ternary in C < method TOP > is less than ideal and it becomes even worse the more
105
- operations we add:
103
+ For instance, if you have a lot of alternations, it may become difficult to
104
+ produce readable code or subclass your grammar. In the C < Actions > class below,
105
+ the ternary in C < method TOP > is less than ideal and it becomes even worse the
106
+ more operations we add:
106
107
107
108
grammar Calculator {
108
109
token TOP { [ <add> | <sub> ] }
@@ -150,10 +151,10 @@ defining a rule prototype with C<proto rule calc-op>. Each of our previous
150
151
alternations have been replaced by a new C < rule calc-op > definition and the
151
152
name of the alternation is attached with C « :sym<> » adverb.
152
153
153
- In the actions class, we now got rid of the ternary operator and simply take
154
- the C < .made > value from the C « $<calc-op> » match object. And the actions for
155
- individual alternations now follow the same naming pattern as in the grammar:
156
- C « method calc-op:sym<add> » and C « method calc-op:sym<sub> » .
154
+ In the class that declares actions , we now got rid of the ternary operator and
155
+ simply take the C < .made > value from the C « $<calc-op> » match object. And the
156
+ actions for individual alternations now follow the same naming pattern as in the
157
+ grammar: C « method calc-op:sym<add> » and C « method calc-op:sym<sub> » .
157
158
158
159
The real beauty of this method can be seen when you subclass that grammar
159
160
and actions class. Let's say we want to add a multiplication feature to the
@@ -238,8 +239,8 @@ You can also redefine the default C<ws> token:
238
239
239
240
= head3 X « C < sym > |<sym>»
240
241
241
- The C « <sym> » token can be used inside proto regexes to match the string value of the C < :sym >
242
- adverb for that particular regex:
242
+ The C « <sym> » token can be used inside proto regexes to match the string value of
243
+ the C < :sym > adverb for that particular regex:
243
244
244
245
grammar Foo {
245
246
token TOP { <letter>+ }
@@ -253,8 +254,9 @@ adverb for that particular regex:
253
254
method TOP($/) { make $<letter>.grep(*.<sym>).join }
254
255
}).made.say; # OUTPUT: «Perl»
255
256
256
- This comes in handy when you're already differentiating the proto regexes with the strings
257
- you're going to match, as using C « <sym> » token prevents repetition of those strings.
257
+ This comes in handy when you're already differentiating the proto regexes with
258
+ the strings you're going to match, as using C « <sym> » token prevents repetition
259
+ of those strings.
258
260
259
261
= head3 X « "Always succeed" assertion|<?> »
260
262
0 commit comments