访问与 Treetop 中的节点关联的方法

发布于 2024-11-14 02:22:43 字数 907 浏览 3 评论 0原文

使用如下定义的语法,为什么我在尝试访问由规则 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虫児飞 2024-11-21 02:22:43

您可以通过 text_value 访问规则本身的内容。语法:

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
        text_value
      end
    }
  end

  rule value
    ('\\"' / [^"])+ {
      def val
        text_value
      end
    }
  end

  rule space
    [ \t]+
  end

end

可以使用: 进行测试

require 'rubygems'
require 'treetop'
require 'polyglot'
require 'command'

parser = CommandParser.new
pair = parser.parse('create name = "foobar"').val
print pair['name'], "\n"

,并将打印:

foobar

到控制台。

You can access the contents of the rule itself through text_value. The grammar:

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
        text_value
      end
    }
  end

  rule value
    ('\\"' / [^"])+ {
      def val
        text_value
      end
    }
  end

  rule space
    [ \t]+
  end

end

which can be tested with:

require 'rubygems'
require 'treetop'
require 'polyglot'
require 'command'

parser = CommandParser.new
pair = parser.parse('create name = "foobar"').val
print pair['name'], "\n"

and will print:

foobar

to the console.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文