Skip to content

Commit 9fc3426

Browse files
committed
Add a section about named regexes
1 parent 8c63949 commit 9fc3426

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

lib/Language/grammars.pod

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,38 @@ L<JSON::Simple module|https://github.com/moritz/json>, which can
1414
deserialize any valid JSON file, and yet the deserializing code is
1515
written in less than 100 lines of simple, extensible code.
1616
17+
=head1 Named Regexes
18+
19+
The main ingredients of grammars are named L<regexes|/language/regexes>.
20+
These have a special syntax, similar to subroutine definitions:
21+
22+
=for code :allow<B>
23+
my B<regex number {> \d+ [ \. \d+ ]? B<}>
24+
25+
In this case, we have to specify that the regex is lexically scoped
26+
using the C<my> keyword, because named regexes are normally used within
27+
grammars.
28+
29+
Being named gives us the advantage of being able to easily reuse the
30+
regex elsewhere:
31+
32+
=begin code :allow<B>
33+
say "32.51" ~~ B<&number>;
34+
say "15 + 4.5" ~~ /B<< <number> >>\s* '+' \s*B<< <number> >>/
35+
=end code
36+
37+
TODO signatures
38+
39+
TODO tokens & rules
40+
1741
=head1 class Grammar
1842
1943
class Grammar is Cursor { }
2044
2145
C<Grammar> is the superclass that classes automatically get when they
2246
are declared with the C<grammar> keyword instead of C<class>. Grammars
2347
should only be used to parse text; if you wish to extract complex data,
24-
an L<action class|/language/grammars#Action classes> is recommended to
48+
an L<action class|/language/grammars#Action_Classes> is recommended to
2549
be used in conjunction with the grammar.
2650
2751
=begin code :allow<B L>
@@ -117,7 +141,7 @@ of the supplied string.
117141
Parses the contents of the file C<$filename> with the L<parse> method,
118142
passing along any named options in C<%opts>.
119143
120-
=head1 Action classes
144+
=head1 Action Classes
121145
122146
TODO
123147

0 commit comments

Comments
 (0)