unparser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +7 -0
  2. data/.circle.yml +4 -0
  3. data/.gitignore +2 -0
  4. data/.rspec +3 -0
  5. data/.travis.yml +16 -0
  6. data/Changelog.md +3 -0
  7. data/Gemfile +6 -0
  8. data/Gemfile.devtools +60 -0
  9. data/Guardfile +18 -0
  10. data/README.md +41 -0
  11. data/Rakefile +24 -0
  12. data/TODO +1 -0
  13. data/config/devtools.yml +2 -0
  14. data/config/flay.yml +3 -0
  15. data/config/flog.yml +2 -0
  16. data/config/mutant.yml +3 -0
  17. data/config/reek.yml +107 -0
  18. data/config/yardstick.yml +2 -0
  19. data/lib/unparser/buffer.rb +93 -0
  20. data/lib/unparser/constants.rb +87 -0
  21. data/lib/unparser/emitter/alias.rb +27 -0
  22. data/lib/unparser/emitter/argument.rb +154 -0
  23. data/lib/unparser/emitter/assignment.rb +140 -0
  24. data/lib/unparser/emitter/begin.rb +179 -0
  25. data/lib/unparser/emitter/binary.rb +52 -0
  26. data/lib/unparser/emitter/block.rb +53 -0
  27. data/lib/unparser/emitter/break.rb +28 -0
  28. data/lib/unparser/emitter/case.rb +97 -0
  29. data/lib/unparser/emitter/cbase.rb +22 -0
  30. data/lib/unparser/emitter/class.rb +72 -0
  31. data/lib/unparser/emitter/def.rb +107 -0
  32. data/lib/unparser/emitter/defined.rb +27 -0
  33. data/lib/unparser/emitter/for.rb +50 -0
  34. data/lib/unparser/emitter/hookexe.rb +42 -0
  35. data/lib/unparser/emitter/if.rb +85 -0
  36. data/lib/unparser/emitter/literal/composed.rb +64 -0
  37. data/lib/unparser/emitter/literal/dynamic.rb +54 -0
  38. data/lib/unparser/emitter/literal/dynamic_body.rb +109 -0
  39. data/lib/unparser/emitter/literal/execute_string.rb +38 -0
  40. data/lib/unparser/emitter/literal/primitive.rb +49 -0
  41. data/lib/unparser/emitter/literal/range.rb +33 -0
  42. data/lib/unparser/emitter/literal/regexp.rb +96 -0
  43. data/lib/unparser/emitter/literal/singleton.rb +24 -0
  44. data/lib/unparser/emitter/literal.rb +7 -0
  45. data/lib/unparser/emitter/module.rb +37 -0
  46. data/lib/unparser/emitter/next.rb +22 -0
  47. data/lib/unparser/emitter/not.rb +25 -0
  48. data/lib/unparser/emitter/op_assign.rb +63 -0
  49. data/lib/unparser/emitter/redo.rb +22 -0
  50. data/lib/unparser/emitter/repetition.rb +33 -0
  51. data/lib/unparser/emitter/retry.rb +22 -0
  52. data/lib/unparser/emitter/return.rb +38 -0
  53. data/lib/unparser/emitter/root.rb +7 -0
  54. data/lib/unparser/emitter/send/binary.rb +100 -0
  55. data/lib/unparser/emitter/send/index.rb +82 -0
  56. data/lib/unparser/emitter/send/unary.rb +29 -0
  57. data/lib/unparser/emitter/send.rb +273 -0
  58. data/lib/unparser/emitter/splat.rb +24 -0
  59. data/lib/unparser/emitter/super.rb +46 -0
  60. data/lib/unparser/emitter/undef.rb +25 -0
  61. data/lib/unparser/emitter/variable.rb +83 -0
  62. data/lib/unparser/emitter/yield.rb +28 -0
  63. data/lib/unparser/emitter.rb +308 -0
  64. data/lib/unparser.rb +87 -0
  65. data/spec/integration/unparser/spike_spec.rb +914 -0
  66. data/spec/spec_helper.rb +13 -0
  67. data/spec/unit/unparser/buffer/append_spec.rb +23 -0
  68. data/spec/unit/unparser/buffer/content_spec.rb +38 -0
  69. data/spec/unit/unparser/buffer/indent_spec.rb +18 -0
  70. data/spec/unit/unparser/buffer/nl_spec.rb +16 -0
  71. data/spec/unit/unparser/buffer/unindent_spec.rb +18 -0
  72. data/spec/unit/unparser/class_methods/unparse_spec.rb +16 -0
  73. data/spec/unit/unparser/emitter/class_methods/handle_spec.rb +17 -0
  74. data/spec/unit/unparser/emitter/class_methods/visit_spec.rb +37 -0
  75. data/spec/unit/unparser/emitter/source_map/class_methods/emit_spec.rb +20 -0
  76. data/unparser.gemspec +24 -0
  77. metadata +201 -0
@@ -0,0 +1,140 @@
1
+ module Unparser
2
+ class Emitter
3
+
4
+ # Base class for assignment emitters
5
+ class Assignment < self
6
+
7
+ private
8
+
9
+ # Perform dispatch
10
+ #
11
+ # @return [undefined]
12
+ #
13
+ # @api private
14
+ #
15
+ def dispatch
16
+ emit_left
17
+ emit_right
18
+ end
19
+
20
+ # Single assignment emitter
21
+ class Single < self
22
+
23
+ # Emit right
24
+ #
25
+ # @return [undefined]
26
+ #
27
+ # @api private
28
+ #
29
+ def emit_right
30
+ right = right_node
31
+ if right
32
+ write(WS, O_ASN, WS)
33
+ visit(right)
34
+ end
35
+ end
36
+
37
+ abstract_method :emit_left
38
+
39
+ # Variable assignment emitter
40
+ class Variable < self
41
+
42
+ handle :lvasgn, :ivasgn, :cvasgn, :gvasgn
43
+
44
+ children :name, :right_node
45
+
46
+ private
47
+
48
+ # Emit left
49
+ #
50
+ # @return [undefined]
51
+ #
52
+ # @api private
53
+ #
54
+ def emit_left
55
+ write(name.to_s)
56
+ end
57
+
58
+ end # Variable
59
+
60
+ # Constant assignment emitter
61
+ class Constant < self
62
+
63
+
64
+ handle :casgn
65
+
66
+ children :base, :name, :right_node
67
+
68
+ private
69
+
70
+ # Emit left
71
+ #
72
+ # @return [undefined]
73
+ #
74
+ # @api private
75
+ #
76
+ def emit_left
77
+ visit(base) if base
78
+ write(name.to_s)
79
+ end
80
+
81
+ end # Constant
82
+ end # Single
83
+
84
+ # Multiple assignment
85
+ class Multiple < self
86
+
87
+ handle :masgn
88
+
89
+ private
90
+
91
+ # Emit left
92
+ #
93
+ # @return [undefined]
94
+ #
95
+ # @api private
96
+ #
97
+ def emit_left
98
+ visit(first_child)
99
+ end
100
+
101
+ # Emit right
102
+ #
103
+ # @return [undefined]
104
+ #
105
+ # @api private
106
+ #
107
+ def emit_right
108
+ write(WS, O_ASN, WS)
109
+ right = children.last
110
+ case right.type
111
+ when :array
112
+ delimited(right.children)
113
+ else
114
+ visit(right)
115
+ end
116
+ end
117
+
118
+ end # Multiple
119
+
120
+ # Emitter for multiple assignment left hand side
121
+ class MLHS < Emitter
122
+
123
+ handle :mlhs
124
+
125
+ private
126
+
127
+ # Perform dispatch
128
+ #
129
+ # @return [undefined]
130
+ #
131
+ # @api private
132
+ #
133
+ def dispatch
134
+ delimited(children)
135
+ end
136
+ end # MLHS
137
+
138
+ end # Assignment
139
+ end # Emitter
140
+ end # Unparser
@@ -0,0 +1,179 @@
1
+ module Unparser
2
+ class Emitter
3
+
4
+ # Emitter for rescue nodes
5
+ class Rescue < self
6
+
7
+ handle :rescue
8
+
9
+ children :body
10
+
11
+ private
12
+
13
+ # Perform dispatch
14
+ #
15
+ # @return [undefined]
16
+ #
17
+ # @api private
18
+ #
19
+ def dispatch
20
+ k_begin
21
+ indented { visit(body) }
22
+ children[1..-2].each do |child|
23
+ visit(child)
24
+ end
25
+ k_end
26
+ end
27
+ end # Rescue
28
+
29
+ # Emitter for enusre nodes
30
+ class Ensure < self
31
+
32
+ handle :ensure
33
+
34
+ children :body, :ensure_body
35
+
36
+ private
37
+
38
+ # Perform dispatch
39
+ #
40
+ # @return [undefined]
41
+ #
42
+ # @api private
43
+ #
44
+ def dispatch
45
+ k_begin
46
+ emit_body
47
+ write(K_ENSURE)
48
+ emit_ensure_body
49
+ k_end
50
+ end
51
+
52
+ # Emit body
53
+ #
54
+ # @return [undefined]
55
+ #
56
+ # @api private
57
+ #
58
+ def emit_body
59
+ indented { visit(body) }
60
+ end
61
+
62
+ # Emit ensure body
63
+ #
64
+ # @return [undefined]
65
+ #
66
+ # @api private
67
+ #
68
+ def emit_ensure_body
69
+ indented { visit(ensure_body) }
70
+ end
71
+
72
+ end # Ensure
73
+
74
+ # Emitter for rescue body nodes
75
+ class Resbody < self
76
+
77
+ handle :resbody
78
+
79
+ children :exception, :assignment, :body
80
+
81
+ private
82
+
83
+ # Perform dispatch
84
+ #
85
+ # @return [undefined]
86
+ #
87
+ # @api private
88
+ #
89
+ def dispatch
90
+ write(K_RESCUE)
91
+ emit_exception
92
+ emit_assignment
93
+ indented { visit(body) }
94
+ end
95
+
96
+ # Emit exception
97
+ #
98
+ # @return [undefined]
99
+ #
100
+ # @api private
101
+ #
102
+ def emit_exception
103
+ return unless exception
104
+ ws
105
+ delimited(exception.children)
106
+ end
107
+
108
+ # Emit assignment
109
+ #
110
+ # @return [undefined]
111
+ #
112
+ # @api private
113
+ #
114
+ def emit_assignment
115
+ return unless assignment
116
+ write(WS, O_ASR, WS)
117
+ visit(assignment)
118
+ end
119
+
120
+ end # Resbody
121
+
122
+ # Emitter for begin nodes
123
+ class Begin < self
124
+
125
+ handle :begin
126
+
127
+ private
128
+
129
+ # Perform dispatch
130
+ #
131
+ # @return [undefined]
132
+ #
133
+ # @api private
134
+ #
135
+ def dispatch
136
+ case children.length
137
+ when 0
138
+ write(K_NIL)
139
+ when 1
140
+ visit(first_child)
141
+ else
142
+ emit_normal
143
+ end
144
+ end
145
+
146
+ # Emit normal begin block
147
+ #
148
+ # @return [undefined]
149
+ #
150
+ # @api private
151
+ #
152
+ def emit_normal
153
+ unless parent.needs_begin?
154
+ emit_inner
155
+ else
156
+ k_begin
157
+ indented { emit_inner }
158
+ k_end
159
+ end
160
+ end
161
+
162
+ # Emit inner nodes
163
+ #
164
+ # @return [undefined]
165
+ #
166
+ # @api private
167
+ #
168
+ def emit_inner
169
+ childs = children
170
+ max = childs.length - 1
171
+ childs.each_with_index do |child, index|
172
+ visit(child)
173
+ nl if index < max
174
+ end
175
+ end
176
+
177
+ end # Body
178
+ end # Emitter
179
+ end # Unparser
@@ -0,0 +1,52 @@
1
+ module Unparser
2
+ class Emitter
3
+ # Base class for binary emitters
4
+ class Binary < self
5
+
6
+ handle :or, :and
7
+ children :left, :right
8
+
9
+ MAP = {
10
+ :or => O_OR,
11
+ :and => O_AND
12
+ }.freeze
13
+
14
+ private
15
+
16
+ # Perform dispatch
17
+ #
18
+ # @return [undefined]
19
+ #
20
+ # @api private
21
+ #
22
+ def dispatch
23
+ parentheses do
24
+ emit_left
25
+ write(WS, MAP.fetch(node.type), WS)
26
+ emit_right
27
+ end
28
+ end
29
+
30
+ # Emit left
31
+ #
32
+ # @return [undefined]
33
+ #
34
+ # @api private
35
+ #
36
+ def emit_left
37
+ parentheses { visit(left) }
38
+ end
39
+
40
+ # Emit right
41
+ #
42
+ # @return [undefined]
43
+ #
44
+ # @api private
45
+ #
46
+ def emit_right
47
+ parentheses { visit(right) }
48
+ end
49
+
50
+ end # Binary
51
+ end # Emitter
52
+ end # Unparser
@@ -0,0 +1,53 @@
1
+ module Unparser
2
+ class Emitter
3
+
4
+ # Block emitter
5
+ class Block < self
6
+
7
+ handle :block
8
+
9
+ children :send, :arguments, :body
10
+
11
+ private
12
+
13
+ # Perform dispatch
14
+ #
15
+ # @return [undefined]
16
+ #
17
+ # @api private
18
+ #
19
+ def dispatch
20
+ visit(send)
21
+ write(WS, K_DO)
22
+ emit_block_arguments
23
+ indented { emit_body }
24
+ k_end
25
+ end
26
+
27
+ # Emit arguments
28
+ #
29
+ # @return [undefined]
30
+ #
31
+ # @api private
32
+ #
33
+ def emit_block_arguments
34
+ return if arguments.children.empty?
35
+ ws
36
+ parentheses(O_PIPE, O_PIPE) do
37
+ visit(arguments)
38
+ end
39
+ end
40
+
41
+ # Emit body
42
+ #
43
+ # @return [undefined]
44
+ #
45
+ # @api private
46
+ #
47
+ def emit_body
48
+ visit(body)
49
+ end
50
+
51
+ end # Block
52
+ end # Emitter
53
+ end # Unparser
@@ -0,0 +1,28 @@
1
+ module Unparser
2
+ class Emitter
3
+ # Emitter for break nodes
4
+ class Break < self
5
+
6
+ handle :break
7
+
8
+ children :arguments
9
+
10
+ private
11
+
12
+ # Perform dispatch
13
+ #
14
+ # @return [undefined]
15
+ #
16
+ # @api private
17
+ #
18
+ def dispatch
19
+ write(K_BREAK)
20
+ return unless arguments
21
+ parentheses do
22
+ visit(arguments)
23
+ end
24
+ end
25
+
26
+ end # Break
27
+ end # Emitter
28
+ end # Unparser
@@ -0,0 +1,97 @@
1
+ module Unparser
2
+ class Emitter
3
+ # Emitter for case nodes
4
+ class Case < self
5
+
6
+ handle :case
7
+
8
+ children :condition
9
+
10
+ private
11
+
12
+ # Perform dispatch
13
+ #
14
+ # @return [undefined]
15
+ #
16
+ # @api private
17
+ #
18
+ def dispatch
19
+ write(K_CASE)
20
+ emit_condition
21
+ emit_whens
22
+ emit_else
23
+ k_end
24
+ end
25
+
26
+ # Emit else
27
+ #
28
+ # @return [undefined]
29
+ #
30
+ # @api private
31
+ #
32
+ def emit_else
33
+ else_branch = children.last
34
+ return unless else_branch
35
+ write(K_ELSE)
36
+ indented { visit(else_branch) }
37
+ end
38
+
39
+ # Emit whens
40
+ #
41
+ # @return [undefined]
42
+ #
43
+ # @api private
44
+ #
45
+ def emit_whens
46
+ nl
47
+ children[1..-2].each do |child|
48
+ visit(child)
49
+ end
50
+ end
51
+
52
+ # Emit condition
53
+ #
54
+ # @return [undefined]
55
+ #
56
+ # @api private
57
+ #
58
+ def emit_condition
59
+ return unless condition
60
+ write(WS)
61
+ visit(condition)
62
+ end
63
+
64
+ end # Case
65
+
66
+ # Emitter for when nodes
67
+ class When < self
68
+
69
+ handle :when
70
+
71
+ private
72
+
73
+ # Perform dispatch
74
+ #
75
+ # @return [undefined]
76
+ #
77
+ # @api private
78
+ #
79
+ def dispatch
80
+ write(K_WHEN, WS)
81
+ emit_captures
82
+ indented { visit(children.last) }
83
+ end
84
+
85
+ # Emit captures
86
+ #
87
+ # @return [undefined]
88
+ #
89
+ # @api private
90
+ #
91
+ def emit_captures
92
+ delimited(children[0..-2])
93
+ end
94
+
95
+ end
96
+ end # Emitter
97
+ end # Unparser
@@ -0,0 +1,22 @@
1
+ module Unparser
2
+ class Emitter
3
+ # Emitter for toplevel constant reference nodes
4
+ class CBase < self
5
+
6
+ handle :cbase
7
+
8
+ private
9
+
10
+ # Perform dispatch
11
+ #
12
+ # @return [undefined]
13
+ #
14
+ # @api private
15
+ #
16
+ def dispatch
17
+ write(O_DCL)
18
+ end
19
+
20
+ end # CBase
21
+ end # Emitter
22
+ end # Unparser
@@ -0,0 +1,72 @@
1
+ module Unparser
2
+ class Emitter
3
+ # Emitter for class nodes
4
+ class Class < self
5
+
6
+ handle :class
7
+
8
+ children :name, :superclass, :body
9
+
10
+ private
11
+
12
+ # Perform dispatch
13
+ #
14
+ # @return [undefined]
15
+ #
16
+ # @api private
17
+ #
18
+ def dispatch
19
+ write(K_CLASS, WS)
20
+ visit(name)
21
+ emit_superclass
22
+ emit_body
23
+ k_end
24
+ end
25
+
26
+ # Emit superclass
27
+ #
28
+ # @return [undefined]
29
+ #
30
+ # @api private
31
+ #
32
+ def emit_superclass
33
+ return unless superclass
34
+ write(WS, O_LT, WS)
35
+ visit(superclass)
36
+ end
37
+
38
+ # Emit body
39
+ #
40
+ # @return [undefined]
41
+ #
42
+ # @api private
43
+ #
44
+ def emit_body
45
+ emit_non_nil_body(body)
46
+ end
47
+
48
+ end # Class
49
+
50
+ # Emitter for sclass nodes
51
+ class SClass < self
52
+
53
+ handle :sclass
54
+
55
+ children :object, :body
56
+
57
+ # Perform dispatch
58
+ #
59
+ # @return [undefined]
60
+ #
61
+ # @api private
62
+ #
63
+ def dispatch
64
+ write(K_CLASS, WS, O_DLT, WS)
65
+ visit(object)
66
+ emit_non_nil_body(body)
67
+ k_end
68
+ end
69
+
70
+ end # SClass
71
+ end # Emitter
72
+ end # Unparser