fparsec 中的位置信息
我的 AST 模型需要携带位置信息(文件名、行、索引)。有没有内置的方法来访问这些信息?从参考文档来看,流似乎带有位置,但我更希望不必实现虚拟解析器来保存位置,并将其添加到任何地方。
提前致谢
My AST model needs to carry location information (filename, line, index). Is there any built in way to access this information? From the reference docs, the stream seems to carry the position, but I'd prefer that I dont have to implement a dummy parser just to save the position, and add that everywhere.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解析器实际上是从流到回复的函数的类型缩写:
记住这一点,您可以轻松地为位置编写自定义解析器:
并将位置信息附加到使用
位置解析器解析的每个位,不会消耗任何输入,因此可以安全地以这种方式结合起来。
您可以将所需的代码更改限制在一行内,并避免无法控制的代码传播:
更新:FParsec 有一个预定义的位置解析器:
http://www.quanttec.com/fparsec/reference/charparsers.html #members.getPosition
Parsers are actually type abbreviations for functions from streams to replies:
Keeping that in mind, you can easily write a custom parser for positions:
and atttach position information to every bit you parse using
position parser does not consume any input and thus it is safe to combine in that way.
You can keep the code change required restricted to a single line and avoid uncontrollable code spread:
Update: FParsec has a predefined parser for positions:
http://www.quanttec.com/fparsec/reference/charparsers.html#members.getPosition