访问与 Treetop 中的节点关联的方法
使用如下定义的语法,为什么我在尝试访问由规则 key
创建的节点的 val
方法时总是出现错误?
错误消息是
(eval):168:in `val': undefined local variable or method `key'
for #<Treetop::Runtime::SyntaxNode:0x00000101b1e160> (NameError)
语法是
grammar Command
rule create_command
'create' space pair {
def val
pair.val
end
}
end
rule pair
key space? '=' space? '"' value '"' {
def val
{ key.val => value.val }
end
}
end
rule key
[A-Za-z_] [A-Za-z0-9_]* {
def val
key.to_sym
end
}
end
rule value
('\\"' / [^"])+ {
def val
value.to_s
end
}
end
rule space
[ \t]+
end
end
测试代码是
require 'treetop'
Treetop.load "command"
p = CommandParser.new
r = p.parse 'create name = "foobar"'
p r.val
With the grammar defined as below, why I keep get error while try to access the val
method of nodes created by rule key
?
The error message is
(eval):168:in `val': undefined local variable or method `key'
for #<Treetop::Runtime::SyntaxNode:0x00000101b1e160> (NameError)
The grammar is
grammar Command
rule create_command
'create' space pair {
def val
pair.val
end
}
end
rule pair
key space? '=' space? '"' value '"' {
def val
{ key.val => value.val }
end
}
end
rule key
[A-Za-z_] [A-Za-z0-9_]* {
def val
key.to_sym
end
}
end
rule value
('\\"' / [^"])+ {
def val
value.to_s
end
}
end
rule space
[ \t]+
end
end
The test code is
require 'treetop'
Treetop.load "command"
p = CommandParser.new
r = p.parse 'create name = "foobar"'
p r.val
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过
text_value
访问规则本身的内容。语法:可以使用: 进行测试
,并将打印:
到控制台。
You can access the contents of the rule itself through
text_value
. The grammar:which can be tested with:
and will print:
to the console.