Skip to content

Commit

Permalink
Adds info on $/ to trammars, closes #2210
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ committed Aug 9, 2018
1 parent b02a982 commit f3dec65
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions doc/Language/grammars.pod6
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,20 @@ they are declared with the C<grammar> keyword instead of C<class>. Grammars
should only be used to parse text; if you wish to extract complex data, you can
add actions within the grammar, or an
L<action object|/language/grammars#Action_Objects> is recommended to be used in
conjunction with the grammar.
conjunction with the grammar. If action objects are not used, C<.parse> returns a L<Match> object and sets, by default, the
L<default match object C<$/>|/syntax/$$SOLIDUS>, to the same value.
=head2 X«Proto regexes| :sym<>; proto regex; declarator,grammar»
L<Grammar|/type/Grammar>s are composed of rules, tokens and regexes;
they are actually methods, since grammars are classes. These methods
can share a name and functionality in common, and thus can use L<proto|/syntax/proto>.
L<Grammar|/type/Grammar>s are composed of rules, tokens and regexes; these are
actually methods, since grammars are classes. These methods can share a name and
functionality in common, and thus can use L<proto|/syntax/proto>.
For instance, if you have a lot of alternations, it may become difficult to produce
readable code or subclass your grammar. In the Actions class below, the
ternary in C<method TOP> is less than ideal and it becomes even worse the more
operations we add:
For instance, if you have a lot of alternations, it may become difficult to
produce readable code or subclass your grammar. In the C<Actions> class below,
the ternary in C<method TOP> is less than ideal and it becomes even worse the
more operations we add:
grammar Calculator {
token TOP { [ <add> | <sub> ] }
Expand Down Expand Up @@ -150,10 +151,10 @@ defining a rule prototype with C<proto rule calc-op>. Each of our previous
alternations have been replaced by a new C<rule calc-op> definition and the
name of the alternation is attached with C«:sym<>» adverb.
In the actions class, we now got rid of the ternary operator and simply take
the C<.made> value from the C«$<calc-op>» match object. And the actions for
individual alternations now follow the same naming pattern as in the grammar:
C«method calc-op:sym<add>» and C«method calc-op:sym<sub>».
In the class that declares actions, we now got rid of the ternary operator and
simply take the C<.made> value from the C«$<calc-op>» match object. And the
actions for individual alternations now follow the same naming pattern as in the
grammar: C«method calc-op:sym<add>» and C«method calc-op:sym<sub>».
The real beauty of this method can be seen when you subclass that grammar
and actions class. Let's say we want to add a multiplication feature to the
Expand Down Expand Up @@ -238,8 +239,8 @@ You can also redefine the default C<ws> token:
=head3 X«C<sym>|<sym>»
The C«<sym>» token can be used inside proto regexes to match the string value of the C<:sym>
adverb for that particular regex:
The C«<sym>» token can be used inside proto regexes to match the string value of
the C<:sym> adverb for that particular regex:
grammar Foo {
token TOP { <letter>+ }
Expand All @@ -253,8 +254,9 @@ adverb for that particular regex:
method TOP($/) { make $<letter>.grep(*.<sym>).join }
}).made.say; # OUTPUT: «Perl␤»
This comes in handy when you're already differentiating the proto regexes with the strings
you're going to match, as using C«<sym>» token prevents repetition of those strings.
This comes in handy when you're already differentiating the proto regexes with
the strings you're going to match, as using C«<sym>» token prevents repetition
of those strings.
=head3 X«"Always succeed" assertion|<?>»
Expand Down

0 comments on commit f3dec65

Please sign in to comment.