Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add token/rule explaination to grammars.pod
  • Loading branch information
Mouq committed Jul 23, 2014
1 parent eb191dc commit 11557f7
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions lib/Language/grammars.pod
Expand Up @@ -16,8 +16,11 @@ written in less than 100 lines of simple, extensible code.
=head1 Named Regexes
The main ingredients of grammars are named L<regexes|/language/regexes>.
These have a special syntax, similar to subroutine definitions:
The main ingredient of grammars is named L<regexes|/language/regexes>. While
the syntax of L<Perl 6 Regexes|/language/regexes> is outside the scope of this
document, I<named> regexes have a special syntax, similar to subroutine
definitions:N<In fact, named regexes can even take extra arguments, using the
same syntax as subroutine parameter lists>
=for code :allow<B>
my B<regex number {> \d+ [ \. \d+ ]? B<}>
Expand All @@ -34,9 +37,29 @@ regex elsewhere:
say "15 + 4.5" ~~ /B<< <number> >>\s* '+' \s*B<< <number> >>/
=end code
TODO signatures
B<C<regex>> isn't the only declarator for named regexes -- in fact, it's the
least common. Most of the time, the B<C<token>> or B<C<rule>> declarators are
used. These are both I<ratcheting>, which means that the match engine won't
back up and try again if it fails to match something. This will usually do what you want, but isn't appropriate for all cases:
TODO tokens & rules
=begin code :allow<B>
my regex works-but-slow { .+ q }
my token fails-but-fast { .+ q }
my $s = 'Tokens won't backtrack, which makes them fail quicker!';
say so $s ~~ &works-but-slow; # True
say so $s ~~ &fails-but-fast; # False, the entire string get taken by the .+
=end code
The only difference between the C<token> and C<rule> declarators is that the
C<rule> declarator causes L<C<:sigspace>|/language/regexes#sigspace> to go into
effect for the Regex:
=begin code :allow<B>
my token non-space-y { once upon a time }
my rule space-y { once upon a time }
say 'onceuponatime' ~~ &non-space-y;
say 'once upon a time' ~~ &space-y;
=end code
=head1 class Grammar
Expand Down

0 comments on commit 11557f7

Please sign in to comment.