Closed nasser closed 2 years ago
@nasser Sorry it took me so long to respond to you. There's a reason this doesn't work but it's also prompted me to add support for the thing you're trying to do.
Actions are for constructing new nodes, i.e. instead of Canopy constructing its own parse tree nodes, it delegates to your action to do that. Therefore they only apply to rules that make new nodes which are:
"string"
, [chars]
or .
They don't apply to the ?
operator because this doesn't construct any nodes itself -- the expression before it constructs a node, and the ?
operator just means it's fine if that expression doesn't match anything.
So for example in ("hello world")?
, the terminals "hello"
and "world"
each construct a node, and the sequence ("hello" "world")
constructs a new node by combining those, but then the ?
operator just passes the sequence node through rather than building any nodes itself. The result of expr?
is either the node that expr
creates, or nothing.
However I think there is possibly an ergonomic benefit in making expr? %action
work, and based on your examples I think it would make sense to interpret it as (expr %action)?
. That is, action
is used to build the node for expr
if it matches, and then ?
returns the result if any.
Does that make sense? I've just pushed ca4041d which implements this functionality and I'd value your feedback.
I just ran into this too. The ca4041d commit solves some of the problems, but my brief attempts to fix the others didn't work as I expected.
I have a rule of the form:
grammar test
root <- number %action2
number <- "NaN" %action1
I was able to get it to parse last night by a similar approach to ca4041d, but not to generate a call to action2.
I managed to figure out an approach to solve this that will be fixed by #49
I'm going to close this because I believe the central problem identified in this issue (making %action
work with rule?
) has been addressed. Anyone that wants to discuss expanding the scope of this to references should comment on #49.
These work:
But this fails with a grammar parsing error