RPR
RPR displays Ruby’s AST on command line.
Support parsers
- Ripper
sexp
lex
tokenize
- Parser
- RuboCop
- RubyParser
- RubyVM::AbstractSyntaxTree (Since Ruby 2.6)
- MinRuby
Installation
Add this line to your application’s Gemfile:
“by gem ‘rpr’
“
And then execute:
$ bundle
Or install it yourself as:
$ gem install rpr
Usage
Basic Usage
“ $ echo ‘puts “Hello world”’ > hello.rb $ rpr hello.rb [:program, [[:command, :@ident, puts, 1, 0, :args_add_block, [[:string_literal, :string_content, :@tstring_content, Hello world, 1, 6], false]]]]
“
Specify Parser
The default parser is sexp
. You can find all supported parsers with --help
option.
For example:
“ $ rpr hello.rb –parser rubocop s(:send, nil, :puts, s(:str, “Hello world”))
“
“ $ rpr hello.rb –method tokenize [puts, , , Hello world, , \n]
“
Specify output formatter
The default formatter is pp
. You can find all supported formatters with --help
option.
For example:
“ $ rpr hello.rb –formatter json [program, command, @ident, puts, 1, 0, args_add_block, string_literal, string_content, @tstring_content, Hello world, 1, 6, false]
“
Inspect an AST with pry.
“ $ rpr hello.rb –formatter pry
[1] pry(#Maruku could not parse this XML/HTML:
<Array>)> self
Maruku could not parse this XML/HTML:
<Array>)> self
[2] pry(#
Maruku could not parse this XML/HTML:
<Array>)> ls
Enumerable#methods:
all? chunk_while detect each_entry each_with_index entries find_all grep group_by lazy max_by min minmax none? partition slice_after slice_when
chunk collect_concat each_cons each_slice each_with_object find flat_map grep_v inject max member? min_by minmax_by one? reduce slice_before sort_by
Array#methods:
&
“
Print an AST as a PNG image with Graphviz.
“ $ rpr hello.rb -f dot | dot -Tpng -oast.png $ open ast.png
“
Configuration
Rpr searches config file from ./.rpr
or ~/.config/rpr
. ./.rpr
is priority.
Config file is space separated options.
Example.
./.rpr
or ~/.config/rpr
“f pry
“
Development
After checking out the repo, run bin/setup
to install dependencies. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/pocke/rpr.
Links
- Ripperをコマンドラインから簡単に使うラッパー rpr を作った - pockestrap (Japanese Blog)
License
These codes are licensed under CC0.