C++ Boost qi recursive rule construction

前端 未结 1 1770
时光说笑
时光说笑 2020-12-06 14:17

[It seems my explanations and expectations are not clear at all, so I added precision on how I\'d like to use the feature at the end of the post]

I\'m currently work

1条回答
  •  遥遥无期
    2020-12-06 14:46

    Note I just extended my answer with a few more informational links. In this particular case I have a hunch that you could just get away with the Nabialek trick and replacing the inherited attribute with a corresponding qi::locals<> instead. If I have enough time, I might work out a demonstration later.

    Caveats, expositioning the problem

    Please be advised that there are issues when copying proto expression trees and spirit parser expressions in particular - it will create dangling references as the internals are not supposed to live past the end of the containing full expressions. See BOOST_SPIRIT_AUTO on Zero to 60 MPH in 2 seconds!

    Also see these answers which also concerns themselves with building/composing rules on the fly (at runtime):

    • Generating Spirit parser expressions from a variadic list of alternative parser expressions
    • Can Boost Spirit Rules be parameterized which demonstrates how to return rules from a function using boost::proto::deepcopy (like BOOST_SPIRIT_AUTO does, actually)

    Nabialek Trick

    In general, I'd very strongly advise against combining rules at runtime. Instead, if you're looking to 'add alternatives' to a rule at runtime, you can always use qi::symbols<> instead. The trick is to store a rule in the symbol-table and use qi::lazy to call the rule. In particular, this is known as the Nabialek Trick.

    I have a toy command-line arguments parser here that demonstrates how you could use this idiom to match a runtime-defined set of command line arguments:

    • https://gist.github.com/sehe/2a556a8231606406fe36

    Limitations of qi::lazy, what's next?

    Unfortunately, qi::lazy does not support inherited arguments see e.g.

    • http://boost.2283326.n4.nabble.com/pass-inhertited-attributes-to-nabialek-trick-td2679066.html

    You might be better off writing a custom parser component, as documented here:

    • http://boost-spirit.com/home/articles/qi-example/creating-your-own-parser-component-for-spirit-qi/

    I'll try to find some time to work out a sample that replaces inherited arguments by qi::locals later.

    0 讨论(0)
提交回复
热议问题