tzlaine / parser

A C++ parser combinator library.
Boost Software License 1.0
69 stars 12 forks source link

Add an attribute-transformation directive #153

Closed tzlaine closed 3 months ago

tzlaine commented 4 months ago

From David Sankel and me, from the Boost review:

If a parser p has attribute type T (a T parser), and you have a function f of type U(T), I would expect there to be an easy way to convert the T parser into a U parser. Something like map(f, p).

This seems like a nice addition. Though you can certainly do this with a rule+semantic action combo, it would be much more ergonomic to be able to do this in a simpler way.  I would make this a directive, like transform(f)[p].

akrzemi1 commented 4 months ago

Will this address the use case from https://github.com/tzlaine/parser/issues/63?

tzlaine commented 4 months ago

I don't think so, or at least I don't yet see it. The thing proposed here is taking an attribute that definitely exists, and transforming it into an attribute of a different type.

akrzemi1 commented 4 months ago

Well, not strictly directive defaulted[], but I can do:

transform(map_nullopt_to_default_constructed)[ (char_ >> int_) % ',') | eps ]
tzlaine commented 4 months ago

I get it now. Then, yes, I could definitely be used in that case. I still think writing attr(x) is easier.