parslet icon indicating copy to clipboard operation
parslet copied to clipboard

Mechanism for matching optional keys in a transform rule

Open ravinggenius opened this issue 8 years ago • 4 comments

Having a way to mark certain keys as optional could go a long way towards reducing mostly redundant transform rules. Here's a simple example to show what I'm asking for.

Instead of two rules:

rule(integer: simple(:integer)) do |integer:|
  Nodes::Integer.new(integer, sign: :+)
end
rule(integer: simple(:integer), sign: simple(:sign)) do |integer:, sign:|
  Nodes::Integer.new(integer, sign: sign)
end

... I'd like to write a single rule to encapsulate the core logic:

rule(integer: simple(:integer), sign: simple(:sign, optional: :+)) do |integer:, sign:|
  Nodes::Integer.new(integer, sign: sign)
end

Notice the optional keyword passed into simple. The value of optional could be the default value to yield to the block if the key isn't present. Alternatively it could be a simple boolean to indicate an optional key, and it would be up to the block to handle the missing value. The optional argument should be available to sequence and subtree as well.

ravinggenius avatar Sep 02 '16 05:09 ravinggenius

Any interest in this?

ravinggenius avatar Sep 07 '16 01:09 ravinggenius

It sounds useful. Do you have a working solution for this that doesn't slow down all transformations? Seems to me that you have to do more checking at each stage.

kschiess avatar Nov 04 '16 13:11 kschiess

I don't have a working solution yet so I don't know what the performance impact would be. I wanted to see what you (@kschiess) thought before starting.

ravinggenius avatar Nov 04 '16 17:11 ravinggenius

It sounds useful.

kschiess avatar Nov 24 '16 13:11 kschiess