Boost.Spirit.Qi替代(|)解析器问题

时间:2016-01-17 14:13:46

标签: c++ parsing boost c++14 boost-spirit

我正在编写一个Qi解析器来解析IRC消息,转录RFC 2812。语法中有一个完全不同的选择:

auto const hostname = shortname >> *('.' >> shortname);
auto const nickUserHost = nickname >> -(-('!' >> user) >> '@' >> host);

auto const prefix = hostname | nickUserHost;

Full code on Coliru here

我很困惑地看到我的测试字符串("D-z!D-z@mib-A3A026FF.rev.sfr.net")与nickUserHost匹配,但不是prefix

我看到的唯一值得注意的事情是nickUserHost的{​​{1}}本身是根据host定义的,但我不确定它会如何影响任何解析方式。

1 个答案:

答案 0 :(得分:2)

通过附加>> eoi,如果它没有到达输入的末尾,则明确地使解析失败。

<强> Live On Coliru

#include <string>
#include <iostream>
#include <iomanip>

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

namespace qi = boost::spirit::qi;

template <typename Expr>
void test(std::string name, Expr const& expr) {
    std::string const test = "D-z!D-z@mib-A3A026FF.rev.sfr.net";

    auto f = begin(test);
    bool ok = qi::parse(f, end(test), expr);
    std::cout << name << ": " << ok << "\n";
    if (f != end(test))
        std::cout << " -- remaining input: '" << std::string(f, end(test)) << "'\n";
}

int main() {
    auto const hexdigit = qi::char_("0123456789ABCDEF");
    auto const special = qi::char_("\x5b-\x60\x7b-\x7d");

    auto const oneToThreeDigits = qi::repeat(1, 3)[qi::digit];
    auto const ip4addr = oneToThreeDigits >> '.' >> oneToThreeDigits >> '.' >> oneToThreeDigits >> '.' >> oneToThreeDigits;
    auto const ip6addr = +(hexdigit >> qi::repeat(7)[':' >> +hexdigit]) | ("0:0:0:0:0:" >> (qi::lit('0') | "FFFF") >> ':' >> ip4addr);
    auto const hostaddr = ip4addr | ip6addr;

    auto const nickname = (qi::alpha | special) >> qi::repeat(0, 8)[qi::alnum | special | '-'];
    auto const user = +(~qi::char_("\x0d\x0a\x20\x40"));

    auto const shortname = qi::alnum >> *(qi::alnum | '-');
    auto const hostname = shortname >> *('.' >> shortname);
    auto const host = hostname | hostaddr;

    auto const nickUserHost = nickname >> -(-('!' >> user) >> '@' >> host);

    auto const prefix = hostname | nickUserHost; // The problematic alternative

    std::cout << std::boolalpha;
    test("hostname",     hostname);
    test("nickUserHost", nickUserHost);
    test("prefix",       prefix);
}

打印

hostname: true
-- remaining input: '!D-z@mib-A3A026FF.rev.sfr.net'
nickUserHost: true
prefix: true
-- remaining input: '!D-z@mib-A3A026FF.rev.sfr.net'