This is similar to this question for classes, except the same procedure does not seem to work for Grammars.
grammar TestGrammar {
token num { \d+ }
}
my $test-grammar = TestGrammar.new();
my $token = $test-grammar.^lookup('num');
say "3" ~~ $token;
This returns:
Type check failed in binding to parameter '<anon>'; expected TestGrammar but got Match (Match.new(:orig("3")...)
in regex num at pointer-to-token.raku line 2
in block <unit> at pointer-to-token.raku line 9
This seems to point to the fact that you need binding to a class/grammar, and not a "bare" token. However, it's not clear how to do that. Passing grammar or an instance of it as a parameter returns a different error:
Cannot look up attributes in a TestGrammar type object. Did you forget a '.new'?
Any idea of why this does not really work?
Update: using ^find_method
as indicated in this question that is referenced from the one above does not work either. Same issue. Using assuming
does not fix it either.
Update 2: I seem to be getting somewhere here:
my $token = $test-grammar.^lookup('num').assuming($test-grammar);
say "33" ~~ $token;
Does not yield any syntax error, however it returns False
no matter what.