如何使用 Boost.Spirit.Qi 解析不同的结构?

发布于 2024-09-24 21:07:56 字数 314 浏览 9 评论 0原文

此示例中,员工结构是以“employee{int, string, string, double}”的形式解析。

我想知道是否可以修改此示例以解析不同类型的结构,例如“intern{int, string, string}”。

具体来说,我想将结构传递给在结构类型上重载的函数。如果我可以避免为此使用多态双重分派,而是保留被解析以静态匹配正确的重载函数的具体类型,那就太好了。

In this example, employee structs are parsed in the form "employee{int, string, string, double}".

I would like to know whether it is possible to modify this example to also parse different types of structs, like "intern{int, string, string}".

Specifically, I would like to then pass the structure to a function overloaded on the structure type. It would be great if I can avoid using polymorphic double dispatch for this, and instead preserve the concrete type that gets parsed to statically match the correct overloaded function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2024-10-01 21:07:56

当然,这是可能的。为您想要解析的每种类型创建一个规则:

rule<Iterator, std::string()> s = ...;
rule<Iterator, intern()> intern_r = int_ >> s >> s;
rule<Iterator, employee()> employee_r = int_ >> s >> s >> double_;

并将它们组合成一个替代方案:

rule<Iterator> r = 
        intern_r   [phoenix::bind(receive_intern, _1)]
    |   employee_r [phoenix::bind(receive_employee, _1)]
    ;

这假设您有 2 个处理解析数据的函数:

void receive_intern(intern const&);
void receive_employee(employee const&);

这​​是您想要的吗?

Sure, that's possible. Create a rule for each of the types you want to parse:

rule<Iterator, std::string()> s = ...;
rule<Iterator, intern()> intern_r = int_ >> s >> s;
rule<Iterator, employee()> employee_r = int_ >> s >> s >> double_;

and combine those into an alternative:

rule<Iterator> r = 
        intern_r   [phoenix::bind(receive_intern, _1)]
    |   employee_r [phoenix::bind(receive_employee, _1)]
    ;

This assumes you have 2 functions handling the parsed data:

void receive_intern(intern const&);
void receive_employee(employee const&);

Is that what you want?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文