to_source 0.2.9 → 0.2.11
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +13 -0
- data/LICENSE +21 -0
- data/README.md +6 -22
- data/TODO +3 -3
- data/lib/to_source.rb +68 -3
- data/lib/to_source/buffer.rb +40 -0
- data/lib/to_source/command.rb +44 -0
- data/lib/to_source/emitter.rb +76 -0
- data/lib/to_source/emitter/access.rb +20 -0
- data/lib/to_source/emitter/actual_arguments.rb +46 -0
- data/lib/to_source/emitter/alias.rb +18 -0
- data/lib/to_source/emitter/assignment.rb +77 -0
- data/lib/to_source/emitter/attribute_assignment.rb +19 -0
- data/lib/to_source/emitter/begin.rb +51 -0
- data/lib/to_source/emitter/binary_operator.rb +36 -0
- data/lib/to_source/emitter/binary_operator_method.rb +29 -0
- data/lib/to_source/emitter/block.rb +22 -0
- data/lib/to_source/emitter/block_argument.rb +16 -0
- data/lib/to_source/emitter/block_pass.rb +16 -0
- data/lib/to_source/emitter/class.rb +29 -0
- data/lib/to_source/emitter/concat_arguments.rb +20 -0
- data/lib/to_source/emitter/default_arguments.rb +15 -0
- data/lib/to_source/emitter/define.rb +61 -0
- data/lib/to_source/emitter/defined.rb +17 -0
- data/lib/to_source/emitter/element_assignment.rb +20 -0
- data/lib/to_source/emitter/element_reference.rb +16 -0
- data/lib/to_source/emitter/empty_body.rb +12 -0
- data/lib/to_source/emitter/ensure.rb +23 -0
- data/lib/to_source/emitter/ensure_body.rb +18 -0
- data/lib/to_source/emitter/execute_string.rb +13 -0
- data/lib/to_source/emitter/formal_arguments.rb +120 -0
- data/lib/to_source/emitter/if.rb +51 -0
- data/lib/to_source/emitter/iter.rb +20 -0
- data/lib/to_source/emitter/keyword_value.rb +43 -0
- data/lib/to_source/emitter/literal.rb +239 -0
- data/lib/to_source/emitter/match3.rb +17 -0
- data/lib/to_source/emitter/module.rb +21 -0
- data/lib/to_source/emitter/multiple_assignment.rb +23 -0
- data/lib/to_source/emitter/nth_ref.rb +15 -0
- data/lib/to_source/emitter/op_assign1.rb +19 -0
- data/lib/to_source/emitter/op_assign2.rb +19 -0
- data/lib/to_source/emitter/pattern_arguments.rb +17 -0
- data/lib/to_source/emitter/receiver_case.rb +35 -0
- data/lib/to_source/emitter/rescue.rb +21 -0
- data/lib/to_source/emitter/rescue_condition.rb +56 -0
- data/lib/to_source/emitter/scope.rb +17 -0
- data/lib/to_source/emitter/scope_name.rb +16 -0
- data/lib/to_source/emitter/scoped_name.rb +17 -0
- data/lib/to_source/emitter/send.rb +47 -0
- data/lib/to_source/emitter/send_with_arguments.rb +62 -0
- data/lib/to_source/emitter/singleton_class.rb +23 -0
- data/lib/to_source/emitter/splat.rb +17 -0
- data/lib/to_source/emitter/splat_when.rb +16 -0
- data/lib/to_source/emitter/static.rb +33 -0
- data/lib/to_source/emitter/super.rb +47 -0
- data/lib/to_source/emitter/to_array.rb +15 -0
- data/lib/to_source/emitter/to_string.rb +17 -0
- data/lib/to_source/emitter/toplevel.rb +15 -0
- data/lib/to_source/emitter/unary_operator_method.rb +20 -0
- data/lib/to_source/emitter/unless.rb +16 -0
- data/lib/to_source/emitter/until.rb +20 -0
- data/lib/to_source/emitter/util.rb +19 -0
- data/lib/to_source/emitter/when.rb +38 -0
- data/lib/to_source/emitter/while.rb +20 -0
- data/lib/to_source/emitter/yield.rb +19 -0
- data/lib/to_source/emitter/z_super.rb +17 -0
- data/lib/to_source/state.rb +60 -0
- data/spec/unit/to_source/{visitor/class_methods → class_methods}/run_spec.rb +84 -33
- data/to_source.gemspec +14 -12
- metadata +106 -11
- data/lib/to_source/visitor.rb +0 -1890
@@ -0,0 +1,120 @@
|
|
1
|
+
module ToSource
|
2
|
+
class Emitter
|
3
|
+
class FormalArguments < self
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def any?
|
8
|
+
required? or defaults? or splat? or block_arg?
|
9
|
+
end
|
10
|
+
|
11
|
+
def required
|
12
|
+
arguments.required
|
13
|
+
end
|
14
|
+
|
15
|
+
def required?
|
16
|
+
required.any?
|
17
|
+
end
|
18
|
+
|
19
|
+
def defaults
|
20
|
+
arguments.defaults
|
21
|
+
end
|
22
|
+
|
23
|
+
def defaults?
|
24
|
+
!!defaults
|
25
|
+
end
|
26
|
+
|
27
|
+
def splat
|
28
|
+
arguments.splat
|
29
|
+
end
|
30
|
+
|
31
|
+
def splat?
|
32
|
+
!!splat
|
33
|
+
end
|
34
|
+
|
35
|
+
def block_arg
|
36
|
+
arguments.block_arg
|
37
|
+
end
|
38
|
+
|
39
|
+
def block_arg?
|
40
|
+
!!block_arg
|
41
|
+
end
|
42
|
+
|
43
|
+
def emit_required
|
44
|
+
array = required
|
45
|
+
max = array.length - 1
|
46
|
+
array.each_with_index do |member, index|
|
47
|
+
if member.kind_of?(Rubinius::AST::Node)
|
48
|
+
visit(member)
|
49
|
+
else
|
50
|
+
emit(member)
|
51
|
+
end
|
52
|
+
emit(', ') if index < max
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def emit_defaults
|
57
|
+
return unless defaults?
|
58
|
+
emit(', ') if required?
|
59
|
+
visit(defaults)
|
60
|
+
end
|
61
|
+
|
62
|
+
def emit_splat
|
63
|
+
return unless splat?
|
64
|
+
|
65
|
+
emit(', ') if required? or defaults?
|
66
|
+
emit('*')
|
67
|
+
value = splat
|
68
|
+
unless value == :@unnamed_splat
|
69
|
+
emit(value)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def emit_block_arg
|
74
|
+
return unless block_arg?
|
75
|
+
emit(', ') if required? or defaults? or splat?
|
76
|
+
visit(block_arg)
|
77
|
+
end
|
78
|
+
|
79
|
+
def arguments
|
80
|
+
node.arguments
|
81
|
+
end
|
82
|
+
|
83
|
+
def dispatch
|
84
|
+
return unless any?
|
85
|
+
|
86
|
+
util = self.class
|
87
|
+
|
88
|
+
emit(util::OPEN)
|
89
|
+
|
90
|
+
emit_required
|
91
|
+
emit_defaults
|
92
|
+
emit_splat
|
93
|
+
emit_block_arg
|
94
|
+
|
95
|
+
emit(util::CLOSE)
|
96
|
+
end
|
97
|
+
|
98
|
+
class Block < self
|
99
|
+
OPEN = ' |'.freeze
|
100
|
+
CLOSE = '|'.freeze
|
101
|
+
end
|
102
|
+
|
103
|
+
class Method < self
|
104
|
+
OPEN = '('.freeze
|
105
|
+
CLOSE = ')'.freeze
|
106
|
+
|
107
|
+
class Generic < self
|
108
|
+
handle(Rubinius::AST::FormalArguments19)
|
109
|
+
|
110
|
+
def arguments
|
111
|
+
node
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module ToSource
|
2
|
+
class Emitter
|
3
|
+
class If < self
|
4
|
+
|
5
|
+
handle(Rubinius::AST::If)
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def if_branch?
|
10
|
+
!node.body.kind_of?(Rubinius::AST::NilLiteral)
|
11
|
+
end
|
12
|
+
|
13
|
+
def else_branch?
|
14
|
+
!node.else.kind_of?(Rubinius::AST::NilLiteral)
|
15
|
+
end
|
16
|
+
|
17
|
+
def dispatch
|
18
|
+
if else_branch? and !if_branch?
|
19
|
+
run(Unless, node)
|
20
|
+
return
|
21
|
+
end
|
22
|
+
|
23
|
+
normal_dispatch
|
24
|
+
end
|
25
|
+
|
26
|
+
def normal_dispatch
|
27
|
+
emit('if ')
|
28
|
+
visit(node.condition)
|
29
|
+
emit_if_branch
|
30
|
+
emit_else_branch
|
31
|
+
emit('end')
|
32
|
+
end
|
33
|
+
|
34
|
+
def emit_if_branch
|
35
|
+
indent
|
36
|
+
visit(node.body)
|
37
|
+
unindent
|
38
|
+
end
|
39
|
+
|
40
|
+
def emit_else_branch
|
41
|
+
body = node.else
|
42
|
+
return unless else_branch?
|
43
|
+
emit('else')
|
44
|
+
indent
|
45
|
+
visit(body)
|
46
|
+
unindent
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ToSource
|
2
|
+
class Emitter
|
3
|
+
class Iter < self
|
4
|
+
|
5
|
+
handle(Rubinius::AST::Iter19)
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def dispatch
|
10
|
+
emit(' do')
|
11
|
+
run(FormalArguments::Block)
|
12
|
+
indent
|
13
|
+
visit(node.body)
|
14
|
+
unindent
|
15
|
+
emit_end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module ToSource
|
2
|
+
class Emitter
|
3
|
+
|
4
|
+
class KeywordValue < self
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
def dispatch
|
9
|
+
emit(self.class::SYMBOL)
|
10
|
+
if value?
|
11
|
+
emit('(')
|
12
|
+
visit(node.value)
|
13
|
+
emit(')')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Return < self
|
18
|
+
SYMBOL = :return
|
19
|
+
handle(Rubinius::AST::Return)
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def value?
|
24
|
+
!!node.value
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Break < self
|
29
|
+
|
30
|
+
handle(Rubinius::AST::Break)
|
31
|
+
|
32
|
+
SYMBOL = :break
|
33
|
+
|
34
|
+
def value?
|
35
|
+
!node.value.kind_of?(Rubinius::AST::NilLiteral)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,239 @@
|
|
1
|
+
module ToSource
|
2
|
+
class Emitter
|
3
|
+
class Literal < self
|
4
|
+
|
5
|
+
class EmptyArray < self
|
6
|
+
|
7
|
+
handle(Rubinius::AST::EmptyArray)
|
8
|
+
|
9
|
+
def dispatch
|
10
|
+
emit('[]')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Array < self
|
15
|
+
|
16
|
+
handle(Rubinius::AST::ArrayLiteral)
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def dispatch
|
21
|
+
emit('[')
|
22
|
+
run(Util::DelimitedBody, node.body)
|
23
|
+
emit(']')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Range < self
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def dispatch
|
32
|
+
visit(node.start)
|
33
|
+
emit(token)
|
34
|
+
visit(node.finish)
|
35
|
+
end
|
36
|
+
|
37
|
+
def token
|
38
|
+
self.class::TOKEN
|
39
|
+
end
|
40
|
+
|
41
|
+
class Inclusive < self
|
42
|
+
handle(Rubinius::AST::Range)
|
43
|
+
TOKEN = '..'
|
44
|
+
end
|
45
|
+
|
46
|
+
class Exclude < self
|
47
|
+
handle(Rubinius::AST::RangeExclude)
|
48
|
+
TOKEN = '...'
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
class Hash < self
|
54
|
+
|
55
|
+
handle(Rubinius::AST::HashLiteral)
|
56
|
+
|
57
|
+
def dispatch
|
58
|
+
body = node.array.each_slice(2).to_a
|
59
|
+
|
60
|
+
max = body.length - 1
|
61
|
+
|
62
|
+
emit '{'
|
63
|
+
|
64
|
+
body.each_with_index do |slice, index|
|
65
|
+
key, value = slice
|
66
|
+
|
67
|
+
visit(key)
|
68
|
+
emit ' => '
|
69
|
+
visit(value)
|
70
|
+
|
71
|
+
if index < max
|
72
|
+
emit ', '
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
emit '}'
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
class Inspect < self
|
82
|
+
|
83
|
+
handle(Rubinius::AST::SymbolLiteral)
|
84
|
+
|
85
|
+
def dispatch
|
86
|
+
emit(value.inspect)
|
87
|
+
end
|
88
|
+
|
89
|
+
def value
|
90
|
+
node.value
|
91
|
+
end
|
92
|
+
|
93
|
+
class Static < self
|
94
|
+
def value
|
95
|
+
self.class::VALUE
|
96
|
+
end
|
97
|
+
|
98
|
+
class True < self
|
99
|
+
handle(Rubinius::AST::TrueLiteral)
|
100
|
+
VALUE = true
|
101
|
+
end
|
102
|
+
|
103
|
+
class False < self
|
104
|
+
handle(Rubinius::AST::FalseLiteral)
|
105
|
+
VALUE = false
|
106
|
+
end
|
107
|
+
|
108
|
+
class Nil < self
|
109
|
+
handle(Rubinius::AST::NilLiteral)
|
110
|
+
VALUE = nil
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class Regexp < self
|
115
|
+
|
116
|
+
handle(Rubinius::AST::RegexLiteral)
|
117
|
+
|
118
|
+
def value
|
119
|
+
::Regexp.new(node.source)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
class String < self
|
124
|
+
|
125
|
+
handle(Rubinius::AST::StringLiteral)
|
126
|
+
|
127
|
+
def value
|
128
|
+
node.string
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
class Dynamic < self
|
134
|
+
|
135
|
+
def dispatch
|
136
|
+
emit(node.string.inspect[0..-2])
|
137
|
+
array.each_with_index do |member, index|
|
138
|
+
emit_member(member, index)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def array
|
143
|
+
node.array
|
144
|
+
end
|
145
|
+
|
146
|
+
def max
|
147
|
+
array.length - 1
|
148
|
+
end
|
149
|
+
memoize :max
|
150
|
+
|
151
|
+
def emit_primitive_member(member, index)
|
152
|
+
last = index < max ? -2 : -1
|
153
|
+
range = 1..last
|
154
|
+
emit(member.string.inspect[range])
|
155
|
+
end
|
156
|
+
|
157
|
+
def emit_member(member, index)
|
158
|
+
case member
|
159
|
+
when Rubinius::AST::StringLiteral
|
160
|
+
emit_primitive_member(member, index)
|
161
|
+
else
|
162
|
+
visit(member)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
class Symbol < self
|
167
|
+
|
168
|
+
handle(Rubinius::AST::DynamicSymbol)
|
169
|
+
|
170
|
+
def dispatch
|
171
|
+
emit(':')
|
172
|
+
super
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
class String < self
|
178
|
+
|
179
|
+
handle(Rubinius::AST::DynamicString)
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
class Execute < self
|
184
|
+
|
185
|
+
handle(Rubinius::AST::DynamicExecuteString)
|
186
|
+
|
187
|
+
def dispatch
|
188
|
+
emit('`')
|
189
|
+
emit(node.string.inspect[1..-2])
|
190
|
+
array.each_with_index do |member, index|
|
191
|
+
emit_member(member, index)
|
192
|
+
end
|
193
|
+
emit('`')
|
194
|
+
end
|
195
|
+
|
196
|
+
def emit_primitive_member(member, index)
|
197
|
+
last = index < max ? -3 : -2
|
198
|
+
range = 1..last
|
199
|
+
emit(member.string.inspect[range])
|
200
|
+
end
|
201
|
+
|
202
|
+
end
|
203
|
+
|
204
|
+
class Regex < self
|
205
|
+
|
206
|
+
handle(Rubinius::AST::DynamicRegex)
|
207
|
+
|
208
|
+
def dispatch
|
209
|
+
emit('/')
|
210
|
+
emit(Regexp.new(node.string).inspect[1..-2])
|
211
|
+
array.each_with_index do |member, index|
|
212
|
+
emit_member(member, index)
|
213
|
+
end
|
214
|
+
emit('/')
|
215
|
+
end
|
216
|
+
|
217
|
+
def emit_primitive_member(member, index)
|
218
|
+
last = index < max ? -3 : -2
|
219
|
+
range = 1..last
|
220
|
+
emit(Regexp.new(member.string).inspect[range])
|
221
|
+
end
|
222
|
+
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
|
227
|
+
class PassThrough < self
|
228
|
+
|
229
|
+
handle(Rubinius::AST::FixnumLiteral)
|
230
|
+
handle(Rubinius::AST::FloatLiteral)
|
231
|
+
|
232
|
+
def dispatch
|
233
|
+
emit(node.value)
|
234
|
+
end
|
235
|
+
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|