Question

How can I setup a rule that returns pre-defined output rather than something parsed from my input text?

Like this example: GiveQuoteOrText will first try to parse the input as a quoted string using Quoted, and if that fails should always output "Text!" regardless of the input.

qi::rule<Iterator, std::string()> GiveQuoteOrText;
qi::rule<Iterator, std::string()> Quoted;

Quoted %= '"' >> *(char_ - '"') > '"';
GiveQuoteOrText %= Quoted | OUTPUT("Text!"); //made up an OUTPUT command

Is this possible? Is there a way to control how the iterator advances when it is done?

Was it helpful?

Solution

GiveQuoteOrText %= Quoted | qi::attr("Text!"); 

If the object is more involved you can use qi::eps:

rule = qi::eps[qi::_val = phx::construct<MyObject>(args)]

It is in each case just a succeeding rule without parsing anything, so the iterator doesn't move. If you want it to move you need to parse the respective part and use qi::omit to block the attributes (if there are any).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top