Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass params to token referenced by variable?

Tags:

raku

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.

like image 639
Pawel Pabian bbkr Avatar asked Jan 08 '20 22:01

Pawel Pabian bbkr


2 Answers

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.

like image 175
Jonathan Worthington Avatar answered Nov 06 '22 21:11

Jonathan Worthington


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」
like image 24
raiph Avatar answered Nov 06 '22 20:11

raiph