使用如下定义的语法,为什么我在尝试访问val
由规则创建的节点的方法时总是出错key
?
错误信息是
(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