惯用语与跳过后完全匹配

时间:2017-11-08 13:25:03

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

跳过后最常用的方法是什么?更具体一点我想确保在匹配我的最高规则后输入中没有“不可跳过的”(垃圾)字符。

So, the conclusion is "Stack" will be in the String Constant pool and s1 + " Overflow" i.e Stack overflow will be in the Heap.

我认为非常难看的一个想法是,如果主迭代器位置不是结束迭代器,那么之后对auto const blankOrComment = ascii::space | x3::lexeme ['#' >> *(x3::char_ - x3::eol) >> -x3::eol ] ; auto const program = rule<AstProgram>("program") = *(as<AstDefinition> (definition > ";")) ; auto const programEntry = x3::skip(blankOrComment) [program]; 进行单独的解析调用。我目前更好的想法是改变根规则:

blankOrComment

是否有更惯用的方式?

1 个答案:

答案 0 :(得分:1)

最简单的黑客攻击是>> eps Live On Coliru

注意我会努力让船长更具自我描述性:

auto const skipper
    = space
    | '#' >> *(char_ - eol) >> (eol|eoi) 
    ;

同样,你可以使postskip hack更具自我描述性:

    auto const post_skip = eps;
    auto const program = "program" >> post_skip;

Live On Coliru

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

namespace Parser {
    namespace x3 = boost::spirit::x3;

    namespace rules {
        using namespace x3;

        auto const skipper
            = space
            | '#' >> *(char_ - eol) >> (eol|eoi) 
            ;

        auto const post_skip = eps;
        auto const program = "program" >> post_skip;

    }

    auto const programEntry = x3::skip(rules::skipper) [rules::program];
}

int main() {
    using It = std::string::const_iterator;
    for (std::string const input : {
            "",
            " program ",
            "#hello\n program # comment\n",
    }) {
        It f = input.begin(), l = input.end();

        if(parse(f, l, Parser::programEntry)) {
            std::cout << "Parse success\n";
        } else {
            std::cout << "Parse failed\n";
        }

        std::cout << "Remaining: '" << std::string(f,l) << "'\n";
    }
}

打印

Parse failed
Remaining: ''
Parse success
Remaining: ''
Parse success
Remaining: ''