If I have something like this in my grammar:
grammar G {
token tab-indent(Int $level) {
# Using just ** $level would require <!before \t> to have the same effect, so use a code block for simplicity.
\t+ <?{ $/.chars == $level }>
}
}
is there some way to directly get the value of $level
in the corresponding action method tab-indent($/)
?
Right now I redo $/.chars
there too, which works, but doesn't seem ideal, especially in more complex situations, where the value of the parameter can be less easy to deduce from the matching text.
Does anyone know of a better way to do this? Thanks in advance!
You can use a dynamic variable to pass information to the methods of an action class.
grammar G {
token TOP {
<tab-indent(2)> $<rest> = .*
}
token tab-indent(Int $level) {
:my $*level = $level; # has to be in this scope, not in a block
\t ** {$level}
}
}
class A {
method tab-indent($/) {
say '$*level = ', $*level;
}
}
say G.parse( actions => A.new, "\t\t\t" );
$*level = 2
「 」
tab-indent => 「 」
rest => 「 」