来自 Boost Spirit X3 教程:



如果我很好理解,这个逻辑在很大程度上依赖于属性的顺序。

现在,我处于需要解析类似内容的情况

Layer "L1" {
    number = 23
    color = green
    visible = true
}

成一个结构
struct LayerInfo
{
    std::string layerName;
    int layerNumber;
    std::string color;
    bool visible;
}

问题是, 层属性的顺序可以改变 ,这与上面看到的逻辑相反。

解析成这样的结构的正确方法是什么?
我是否一定需要使用语义 Action ?

最佳答案

我喜欢@llonesmiz 在评论中的做法。

不过,我也“不得不”尝试使用函数组合使用 X3 的我最喜欢的方法。这是解析和传播值的方法的草图。



目前,不加评论地提出:

Live On Coliru

#include <iostream>
//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>

struct LayerInfo
{
    std::string layerName;
    int layerNumber = 0;
    std::string color;
    bool visible = false;
};

namespace Parser {
    namespace x3 = boost::spirit::x3;

    // custom type parsers
    auto quoted = rule<std::string>("quoted", x3::lexeme [ '"' >> *('\\' >> x3::char_ | ~x3::char_('"')) >> '"' ]);
    struct colors_type : x3::symbols<char> {
        colors_type() {
            this->add("red")("blue")("green")("black");
        }
    } static const colors;

    namespace detail {
        template <typename T> auto propagate(T member) {
            return [=](auto& ctx){ x3::traits::move_to(x3::_attr(ctx), x3::_val(ctx).*member); };
        }

        template <typename T> auto make_member_parser(int T::* const member) { return x3::int_ [propagate(member)]; }
        template <typename T> auto make_member_parser(bool T::* const member) { return x3::bool_ [propagate(member)]; }
        template <typename T> auto make_member_parser(std::string T::* const member) { return x3::raw[colors] [propagate(member)]; }

        template <typename T = LayerInfo, typename P>
            auto rule(const char* debug, P p) { return x3::rule<struct _, T> {debug} = x3::skip(x3::space)[p]; };

        auto property = [](auto label, auto member) {
            return rule(label, x3::as_parser(label) >> '=' >> make_member_parser(member));
        };
    }

    using detail::rule;
    using detail::propagate;
    using detail::property;

    auto name       = rule("name", "Layer" >> quoted [propagate(&LayerInfo::layerName)]);

    auto number     = property("number", &LayerInfo::layerNumber);
    auto color      = property("color", &LayerInfo::color);
    auto visible    = property("visible", &LayerInfo::visible);

    auto layer_info = name >> '{' >> +(number | color | visible) >> '}';

    auto grammar    = rule("layer_info", layer_info);
}

std::ostream& operator<<(std::ostream& os, LayerInfo const& li) {
    return os << "LayerInfo \"" << li.layerName << "\"{"
        << "number="  << li.layerNumber   << " "
        << "color="   << li.color         << " "
        << "visible=" << std::boolalpha << li.visible
        << "}\n";
}

int main() {
    std::string const sample = R"(Layer "L1" {
    number = 23
    color = green
    visible = true
})";

    LayerInfo v;
    auto f = sample.begin(), l = sample.end();
    bool ok = parse(f, l, Parser::grammar, v);


    if (ok)
        std::cout << "Parsed: " << v << "\n";
    else
        std::cout << "Parse failed\n";

    if (f!=l)
        std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}

打印
Parsed: LayerInfo "L1"{number=23 color=green visible=true}

机智调试信息: Live On Coliru

<layer_info>
  <try>Layer "L1" {\n    num</try>
  <name>
    <try>Layer "L1" {\n    num</try>
    <quoted>
      <try> "L1" {\n    number =</try>
      <success> {\n    number = 23\n </success>
      <attributes>[L, 1]</attributes>
    </quoted>
    <success> {\n    number = 23\n </success>
    <attributes>LayerInfo "L1"{number=0 color= visible=false}
</attributes>
  </name>
  <number>
    <try>\n    number = 23\n   </try>
    <success>\n    color = green\n </success>
    <attributes>LayerInfo "L1"{number=23 color= visible=false}
</attributes>
  </number>
  <number>
    <try>\n    color = green\n </try>
    <fail/>
  </number>
  <color>
    <try>\n    color = green\n </try>
    <success>\n    visible = true\n</success>
    <attributes>LayerInfo "L1"{number=23 color=green visible=false}
</attributes>
  </color>
  <number>
    <try>\n    visible = true\n</try>
    <fail/>
  </number>
  <color>
    <try>\n    visible = true\n</try>
    <fail/>
  </color>
  <visible>
    <try>\n    visible = true\n</try>
    <success>\n}</success>
    <attributes>LayerInfo "L1"{number=23 color=green visible=true}
</attributes>
  </visible>
  <number>
    <try>\n}</try>
    <fail/>
  </number>
  <color>
    <try>\n}</try>
    <fail/>
  </color>
  <visible>
    <try>\n}</try>
    <fail/>
  </visible>
  <success></success>
  <attributes>LayerInfo "L1"{number=23 color=green visible=true}
</attributes>
</layer_info>

关于c++ - boost spirit x3 : parse into structs,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45673378/

10-15 00:31