4

I am trying to modify the following example : http://www.boost.org/doc/libs/1_57_0/libs/spirit/example/qi/employee.cpp

I would like to add a 2D vector in the employee struct, such as :

struct employee
{
    int age;
    std::string surname;
    std::string forename;
    double salary;
    std::vector<std::vector<int> > work_hours;
};

To parse something like

employee{42, "Foo", "Bar", 0
1 2 3
4 5 6 7 8
}

So that work_hours = {{1,2,3},{4,5,6,7,8}}. (I really need a 2D array, because the subvectors can vary in length).

I modified the BOOST_ADAPT_STRUCT, and changed the parsing rule into :

start %=
            lit("employee")
            >> '{'
            >>  int_ >> ','
            >>  quoted_string >> ','
            >>  quoted_string >> ','
            >>  double_
            >>  +(qi::int_) % qi::eol
            >>  '}'
            ;

Unfortunately, this rule makes spirit returns work_hours = {{1,2,3,4,5,6,7,8}}, and not : {{1,2,3},{4,5,6,7,8}}

Does anybody have a workaround ?

Thanks in advance

1
  • How is the parser supposed to know which numbers go into which subarray or the 2D array? There's no apparent structure in the input data that it could use to determine that. EDIT: Oh, formatting snafu. Hold on.
    – Wintermute
    Jan 31, 2015 at 14:44

1 Answer 1

2

qi::eol is matched by the skip parser, so you have to do the list parsing in a lexeme directive. For example:

        start %=
            lit("employee")
            >> '{'
            >>  int_ >> ','
            >>  quoted_string >> ','
            >>  quoted_string >> ','
            >>  double_
            >>  *lexeme[int_ % ' '] // <-- here
            >>  '}'
            ;

lexeme[int_ % ' '] does not use the skip parser, so it will match integers delimited by exactly one space. If you want to allow the integers to be delimited by more than one space (for indentation purposes or so), and depending on the way you want to handle tabs and other non-newline whitespace, you can use a more complex delimiter. Probably the closest to your original code is

            >>  *lexeme[int_ % +(ascii::space - '\n')]

What exactly the best approach is is something you'll have to answer yourself, though.

2
  • 1
    I think ascii::blank is what you would like use in your last approach.
    – llonesmiz
    Jan 31, 2015 at 17:06
  • @cv_and_he That looks like a sane option, agreed. I was just thinking along the lines of "skip parser minus newline" (ascii::space is the skip parser in the example OP linked to).
    – Wintermute
    Jan 31, 2015 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.