I looked into adding this feature, and it turns out that I've already
implemented it. It's just not documented.
In any action, $Marpa::R2::Context::rule is the ID of the current
rule. The rule ID's can be translated into symbols with the Grammar
rule() method,
which is
documented. There is also a $Marpa::R2::Context::grammar variable,
which can be used to find the grammar for the action.
There's
a
test for it in the test suite, and the test is doing pretty close
to what you describe. It can be used as an example.
Why hadn't I documented it? Whenever I document a feature, I add a
synopsis and whenever I add a synopsis, I make sure the accuracy of the
synopsis is automatically checked as part of the test suite. My
development/build setup ensures that the snippets in the POD are all
accurate and all run correctly. I don't regret this, because IMHO it
means my documentation is unusually accurate given the size,
complexity, and rate of evolution of Marpa. But it certainly makes
documenting features take much more time.
A second reason I left it undocumented is that whenever I document
something, I am to one degree or another committed to it. I may have
been wondering whether I should have a more extensive "Context"
interface, use callbacks instead of variables, change the names, etc.
etc.
As of this email you may consider $Marpa::R2::Context::rule and
$Marpa::R2::Context::grammar part of the documented interface for
Marpa::R2, 2.018000. I'll add them to the official documentation in
the next indexed release.
Thanks, jeffrey kegler