Boost Spirit: Parse an int into a string.
-
I am trying to parse an expression like this:
F( 1, 2, 3)
and extract 1, 2 and 3 as a strings. My test grammar and program parse properly, but the output is not "nice", In the start, limit and increment variable contain strings like "\x1", "\x2" and "\x3" This is part of my grammar. (the pre tag does not work "good" here) I also tried to read the numerical value as an int and use a boost::variant to hold either an int or std:wstring, but that did not work. Any thoughts, hints or tips ? Thanks. More friendly code here http://pastebin.com/2mmK9gns
struct RangeResult
{
std::wstring start;
std::wstring limit;
std::wstring increment;
};BOOST_FUSION_ADAPT_STRUCT(
RangeGrammar::RangeResult,
(std::wstring, start)
(std::wstring, limit)
(std::wstring, increment)
)template struct range_parser : boost::spirit::qi::grammar
{
range_parser() : range_parser::base_type(start)
{
using qi::lit;
using qi::lexeme;
using qi::int_;
using iso8859_1::char_;
using iso8859_1::digit;variable\_string %= lexeme\["$(" >> +(char\_ - ")") >> ")"\]; node = (int\_ | variable\_string); start %= lit("\_RANGE") >> '(' >> node >> ',' >> node >> ',' >> node >> ")\_" ; debug(variable\_string); debug(start); } qi::rule variable\_string; qi::rule node; qi::rule start;
};
My test program:
void TestRangeGrammar()
{
using boost::spirit::iso8859_1::space;
typedef std::wstring::const_iterator iterator_type;
typedef RangeGrammar::range_parser range_parser;range\_parser grammar; // Our grammar std::array< std::wstring, 1 > testStrings = {{ \_TEXT("\_RANGE(1, 2, 3 )\_") }}; for (auto str : testStrings) { std::wstring::const\_iterator iter = str.begin(); std::wstring::const\_iterator end = str.end(); RangeGrammar::RangeResult emp; bool r = phrase\_parse(iter, end, grammar, space, emp); if (r) { std::wcout << \_TEXT("-------------------------\\n"); std::wcout << \_TEXT("Range Parsing succeeded\\n"); std::wcout << \_TEXT("Start: ") << emp.start << std::endl; std::wcout << \_TEXT("Start: ") << emp.limit << std::endl; std::wcou