Skip to content

Commit 8c4a8b8

Browse files
committed
Document always succeed assertion
1 parent 24c6013 commit 8c4a8b8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

doc/Language/grammars.pod6

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,43 @@ It's perfectly fine to provide your own C<ws> token:
205205
token ws { \h* }
206206
}.parse: "4 \n\n 5"; # Fails
207207
208+
209+
X<< <?> >>
210+
=head3 Always Succeed Assertion
211+
212+
The C<< <?> >> is the I<always succeed> assertion. When used as a grammar
213+
token, it can be used to trigger an Action class method. In the following,
214+
admittedly convoluted, program we use a grammar we look for Arabic digits and
215+
define a C<succ> token with the always succeed assertion. In the action class
216+
convert an Arabic digits into a Devanagari digit for each digit found by
217+
grammar. In the C<succ> method, we print the converted number.
218+
219+
grammar Digifier {
220+
token TOP {
221+
[ <digit>+ <.succ> <.ws> ]+
222+
}
223+
token digit { <[0..9]> }
224+
token succ { <?> }
225+
}
226+
227+
class Devanagari {
228+
has $digit = '';
229+
method digit ($/) { $digit ~= $/.ord.&[+](2358).chr }
230+
method succ ($) {
231+
say $digit;
232+
$digit = '';
233+
}
234+
}
235+
236+
Digifier.parse: '255 435 fail parsing here', actions => Devanagari.new;
237+
238+
# OUTPUT:
239+
# २५५
240+
# ४३५
241+
242+
Notice that even though the grammar's C<.parse> failed to match the entire
243+
string, we still parsed some of the things we were interested in.
244+
208245
=head2 Methods in Grammar
209246
210247
It's fine to use methods instead of rules or tokens in a grammar, as long

0 commit comments

Comments
 (0)