Treetop:获取节点的偏移量
我正在使用 Treetop 为小型编程语言生成解析器。
解析成功后,我想对语法树进行一些语义分析。理想情况下,每当我遇到一段(语义上)无效的代码时,我想打印出一条错误消息,其中包括发生错误的行。我知道这是可能的,因为如果我这样做,
parser = MyParser.new
tree = parser.parse("foobar")
p tree
我会得到类似的内容
SyntaxNode offset=0, "foobar":
SyntaxNode offset=0, "f"
SyntaxNode offset=1, "o"
SyntaxNode offset=2, "o"
SyntaxNode offset=3, "b"
SyntaxNode offset=4, "a"
SyntaxNode offset=5, "r"
本质上,我想要一种访问给定 SyntaxNode
对象(或其子类)的 offset
属性的方法。不幸的是,根据 http://treetop.rubyforge.org/semantic_interpretation.html 唯一可用的方法Treetop::Runtime::SyntaxNode
上有 terminal?
、nonterminal?
、text_value
和 elements< /code>,所以似乎没有内置的方法可以做到这一点。
I'm using Treetop to generate a parser for a small programming language.
Upon successful parsing, I'd like to do some semantic analysis on the syntax tree. Ideally, whenever I encounter a piece of (semantically) invalid code, I would like to print out an error message that includes the line where the error occurred. I know this is possible because if I do
parser = MyParser.new
tree = parser.parse("foobar")
p tree
I get something like
SyntaxNode offset=0, "foobar":
SyntaxNode offset=0, "f"
SyntaxNode offset=1, "o"
SyntaxNode offset=2, "o"
SyntaxNode offset=3, "b"
SyntaxNode offset=4, "a"
SyntaxNode offset=5, "r"
Essentially, I'd like a way to access the offset
attribute of a given SyntaxNode
object (or subclass thereof). Unfortunately, according to http://treetop.rubyforge.org/semantic_interpretation.html the only methods available on Treetop::Runtime::SyntaxNode
are terminal?
, nonterminal?
, text_value
and elements
, so there doesn't seem to be a built-in way of doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个 SyntaxNode 都有一个“interval”方法,它是一个 Ruby Range 对象。偏移量是interval.start。
Treetop 的支持邮件列表位于 http://groups.google.com/group/treetop-dev
Each SyntaxNode has an "interval" method which is a Ruby Range object. Offset is interval.start.
Treetop's support mailing list is at http://groups.google.com/group/treetop-dev