How can I track my input position with multiple inputs using Boost::Spirit::Qi?

徘徊边缘 提交于 2019-12-10 19:27:45

问题


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.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/3146372/how-can-i-track-my-input-position-with-multiple-inputs-using-boostspiritqi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!