I'm trying to implement a rule in my boost spirit qi grammar that will behave like QUOTE in a Lisp-like language.
Something like: QUOTE(a b c)
The idea is that anything between QUOTE's opening and closing parenthesis will be captured into a string literal, rather than being parsed any further.
Since the '(' or ')' character may be present inside of a QUOTE, I can't use: '\"' >> *~char_('\"') >> '\"' and instead need to keep track of parenthesis to determine the end-of-quote.
For example: QUOTE(a ( b c ) d) Here, I would want to parse the contents "a ( b c ) d" as a string literal.
I can think of a few ways to do this, the general idea being to keep a local variable to increment/decrement the parenthesis depth, etc.
Due to the inefficiency of semantic actions, I was hoping someone might comment an efficient way to approach this problem.
Thanks!
There's qi::raw[]
for this.
It exposes the source iterator range, so you can decide to copy or construct, e.g., a boost::string_ref
from it. It doesn't get much more efficient than this.
For example:
qi::rule<It, std::string()> demo =
qi::raw [
qi::int_ >> '{' >> (qi::double_ % ',') >> '}'
];