rpr 1.8.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +4 -4
- data/README.md +24 -3
- data/lib/rpr.rb +1 -1
- data/lib/rpr/formatter/dot.rb +92 -0
- data/lib/rpr/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d923de497b9fdd3e34eddd7abee262a84812bd7236e25bb7a5b304e2fdd78a08
|
4
|
+
data.tar.gz: 3dba086559d87976fc1ebf508efafcb676d997042e964890250cec3ef6990a62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 855dc01da970373f90a5f04345c9a721431c21ae1fbe063be8e5de4e4a0daae43144548208aa349a0b7e200069db88917d60c2871772156688af40a8a25fb94e
|
7
|
+
data.tar.gz: bd4b639388586ec71556215eaa81a4eda67b4719446983f0610fbf228d6fc7b5fd6ceaca220ff606dd4ec2b26d9906a15576a7d122c486eabbab7e36de60f65e
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -9,8 +9,14 @@ RPR displays Ruby's AST on command line.
|
|
9
9
|
## Support parsers
|
10
10
|
|
11
11
|
- [Ripper](http://ruby-doc.org/stdlib-2.3.0/libdoc/ripper/rdoc/Ripper.html)
|
12
|
-
-
|
12
|
+
- `sexp`
|
13
|
+
- `lex`
|
14
|
+
- `tokenize`
|
13
15
|
- [Parser](https://github.com/whitequark/parser)
|
16
|
+
- [RuboCop](https://github.com/bbatsov/rubocop)
|
17
|
+
- [RubyParser](https://github.com/seattlerb/ruby_parser)
|
18
|
+
- RubyVM::AbstractSyntaxTree (Since Ruby 2.6)
|
19
|
+
- [MinRuby](https://github.com/mame/minruby)
|
14
20
|
|
15
21
|
## Installation
|
16
22
|
|
@@ -46,7 +52,10 @@ $ rpr hello.rb
|
|
46
52
|
|
47
53
|
### Specify Parser
|
48
54
|
|
49
|
-
|
55
|
+
The default parser is `sexp`.
|
56
|
+
You can find all supported parsers with `--help` option.
|
57
|
+
|
58
|
+
For example:
|
50
59
|
|
51
60
|
```sh
|
52
61
|
$ rpr hello.rb --parser rubocop
|
@@ -61,7 +70,10 @@ $ rpr hello.rb --method tokenize
|
|
61
70
|
|
62
71
|
### Specify output formatter
|
63
72
|
|
64
|
-
|
73
|
+
The default formatter is `pp`.
|
74
|
+
You can find all supported formatters with `--help` option.
|
75
|
+
|
76
|
+
For example:
|
65
77
|
|
66
78
|
```sh
|
67
79
|
$ rpr hello.rb --formatter json
|
@@ -123,6 +135,15 @@ locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_
|
|
123
135
|
[3] pry(#<Array>)> exit
|
124
136
|
```
|
125
137
|
|
138
|
+
Print an AST as a PNG image with Graphviz.
|
139
|
+
|
140
|
+
```sh
|
141
|
+
$ rpr hello.rb -f dot | dot -Tpng -oast.png
|
142
|
+
$ open ast.png
|
143
|
+
```
|
144
|
+
|
145
|
+

|
146
|
+
|
126
147
|
### Configuration
|
127
148
|
|
128
149
|
Rpr searches config file from `./.rpr` or `~/.config/rpr`. `./.rpr` is priority.
|
data/lib/rpr.rb
CHANGED
@@ -0,0 +1,92 @@
|
|
1
|
+
module Rpr
|
2
|
+
module Formatter
|
3
|
+
module UnifiedInterface
|
4
|
+
refine Object do
|
5
|
+
def node_value() inspect end
|
6
|
+
def traversable?() false end
|
7
|
+
end
|
8
|
+
|
9
|
+
if defined?(Rpr::Parser::Sexp)
|
10
|
+
refine Array do
|
11
|
+
def node_value() traversable? ? self[0].to_s : super end
|
12
|
+
def children
|
13
|
+
res = []
|
14
|
+
self[1..-1].each do |child|
|
15
|
+
if child.is_a?(Array) &&
|
16
|
+
!child.traversable? &&
|
17
|
+
!(child.size == 2 && child[0].is_a?(Integer) && child[1].is_a?(Integer))
|
18
|
+
res.concat(child)
|
19
|
+
else
|
20
|
+
res << child
|
21
|
+
end
|
22
|
+
end
|
23
|
+
res
|
24
|
+
end
|
25
|
+
def traversable?() self.first.is_a?(Symbol) end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
if defined?(Rpr::Parser::Parser) || defined?(Rpr::Parser::Rubocop)
|
30
|
+
refine ::Parser::AST::Node do
|
31
|
+
def node_value() self.type.to_s end
|
32
|
+
def traversable?() true end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
if defined?(Rpr::Parser::Rubyparser)
|
37
|
+
refine Sexp do
|
38
|
+
def node_value() self.sexp_type.to_s end
|
39
|
+
def children() self.each.to_a end
|
40
|
+
def traversable?() true end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
if defined?(Rpr::Parser::Rubyvm_ast)
|
45
|
+
refine RubyVM::AbstractSyntaxTree::Node do
|
46
|
+
def node_value() self.type.to_s end
|
47
|
+
def traversable?() true end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Formatter for Graphviz
|
53
|
+
class Dot
|
54
|
+
using UnifiedInterface
|
55
|
+
|
56
|
+
def self.print(object)
|
57
|
+
self.new.print(object)
|
58
|
+
end
|
59
|
+
|
60
|
+
def initialize
|
61
|
+
@id = :a
|
62
|
+
end
|
63
|
+
|
64
|
+
def print(object)
|
65
|
+
puts 'digraph{graph [dpi=288;];'
|
66
|
+
traverse_and_print(object)
|
67
|
+
puts "}"
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def traverse_and_print(object, parent = 'ROOT')
|
73
|
+
label = object.node_value
|
74
|
+
shape = object.traversable? ? 'oval' : 'box'
|
75
|
+
id = gen_id()
|
76
|
+
|
77
|
+
puts "#{id} -> #{parent}"
|
78
|
+
puts "#{id}[ label=#{label.inspect} shape=#{shape} ]"
|
79
|
+
|
80
|
+
if object.traversable?
|
81
|
+
object.children.each do |child|
|
82
|
+
traverse_and_print(child, id)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def gen_id
|
88
|
+
@id = @id.succ
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/lib/rpr/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rpr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masataka Kuwabara
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-12-
|
11
|
+
date: 2018-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -228,6 +228,7 @@ files:
|
|
228
228
|
- exe/rpr
|
229
229
|
- lib/rpr.rb
|
230
230
|
- lib/rpr/config.rb
|
231
|
+
- lib/rpr/formatter/dot.rb
|
231
232
|
- lib/rpr/formatter/json.rb
|
232
233
|
- lib/rpr/formatter/pp.rb
|
233
234
|
- lib/rpr/formatter/pry.rb
|