I have this, in my boost::spirit grammar;
paren = (qi::token(LEFT_PAREN) >> character >> qi::token(RIGHT_PAREN)) [ build_paren ]
;
character = qi::token(CHARACTER) [ build_character]
;
Where these are defined as;
qi::rule<Iterator> paren;
qi::rule<Iterator, char> character;
The function build_paren
, has the following prototype (found via compiler cast error);
void build_paren(boost::fusion::vector2<boost::iterator_range<__gnu_cxx::__normal_iterator<char*, std::basic_string<char>>>, boost::iterator_range<__gnu_cxx::__normal_iterator<char*, std::basic_string<char>>>> v)
Here the vector, holds two strings, respectively "(\0"
and ")\0"
, this is as I would expect, however how do I get the char
matched in character?
Really, the prototype I'd like for my build_paran
function is;
void build_paren(std::string left_paren, char character, std::string right_paren)
Or alternatively, the same, however with the char
argument as the last one in the list.
You don't have to work that hard :)
Spirit has automatic attribute propagation. Actually, I'd say that is it's main selling feature. So you can:
This will simply bind the exposed attribute of the
char_
parser component to the attribute reference (parsed_char
) passed into the variadic parsing API (phrase_parse
).Below is a generalized demonstration, showing the many ways in which you can influence what exactly gets exposed. Exactly what gets exposed is documented with the parser directives, e.g. here, for the '%' list parser.
For your specific question, you'd want to simply:
Demonstrations
See it Live on Coliru: