提升精神 如何将本地引用作为属性传递

Boost Spirit how to pass a local reference as attribute

本文关键字:引用 属性      更新时间:2023-10-16

如何将_a作为对具有相同类型属性的子规则的引用传递:

rule(_a)

不起作用。

代码如下所示:

qi::rule<Iterator, Mdlx(), qi::locals<std::string, long32>, Skipper> mdl; // parent rule
qi::rule<Iterator, Model(long32&), Skipper> model; // sub rule
...
mdl =
        -version[at_c<0>(_val) = _1]
        // wrong code! pass _b because it has the type long32
        //>> -model(_a)[at_c<1>(_val) = _1] // pass local as attribute
        >> -model(_b)[at_c<1>(_val) = _1] // pass local as attribute
        >> -sequences[at_c<2>(_val) = _1]
        >> -global_sequences[at_c<3>(_val) = _1]
        >> -textures[at_c<4>(_val) = _1]
        >> -materials[at_c<5>(_val) = _1]
        >> repeat(_a)[
            geoset_animation
        ]
    ;
model =
        lit("Model")
        >> string_literal[at_c<0>(_val) = _1]
        >> lit('{')
            >> -(
                lit("NumGeosetAnims")
                >> integer_literal[_r1 = _1] // assign the value to the passed attribute
                >> lit(',')
            )
            >> lit("BlendTime") >> integer_literal[at_c<1>(_val) = _1]
            >> lit(',')
            >> bounds[at_c<2>(_val) = _1]
        >> lit('}')
    ;

如您所见,我想传递一个局部作为对子规则的引用,该子规则应设置 long32 值。然后,该值在父规则中用作局部变量_a。

编辑:我在代码中发现了错误。我正在传递_a但第一个本地具有 std::string 类型。我不得不通过_b!

在澄清问题之后,我推测您正在寻找更像精神继承属性的功能。最重要的是,它们总是在调用时传递,因此它们不需要是默认可构造的,并且可以是引用类型。

住在科里鲁

#define BOOST_SPIRIT_DEBUG
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
template <typename It>
struct my_parser : qi::grammar<It> {
    my_parser() : my_parser::base_type(start) {
        using namespace qi;
        start                            = my_int_rule;
        my_int_rule                      = my_rule_with_inherited_attribute(_val);
        my_rule_with_inherited_attribute = int_ [ _r1 = _1 ]; // copied into the reference passed
        BOOST_SPIRIT_DEBUG_NODES((start)(my_rule_with_inherited_attribute)(my_int_rule))
    }
  private:
    qi::rule<It> start;
    qi::rule<It, int() > my_int_rule;
    qi::rule<It, void(int&) > my_rule_with_inherited_attribute;
};
int main()
{
    using It = std::string::const_iterator;
    my_parser<It> p;
    std::string const input = "123";
    bool ok = qi::parse(input.begin(), input.end(), p);
    std::cout << "Parse " << (ok? "success":"failed") << "n";
}

指纹

<start>
  <try>123</try>
  <my_int_rule>
    <try>123</try>
    <my_rule_with_inherited_attribute>
      <try>123</try>
      <success></success>
      <attributes>[, 123]</attributes>
    </my_rule_with_inherited_attribute>
    <success></success>
    <attributes>[123]</attributes>
  </my_int_rule>
  <success></success>
  <attributes>[]</attributes>
</start>
Parse success

假设我猜对了你在这里的实际意思:

住在科里鲁

#define BOOST_SPIRIT_DEBUG
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
template <typename It>
struct my_parser : qi::grammar<It> {
    my_parser() : my_parser::base_type(start) {
        using namespace qi;
        my_rule_with_a_local = my_int_rule [ _a = _1 ];
        my_int_rule          = int_; 
        start = my_rule_with_a_local;
        BOOST_SPIRIT_DEBUG_NODES((start)(my_rule_with_a_local)(my_int_rule))
    }
  private:
    qi::rule<It> start;
    qi::rule<It, int() > my_int_rule;
    qi::rule<It, qi::locals<int> > my_rule_with_a_local;
};
int main()
{
    using It = std::string::const_iterator;
    my_parser<It> p;
    std::string const input = "123";
    bool ok = qi::parse(input.begin(), input.end(), p);
    std::cout << "Parse " << (ok? "success":"failed") << "n";
}

指纹

<start>
  <try>123</try>
  <my_rule_with_a_local>
    <try>123</try>
    <my_int_rule>
      <try>123</try>
      <success></success>
      <attributes>[123]</attributes>
    </my_int_rule>
    <success></success>
    <attributes>[]</attributes><locals>(123)</locals>
  </my_rule_with_a_local>
  <success></success>
  <attributes>[]</attributes>
</start>