Spirit X3,如何在非ascii输入上解析失败?

时间:2019-07-06 18:17:23

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

因此,目标是不允许输入字符串中80h到FFh之间的字符。我的印象是

using ascii::char_;

会照顾这个。但是,如您在示例代码中看到的那样,它将很高兴地成功打印了解析。

在下面的Spirit邮件列表帖子中,Joel建议让解析在这些非ascii字符上失败。但是我不确定他是否继续这样做。 [Spirit-general] ascii encoding assert on invalid input ...

这是我的示例代码:

#include <iostream>
#include <boost/spirit/home/x3.hpp>

namespace client::parser
{
    namespace x3 = boost::spirit::x3;
    namespace ascii = boost::spirit::x3::ascii;

    using ascii::char_;
    using ascii::space;
    using x3::lexeme;
    using x3::skip;

    const auto quoted_string = lexeme[char_('"') >> *(char_ - '"') >> char_('"')];
    const auto entry_point = skip(space) [ quoted_string ];
}

int main()
{
    for(std::string const input : { "\"naughty \x80" "bla bla bla\"" }) {
        std::string output;
        if (parse(input.begin(), input.end(), client::parser::entry_point, output)) {
            std::cout << "Parsing succeeded\n";
            std::cout << "input:  " << input << "\n";
            std::cout << "output: " << output << "\n";
        } else {
            std::cout << "Parsing failed\n";
        }
    }
}

如何更改示例以使Spirit在此无效输入上失败?

此外,但又非常相关,我想知道如何使用定义char_set编码的字符解析器。您知道X3 docs: Character Parsers develop branchchar_(charset)

文档非常缺乏描述基本功能的信息。为什么提拔高层的人至少不能在cppreference.com级别上强迫库作者提供文档?

2 个答案:

答案 0 :(得分:2)

这里的文档没什么不好的。这只是一个库错误。

any_char的代码如下:

template <typename Char, typename Context>
bool test(Char ch_, Context const&) const
{
    return ((sizeof(Char) <= sizeof(char_type)) || encoding::ischar(ch_));
}

应该说

template <typename Char, typename Context>
bool test(Char ch_, Context const&) const
{
    return ((sizeof(Char) <= sizeof(char_type)) && encoding::ischar(ch_));
}

这使您的程序按预期和要求运行。该行为也符合Qi行为:

Live On Coliru

#include <boost/spirit/include/qi.hpp>

int main() {
    namespace qi = boost::spirit::qi;

    char const* input = "\x80";
    assert(!qi::parse(input, input+1, qi::ascii::char_));
}

在此处提交了错误:https://github.com/boostorg/spirit/issues/520

答案 1 :(得分:0)

您可以通过使用print解析器来实现:

#include <iostream>
#include <boost/spirit/home/x3.hpp>

namespace client::parser
{
    namespace x3 = boost::spirit::x3;
    namespace ascii = boost::spirit::x3::ascii;

    using ascii::char_;
    using ascii::print;
    using ascii::space;
    using x3::lexeme;
    using x3::skip;

    const auto quoted_string = lexeme[char_('"') >> *(print - '"') >> char_('"')];
    const auto entry_point = skip(space) [ quoted_string ];
}

int main()
{
    for(std::string const input : { "\"naughty \x80\"", "\"bla bla bla\"" }) {
        std::string output;
        std::cout << "input:  " << input << "\n";
        if (parse(input.begin(), input.end(), client::parser::entry_point, output)) {
            std::cout << "output: " << output << "\n";
            std::cout << "Parsing succeeded\n";
        } else {
            std::cout << "Parsing failed\n";
        }
    }
}

输出:

input:  "naughty �"
Parsing failed
input:  "bla bla bla"
output: "bla bla bla"
Parsing succeeded

https://wandbox.org/permlink/HSoB8uqMC3WME5yI


一个令人惊讶的事实是,出于某种原因,char_的检查仅在sizeof(iterator char type) > sizeof(char)时进行:

#include <boost/spirit/home/x3.hpp>
#include <iostream>
#include <string>
#include <boost/core/demangle.hpp>
#include <typeinfo>

namespace x3 = boost::spirit::x3;

template <typename Char>
void test(Char const* str)
{
    std::basic_string<Char> s = str;
    std::cout << boost::core::demangle(typeid(Char).name()) << ":\t";
    Char c;
    auto it = s.begin();
    if (x3::parse(it, s.end(), x3::ascii::char_, c) && it == s.end())
        std::cout << "OK: " << int(c) << "\n";
    else
        std::cout << "Failed\n";
}

int main()
{
    test("\x80");
    test(L"\x80");
    test(u8"\x80");
    test(u"\x80");
    test(U"\x80");
}

输出:

char:   OK: -128
wchar_t:    Failed
char8_t:    OK: 128
char16_t:   Failed
char32_t:   Failed

https://wandbox.org/permlink/j9PQeRVnGZQeELFA