I have the trouble with parsing on my project. At least I found the minimum code sample on which the problem appears. So at now tries to parse the string
"bool func1; const bool func2;"
Here the minimum sample code:
namespace qi = boost::spirit::qi;
using qi::lexeme;
using qi::string;
typedef boost::spirit::ascii::space_type TSkipper;
struct MyField
{
bool isConst;
std::string mtype;
std::string name;
};
BOOST_FUSION_ADAPT_STRUCT
(
MyField,
(bool, isConst)
(std::string, mtype)
(std::string, name)
)
void SpiritTestSimple()
{
qi::rule<std::string::const_iterator, std::string(), TSkipper> valid_symbols;
qi::rule<std::string::const_iterator, MyField(), TSkipper> field;
valid_symbols %= lexeme[*qi::char_("a-zA-Z")];
field %= qi::matches[string("const")] >> valid_symbols >> valid_symbols;
std::string data = "aaa aaaa; const bbb bbbb;";
//-----------------------------------------------------------------
std::string::const_iterator first = data.begin(), last = data.end();
std::list<MyField> parsed_vals;
bool is_parsed;
try
{
is_parsed = qi::phrase_parse( first, last,
+(field > ';'),
boost::spirit::ascii::space, parsed_vals);
}
catch(const qi::expectation_failure<std::string::const_iterator>& e)
{
std::string frag(e.first, e.last);
std::cout << e.what() << "'" << frag << "'" << std::endl;
}
BOOST_ASSERT(is_parsed && "the example not parsed");
}
I wrote
+(field > ';')
because in my project this block can be absent. And each function declaration must be ended by ';'. When the above code sample runs I see that raised exception and in the console appeared record:
boost::spirit::qi::expectation_failure''
As I understand right - after last semicolon the parser tries parse the next field rule and it parsed the "matches" (because it return value in any case) but the next parse of the valid_symbols is failed because after the last semicolon there are no data. So parsing fails.
How to avoid the parse fails in my case?