0

我使用增强精神来解析颜色。效果很好,但是在我更改了迭代器类型之后,船长停止了工作。

"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);

我敢肯定,这只是一个小错误,但我看不到它。也许我在源头上看了太久……你能看出一个错误吗?

4

1 回答 1

2

我看不出有什么问题:我已努力重建您的 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

于 2013-02-11T13:09:11.713 回答