The code below (adapted from spirit qi mini_xml example) does not compile. There is an error related to the rule brac that has an attribute of an recursive
I'd suggest the problem is Attribute compatibility. Contrary to the documentation, the ascii::string parser appears to expose an iterator range instead of a string.
name = string("no way");
is no issue, because the attribute exposed by ascii::string can be coerced into the rule's attribute type without any difficulty.
However, the brac rule's attribute type is ast_branch, which is only a variant with one of it's possible contained types. Therefore, the ast_branch type has several constructors, and it isn't clear to Spirit which one is adequate for this particular conversion.
There are several ways about this (besides the approaches you already showed):
use attr_cast
brac = qi::attr_cast( string("no way") );
use as_string
brac = qi::as_string[ string("no way") ];
use customization points
namespace boost { namespace spirit { namespace traits {
template
struct assign_to_attribute_from_iterators
{
static void call(It const& f, It const& l, client::ast_branch& val)
{
val = std::string(f, l);
}
};
}}}
Each of these have the same effect: make Spirit realize what attribute conversion to use.
Here is a full working sample showing all three:
// #define BOOST_SPIRIT_ACTIONS_ALLOW_ATTR_COMPAT
#include
#include
#include
#include
#include
#include
#include
namespace client
{
namespace fusion = boost::fusion;
namespace phoenix = boost::phoenix;
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
struct ast_node;
typedef boost::variant<
boost::recursive_wrapper,
std::string
> ast_branch;
struct ast_node
{
std::string text;
std::vector children;
};
}
namespace boost { namespace spirit { namespace traits {
template
struct assign_to_attribute_from_iterators
{
static void call(It const& f, It const& l, client::ast_branch& val)
{
val = std::string(f, l);
}
};
}}}
BOOST_FUSION_ADAPT_STRUCT(
client::ast_node,
(std::string, text)
(std::vector, children)
)
namespace client
{
template
struct ast_node_grammar : qi::grammar
{
ast_node_grammar()
: ast_node_grammar::base_type(brac)
{
using qi::_1;
using qi::_val;
using ascii::char_;
using ascii::string;
name %= *char_;
brac = string("works");
brac = string("works")[_val = _1] ;
brac %= string("this") | string("works");
brac %= name ; // works
brac %= *char_ ; // works
brac = qi::as_string[ string("no way") ];
brac = qi::attr_cast( string("no way") );
}
qi::rule name;
qi::rule brac;
};
}
int main(int argc, char **argv)
{
typedef client::ast_node_grammar ast_node_grammar;
ast_node_grammar gram;
client::ast_branch ast;
std::string text("dummy");
using boost::spirit::ascii::space;
std::string::const_iterator iter = text.begin();
std::string::const_iterator end = text.end();
bool r = phrase_parse(iter, end, gram, space, ast);
return r ? 0 : 1;
}