Dear Boosters,
The following code is one of my first attempts to use Spirit/Qi.
For my education, I have tried 2 techniques to parse the simple
"aaa=131;" string.
The first one (Block A) compiles and seems to output the expected result.
The second (B) does not compile with a huge massively templatized
backtrace that I don't understand.
What is the difference between using online rules and
rule objects ? Is there a syntax error somewhere ?
I'm afraid the documentation and example are rather terse about the
syntax to be used here when coupling Qi and Phoenix...
Many thanks for your help.
best regards
frc
--
<pre>
#include <iostream>
#include <string>
#include <vector>
// - Boost/Spirit
#include
#include
#include
int main( int argc_, char * argv_[])
{
int error_code = EXIT_SUCCESS;
using boost::spirit::qi::double_;
using boost::spirit::qi::string;
using boost::spirit::qi::lit;
using boost::spirit::qi::lexeme;
using boost::spirit::qi::phrase_parse;
using boost::spirit::qi::_1;
using boost::spirit::qi::as_string;
using boost::spirit::ascii::space;
using boost::spirit::ascii::char_;
using boost::phoenix::ref;
{ // Block A
/// This compiles:
std::string s = "aaa=131;";
std::cout << "s: '" << s << "'" << std::endl;
std::string::iterator strbegin = s.begin();
std::vector<char> vkey;
std::vector<char> vvalue;
bool r = phrase_parse(
strbegin,
s.end(),
(+(char_ - '='))[boost::phoenix::ref(vkey) =
boost::spirit::qi::_1 ]
>> '=' >> *space
>> lexeme[+(char_ -
';')][boost::phoenix::ref(vvalue) = boost::spirit::qi::_1 ],
space
);
if (r) {
std::string key(vkey.begin(), vkey.end());
std::string value(vvalue.begin(), vvalue.end());
std::cout << "key : '" << key << "'" << std::endl;
std::cout << "value : '" << value << "'" << std::endl;
}
}
{ /// Block B
/// This doesn't:
std::string s = "aaa=131;";
std::cout << "s: '" << s << "'" << std::endl;
std::string::iterator strbegin = s.begin();
std::vector<char> vkey;
std::vector<char> vvalue;
boost::spirit::qi::rulestd::string::iterator key_rule = +(char_
- '=');
boost::spirit::qi::rulestd::string::iterator value_rule =
lexeme[+(char_ - ';')];
bool r = phrase_parse(
strbegin,
s.end(),
key_rule[boost::phoenix::ref(vkey) =
boost::spirit::qi::_1 ]
>> '=' >> *space
>> value_rule[boost::phoenix::ref(vvalue) =
boost::spirit::qi::_1 ],
space
);
if (r) {
std::string key(vkey.begin(), vkey.end());
std::string value(vvalue.begin(), vvalue.end());
std::cout << "key : '" << key << "'" << std::endl;
std::cout << "value : '" << value << "'" << std::endl;
}
}
return error_code;
}
</pre>
--
François Mauger