将已解析序列的每个元素传递给返回规则属性类型的函数

时间:2018-12-01 17:43:35

标签: c++ css c++17 boost-spirit boost-spirit-x3

我想解析CSS颜色函数(为简单起见,所有参数都是0到255之间的数字)

rgb(r,g,b)
rgba(r,g,b,a)
hsl(h,s,l)
hsla(h,s,l,a)

进入

struct color
{
    color(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a) : red{r}, green{g}, blue{b}, alpha{a} {}
    static color hsl(std::uint8_t h, std::uint8_t s, std::uint8_t l, std::uint8_t a) { ... }
    std::uint8_t red;
    std::uint8_t green;
    std::uint8_t blue;
    std::uint8_t alpha;
}

我有一个有效的hsl函数实现,可以将h,s和l转换为rgb值。

我还有rule来处理前两个功能:

constexpr auto uint8 = uint_parser<std::uint8_t>{};
const auto color_rgb = rule<struct rgb, color>{"rgb"}
                     = lit("rgb") >> '(' >> uint8 >> ',' >> uint8 >> ',' >> uint8 >> ')' >> attr(255);
const auto color_rgba = rule<struct rgba, color>{"rgba"}
                      = lit("rgba") >> '(' >> uint8 >> ',' >> uint8 >> ',' >> uint8 >> ',' >> uint8 >> ')';

这是有效的,因为我用过

BOOST_FUSION_ADAPT_STRUCT(color,
                          red, green, blue, alpha)

问题出在hsl函数中。我无法再进行BOOST_FUSION_ADAPT_STRUCT的操作,因此我想到了在各个值序列上使用语义动作,其中语义动作将仅从该序列构造color。像这样:

const auto color_hsl = rule<struct hsl, color, true>{"hsl"}
                   = (lit("hsl") >> '(' >> uint8 >> ',' >> uint8 >> ',' >> uint8 >> ')' >> attr(255))[color::hsl];

这行不通,否则我不会问这个问题。 Boost.Spirit.X3都没有

[ qi::_val = phx::construct<color>(...), qi::_1, qi::_2, qi::_3, qi::_4) ];

似乎我想手动执行BOOST_FUSION_ADAPT_STRUCT的操作,但是要进行语义操作。这可能吗,我应该如何处理?我知道序列的属性应该是具有四个解析值的“融合向量”元组状实体。我想提取它们并将其填充到color::hsl中以生成规则的属性。

1 个答案:

答案 0 :(得分:5)

此处有许多提示。

  1.   

    我不能再进行一次BOOST_FUSION_ADAPT_STRUCT

    可以,请参阅BOOST_FUSION_ADAPT_STRUCT_NAMED

  2. 在Qi中,这种通用形式似乎适用:

    [ qi::_val = phxfunction(qi::_0) ]
    

    您可以通过设置自己的演员类型来进一步简化,以便您只需提供“工厂行动”即可:[ factory(&Foo::makeBar) ]

    如果您使用fusion::apply¹的实现,则可以避免处理Fusion序列手册lt

  3. 但是,您可能想了解以下内容-非常隐蔽-语义动作的属性兼容模式:BOOST_SPIRIT_ACTIONS_ALLOW_ATTR_COMPAT。埋在该更改日志中:

      

    语义动作现在支持属性兼容性。这是一项重大更改,但是必须定义#define BOOST_SPIRIT_ACTIONS_ALLOW_ATTR_COMPAT才能使新行为生效。默认情况下,旧行为仍然存在。

    您可以通过更少的调整来获得所需的行为。

  4. X3确实具有延展性。我们可以用最少的时间获得上述factory助手:

    auto factory = [](auto f) {
        return [f](auto& ctx) {
            x3::_val(ctx) = my_apply(f, x3::_attr(ctx));
        };
    };
    

    我将快速提交my_apply的草稿(针对先前描述的boost::fusion::apply):

    namespace detail {
        template <class F, class Sequence, std::size_t... I>
            constexpr decltype(auto) apply_impl(F&& f, Sequence&& t, std::index_sequence<I...>)
            {
                return std::invoke(std::forward<F>(f), boost::fusion::at_c<I>(std::forward<Sequence>(t))...);
            }
    }
    
    template <class F, class Sequence>
        constexpr decltype(auto) my_apply(F&& f, Sequence&& t)
        {
            return detail::apply_impl(
                    std::forward<F>(f), std::forward<Sequence>(t),
                    std::make_index_sequence<typename boost::fusion::result_of::size<std::remove_reference_t<Sequence> >::type{}>{});
        }
    

现在我们可以使用解析器了:

namespace parser {
    using namespace x3;

    constexpr auto uint8  = uint_parser<std::uint8_t>{};

    auto factory = [](auto f) {
        return [f](auto& ctx) {
            x3::_val(ctx) = my_apply(f, x3::_attr(ctx));
        };
    };

    const auto color_rgb  = rule<struct rgb, ast::color>{"rgb"}
                          = lit("rgb") >> '(' >> uint8 >> ',' >> uint8 >> ',' >> uint8 >> x3::attr(255u) >> ')';
    const auto color_rgba = rule<struct rgba, ast::color>{"rgba"}
                          = lit("rgba") >> '(' >> uint8 >> ',' >> uint8 >> ',' >> uint8 >> ',' >> uint8 >> ')';
    const auto color_hsl  = rule<struct hsl, ast::color>{"hsl"}
                          = (lit("hsl") >> '(' >> uint8 >> ',' >> uint8 >> ',' >> uint8 >> attr(255u) >> ')') [factory(ast::color::hsl)];

    const auto color = skip(space) [ color_rgba | color_rgb | color_hsl ];
} 

并使用以下命令进行测试:

int main() {
    for (std::string const input : {
            "rgb(1,2,3)",
            "rgba(4,5,6,7)",
            "hsl(8,9,10)" }) 
    {
        std::cout << " ----- Parsng " << std::quoted(input) << " --------\n";
        auto begin = input.begin(), end = input.end();

        ast::color result;
        bool success = parse(begin, end, parser::color, result);

        if (success) {
            std::cout << "parsed: ";
            std::cout << result << "\n";
        } else {
            std::cout << "failed\n";
        }

        if (begin != end) {
            std::cout << "Remaining unparsed: " << std::quoted(std::string(begin, end)) << std::endl;
        }
    }
}

打印:

Live On Coliru

 ----- Parsng "rgb(1,2,3)" --------
parsed: rgba(1,2,3,255)
 ----- Parsng "rgba(4,5,6,7)" --------
parsed: rgba(4,5,6,7)
 ----- Parsng "hsl(8,9,10)" --------
TODO: implement static ast::color ast::color::hsl(uint8_t, uint8_t, uint8_t, uint8_t)(8,9,10,255)
parsed: rgba(8,9,10,255)

完整列表

Live On Coliru

#include <iostream>
#include <iomanip>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <boost/fusion/include/size.hpp>

namespace x3 = boost::spirit::x3;


namespace ast {
    using std::uint8_t;

    struct color {
        uint8_t red, green, blue, alpha;

        color(uint8_t r=0, uint8_t g=0, uint8_t b=0, uint8_t a=255) : red{r}, green{g}, blue{b}, alpha{a} {}

        static color hsl(uint8_t h, uint8_t s, uint8_t l, uint8_t a) { 
            std::cerr << "TODO: implement " << __PRETTY_FUNCTION__ << "(" << 1*h << "," << 1*s << "," << 1*l << "," << 1*a << ")\n";
            return {h,s,l,a}; }
    };

    static inline std::ostream& operator<<(std::ostream& os, color const& c) {
        return os << "rgba(" << 1*c.red << "," << 1*c.green << "," << 1*c.blue << "," << 1*c.alpha << ")";
    }
}

BOOST_FUSION_ADAPT_STRUCT(ast::color, red, green, blue, alpha);

namespace {
    namespace detail {
        template <class F, class Sequence, std::size_t... I>
            constexpr decltype(auto) apply_impl(F&& f, Sequence&& t, std::index_sequence<I...>)
            {
                return std::invoke(std::forward<F>(f), boost::fusion::at_c<I>(std::forward<Sequence>(t))...);
            }
    }

    template <class F, class Sequence>
        constexpr decltype(auto) my_apply(F&& f, Sequence&& t)
        {
            return detail::apply_impl(
                    std::forward<F>(f), std::forward<Sequence>(t),
                    std::make_index_sequence<typename boost::fusion::result_of::size<std::remove_reference_t<Sequence> >::type{}>{});
        }
}

namespace parser {
    using namespace x3;

    constexpr auto uint8  = uint_parser<std::uint8_t>{};

    auto factory = [](auto f) {
        return [f](auto& ctx) {
            x3::_val(ctx) = my_apply(f, x3::_attr(ctx));
        };
    };

    const auto color_rgb  = rule<struct rgb, ast::color>{"rgb"}
                          = lit("rgb") >> '(' >> uint8 >> ',' >> uint8 >> ',' >> uint8 >> x3::attr(255u) >> ')';
    const auto color_rgba = rule<struct rgba, ast::color>{"rgba"}
                          = lit("rgba") >> '(' >> uint8 >> ',' >> uint8 >> ',' >> uint8 >> ',' >> uint8 >> ')';
    const auto color_hsl  = rule<struct hsl, ast::color>{"hsl"}
                          = (lit("hsl") >> '(' >> uint8 >> ',' >> uint8 >> ',' >> uint8 >> attr(255u) >> ')') [factory(ast::color::hsl)];

    const auto color = skip(space) [ color_rgba | color_rgb | color_hsl ];
} 

int main() {
    for (std::string const input : {
            "rgb(1,2,3)",
            "rgba(4,5,6,7)",
            "hsl(8,9,10)" }) 
    {
        std::cout << " ----- Parsng " << std::quoted(input) << " --------\n";
        auto begin = input.begin(), end = input.end();

        ast::color result;
        bool success = parse(begin, end, parser::color, result);

        if (success) {
            std::cout << "parsed: ";
            std::cout << result << "\n";
        } else {
            std::cout << "failed\n";
        }

        if (begin != end) {
            std::cout << "Remaining unparsed: " << std::quoted(std::string(begin, end)) << std::endl;
        }
    }
}

¹似乎不存在。当然,您可以复制到std::tuple并使用std::apply(也可以是experimental