繁体   English   中英

船长在boost :: spirit中不起作用

[英]Skipper does not work in boost::spirit

我使用boost spirit来解析颜色。 这非常有效,但在我更改了迭代器类型之后,船长停止了工作。

"rgb(1.0,1.0,0.5)"  // this works
" rgb(0.2,0.2,0.2)" // this fails

这是标题:

struct ColorGrammar : public qi::grammar<StringIterator, Color(), chs::space_type>
{
//! Iterator type for this grammar
typedef StringIterator ItType;
//! Skipper type used in this grammar
typedef chs::space_type Skipper;



//! Rule to parse a number with up to 3 digits
qi::uint_parser<uint8, 10, 1, 3> number;
//! Rule to parse a hex digit
qi::uint_parser<uint8, 16, 1, 1> hexdigit;

ColorGrammar();

//! Rule for rgb(...)
qi::rule<ItType, Color(), qi::locals<float, float>, Skipper> rule_rgb;
//! Rule for rgba(...)
qi::rule<ItType, Color(), qi::locals<float, float, float>, Skipper> rule_rgba;
//! Mainrule
qi::rule<ItType, Color(), Skipper> rule_color;
};

这是cpp

ColorGrammar::ColorGrammar()
: ColorGrammar::base_type(rule_color, "color-grammar")
{
using namespace qi::labels;
using boost::phoenix::construct;
auto& _1 = qi::_1;


rule_rgb = '(' >> qi::float_[_a = _1] >> ',' >> qi::float_[_b = _1] >> ',' >> qi::float_[_val = phx::construct<Color>(_a, _b, _1)] >> ')';
rule_rgba = '(' >> qi::float_[_a = _1] >> ',' >> qi::float_[_b = _1] >> ',' >> qi::float_[_c = _1] >> ',' >> qi::float_[_val = phx::construct<Color>(_a, _b, _c, _1)] >> ')';
rule_color = (qi::lit("rgb") >> rule_rgb)
    | (qi::lit("rgba") >> rule_rgba);
}

电话:

Color out;
StringIterator begin = str.cbegin();
StringIterator end = str.cend();

bool result = qi::phrase_parse(begin, end, color_, chs::space, out);

我敢肯定,这只是一个小小的错误,但我无法看到它。 也许我在源头看了太长时间......你能看到一个错误吗?

我看不出有什么问题:我已经努力重建你的SSCCE。

在这个过程中,似乎我必须已经解决了这个问题。 我建议你这样做。

哦,这就是我写这个的方式:

  • 没有更多的凤凰
  • 没有更多的构造者
  • 没有更多qi :: locals
  • 没有更多不必要的复制
  • 使用期望点

简而言之:不要大惊小怪。

#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <cstdint>

namespace qi  = boost::spirit::qi;
namespace chs = boost::spirit::ascii; //qi;

typedef std::string::const_iterator StringIterator;

struct Color
{
    float r,g,b,a; 
};

BOOST_FUSION_ADAPT_STRUCT(Color, (float, r)(float, g)(float, b)(float, a))

template <typename ItType, typename Skipper>
struct ColorGrammar : public qi::grammar<StringIterator, Color(), Skipper>
{
    ColorGrammar()
        : ColorGrammar::base_type(rule_color, "color-grammar")
    {
        using namespace qi;
        rule_rgb   = lit("rgb")  >> '(' > float_ > ',' > float_ > ',' > float_ >       attr(1.0f) > ')';
        rule_rgba  = lit("rgba") >> '(' > float_ > ',' > float_ > ',' > float_ > ',' > float_     > ')';
        rule_color = rule_rgb | rule_rgba;
    }

  private:
    qi::uint_parser<uint8_t, 10, 1, 3> number;   // unused
    qi::uint_parser<uint8_t, 16, 1, 1> hexdigit; // unused

    qi::rule<ItType, Color(), Skipper> rule_rgb, rule_rgba, rule_color;
};

int main()
{
    Color out;
    std::string str = " rgb ( 0.3 , .4 , 0.5 )";
    StringIterator begin = str.cbegin();
    StringIterator end   = str.cend();

    ColorGrammar<StringIterator, chs::space_type> color_;

    bool result = qi::phrase_parse(begin, end, color_, chs::space, out);
    std::cout << std::boolalpha << result << '\n';
    std::cout << "remains: '" << std::string(begin, end) << "'\n";
}

住在http://liveworkspace.org/code/35htD$3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM