簡體   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