[英]How would I recompile Ripper's AST back to Ruby code?
Ripper is the the parsing library that ships with Ruby 1.9. Ripper是Ruby 1.9附带的解析库。 It transforms Ruby code into an AST, like so: 它将Ruby代码转换为AST,如下所示:
pp Ripper.sexp("def foo; yield :a; return 1 end")
#=>
[:program,
[[:def,
[:@ident, "foo", [1, 4]],
[:params, nil, nil, nil, nil, nil],
[:bodystmt,
[[:yield,
[:args_add_block,
[[:symbol_literal, [:symbol, [:@ident, "a", [1, 16]]]]],
false]],
[:return, [:args_add_block, [[:@int, "1", [1, 26]]], false]]],
nil,
nil,
nil]]]]
Is there a library to take this AST and transform it back into Ruby code? 是否有一个库可以使用这个AST并将其转换回Ruby代码?
ruby_parser and ruby2ruby used to do this, but I would like to use Ripper as my parser. ruby_parser和ruby2ruby曾经这样做,但我想使用Ripper作为我的解析器。 (Ruby 1.9 may even ship with such a library, but I'm struggling to find documentation even on Ripper itself) (Ruby 1.9甚至可能附带这样的库,但是我甚至在Ripper本身也很难找到文档)
See " Sorcerer ". 见“ 巫师 ”。 This works well but I found a bug when parsing methods. 这很好但我在解析方法时发现了一个错误。 If you add src.emit("; ")
below the line 301 of the file "lib/sorcerer/resource.rb", this will be fixed. 如果在文件“lib / sorcerer / resource.rb”的第301行下面添加src.emit("; ")
,则会修复此问题。 But you may find more if you decide to use this. 但如果你决定使用它,你可能会发现更多。 Good luck. 祝好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.