Skip to content

Commit f3dec65

Browse files
committed
Adds info on $/ to trammars, closes #2210
1 parent b02a982 commit f3dec65

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

doc/Language/grammars.pod6

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,20 @@ they are declared with the C<grammar> keyword instead of C<class>. Grammars
9090
should only be used to parse text; if you wish to extract complex data, you can
9191
add actions within the grammar, or an
9292
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.
9495
9596
9697
=head2 X«Proto regexes| :sym<>; proto regex; declarator,grammar»
9798
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>.
101102
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:
106107
107108
grammar Calculator {
108109
token TOP { [ <add> | <sub> ] }
@@ -150,10 +151,10 @@ defining a rule prototype with C<proto rule calc-op>. Each of our previous
150151
alternations have been replaced by a new C<rule calc-op> definition and the
151152
name of the alternation is attached with C«:sym<>» adverb.
152153
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>».
157158
158159
The real beauty of this method can be seen when you subclass that grammar
159160
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:
238239
239240
=head3 X«C<sym>|<sym>»
240241
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:
243244
244245
grammar Foo {
245246
token TOP { <letter>+ }
@@ -253,8 +254,9 @@ adverb for that particular regex:
253254
method TOP($/) { make $<letter>.grep(*.<sym>).join }
254255
}).made.say; # OUTPUT: «Perl␤»
255256
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.
258260
259261
=head3 X«"Always succeed" assertion|<?>»
260262

0 commit comments

Comments
 (0)