File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -205,6 +205,43 @@ It's perfectly fine to provide your own C<ws> token:
205
205
token ws { \h* }
206
206
}.parse: "4 \n\n 5"; # Fails
207
207
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
+
208
245
= head2 Methods in Grammar
209
246
210
247
It's fine to use methods instead of rules or tokens in a grammar, as long
You can’t perform that action at this time.
0 commit comments