Search code examples
c++boost-spirit-qi

how to use list syntax with defaults spirit


I am attempting to parse comma separated integers, with possible blanks. For instance, 1,2,,3,,-1 should be parsed as {1,2,n,3,n,-1} where is n is some constant.

The expression,

(int_ | eps) % ','

works when n == 0. More specifically, the following code works special cased for 0:

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

int main() {
   using namespace boost::qi;
   std::vector<int> v;
   std::string s("1,2,,3,4,,-1");
   phrase_parse(s.begin(), s.end(), 
      (int_|eps) % ','
      , space, v);
}

I tried the following expression for arbitrary n:

(int_ | eps[_val = 3]) % ','

But apparently this is wrong. The compiler generates an error novel. I refrain from pasting all that here, as most likely what I am trying is incorrect (rather than specific compiler issues).

What would be the right way?

Nick


Solution

  • The attr() parser exists for this purpose:

    (int_ | attr(3)) % ','