[xpressive] Using placeholder in semantic actions
Hi! I'm trying to use xpressive with semantic actions that use a placeholder, but I can't quite see the right way to do it. Here's my code: namespace grammar { using namespace boost::xpressive; struct ParsedUri { std::string hostname; std::string modelPath; std::string diagramPath; }; static placeholder<ParsedUri> result; static const mark_tag model(1), diagram(2), host(1); static const char pathSep = '/'; static sregex unreserved = set [ alpha | digit | '-' | '.' | '_' | '~' | ' ' | ':' | '$' ]; static sregex pctEncoded = as_xpr('%') >> xdigit >> xdigit; static sregex pathElem = *(unreserved | pctEncoded); static sregex path = pathElem | *(pathElem >> pathSep >> by_ref(path)); static sregex drive = alpha >> ':'; static sregex quad = repeat<1,3>(digit) [ check(as<int>(_) >= 1 && as<int>(_) <= 255) ]; static sregex ipv4 = repeat<3>(quad >> '.') >> quad; static sregex hostname = !(ipv4 | pathElem); static sregex pathNoHost = pathSep >> drive >> pathSep >> path; static sregex pathHost = (host= hostname) >> pathSep >> path [ result.hostname = host ]; static sregex modelPath = pathNoHost | pathHost static sregex uri = "rosert://" >> (model= modelPath) >> '?' >> (diagram= path) [ result.modelPath = model, result.diagramPath = diagram ]; } What happens is that when I compile the code, I get a compile error for the lines accessing the placeholder saying that the various members of the structure are not members of placeholder<T>, which looking at regex_actions.hpp seems to make sense to me. But in the example using placeholder<T> in the docs, it looks like you can use it in the semantic action as if it were a T. Somewhere I'm misunderstanding something, and I don't know what. Can anyone help me here? -- Gregory Symons Software Engineer ITT Space Systems Division greg.symons@itt.com (260) 451-6402 This e-mail and any files transmitted with it may be proprietary and are intended solely for the use of the individual or entity to whom they are addressed. If you have received this e-mail in error please notify the sender. Please note that any views or opinions presented in this e-mail are solely those of the author and do not necessarily represent those of ITT Corporation. The recipient should check this e-mail and any attachments for the presence of viruses. ITT accepts no liability for any damage caused by any virus transmitted by this e-mail.
Symons, Greg - SSD wrote:
Hi!
I'm trying to use xpressive with semantic actions that use a placeholder, but I can't quite see the right way to do it. Here's my code:
namespace grammar { using namespace boost::xpressive;
struct ParsedUri { std::string hostname; std::string modelPath; std::string diagramPath; };
static placeholder<ParsedUri> result; <snip>
static sregex pathHost = (host= hostname) >> pathSep >> path [ result.hostname = host ]; static sregex modelPath = pathNoHost | pathHost static sregex uri = "rosert://" >> (model= modelPath) >> '?' >> (diagram= path) [ result.modelPath = model, result.diagramPath = diagram ]; }
What happens is that when I compile the code, I get a compile error for the lines accessing the placeholder saying that the various members of the structure are not members of placeholder<T>, which looking at regex_actions.hpp seems to make sense to me.
Right.
But in the example using placeholder<T> in the docs, it looks like you can use it in the semantic action as if it were a T. Somewhere I'm misunderstanding something, and I don't know what. Can anyone help me here?
Xpressive overloads operators to give the impression that your can use a placeholder<T> as a T. But xpressive cannot overload operator. to give the impression of data member access because in C++ you can't overload operator.. Try this instead... xpressive::function<std::string ParsedUri::*>::type getHostname = {&ParsedUri::hostname}; Now in your semantic actions, instead of doing this: [ result.hostname = host ] You can do this: [ result->*getHostname() = host ] Xpressive overloads operator->*, which is the closest thing to operator.. Note to self: it would be better if we could leave off the parens in the above, like this: [ result->*getHostname = host ]. There's always more to do. HTH, -- Eric Niebler BoostPro Computing http://www.boostpro.com
Eric Niebler wrote: [snip]
Xpressive overloads operators to give the impression that your can use a placeholder<T> as a T. But xpressive cannot overload operator. to give the impression of data member access because in C++ you can't overload operator..
Oh yeah... that would do it:) I'm glad you saw that right away, 'cause it would've taken me forever to wade through the source to figure out what I was doing wrong.
Try this instead...
xpressive::function<std::string ParsedUri::*>::type getHostname = {&ParsedUri::hostname};
Now in your semantic actions, instead of doing this:
[ result.hostname = host ]
You can do this:
[ result->*getHostname() = host ]
Xpressive overloads operator->*, which is the closest thing to operator..
Note to self: it would be better if we could leave off the parens in the above, like this: [ result->*getHostname = host ]. There's always more to do.
Thanks for the tip, it worked perfectly! Oh, and being able to leave off the parens would definitely be cool. The more I use xpressive, the more I really come to like it:) -- Greg Symons Software Engineer ITT Corporation Space Systems Division greg.symons@itt.com (260) 451-6402 This e-mail and any files transmitted with it may be proprietary and are intended solely for the use of the individual or entity to whom they are addressed. If you have received this e-mail in error please notify the sender. Please note that any views or opinions presented in this e-mail are solely those of the author and do not necessarily represent those of ITT Corporation. The recipient should check this e-mail and any attachments for the presence of viruses. ITT accepts no liability for any damage caused by any virus transmitted by this e-mail.
participants (2)
-
Eric Niebler
-
Symons, Greg - SSD