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 ?

leezu
  • 512
  • 3
  • 17
experquisite
  • 879
  • 5
  • 14

1 Answers1

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 
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    I think in general one needs to `iss.unsetf(std::ios::skipws); // No white space skipping!`. Here it works by accident it seems. – alfC Jun 20 '19 at 01:17