[Awesome Ruby Gem - Abstract Syntax Tree (AST)] Use unparser gem to produce equivalent source code from Parser's ASTs
parser
unparser generate equivalent source for ASTs from parser. You can also use unparser to produce equivalent source code from Parser’s ASTs whitequark/parser: A Ruby parser. - https://github.com/whitequark/parser.
Installation
You can install it as a gem:
1 | gem install unparser |
or add it into a Gemfile (Bundler):
1 | # Gemfile |
Then, run bundle install
.
1 | bundle install |
Usage
1 | require 'parser/current' |
To preserve the comments from the source:
1 | require 'parser/current' |
Passing in manually constructed AST:
1 | require 'parser/current' |
Note: DO NOT attempt to pass in nodes generated via AST::Sexp#s
, these ones return API incompatible AST::Node
instances, unparser needs Parser::AST::Node
instances.
Equivalent vs identical:
1 | require 'unparser' |
Summary: unparser does not reproduce your source! It produces equivalent source.
References
[2] unparser | RubyGems.org | your community gem host - https://rubygems.org/gems/unparser
[3] whitequark/parser: A Ruby parser. - https://github.com/whitequark/parser