[proto] Handling String Terminals

If I have a grammar like this: typedef boost::proto::literalstd::string::type StringTerminal; typedef or_< StringTerminal, ...
Rule;
and I do this: checkMatch<Rule>(boost::proto::lit(std::string("str"))); checkMatch<Rule>(std::string("str")); checkMatch<Rule>("str"); where checkMatch is a wrapper around matches<>, the first line is the only one that passes. This is presumably because the string needs to be "protoized" and to match StringTerminal it has to be cast to std::string explicitly. This is rather ugly for constructs like this: term["str"] => term[lit(string("str"))] I would really rather not have to do the lit(string()) thing. Is there a good strategy to avoid this clutter? I could predefine literals like this: StringTerminal strlit = {{"str"}}. term[strlit] but that's a bit burdensome on the user. Would the same massaging be needed for, say, term + "strlit" Perhaps I can reformulate the grammar to avoid the cluttter by using constructs that promote constants to proper proto terminals. Any advice? Thanks! -Dave

On 2/8/2010 9:05 AM, David A. Greene wrote:
If I have a grammar like this:
typedef boost::proto::literalstd::string::type StringTerminal;
typedef or_< StringTerminal, ...
Rule;
and I do this:
checkMatch<Rule>(boost::proto::lit(std::string("str"))); checkMatch<Rule>(std::string("str")); checkMatch<Rule>("str");
where checkMatch is a wrapper around matches<>, the first line is the only one that passes. This is presumably because the string needs to be "protoized" and to match StringTerminal it has to be cast to std::string explicitly. <snip>
proto::convertible_to is your friend here, as well as proto::as_expr.
See below:
#include

On Sunday 07 February 2010 16:45:05 Eric Niebler wrote:
As its name implies, proto::convertible_to matches terminal types that have an implicit conversion. proto::result_of::as_expr converts non-proto types to proto::terminals and leaves proto expression types alone.
Ah, cool. I think these are exactly what I need. Thanks. -Dave
participants (3)
-
David A. Greene
-
David Greene
-
Eric Niebler