tzlaine / parser

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

Shorter way of defining a rule? #71

Closed akrzemi1 closed 5 months ago

akrzemi1 commented 5 months ago

Is there any implementation difficulty in changing macro BOOST_PARSER_DEFINE_RULE to accept two parameters: rule and the parser?

bp::rule<struct doubles, std::vector<double>> doubles = "doubles";
BOOST_PARSER_DEFINE_RULE(doubles, bp::double_ % ',');

Because this is definately shorter. And the longer variant would still be available with the plural form:

bp::rule<struct doubles, std::vector<double>> doubles = "doubles";
auto const doubles_def = bp::double_ % ',';
BOOST_PARSER_DEFINE_RULES(doubles);
akrzemi1 commented 5 months ago

BTW, section More About Rules says, "The reason for this is that, inside the rule parsing implementation, there is code something like this".

It may be just a bad choice of words, but it sounds like the implementation is driving the interface, as if you were saying, "this is how rules work because I couldn't implement it differently."

tzlaine commented 5 months ago

That seems reasonable. I like the idea of reducing the repetition of the rule name. This is pretty low-priority, though.

BTW, section More About Rules says, "The reason for this is that, inside the rule parsing implementation, there is code something like this".

It may be just a bad choice of words, but it sounds like the implementation is driving the interface, as if you were saying, "this is how rules work because I couldn't implement it differently."

That's the way the entire library works internally. It's not so much a limitation on the implementation as a conscious choice to organize things a certain way, with resulting sematics. The example wasn't to explain "this is why you can't have nice things". It was intended to give you a little example that would be easy to remember (with C++ programmers I find that that usually means code) for why the code in the example does what it does.

tzlaine commented 5 months ago

I just wrote this out, and it's:

#define BOOST_PARSER_DEFINE_RULE_DEF(rule, parser_expr)                        \
    constexpr auto rule##_def = parser_expr

People should just write that out. The macro is not really making like any easier. So, I'm not going to do this one. However, I am going to get rid of BOOST_PARSER_DEFINE_RULE, which is superfluous now that we always have BOOST_PARSER_DEFINE_RULES. The only reaons for the singular form was that the plural form was previously optionally defined.