I'd like to support something like C++'s #include
mechanism in a boost spirit parser. Essentially, I have a script command that asks my parser to load a sub script from a file. I'd like to be able to report error messages as described in the tracking input position while parsing post, but they don't cover parsing for multiple inputs.开发者_如何学C
Can this be reasonably accomplished using boost::spirit::qi
?
I've worked around getting the differing inputs in using a smarter iterator type. I'd still like to see accurate positioning though.
IMHO, using a smart iterator is the way to go. What needs to be done is to have a stack of input contexts maintained by the iterator. Each input context stores the information related to a specific file.
Whenever a new file needs to be read (i.e. after seeing an #include
statement) a new input context is created. The current input context gets pushed onto the stack, while the new context gets to be the active one. On EOF you pop the next input context from the stack, returning to the point right after the #include
. If the stack is empty you reached the EOF of the main file.
In any case, the iterator only gets its input from the active input context.
精彩评论