Boost Spirit lexeme vs no_skip

本小妞迷上赌 提交于 2021-02-05 07:46:36

问题


As like the description on Boost.Spirit, the only difference between lexeme and no_skip is the pre_skip.

But after some test, I'm still confusing about the exactly meaning for pre_skip.

So what kind of condition will make a difference, maybe a example can help me understand it much better.

Thanks!


回答1:


Pre-skip ignores whitespace at the start of the expression.

Contrasting:

Live On Coliru

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

namespace qi = boost::spirit::qi;
static std::string const input = " 42j";

int main() {
    auto run_test = [](auto p) {
        auto f = input.begin(), l = input.end();
        int i;
        return qi::phrase_parse(f, l, p, qi::space, i)
            ? std::to_string(i)
            : "unparsed";
    };

    std::cout << "no_skip: " << run_test(qi::no_skip[ qi::int_ >> 'j' ]) << "\n";
    std::cout << "lexeme: "  << run_test(qi::lexeme[ qi::int_ >> 'j' ]) << "\n";
}

Prints:

no_skip: unparsed
lexeme: 42

As you can see lexeme will silently eat the leading white space. That's the pre-skip.



来源:https://stackoverflow.com/questions/63234428/boost-spirit-lexeme-vs-no-skip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!