Error Boost.Spirit when mixing "alternates" with "optional"?

I only work with Boost.Spirit (from Boost 1.44) for three days trying to parse raw emails using accurate grammar in RFC2822. I thought I was beginning to understand this and get out of the way, but then I ran into a problem:

#include <iostream> #include <boost/spirit/include/qi.hpp> namespace qi = boost::spirit::qi; using qi::omit; using qi::repeat; using std::cout; using std::endl; typedef qi::rule<std::string::const_iterator, std::string()> strrule_t; void test(const std::string input, strrule_t rule) { std::string target; std::string::const_iterator i = input.begin(), ie = input.end(); if (qi::parse(i, ie, rule, target)) { cout << "Success: '" << target << "'" << endl; } else { cout << "Failed to match." << endl; } } int main() { strrule_t obsolete_year = omit[-qi::char_(" \t")] >> repeat(2)[qi::digit] >> omit[-qi::char_(" \t")]; strrule_t correct_year = repeat(4)[qi::digit]; test("1776", correct_year | repeat(2)[qi::digit]); // 1: Works, reports 1776. test("76", obsolete_year); // 2: Works, reports 76. test("76", obsolete_year | correct_year); // 3: Works, reports 76. test(" 76", correct_year | obsolete_year); // 4: Works, reports 76. test("76", correct_year | obsolete_year); // 5: Fails. test("76", correct_year | repeat(2)[qi::digit]); // 6: Also fails. } 

If test No. 3 works, then why test No. 5 — the same test with two alternatives — fails?

Similarly, if you pardon the expression: if test No. 4 works, and the place at the beginning is marked as optional, then why test No. 5 (the same test with the same input, except that there is no leading place at the entrance ) failed?

And finally, if this is a bug in Boost.Spirit (as I suspect it should be), how can I get around this?

+6
c ++ boost boost-spirit boost-spirit-qi
source share
1 answer

This is because you got an error in the Spirit repeat[] directive. Thanks for the report, I fixed this problem in SVN (rev. [66167]) and it will be available in Boost V1.45. At the same time, I would like to add your little test as a regression test to the Spirit test suite. I hope you do not mind me doing this.

+7
source share

All Articles