astree 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -4
- data/README.md +28 -28
- data/astree.gemspec +1 -1
- data/lib/astree/pretty_node/args.rb +26 -0
- data/lib/astree/pretty_node/array.rb +5 -0
- data/lib/astree/pretty_node/attrasgn.rb +10 -0
- data/lib/astree/pretty_node/back_ref.rb +8 -0
- data/lib/astree/pretty_node/begin.rb +8 -0
- data/lib/astree/pretty_node/block.rb +5 -0
- data/lib/astree/pretty_node/block_pass.rb +8 -0
- data/lib/astree/pretty_node/break.rb +8 -0
- data/lib/astree/pretty_node/call.rb +10 -0
- data/lib/astree/pretty_node/case2.rb +8 -0
- data/lib/astree/pretty_node/cdecl.rb +10 -0
- data/lib/astree/pretty_node/class.rb +8 -0
- data/lib/astree/pretty_node/colon2.rb +10 -0
- data/lib/astree/pretty_node/colon3.rb +8 -0
- data/lib/astree/pretty_node/const.rb +8 -0
- data/lib/astree/pretty_node/cvar.rb +8 -0
- data/lib/astree/pretty_node/cvasgn.rb +10 -0
- data/lib/astree/pretty_node/dasgn.rb +10 -0
- data/lib/astree/pretty_node/dasgn_curr.rb +10 -0
- data/lib/astree/pretty_node/defn.rb +8 -0
- data/lib/astree/pretty_node/defs.rb +8 -0
- data/lib/astree/pretty_node/dregx.rb +10 -0
- data/lib/astree/pretty_node/dstr.rb +10 -0
- data/lib/astree/pretty_node/dsym.rb +10 -0
- data/lib/astree/pretty_node/dvar.rb +8 -0
- data/lib/astree/pretty_node/dxstr.rb +10 -0
- data/lib/astree/pretty_node/fcall.rb +10 -0
- data/lib/astree/pretty_node/gasgn.rb +10 -0
- data/lib/astree/pretty_node/gvar.rb +8 -0
- data/lib/astree/pretty_node/hash.rb +5 -0
- data/lib/astree/pretty_node/iasgn.rb +10 -0
- data/lib/astree/pretty_node/if.rb +10 -0
- data/lib/astree/pretty_node/ivar.rb +8 -0
- data/lib/astree/pretty_node/kw_arg.rb +8 -0
- data/lib/astree/pretty_node/lasgn.rb +10 -0
- data/lib/astree/pretty_node/lit.rb +8 -0
- data/lib/astree/pretty_node/lvar.rb +8 -0
- data/lib/astree/pretty_node/masgn.rb +12 -0
- data/lib/astree/pretty_node/match.rb +8 -0
- data/lib/astree/pretty_node/next.rb +8 -0
- data/lib/astree/pretty_node/nth_ref.rb +8 -0
- data/lib/astree/pretty_node/op_asgn1.rb +8 -0
- data/lib/astree/pretty_node/op_asgn2.rb +10 -0
- data/lib/astree/pretty_node/op_asgn_and.rb +8 -0
- data/lib/astree/pretty_node/op_asgn_or.rb +8 -0
- data/lib/astree/pretty_node/op_cdecl.rb +8 -0
- data/lib/astree/pretty_node/opcall.rb +10 -0
- data/lib/astree/pretty_node/opt_arg.rb +8 -0
- data/lib/astree/pretty_node/qcall.rb +10 -0
- data/lib/astree/pretty_node/resbody.rb +10 -0
- data/lib/astree/pretty_node/rescue.rb +8 -0
- data/lib/astree/pretty_node/return.rb +8 -0
- data/lib/astree/pretty_node/scope.rb +12 -0
- data/lib/astree/pretty_node/str.rb +8 -0
- data/lib/astree/pretty_node/super.rb +8 -0
- data/lib/astree/pretty_node/unless.rb +10 -0
- data/lib/astree/pretty_node/valias.rb +10 -0
- data/lib/astree/pretty_node/values.rb +5 -0
- data/lib/astree/pretty_node/vcall.rb +8 -0
- data/lib/astree/pretty_node/when.rb +10 -0
- data/lib/astree/pretty_node/xstr.rb +8 -0
- data/lib/astree/pretty_node/yield.rb +8 -0
- data/lib/astree/pretty_node.rb +42 -0
- data/lib/astree/version.rb +1 -1
- data/lib/astree.rb +20 -12
- metadata +67 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab953e90281ab9114670702d71d2fdd924da85ff417188fd34b1affc1593d43b
|
4
|
+
data.tar.gz: fd9182d9674c2d92a1da5c0b4e866ca6d17476aac9c037458f0cd4e3b37f2e5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e57fccb280f013235dfbb6be78c4af22de39208f6ad31ce517d242ee6d74c68672869b3127e8b6829e6722023b878ddd1710061750c9aeb66d4fa81db162480c
|
7
|
+
data.tar.gz: 7526a78383c05eec62cbcfd07f575cca6642208d100515aed717344b60af5761c866a579fc99d599365b2be8a78319940e3865636054fb88be92fef1fee40d75
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
astree (1.
|
4
|
+
astree (1.1.0)
|
5
5
|
colorize (~> 0.8)
|
6
6
|
|
7
7
|
GEM
|
@@ -9,7 +9,7 @@ GEM
|
|
9
9
|
specs:
|
10
10
|
colorize (0.8.1)
|
11
11
|
diff-lcs (1.3)
|
12
|
-
rake (
|
12
|
+
rake (12.3.2)
|
13
13
|
rspec (3.8.0)
|
14
14
|
rspec-core (~> 3.8.0)
|
15
15
|
rspec-expectations (~> 3.8.0)
|
@@ -30,8 +30,8 @@ PLATFORMS
|
|
30
30
|
DEPENDENCIES
|
31
31
|
astree!
|
32
32
|
bundler (~> 1.17)
|
33
|
-
rake (~>
|
33
|
+
rake (~> 12.0)
|
34
34
|
rspec (~> 3.0)
|
35
35
|
|
36
36
|
BUNDLED WITH
|
37
|
-
1.17.
|
37
|
+
1.17.3
|
data/README.md
CHANGED
@@ -26,34 +26,34 @@ end
|
|
26
26
|
result
|
27
27
|
|
28
28
|
```
|
29
|
-
<SCOPE>
|
30
|
-
├───── []
|
31
|
-
├───── nil
|
32
|
-
└───── <ITER>
|
33
|
-
├───── <CALL>
|
34
|
-
│ ├───── <LIT>
|
35
|
-
│ │ └───── 10
|
36
|
-
│ ├───── :times
|
37
|
-
│ └───── nil
|
38
|
-
└───── <SCOPE>
|
39
|
-
├───── [:i]
|
40
|
-
├───── <ARGS>
|
41
|
-
│ ├───── 1
|
42
|
-
│ ├───── nil
|
43
|
-
│ ├───── nil
|
44
|
-
│ ├───── nil
|
45
|
-
│ ├───── 0
|
46
|
-
│ ├───── nil
|
47
|
-
│ ├───── nil
|
48
|
-
│ ├───── nil
|
49
|
-
│ ├───── nil
|
50
|
-
│ └───── nil
|
51
|
-
└───── <FCALL>
|
52
|
-
├───── :puts
|
53
|
-
└───── <ARRAY>
|
54
|
-
├───── <DVAR>
|
55
|
-
│ └───── :i
|
56
|
-
└───── nil
|
29
|
+
<SCOPE> [1:0-3:3]
|
30
|
+
├───── [] (local table)
|
31
|
+
├───── nil (arguments)
|
32
|
+
└───── <ITER> [1:0-3:3]
|
33
|
+
├───── <CALL> [1:0-1:8]
|
34
|
+
│ ├───── <LIT> [1:0-1:2]
|
35
|
+
│ │ └───── 10 (value)
|
36
|
+
│ ├───── :times (method id)
|
37
|
+
│ └───── nil (arguments)
|
38
|
+
└───── <SCOPE> [1:9-3:3]
|
39
|
+
├───── [:i] (local table)
|
40
|
+
├───── <ARGS> [1:13-1:14]
|
41
|
+
│ ├───── 1 (pre_num)
|
42
|
+
│ ├───── nil (pre_init)
|
43
|
+
│ ├───── nil (opt)
|
44
|
+
│ ├───── nil (first_post)
|
45
|
+
│ ├───── 0 (post_num)
|
46
|
+
│ ├───── nil (post_init)
|
47
|
+
│ ├───── nil (rest)
|
48
|
+
│ ├───── nil (kw)
|
49
|
+
│ ├───── nil (kwrest)
|
50
|
+
│ └───── nil (block)
|
51
|
+
└───── <FCALL> [2:2-2:8]
|
52
|
+
├───── :puts (method id)
|
53
|
+
└───── <ARRAY> [2:7-2:8]
|
54
|
+
├───── <DVAR> [2:7-2:8]
|
55
|
+
│ └───── :i (variable name)
|
56
|
+
└───── nil (unknown)
|
57
57
|
```
|
58
58
|
|
59
59
|
## Development
|
data/astree.gemspec
CHANGED
@@ -26,6 +26,6 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_runtime_dependency 'colorize', '~> 0.8'
|
27
27
|
|
28
28
|
spec.add_development_dependency 'bundler', '~> 1.17'
|
29
|
-
spec.add_development_dependency 'rake', '~>
|
29
|
+
spec.add_development_dependency 'rake', '~> 12.0'
|
30
30
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
31
31
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class ASTree::PrettyNode::ARGS < ASTree::PrettyNode
|
2
|
+
def label_name(index)
|
3
|
+
case index
|
4
|
+
when 0
|
5
|
+
'pre_num'
|
6
|
+
when 1
|
7
|
+
'pre_init'
|
8
|
+
when 2
|
9
|
+
'opt'
|
10
|
+
when 3
|
11
|
+
'first_post'
|
12
|
+
when 4
|
13
|
+
'post_num'
|
14
|
+
when 5
|
15
|
+
'post_init'
|
16
|
+
when 6
|
17
|
+
'rest'
|
18
|
+
when 7
|
19
|
+
'kw'
|
20
|
+
when 8
|
21
|
+
'kwrest'
|
22
|
+
when 9
|
23
|
+
'block'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class ASTree
|
2
|
+
class PrettyNode
|
3
|
+
attr_reader :node
|
4
|
+
|
5
|
+
def initialize(node)
|
6
|
+
@node = node
|
7
|
+
end
|
8
|
+
|
9
|
+
def stringify_element(index)
|
10
|
+
label = label_name(index)
|
11
|
+
|
12
|
+
if label.nil?
|
13
|
+
raise "Unexpected index [#{index}] - #{node.inspect}."
|
14
|
+
end
|
15
|
+
|
16
|
+
"%s (%s)\n" % [colorize_element(element_value(index)), label]
|
17
|
+
end
|
18
|
+
|
19
|
+
def element_value(index)
|
20
|
+
node.children[index]
|
21
|
+
end
|
22
|
+
|
23
|
+
def label_name(index)
|
24
|
+
raise NotImplementedError
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def colorize_element(value)
|
30
|
+
case value
|
31
|
+
when String
|
32
|
+
value.inspect.colorize(:red)
|
33
|
+
when Symbol
|
34
|
+
value.inspect.colorize(:yellow)
|
35
|
+
when NilClass
|
36
|
+
value.inspect.colorize(:cyan)
|
37
|
+
else
|
38
|
+
value.inspect
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/astree/version.rb
CHANGED
data/lib/astree.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
-
require 'astree/version'
|
2
1
|
require 'colorize'
|
3
2
|
|
3
|
+
Dir[File.join(__dir__, '**/*.rb')].each do |f|
|
4
|
+
require f
|
5
|
+
end
|
6
|
+
|
4
7
|
class ASTree
|
5
8
|
SPACE_SIZE = 7
|
6
9
|
T_LINE = '├─────'.freeze
|
@@ -22,17 +25,22 @@ class ASTree
|
|
22
25
|
|
23
26
|
private
|
24
27
|
|
25
|
-
def traverse(node
|
26
|
-
return stringify_element(node) unless node.instance_of?(RubyVM::AbstractSyntaxTree::Node)
|
27
|
-
|
28
|
+
def traverse(node)
|
28
29
|
buffer = stringify_node(node)
|
29
30
|
|
30
31
|
children = node.children
|
32
|
+
children_count = children.size
|
33
|
+
|
34
|
+
children_count.times do |index|
|
35
|
+
child = children[index]
|
36
|
+
last_element = children_count == index + 1
|
37
|
+
|
38
|
+
child_buffer = if child.instance_of?(RubyVM::AbstractSyntaxTree::Node)
|
39
|
+
traverse(child)
|
40
|
+
else
|
41
|
+
pretty_element(node: node, index: index)
|
42
|
+
end.lines
|
31
43
|
|
32
|
-
until children.empty?
|
33
|
-
child = children.shift
|
34
|
-
last_element = children.empty?
|
35
|
-
child_buffer = traverse(child, parent: node).lines
|
36
44
|
buffer << draw_line(token: child_buffer.shift, last_element: last_element)
|
37
45
|
|
38
46
|
child_buffer.each do |line|
|
@@ -43,13 +51,13 @@ class ASTree
|
|
43
51
|
buffer
|
44
52
|
end
|
45
53
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
54
|
+
def pretty_element(node:, index:)
|
55
|
+
klass = PrettyNode.const_get(node.type)
|
56
|
+
klass.new(node).stringify_element(index)
|
49
57
|
end
|
50
58
|
|
51
59
|
def stringify_node(node)
|
52
|
-
"<%s
|
60
|
+
"<%s> [%d:%d-%d:%d]\n" % [node.type.to_s.colorize(:blue), node.first_lineno, node.first_column, node.last_lineno, node.last_column]
|
53
61
|
end
|
54
62
|
|
55
63
|
def draw_space(last_element:)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: astree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shuichi Tamayose
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '12.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '12.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,6 +88,69 @@ files:
|
|
88
88
|
- bin/setup
|
89
89
|
- exe/astree
|
90
90
|
- lib/astree.rb
|
91
|
+
- lib/astree/pretty_node.rb
|
92
|
+
- lib/astree/pretty_node/args.rb
|
93
|
+
- lib/astree/pretty_node/array.rb
|
94
|
+
- lib/astree/pretty_node/attrasgn.rb
|
95
|
+
- lib/astree/pretty_node/back_ref.rb
|
96
|
+
- lib/astree/pretty_node/begin.rb
|
97
|
+
- lib/astree/pretty_node/block.rb
|
98
|
+
- lib/astree/pretty_node/block_pass.rb
|
99
|
+
- lib/astree/pretty_node/break.rb
|
100
|
+
- lib/astree/pretty_node/call.rb
|
101
|
+
- lib/astree/pretty_node/case2.rb
|
102
|
+
- lib/astree/pretty_node/cdecl.rb
|
103
|
+
- lib/astree/pretty_node/class.rb
|
104
|
+
- lib/astree/pretty_node/colon2.rb
|
105
|
+
- lib/astree/pretty_node/colon3.rb
|
106
|
+
- lib/astree/pretty_node/const.rb
|
107
|
+
- lib/astree/pretty_node/cvar.rb
|
108
|
+
- lib/astree/pretty_node/cvasgn.rb
|
109
|
+
- lib/astree/pretty_node/dasgn.rb
|
110
|
+
- lib/astree/pretty_node/dasgn_curr.rb
|
111
|
+
- lib/astree/pretty_node/defn.rb
|
112
|
+
- lib/astree/pretty_node/defs.rb
|
113
|
+
- lib/astree/pretty_node/dregx.rb
|
114
|
+
- lib/astree/pretty_node/dstr.rb
|
115
|
+
- lib/astree/pretty_node/dsym.rb
|
116
|
+
- lib/astree/pretty_node/dvar.rb
|
117
|
+
- lib/astree/pretty_node/dxstr.rb
|
118
|
+
- lib/astree/pretty_node/fcall.rb
|
119
|
+
- lib/astree/pretty_node/gasgn.rb
|
120
|
+
- lib/astree/pretty_node/gvar.rb
|
121
|
+
- lib/astree/pretty_node/hash.rb
|
122
|
+
- lib/astree/pretty_node/iasgn.rb
|
123
|
+
- lib/astree/pretty_node/if.rb
|
124
|
+
- lib/astree/pretty_node/ivar.rb
|
125
|
+
- lib/astree/pretty_node/kw_arg.rb
|
126
|
+
- lib/astree/pretty_node/lasgn.rb
|
127
|
+
- lib/astree/pretty_node/lit.rb
|
128
|
+
- lib/astree/pretty_node/lvar.rb
|
129
|
+
- lib/astree/pretty_node/masgn.rb
|
130
|
+
- lib/astree/pretty_node/match.rb
|
131
|
+
- lib/astree/pretty_node/next.rb
|
132
|
+
- lib/astree/pretty_node/nth_ref.rb
|
133
|
+
- lib/astree/pretty_node/op_asgn1.rb
|
134
|
+
- lib/astree/pretty_node/op_asgn2.rb
|
135
|
+
- lib/astree/pretty_node/op_asgn_and.rb
|
136
|
+
- lib/astree/pretty_node/op_asgn_or.rb
|
137
|
+
- lib/astree/pretty_node/op_cdecl.rb
|
138
|
+
- lib/astree/pretty_node/opcall.rb
|
139
|
+
- lib/astree/pretty_node/opt_arg.rb
|
140
|
+
- lib/astree/pretty_node/qcall.rb
|
141
|
+
- lib/astree/pretty_node/resbody.rb
|
142
|
+
- lib/astree/pretty_node/rescue.rb
|
143
|
+
- lib/astree/pretty_node/return.rb
|
144
|
+
- lib/astree/pretty_node/scope.rb
|
145
|
+
- lib/astree/pretty_node/str.rb
|
146
|
+
- lib/astree/pretty_node/super.rb
|
147
|
+
- lib/astree/pretty_node/unless.rb
|
148
|
+
- lib/astree/pretty_node/valias.rb
|
149
|
+
- lib/astree/pretty_node/values.rb
|
150
|
+
- lib/astree/pretty_node/vcall.rb
|
151
|
+
- lib/astree/pretty_node/when.rb
|
152
|
+
- lib/astree/pretty_node/xstr.rb
|
153
|
+
- lib/astree/pretty_node/yield.rb
|
91
154
|
- lib/astree/version.rb
|
92
155
|
homepage: https://github.com/siman-man/astree
|
93
156
|
licenses:
|