I can easily use token signatures by using token name directly:
my token t ( $x ) { $x };
'axb' ~~ / 'a' <t: 'x'> 'b' /; # match
'axb' ~~ / 'a' <t( 'x' )> 'b' /; # match
However I haven't found a way to do this, when token is stored in variable:
my $t = token ( $x ) { $x };
'axb' ~~ / 'a' <$t: 'x'> 'b' /;
'axb' ~~ / 'a' <$t( 'x' )> 'b' /;
Both give:
===SORRY!=== Error while compiling ...
Unable to parse expression in metachar:sym<assert>; couldn't find final '>'
What is the magic syntax to do that?
BTW: I've even browsed Raku test suite and it does not include such case in roast/S05-grammar/signatures.t
.
Place an &
before the variable:
my $t = token ( $x ) { $x };
say 'axb' ~~ / 'a' <&$t: 'x'> 'b' /;
say 'axb' ~~ / 'a' <&$t( 'x' )> 'b' /;
The parser looks for the &
, and then delegates to the Raku variable parse rule, which will happily parse a contextualizer like this.
Either:
Use the solution in jnthn's answer to let Raku explicitly know you wish to use your $
sigil'd token variable as a Callable
.
Declare the variable as explicitly being Callable
in the first place and make the corresponding change in the call:
my &t = token ( $x ) { $x };
say 'axb' ~~ / 'a' <&t: 'x'> 'b' /; # 「axb」
say 'axb' ~~ / 'a' <&t( 'x' )> 'b' /; # 「axb」
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With