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

时间:2014-11-15 17:39:55

标签: c++ boost-spirit

如何将_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!

2 个答案:

答案 0 :(得分:4)

在澄清问题之后,我猜测你正在寻找更像灵魂 Inherited Attributes 的功能。最重要的是,它们总是在调用时传递,因此它们不需要是可默认构造的,并且可以是引用类型。

<强> Live On Coliru

#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

答案 1 :(得分:0)

假设我猜测了你在这里的确实含义:

<强> Live On Coliru

#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>