使用 Ruby 解析 Tcl DSL

发布于 2024-11-02 10:32:52 字数 937 浏览 8 评论 0原文

我希望能够解析一些参数没有被字符串包围的 Tcl 代码。

考虑这个 tcl 代码:

proc foo {name} {
  puts "Foo --> $name"
}    

foo bar

对于那些不熟悉 Tcl 的人来说,foo 是方法名称,bar 是参数(引号在 Tcl 中是可选的)。

前面的代码将输出:

Foo --> bar

Is it possible to parse exactly using ruby​​ (bar still unquoted)?

等效的 ruby​​ 代码是:

def foo(name)
  puts "Foo --> #{name}"
end

tcl = <<-TCL.gsub(/^\s+/, "").chop
  foo bar
TCL
instance_eval(tcl)

当然,当它到达 bar 时会失败,因为它预计会被引用。

我尝试过修改 method_missing

def method_missing(meth, *args)
    puts meth.to_s + " --> args.to_s
end

但它以相反的顺序解析:

to_hash --> []
bar --> []
foo --> [nil]

是否有人对此类问题有一个干净的解决方案。我想避免对字符串进行标记,因为与词法分析相比,通过调用方法读取数据所需的工作量最少。但如果我想做一些不可能的事情,我想知道。谢谢。

I would like to be able to parse some Tcl code where arguments are not surrounded by strings.

Consider this tcl code:

proc foo {name} {
  puts "Foo --> $name"
}    

foo bar

For those unfamiliar with Tcl, foo is the method name and bar is the argument (quotes are optional in Tcl).

The previous code will output:

Foo --> bar

Is it possible to parse exactly the same input using ruby (bar remains unquoted)?

The equivalent ruby code is:

def foo(name)
  puts "Foo --> #{name}"
end

tcl = <<-TCL.gsub(/^\s+/, "").chop
  foo bar
TCL
instance_eval(tcl)

Of course that fails when it reaches bar since it's expected it to be quoted.

I've tried tinkering with method_missing

def method_missing(meth, *args)
    puts meth.to_s + " --> args.to_s
end

but it parses in reverse order:

to_hash --> []
bar --> []
foo --> [nil]

Does anyone have a clean solution to this type of problem. I'd like to avoid tokenizing the strings since reading the data in by calling a method requires minimal work compared to lexical analysis. But if I'm trying to do something that's not possible, I'd like to know. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

花之痕靓丽 2024-11-09 10:32:52

它对你不起作用,因为 .puts 方法返回 nil 而不是字符串:

irb(main):003:0> puts "42"
42
=> nil

我真的不知道为什么 to_hash 出现在这个 < code>method_missing 但它有效:

def foo(name)
  puts "Foo --> #{name}"
end

def method_missing(meth, *args)
     meth.to_s unless meth.to_s  == "to_hash"
end

tcl = <<-TCL.gsub(/^\s+/, "").chop
  foo bar
TCL

instance_eval(tcl)

=> Foo --> bar

It's doesn't work for you because .puts method returns nil instead of string:

irb(main):003:0> puts "42"
42
=> nil

I really don't know why to_hash appears in this method_missing but it works:

def foo(name)
  puts "Foo --> #{name}"
end

def method_missing(meth, *args)
     meth.to_s unless meth.to_s  == "to_hash"
end

tcl = <<-TCL.gsub(/^\s+/, "").chop
  foo bar
TCL

instance_eval(tcl)

=> Foo --> bar
柏林苍穹下 2024-11-09 10:32:52

这是一个尝试使语法接近TCL语法的实现。

class TCL
  class << self
    alias run instance_eval
    def proc(n, &b)
      self.class.__send__(:define_method, n, &b)
    end
    def method_missing(n, *a, &b)
      n.to_s
    end
  end
end

TCL.run do

  proc(:foo) { |name|
    puts "Foo --> #{name}"
  }

  foo bar

end

# prints Foo --> bar

This is an implementation that try to make the syntax near to the TCL syntax.

class TCL
  class << self
    alias run instance_eval
    def proc(n, &b)
      self.class.__send__(:define_method, n, &b)
    end
    def method_missing(n, *a, &b)
      n.to_s
    end
  end
end

TCL.run do

  proc(:foo) { |name|
    puts "Foo --> #{name}"
  }

  foo bar

end

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