Getting rule name in action

52 views
Skip to first unread message

Roded Bahat

unread,
Dec 1, 2012, 8:45:07 AM12/1/12
to pe...@googlegroups.com
Hi,
I'm aspiring to use grammar rule names as node type in the AST built by my grammar.
At the moment, I'm retrieving the rule name by parsing arguments.callee.caller.caller... or something as ridiculous as that.
In my case then, for the purpose of minimizing repetitive action code, it would be useful to be able to dynamically get the current rule name.

I've noticed work is being done on the track-line-and-column issue (https://trello.com/card/kill-the-track-line-and-column-option/50a8eba48cf95d4957006b01/1), 
and was wondering if it would be it possible to add a similar method for rule name retrieval.
(The line/col and rule name obviously have different requirements, not sure if this is even a viable option).

Thanks,
Roded

David Majda

unread,
Dec 2, 2012, 8:03:43 AM12/2/12
to Roded Bahat, pegjs
2012/12/1 Roded Bahat <rod...@gmail.com>:
> At the moment, I'm retrieving the rule name by parsing
> arguments.callee.caller.caller... or something as ridiculous as that.
> In my case then, for the purpose of minimizing repetitive action code, it
> would be useful to be able to dynamically get the current rule name.

Could you file an issue on GitHub for that? Please also include
examples showing what your actions currently look like.

The information about the current rule can be computed statically for
each action so this may be doable without a perf hit.

--
David Majda
Entropy fighter
http://majda.cz/

Roded Bahat

unread,
Dec 3, 2012, 10:05:26 AM12/3/12
to pe...@googlegroups.com, Roded Bahat
Hi David, 

Another related issue is that in cases such as:
myRule = a:"sometoken" b:"optionaltoken"?
In the action, the Javascript variable "a" will of course be always defined, this is not the case with "b".
Adding to that, Javascript's inability to pass undefined variables as method arguments requires the typeof check of every optional variable in the rule action.
Is there a way I'm missing to avoid this?
Thanks!
Roded

David Majda

unread,
Dec 5, 2012, 2:19:52 AM12/5/12
to Roded Bahat, pegjs
2012/12/3 Roded Bahat <rod...@gmail.com>:
Thanks.

> Another related issue is that in cases such as:
> myRule = a:"sometoken" b:"optionaltoken"?
> In the action, the Javascript variable "a" will of course be always defined,
> this is not the case with "b".

This is not true. The "b" variable will always be defined in an action
following the expression and (in this specific case) either contain
"optionaltoken" or "", depending on the success of the match.

Roded Bahat

unread,
Dec 5, 2012, 3:53:06 AM12/5/12
to pe...@googlegroups.com, Roded Bahat
You are obviously right.
I was veiled in a misconception.
Thanks.

Roded Bahat

unread,
Dec 5, 2012, 3:53:47 AM12/5/12
to pe...@googlegroups.com, Roded Bahat
You are obviously right.
I was veiled in a misconception.
Thanks.

On Wednesday, December 5, 2012 9:19:52 AM UTC+2, David Majda wrote:
Reply all
Reply to author
Forward
0 new messages