Search code examples
c++boostboost-spiritboost-spirit-qi

Sequential Or parser a || b


I can't get a modified version of the example provided in boost's documentation to output correctly

Here's the documentation on sequential OR parser: http://www.boost.org/doc/libs/1_56_0/libs/spirit/doc/html/spirit/qi/reference/operator/sequential_or.html

test_parser("123.456", int_ || ('.' >> int_));  // full

I want this expression to populate a vector<int> with 2 entries:

[0] = 123
[1] = 456

Why doesn't this work?

string input("123.456");
vector<int> output;

string::iterator i = input.begin();

parse(i, input.end(), int_ || ('.' >> int_), output);

I have verified parse returns true and i == input.end(). I have also tried different data structures for output including tuples with optionals, and vectors of optionals. And they all produce a single entry containing just 123, never 456.


Solution

  • The || parser will parse into tuple<optional<A>, optional<B> > (for the optimistic scenario). This is never gonna be compatible with your container attribute.

    However, it looks like you could use

    parse(i, input.end(), -int_ >> -('.' >> int_), output);
    

    That said... if I were secretly Clippy, I might say "it looks like you are trying to parse real numbers.

    Consider float_, double_, or the underlying real_parser with perhaps a custom policy. See also: