微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Boost Spirit X3-如何解析嵌套结构?

如何解决Boost Spirit X3-如何解析嵌套结构?

我尝试解析

list<char> fldName

我使用了嵌套结构。但是当一个结构嵌套在另一个结构中时,我很难解析。查看下面的最小示例代码

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <string>
#include <string_view>

using namespace std::string_view_literals;
using namespace boost::spirit::x3;
namespace ascii = boost::spirit::x3::ascii;

namespace client::ast
{
    struct ValidType
    {
        std::string Name;
        std::string SubName1;
        std::string SubName2;
    };

    struct StructField
    {
        ValidType Type;
        std::string Name;
    };

}   // namespace client::ast

BOOST_FUSION_ADAPT_STRUCT(client::ast::ValidType,Name,SubName1,SubName2
)

BOOST_FUSION_ADAPT_STRUCT(client::ast::StructField,Type,Name
)

namespace client::parser
{
    using ascii::char_;

    template <typename T> static auto as = [](auto p) { return rule<struct tag,T> {"as"} = p; };
#define STRING(x) as<std::string>(x)

    rule<class ValidType,ast::ValidType> const ValidType = "ValidType";
    rule<class StructField,ast::StructField> const StructField = "StructField";

    auto const ValidName = lexeme[(alpha | char_('_')) > *(alnum | char_('_'))];

    auto const ValidType_SecondPart = char('<') > STRING(ValidName) > ('>' | ',' > STRING(ValidName) > '>');
    auto const ValidType_def = STRING(ValidName) > -(ValidType_SecondPart);

    auto const StructField_def = ValidType_def > STRING(ValidName);

    BOOST_SPIRIT_DEFINE(ValidType);
    BOOST_SPIRIT_DEFINE(StructField);

}   // namespace client::parser

int main()
{
    using boost::spirit::x3::ascii::space;
    auto theData = R"(
                list<char> fldName
        )"sv;
    using client::parser::StructField;
    client::ast::StructField fld;

    bool result = phrase_parse(theData.begin(),theData.end(),StructField,space,fld);
    return result;
}

我收到以下错误

Error   C2338   Size of the passed attribute is less than expected

但是我不知道出什么问题了。看起来boost :: spirit :: x3有一个解析嵌套结构的错误

有没有办法解析嵌套结构?

答案- ValidType_def -> ValidType

auto const StructField_def = ValidType_def > STRING(ValidName); 
-> 
auto const StructField_def = ValidType > STRING(ValidName);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。