astree 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +4 -4
  3. data/README.md +28 -28
  4. data/astree.gemspec +1 -1
  5. data/lib/astree/pretty_node/args.rb +26 -0
  6. data/lib/astree/pretty_node/array.rb +5 -0
  7. data/lib/astree/pretty_node/attrasgn.rb +10 -0
  8. data/lib/astree/pretty_node/back_ref.rb +8 -0
  9. data/lib/astree/pretty_node/begin.rb +8 -0
  10. data/lib/astree/pretty_node/block.rb +5 -0
  11. data/lib/astree/pretty_node/block_pass.rb +8 -0
  12. data/lib/astree/pretty_node/break.rb +8 -0
  13. data/lib/astree/pretty_node/call.rb +10 -0
  14. data/lib/astree/pretty_node/case2.rb +8 -0
  15. data/lib/astree/pretty_node/cdecl.rb +10 -0
  16. data/lib/astree/pretty_node/class.rb +8 -0
  17. data/lib/astree/pretty_node/colon2.rb +10 -0
  18. data/lib/astree/pretty_node/colon3.rb +8 -0
  19. data/lib/astree/pretty_node/const.rb +8 -0
  20. data/lib/astree/pretty_node/cvar.rb +8 -0
  21. data/lib/astree/pretty_node/cvasgn.rb +10 -0
  22. data/lib/astree/pretty_node/dasgn.rb +10 -0
  23. data/lib/astree/pretty_node/dasgn_curr.rb +10 -0
  24. data/lib/astree/pretty_node/defn.rb +8 -0
  25. data/lib/astree/pretty_node/defs.rb +8 -0
  26. data/lib/astree/pretty_node/dregx.rb +10 -0
  27. data/lib/astree/pretty_node/dstr.rb +10 -0
  28. data/lib/astree/pretty_node/dsym.rb +10 -0
  29. data/lib/astree/pretty_node/dvar.rb +8 -0
  30. data/lib/astree/pretty_node/dxstr.rb +10 -0
  31. data/lib/astree/pretty_node/fcall.rb +10 -0
  32. data/lib/astree/pretty_node/gasgn.rb +10 -0
  33. data/lib/astree/pretty_node/gvar.rb +8 -0
  34. data/lib/astree/pretty_node/hash.rb +5 -0
  35. data/lib/astree/pretty_node/iasgn.rb +10 -0
  36. data/lib/astree/pretty_node/if.rb +10 -0
  37. data/lib/astree/pretty_node/ivar.rb +8 -0
  38. data/lib/astree/pretty_node/kw_arg.rb +8 -0
  39. data/lib/astree/pretty_node/lasgn.rb +10 -0
  40. data/lib/astree/pretty_node/lit.rb +8 -0
  41. data/lib/astree/pretty_node/lvar.rb +8 -0
  42. data/lib/astree/pretty_node/masgn.rb +12 -0
  43. data/lib/astree/pretty_node/match.rb +8 -0
  44. data/lib/astree/pretty_node/next.rb +8 -0
  45. data/lib/astree/pretty_node/nth_ref.rb +8 -0
  46. data/lib/astree/pretty_node/op_asgn1.rb +8 -0
  47. data/lib/astree/pretty_node/op_asgn2.rb +10 -0
  48. data/lib/astree/pretty_node/op_asgn_and.rb +8 -0
  49. data/lib/astree/pretty_node/op_asgn_or.rb +8 -0
  50. data/lib/astree/pretty_node/op_cdecl.rb +8 -0
  51. data/lib/astree/pretty_node/opcall.rb +10 -0
  52. data/lib/astree/pretty_node/opt_arg.rb +8 -0
  53. data/lib/astree/pretty_node/qcall.rb +10 -0
  54. data/lib/astree/pretty_node/resbody.rb +10 -0
  55. data/lib/astree/pretty_node/rescue.rb +8 -0
  56. data/lib/astree/pretty_node/return.rb +8 -0
  57. data/lib/astree/pretty_node/scope.rb +12 -0
  58. data/lib/astree/pretty_node/str.rb +8 -0
  59. data/lib/astree/pretty_node/super.rb +8 -0
  60. data/lib/astree/pretty_node/unless.rb +10 -0
  61. data/lib/astree/pretty_node/valias.rb +10 -0
  62. data/lib/astree/pretty_node/values.rb +5 -0
  63. data/lib/astree/pretty_node/vcall.rb +8 -0
  64. data/lib/astree/pretty_node/when.rb +10 -0
  65. data/lib/astree/pretty_node/xstr.rb +8 -0
  66. data/lib/astree/pretty_node/yield.rb +8 -0
  67. data/lib/astree/pretty_node.rb +42 -0
  68. data/lib/astree/version.rb +1 -1
  69. data/lib/astree.rb +20 -12
  70. metadata +67 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2976d06842addc68dc0fecbfe3da8b1b06e7ab7379a4b5b73d4b77e24686224c
4
- data.tar.gz: 44fdd90beb871078c21530a61e9ed2a7de3b8b6d63723b9ca8b2190339ca5f32
3
+ metadata.gz: ab953e90281ab9114670702d71d2fdd924da85ff417188fd34b1affc1593d43b
4
+ data.tar.gz: fd9182d9674c2d92a1da5c0b4e866ca6d17476aac9c037458f0cd4e3b37f2e5e
5
5
  SHA512:
6
- metadata.gz: e5426c45eddf97840fe87b897da5493f16a923ed1d38d8e88fbc2f7bd29f0f33b7f8437d43565946404ae3e47cf3b5f8b56f09d5ac2b6c3c7ac4e51d773b7d49
7
- data.tar.gz: 9229f371cf0a49e826a752fd184651e30cd7dffb663efe804bbe0e054692aee41c16ccf671fb8039def4a19cacf60461bb45385647fb0594455840bb1d09cd77
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.0.0)
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 (10.5.0)
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 (~> 10.0)
33
+ rake (~> 12.0)
34
34
  rspec (~> 3.0)
35
35
 
36
36
  BUNDLED WITH
37
- 1.17.2
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', '~> 10.0'
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,5 @@
1
+ class ASTree::PrettyNode::ARRAY < ASTree::PrettyNode
2
+ def label_name(_index)
3
+ 'unknown'
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::ATTRASGN < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'method id'
6
+ when 2
7
+ 'unknown'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::BACK_REF < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'variable name'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::BEGIN < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'body'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class ASTree::PrettyNode::BLOCK < ASTree::PrettyNode
2
+ def label_name(index)
3
+ 'unknown'
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::BLOCK_PASS < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'other arguments'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::BREAK < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'value'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::CALL < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'method id'
6
+ when 2
7
+ 'arguments'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::CASE2 < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'condition'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::CDECL < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'const name'
6
+ when 1
7
+ 'const name'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::CLASS < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'super class'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::COLON2 < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'const path'
6
+ when 1
7
+ 'const name'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::COLON3 < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'const name'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::CONST < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'const name'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::CVAR < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'variable name'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::CVASGN < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'variable name'
6
+ when 1
7
+ 'unknown'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::DASGN < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'variable name'
6
+ when 1
7
+ 'unknown'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::DASGN_CURR < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'variable name'
6
+ when 1
7
+ 'unknown'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::DEFN < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'method id'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::DEFS < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'method id'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::DREGX < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'preceding string'
6
+ when 2
7
+ 'tailing string'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::DSTR < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'preceding string'
6
+ when 2
7
+ 'tailing string'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::DSYM < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'preceding string'
6
+ when 2
7
+ 'tailing string'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::DVAR < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'variable name'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::DXSTR < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'preceding string'
6
+ when 2
7
+ 'tailing string'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::FCALL < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'method id'
6
+ when 1
7
+ 'arguments'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::GASGN < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'variable name'
6
+ when 1
7
+ 'unknown'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::GVAR < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'variable name'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class ASTree::PrettyNode::HASH < ASTree::PrettyNode
2
+ def label_name(_index)
3
+ 'unknown'
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::IASGN < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'variable name'
6
+ when 1
7
+ 'unknown'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::IF < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'then clause'
6
+ when 2
7
+ 'else clause'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::IVAR < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'variable name'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::KW_ARG < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'next kw_arg'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::LASGN < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'variable name'
6
+ when 1
7
+ 'unknown'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::LIT < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'value'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::LVAR < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'variable name'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ class ASTree::PrettyNode::MASGN < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'right hand side'
6
+ when 1
7
+ 'left hand side'
8
+ when 2
9
+ 'rest variable'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::MATCH < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'regexp'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::NEXT < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'value'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::NTH_REF < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'variable name'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::OP_ASGN1 < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'operator'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::OP_ASGN2 < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'unknown'
6
+ when 2
7
+ 'field name'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::OP_ASGN_AND < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'operator'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::OP_ASGN_OR < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'operator'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::OP_CDECL < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'operator'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::OPCALL < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'method id'
6
+ when 2
7
+ 'arguments'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::OPT_ARG < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'next opt_arg'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::QCALL < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'method id'
6
+ when 2
7
+ 'arguments'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::RESBODY < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'rescue clause list'
6
+ when 2
7
+ 'next clause'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::RESCUE < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 2
5
+ 'next clause'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::RETURN < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'value'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ class ASTree::PrettyNode::SCOPE < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'local table'
6
+ when 1
7
+ 'arguments'
8
+ when 2
9
+ 'body'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::STR < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'value'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::SUPER < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'arguments'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::UNLESS < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'then clause'
6
+ when 2
7
+ 'else clause'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::VALIAS < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'new name'
6
+ when 1
7
+ 'old name'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ class ASTree::PrettyNode::VALUES < ASTree::PrettyNode
2
+ def label_name(index)
3
+ 'unknown'
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::VCALL < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'method id'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class ASTree::PrettyNode::WHEN < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 1
5
+ 'body'
6
+ when 2
7
+ 'next clause'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::XSTR < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'value'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class ASTree::PrettyNode::YIELD < ASTree::PrettyNode
2
+ def label_name(index)
3
+ case index
4
+ when 0
5
+ 'arguments'
6
+ end
7
+ end
8
+ 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
@@ -1,3 +1,3 @@
1
1
  class ASTree
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
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, parent: nil)
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 stringify_element(element)
47
- str = element.inspect
48
- (element.is_a?(Symbol) ? str.colorize(:magenta) : str) + "\n"
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>\n" % [node.type.to_s.colorize(:green)]
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.0.0
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-05 00:00:00.000000000 Z
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: '10.0'
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: '10.0'
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: