我想在 boost Spirit 解析器中支持 C++ 的 #include
机制。本质上,我有一个脚本命令,要求我的解析器从文件加载子脚本。我希望能够报告错误消息,如 在解析时跟踪输入位置 post,但它们不涵盖多个输入的解析。
使用 boost::spirit::qi
可以合理地实现这一点吗?
我一直致力于使用更智能的迭代器类型来获取不同的输入。不过我还是希望看到准确的定位。
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)
恕我直言,使用智能迭代器是正确的选择。需要做的是让迭代器维护一个输入上下文堆栈。每个输入上下文存储与特定文件相关的信息。
每当需要读取新文件时(即在看到
#include
语句后),都会创建一个新的输入上下文。当前输入上下文被压入堆栈,而新上下文将成为活动上下文。在 EOF 上,您从堆栈中弹出下一个输入上下文,返回到#include
之后的位置。如果堆栈为空,则到达主文件的 EOF。在任何情况下,迭代器仅从活动输入上下文获取输入。
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.