Skip to content

Commit e45d20d

Browse files
committed
update/correct some text
1 parent 84489ff commit e45d20d

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/grammars.pod

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,33 @@ C<JSON::Tiny::Grammar.parse($tester)> attempts to match the regex named
8383
C<TOP> to the string C<$tester>.
8484

8585
However, the example code does not seem to have a regex named C<TOP>; it
86-
has a I<rule> named C<TOP>. What's the difference? A rule is a regex
87-
with special behavior. Within a rule, sequences of whitespace are deemed
88-
significant and will match actual whitespace within the string (this is
89-
equivalent to using the C<:sigspace> modifier). Looking at the grammar,
90-
you'll also note that there are also C<token> declarations as well. A
91-
C<token> is also a regex with special behavior. A C<token> is the same
92-
as a rule (has significant whitespace), but it also does not backtrack
93-
so that when a partial pattern match fails, the regex engine will not go
94-
back and try another alternative (this is equivalent to using the
95-
C<:ratchet> modifier).
86+
has a I<rule> named C<TOP>. Looking at the grammar, you'll also note
87+
that there are also C<token> declarations as well. What's the
88+
difference? Each of the C<token> and C<rule> declarations are just
89+
C<regex> declarations with special default behavior. A C<token> declares
90+
a regex that does not backtrack by default, so that when a partial
91+
pattern match fails, the regex engine will not go back up and try
92+
another alternative (this is equivalent to using the C<:ratchet>
93+
modifier). A C<rule> will also not backtrack by default, additionally,
94+
sequences of whitespace are deemed significant and will match actual
95+
whitespace within the string using the built-in C<< <ws> >> rule (this
96+
is equivalent to using the C<:sigspace> modifier).
9697

9798
=for author
9899

99-
Add code to POD processor that does the right thing with L<S??> and
100-
L<src/...> links. Or update this document appropriately.
100+
Add code to POD processor that does the right thing with L<S??> links.
101101

102102
=end author
103103

104-
See L<src/regex> for more information on modifiers and how they may be
104+
See L<sec:regexes> for more information on modifiers and how they may be
105105
used or look at L<S05>.
106106

107107
Usually, when talking about regex that are rules or tokens, we tend to
108108
call them rules or tokens rather than the more general term "regex", to
109109
distinguish their special behaviors.
110110

111-
In this example, the C<TOP> rule anchors the match to the start and end
112-
of the string, so that the whole string has to be in valid JSON format
111+
In this example, the C<TOP> rule anchors the match to the start (with C<^>) and end
112+
of the string (with C<$>), so that the whole string has to be in valid JSON format
113113
for the match to succeed. After matching the anchor at the start of the
114114
string, the regex attempts to match either an C<< <array> >> or an C<<
115115
<object> >>. Enclosing a regex name in angle brackets causes the regex
@@ -219,7 +219,7 @@ The first two lines introduce a grammar that inherits from
219219
C<JSON::Tiny::Grammar>. Just as subclasses inherit methods from superclasses,
220220
so grammars inherit rules from its base grammar. Any rule used within
221221
the grammar will be looked for first in the grammar in which it was used,
222-
then within its parent grammar.
222+
then within its parent(s).
223223

224224
In this minimal JSON grammar, whitespace is never mandatory, so C<ws>
225225
can match nothing at all. After optional spaces, two slashes C<'//'>
@@ -248,7 +248,7 @@ ordinary, C<|> delimited alternatives.
248248
X<reduction methods>
249249
X<action methods>
250250

251-
The C<parse> method of a grammar returns a C<Match> object through which
251+
The C<.parse> method of a grammar returns a C<Match> object through which
252252
you can access all the relevant information of the match. Named regex
253253
that match within the grammar may be accessed via the C<Match> object
254254
similar to a hash where the keys are the regex names and the values are
@@ -259,10 +259,12 @@ parentheses are available as positional elements of the C<Match> object
259259

260260
Once you have the Match object, what can you I<do> with it? You could
261261
recursively traverse this object and create data structures based on what
262-
you find or execute code. An alternative solution exists: I<action methods>.
262+
you find or execute code. Perl 6 provides another alternative: I<action methods>.
263263

264264
=begin programlisting
265265

266+
# JSON::Tiny::Grammar as above
267+
# ...
266268
class JSON::Tiny::Actions {
267269
method TOP($/) { make $/.values.[0].ast }
268270
method object($/) { make $<pairlist>.ast.hash }
@@ -305,9 +307,10 @@ you find or execute code. An alternative solution exists: I<action methods>.
305307

306308
This example passes an actions object to the grammar's C<parse> method.
307309
Whenever the grammar engine finishes parsing a regex, it calls a method on
308-
the actions object with the same name as the current regex. If no such method
309-
exists, the grammar engine moves along. If a method does exist, the grammar
310-
engine passes the current match object as a positional argument.
310+
the actions object with the same name as the regex. If no such method
311+
exists, the grammar engine continues parsing the rest of the grammar. If
312+
a method does exist, the grammar engine passes the current match object
313+
as a positional argument.
311314

312315
X<abstract syntax tree>
313316
X<AST>

0 commit comments

Comments
 (0)