Boost.Spirit Karma: Alternative generator
Hello,
I am construction karma::rule
I am construction karma::rule
which I would like to be either a string or quoted string if the string contains spaces. Now I think that the rule should be something like:
rule = (&true[...] << string) | (lit("\"") << string << lit("\""));
but I've failed to write proper semantic action. How could I obtain std::string object reference to write something like:
(obj.find(" ") == std::string::npos)
? Or maybe is there a better way to do the same?
Please keep in mind that semantic actions are executed 'lazily'. That means that you need to use a function object which executes the semantic code in its operator()(). The easiest way is to use phoenix::function for that. Please see the corresponding documentation. Regards Hartmut --------------- http://boost-spirit.com http://stellar.cct.lsu.edu
01.03.2016 17:51, Hartmut Kaiser пишет:
I am construction karma::rule
which I would like to be either a string or quoted string if the string contains spaces. Now I think that the rule should be something like:
rule = (&true[...] << string) | (lit("\"") << string << lit("\""));
but I've failed to write proper semantic action. How could I obtain std::string object reference to write something like:
(obj.find(" ") == std::string::npos)
? Or maybe is there a better way to do the same?
Please keep in mind that semantic actions are executed 'lazily'. That means that you need to use a function object which executes the semantic code in its operator()(). The easiest way is to use phoenix::function for that. Please see the corresponding documentation.
Could this be implemented using char_ like the following? rule = (repeat[~char_(' ')]) | (lit("\"") << string << lit("\"")); The documentation says about repeat[a] that: "This generator succeeds as long as its embedded generator a does not fail" Unfortunately, I've failed to make first term (repeat[...]) fail on spaces.
Regards Hartmut --------------- http://boost-spirit.com http://stellar.cct.lsu.edu
participants (2)
-
Hartmut Kaiser
-
Matwey V. Kornilov