How to do "stream" parsing in Boost Spirit X3?
Asked Answered
C

1

6

I am trying to find out the correct way to parse from an istream using x3. Older docs refer to multi_pass stuff, can I still use this? Or is there some other way to buffer streams for X3 so that it can backtrack ?

Commitment answered 6/5, 2016 at 18:52 Comment(0)
G
8

You can still use this. Just include

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

Example Live On Coliru

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

int main() {
    std::istringstream iss("{ 123, 234, 345, 456, 567, 678, 789, 900, 1011 }");
    boost::spirit::istream_iterator f(iss), l;

    std::vector<int> values;

    namespace x3 = boost::spirit::x3;

    if (x3::phrase_parse(f, l, '{' >> (x3::int_ % ',') >> '}', x3::space, values)) {
        std::cout << "Parse results:\n";
        for (auto v : values) std::cout << v << " ";
    } else
        std::cout << "Parse failed\n";
}

Prints

Parse results:
123 234 345 456 567 678 789 900 1011 
Gaona answered 6/5, 2016 at 20:36 Comment(1)
I think in general one needs to iss.unsetf(std::ios::skipws); // No white space skipping!. Here it works by accident it seems.Grantley

© 2022 - 2024 — McMap. All rights reserved.