I'm trying to learn the basics of Boost::Spirit, and its not going well. I'm trying to parse a simple "logical and" expression written in c++ syntax. And for some reason, I can't get the space skipping to work.
Here's my code so far
template <typename Iterator>
struct boolGrammar : public qi::grammar<Iterator, bool>
{
public:
boolGrammar() : boolGrammar::base_type(expression)
{
andExpr = (qi::lit(L"1") >> qi::lit(L"&&") >> qi::lit(L"1"))[qi::_val = true];
}
qi::rule<Iterator, bool> andExpr;
};
bool conditionEvalAndParse(std::wstring condition)
{
boolGrammar<std::wstring::iterator> g;
bool result = false;
std::wstring::iterator it = condition.begin();
bool parseResult = qi::phrase_parse(it, condition.end(), g, boost::spirit::standard_wide::space , result);
if (parseResult) {
return result;
}
else
{
std::wcout << L"Failed to parse condition " << condition << L". The following wasn't parsed : " << std::wstring(condition, it - condition.begin(), std::wstring::npos) << std::endl;
return false;
}
}
In my test code, I call :
conditionEvalAndParse(L"1&&1");
conditionEvalAndParse(L"1 && 1");
And sure enough, I get a lovely console output :
"Failed to parse condition 1 && 1. The following wasn't parsed : 1 && 1"
Anyone care to point out a newbie's mistake? :)