Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using $/ is not exactly the same as using any other variable in grammar actions

Tags:

grammar

raku

In theory, and according to the documentation, you can use any argument for methods in grammar actions.

grammar G {
    token TOP { \w+ }
}

class Action-Arg {
    method TOP ($match) { $match.make: ~$match }
}

class Action {
    method TOP ($/) { make ~$/ }
}

class Action-Fails {
    method TOP ($match) { make ~$match }
}

say G.parse( "zipi", actions => Action-Arg );
say G.parse( "zape", actions => Action );
say G.parse( "pantuflo", actions => Action-Fails );

However, the two first versions work as expected. But the third one (which would be a direct translation of the second), fails with

Cannot bind attributes in a Nil type object
  in method TOP at match-and-match.p6 line 19
  in regex TOP at match-and-match.p6 line 7
  in block <unit> at match-and-match.p6 line 24

There's probably some special syntax going on (in the sense of make being actually $/.make, probably), but I'd just like to clarify if this is according to spec or is a bug.

like image 904
jjmerelo Avatar asked Jul 01 '19 10:07

jjmerelo


1 Answers

That is because the make subroutine is one of those rare cases in Rakudo where it actually tries to access the $/ variable from the scope it is called from. Which is also how it is documented:

The sub form operates on the current $/

(from the documentation)

like image 188
Elizabeth Mattijsen Avatar answered Nov 11 '22 14:11

Elizabeth Mattijsen