c++ - Spirit X3 没有抛出期望失败

标签 c++ boost-spirit boost-spirit-x3

根据文档,它说当我使用 expect 运算符时,当运算符无法匹配时,我应该得到一个 expectation_failure。我想捕获异常以指示用户错误输入的位置。但似乎我得到了某种包装异常:

 terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::spirit::x3::expectation_failure<__gnu_cxx::__normal_iterator<char*, std::string> > > >'
  what():  boost::spirit::x3::expectation_failure
Aborted

我的捕获语句是:

try {
    r = parse(iter, end, wctl_parser::entry, root);
} catch (x3::expectation_failure<char const*> const& x) {
    std::cout << "Never runs,";
} catch (x3::expectation_failure<std::string::const_iterator> const& e) {
    std::cout << "me neither" << std::endl;
}

更新:这是一个展示行为的小程序:

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>
#include <boost/spirit/include/qi_char_class.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

#include <iostream>
#include <string>
#include <vector>

namespace wccs_parser {

namespace x3 = boost::spirit::x3;
namespace ascii = x3::ascii;
namespace qi = boost::spirit::qi;

struct AstNullProcess;
struct AstChoiceProcess;

using AstAnyProcess = x3::variant<
     x3::forward_ast<AstNullProcess>,
     x3::forward_ast<AstChoiceProcess>
>;

struct AstNullProcess {};
struct AstChoiceProcess {
    AstAnyProcess left;
    AstAnyProcess right;
};


} // End namespace

BOOST_FUSION_ADAPT_STRUCT(wccs_parser::AstChoiceProcess, left, right)

namespace wccs_parser {

template <typename T> auto rule = [](const char* name = typeid(T).name()) {
     struct _{};
     return x3::rule<_, T> {name};
};

template <typename T> auto as = [](auto p) { return rule<T>() = p; };

auto nullProcess  = as<AstNullProcess>(x3::omit['0']);

auto const choiceActual = as<AstChoiceProcess> (nullProcess > '+' > nullProcess);
auto const choice = rule<AstAnyProcess> ("choice")
     = nullProcess >> !x3::lit('+')
     | choiceActual;

auto const entry = x3::skip(ascii::space) [choice];


} //End namespace

namespace x3 = boost::spirit::x3;

int main() {
    std::string str("0 + ");
    wccs_parser::AstAnyProcess root;
    auto iter = str.begin();
    auto end = str.end();
    bool r = false;
    try {
        r = parse(iter, end, wccs_parser::entry, root);
    } catch (x3::expectation_failure<char const*> const& x) {
        std::cout << "Never runs," << std::endl;
    } catch (x3::expectation_failure<std::string::const_iterator> const& e) {
        std::cout << "me neither" << std::endl;
    }
    if (r) {
        std::cout << str << std::endl << std::endl << " Parses OK: " << std::endl;
        std::cout << "\n-------------------------\n";
    } else {
        std::cout << "Parsing failed\n";
        std::cout << "-------------------------\n";
    }
    if (iter != end) std::cout << "Partial match" << std::endl;
    return 0;
}

最佳答案

如果您通过 const_iterator 捕获,您需要确保 const_iterator 是您传递给解析的内容:

std::string const str("0 + ");

查看 Live On Coliru

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>
#include <boost/spirit/include/qi_char_class.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

#include <iostream>
#include <string>
#include <vector>

namespace wccs_parser {
    using boost::spirit::x3::variant;
    using boost::spirit::x3::forward_ast;

    struct AstNullProcess;
    struct AstChoiceProcess;

    using AstAnyProcess = variant<
        forward_ast<AstNullProcess>,
        forward_ast<AstChoiceProcess>
    >;

    struct AstNullProcess {};
    struct AstChoiceProcess {
        AstAnyProcess left;
        AstAnyProcess right;
    };

} // End namespace

BOOST_FUSION_ADAPT_STRUCT(wccs_parser::AstChoiceProcess, left, right)

namespace wccs_parser {
    namespace x3 = boost::spirit::x3;
    using x3::expectation_failure;

    template <typename T> auto rule = [](const char* name = typeid(T).name()) {
         struct _{};
         return x3::rule<_, T> {name};
    };

    template <typename T> auto as = [](auto p, const char* name = typeid(T).name()) { return rule<T>(name) = p; };

    auto nullProcess  = as<AstNullProcess>(x3::omit['0'], "nullProcess");

    auto const choiceActual = as<AstChoiceProcess> (nullProcess > '+' > nullProcess, "choiceActual");
    auto const choice = rule<AstAnyProcess> ("choice")
         = nullProcess >> !x3::lit('+')
         | choiceActual;

    auto const entry = x3::skip(x3::ascii::space) [choice];
}

int main() {

    std::string const str("0 + ");

    try {
        auto iter = str.cbegin();
        auto end = str.cend();

        wccs_parser::AstAnyProcess root;
        bool const r = parse(iter, end, wccs_parser::entry, root);
        if (r) {
            std::cout << str << "\n\n Parses OK: \n";
            std::cout << "\n-------------------------\n";
        } else {
            std::cout << "Parsing failed\n";
            std::cout << "-------------------------\n";
        }

        if (iter != end) {
            std::cout << "Partial match, leaving '" << std::string(iter,end) << "'\n";
        }
    } catch (wccs_parser::expectation_failure<std::string::const_iterator> const& e) {
        std::cout << "Expected: " << e.which() << " at '" << std::string(e.where(), str.end()) << "'\n";
        return 1;
    }
}

打印

Expected: nullProcess at ' '

关于c++ - Spirit X3 没有抛出期望失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47158931/

相关文章:

c++ - Spirit X3,如何让属性类型匹配规则类型?

c++ - Boost spirit x3 解析为结构,如果它为空则跳过成员

C++ 模板,不同的声明和定义,链接器无法解析符号

c++ - 使用 boost spirit qi 解析器迭代填充 BGL 图

c++ - 请指出这个字符串反转函数有什么问题?

c++ - 动态 Spirit 解析器取决于标志位

c++ - 使用 Boost Karma 打印 boost::posix_time::time_duration

c++ - 使用容器解析为结构

c++ - boost spirit语法的不一致行为

c++ - 如何遍历 fd_set