Search code examples
parsingboost-spiritboost-spirit-qi

Parsing string with more then 15 characters with boost-spirit


I have a somewhat simple problem that i somehow cannot find any answers for. While working on parsing a larger grammar, i discovered that parsing any string larger then 15 characters would lead the parser to return as failed. The parser looks like this:

namespace parser {
    template <typename Iterator>
    struct p_grammar : qi::grammar<Iterator, standard::space_type> {
        p_grammar() : p_grammar::base_type(spec) {
           spec = "qwertyuiopasdfgh";
        }
        qi::rule<Iterator, standard::space_type> spec;
    };

And will be run from within another function:

void MainWindow::parserTest() {
    typedef parser::p_grammar<std::string::const_iterator> p_grammar;
    p_grammar grammar;
    using boost::spirit::standard::space;
    std::string::const_iterator iter = editor->toPlainText().toStdString().begin();
    std::string::const_iterator end = editor->toPlainText().toStdString().end();

    if ( phrase_parse(iter,end,grammar,space) ) {
        outputLog->append("Parsing succesfull");
    } else {
        outputLog->append("Parsing failed");
    }
}

Removing the last character in "qwertyuiopasdfgh", so only 15 characters are present, makes it parse without failure.

Feel like I'm overlooking something obvious here.


Solution

  • You should be using valid iterators:

    std::string value = editor->toPlainText().toStdString()
    std::string::const_iterator iter = value.begin(), end = value.end();
    

    You were using iterators into a temporary that wasn't stored.