ruby_scribe 0.0.4 → 0.1.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.
- data/lib/ruby_scribe/emitter.rb +111 -129
- data/lib/ruby_scribe/emitter_config.rb +28 -0
- data/lib/ruby_scribe/emitter_helpers.rb +1 -1
- data/lib/ruby_scribe/ext/sexp.rb +1 -7
- data/lib/ruby_scribe/sexp_helpers.rb +285 -0
- data/lib/ruby_scribe/version.rb +1 -1
- data/lib/ruby_scribe.rb +2 -0
- data/spec/ruby_scribe/sexp_helpers_spec.rb +256 -0
- metadata +7 -17
- data/lib/ruby_scribe/transformer.rb +0 -18
- data/lib/ruby_scribe/transformer_helpers.rb +0 -7
- data/lib/ruby_scribe/transformers/block_method_to_procifier.rb +0 -43
- data/lib/ruby_scribe/transformers/custom.rb +0 -27
- data/lib/ruby_scribe/transformers/eachifier.rb +0 -39
- data/lib/ruby_scribe/transformers/symbol_names.rb +0 -68
- data/lib/ruby_scribe/transformers/tapifier.rb +0 -73
- data/spec/ruby_scribe/transformer_spec.rb +0 -27
- data/spec/ruby_scribe/transformers/block_method_to_procifier_spec.rb +0 -20
- data/spec/ruby_scribe/transformers/custom_spec.rb +0 -25
- data/spec/ruby_scribe/transformers/eachifier_spec.rb +0 -27
- data/spec/ruby_scribe/transformers/symbol_names_spec.rb +0 -44
- data/spec/ruby_scribe/transformers/tapifier_spec.rb +0 -57
data/lib/ruby_scribe/emitter.rb
CHANGED
@@ -4,26 +4,7 @@ module RubyScribe
|
|
4
4
|
# Takes a proprocessed S-expression and emits formatted Ruby code
|
5
5
|
class Emitter
|
6
6
|
include EmitterHelpers
|
7
|
-
|
8
|
-
class_inheritable_accessor :methods_without_parenthesis
|
9
|
-
self.methods_without_parenthesis = %w(
|
10
|
-
attr_accessor attr_reader attr_writer
|
11
|
-
alias alias_method alias_attribute
|
12
|
-
gem require extend include raise
|
13
|
-
delegate autoload raise
|
14
|
-
puts
|
15
|
-
)
|
16
|
-
|
17
|
-
class_inheritable_accessor :grouped_methods
|
18
|
-
self.grouped_methods = %w(require attr_accessor autoload)
|
19
|
-
|
20
|
-
class_inheritable_accessor :long_hash_key_size
|
21
|
-
self.long_hash_key_size = 5
|
22
|
-
|
23
|
-
class_inheritable_accessor :default_indent
|
24
|
-
self.default_indent = 2
|
25
|
-
|
26
|
-
SYNTACTIC_METHODS = ['+', '-', '<<', '==', '===', '>', '<']
|
7
|
+
include EmitterConfig
|
27
8
|
|
28
9
|
def emit(e)
|
29
10
|
return "" unless e
|
@@ -65,7 +46,7 @@ module RubyScribe
|
|
65
46
|
emit_multiple_assignment(e)
|
66
47
|
when :cdecl
|
67
48
|
emit_constant_declaration(e)
|
68
|
-
when :if
|
49
|
+
when :if
|
69
50
|
emit_conditional_block(e)
|
70
51
|
when :case
|
71
52
|
emit_case_statement(e)
|
@@ -102,10 +83,10 @@ module RubyScribe
|
|
102
83
|
end
|
103
84
|
|
104
85
|
def emit_block(e)
|
105
|
-
return "" if e.body
|
86
|
+
return "" if e.body.first == s(:nil)
|
106
87
|
|
107
88
|
# Special case for handling rescue blocks around entire methods (excluding the indent):
|
108
|
-
return emit_method_rescue(e.body
|
89
|
+
return emit_method_rescue(e.body.first) if e.body.first.rescue? && e.body.size == 1
|
109
90
|
|
110
91
|
e.body.map do |child|
|
111
92
|
emit_block_member_prefix(e.body, child) +
|
@@ -113,6 +94,7 @@ module RubyScribe
|
|
113
94
|
end.join(nl)
|
114
95
|
end
|
115
96
|
|
97
|
+
# TODO: Clean up block member prefix emitting
|
116
98
|
def emit_block_member_prefix(members, current_member)
|
117
99
|
previous_member_index = members.index(current_member) - 1
|
118
100
|
previous_member = previous_member_index >= 0 ? members[previous_member_index] : nil
|
@@ -125,17 +107,17 @@ module RubyScribe
|
|
125
107
|
return nl if (from == :any || from.include?(previous_member.kind)) && (to == :any || to.include?(current_member.kind))
|
126
108
|
end
|
127
109
|
|
128
|
-
if current_member.
|
110
|
+
if current_member.conditional? && [:block_if, :block_unless].include?(determine_if_type(current_member))
|
129
111
|
return nl
|
130
|
-
elsif previous_member.
|
112
|
+
elsif previous_member.conditional? && [:block_if, :block_unless].include?(determine_if_type(previous_member))
|
131
113
|
return nl
|
132
114
|
end
|
133
115
|
|
134
|
-
if previous_member.
|
116
|
+
if previous_member.call? && grouped_methods.include?(previous_member.name.to_s) && (!current_member.call? || (current_member.call? && current_member.name != previous_member.name))
|
135
117
|
return nl
|
136
118
|
end
|
137
119
|
|
138
|
-
if current_member.
|
120
|
+
if current_member.call? && grouped_methods.include?(current_member.name.to_s) && (!previous_member.call? || (previous_member.call? && previous_member.name != current_member.name))
|
139
121
|
return nl
|
140
122
|
end
|
141
123
|
|
@@ -143,14 +125,14 @@ module RubyScribe
|
|
143
125
|
end
|
144
126
|
|
145
127
|
def emit_scope(e)
|
146
|
-
emit(e.body
|
128
|
+
emit(e.body.first)
|
147
129
|
end
|
148
130
|
|
149
131
|
def emit_rescue_ensure_wrapper(e)
|
150
|
-
rescue_sexp = e.body
|
151
|
-
block = rescue_sexp.body.size == 1 ? nil : rescue_sexp.body
|
152
|
-
resbody = rescue_sexp.body.size == 1 ? rescue_sexp.body
|
153
|
-
ensure_sexp = e.body
|
132
|
+
rescue_sexp = e.body.first
|
133
|
+
block = rescue_sexp.body.size == 1 ? nil : rescue_sexp.body.first
|
134
|
+
resbody = rescue_sexp.body.size == 1 ? rescue_sexp.body.first : rescue_sexp.body.second
|
135
|
+
ensure_sexp = e.body.second
|
154
136
|
|
155
137
|
"begin" + indent { nl + emit(block) } +
|
156
138
|
emit(resbody) +
|
@@ -160,11 +142,11 @@ module RubyScribe
|
|
160
142
|
end
|
161
143
|
|
162
144
|
def emit_rescue(e, force_long = false)
|
163
|
-
block = e.body.size == 1 ? nil : e.body
|
164
|
-
resbody = e.body.size == 1 ? e.body
|
145
|
+
block = e.body.size == 1 ? nil : e.body.first
|
146
|
+
resbody = e.body.size == 1 ? e.body.first : e.body.second
|
165
147
|
|
166
|
-
if !force_long && e.line == resbody.line && block.kind != :block && resbody && resbody.body
|
167
|
-
"#{emit(block)} rescue #{emit(resbody.body
|
148
|
+
if !force_long && e.line == resbody.line && block.kind != :block && resbody && resbody.body.second && resbody.body.second.kind != :block
|
149
|
+
"#{emit(block)} rescue #{emit(resbody.body.second)}"
|
168
150
|
else
|
169
151
|
"begin" + indent { nl + emit(block) } +
|
170
152
|
emit(resbody) +
|
@@ -174,12 +156,12 @@ module RubyScribe
|
|
174
156
|
|
175
157
|
def emit_rescue_body(e)
|
176
158
|
nl("rescue ".gsub(/ $/, '')) +
|
177
|
-
indent { nl + emit(e.body
|
159
|
+
indent { nl + emit(e.body.second) }
|
178
160
|
end
|
179
161
|
|
180
162
|
def emit_method_rescue(e)
|
181
|
-
block = e.body.size == 1 ? nil : e.body
|
182
|
-
resbody = e.body.size == 1 ? e.body
|
163
|
+
block = e.body.size == 1 ? nil : e.body.first
|
164
|
+
resbody = e.body.size == 1 ? e.body.first : e.body.second
|
183
165
|
|
184
166
|
emit(block) +
|
185
167
|
indent(-2) { emit(resbody) }
|
@@ -187,37 +169,37 @@ module RubyScribe
|
|
187
169
|
|
188
170
|
def emit_class_definition(e)
|
189
171
|
emit_comments(e.comments) +
|
190
|
-
"#{e.kind} #{emit(e.body
|
191
|
-
(e.body
|
192
|
-
indent { nl + emit(e.body
|
172
|
+
"#{e.kind} #{emit(e.body.first)}" +
|
173
|
+
(e.body.second ? " < #{emit(e.body.second)}" : "") +
|
174
|
+
indent { nl + emit(e.body.third) } +
|
193
175
|
nl("end")
|
194
176
|
end
|
195
177
|
|
196
178
|
def emit_self_class_definition(e)
|
197
|
-
"class << #{emit(e.body
|
198
|
-
indent { nl + emit(e.body
|
179
|
+
"class << #{emit(e.body.first)}" +
|
180
|
+
indent { nl + emit(e.body.second) } +
|
199
181
|
nl("end")
|
200
182
|
end
|
201
183
|
|
202
184
|
def emit_module_definition(e)
|
203
185
|
emit_comments(e.comments) +
|
204
|
-
"module #{emit(e.body
|
205
|
-
indent { nl + emit(e.body
|
186
|
+
"module #{emit(e.body.first)}" +
|
187
|
+
indent { nl + emit(e.body.second) } +
|
206
188
|
nl("end")
|
207
189
|
end
|
208
190
|
|
209
191
|
def emit_method_definition(e)
|
210
192
|
emit_comments(e.comments) +
|
211
|
-
"def #{e.body
|
212
|
-
(e.body
|
213
|
-
indent { nl + emit(e.body
|
193
|
+
"def #{e.body.first}" +
|
194
|
+
(e.body.second.body.empty? ? "" : "(#{emit(e.body.second)})") +
|
195
|
+
indent { nl + emit(e.body.third) } +
|
214
196
|
nl("end")
|
215
197
|
end
|
216
198
|
|
217
199
|
def emit_method_with_receiver_definition(e)
|
218
200
|
emit_comments(e.comments) +
|
219
|
-
"def #{emit(e.body
|
220
|
-
(e.body
|
201
|
+
"def #{emit(e.body.first)}.#{e.body.second}" +
|
202
|
+
(e.body.third.body.empty? ? "" : "(#{emit(e.body.third)})") +
|
221
203
|
indent { nl + emit(e.body[3]) } +
|
222
204
|
nl("end")
|
223
205
|
end
|
@@ -227,7 +209,7 @@ module RubyScribe
|
|
227
209
|
e.body.each do |child|
|
228
210
|
if child.is_a?(Sexp) and child.kind == :block
|
229
211
|
child.body.each do |body_child|
|
230
|
-
array[array.index(body_child.body
|
212
|
+
array[array.index(body_child.body.first)] = emit(body_child)
|
231
213
|
end
|
232
214
|
else
|
233
215
|
array << child
|
@@ -237,8 +219,8 @@ module RubyScribe
|
|
237
219
|
end
|
238
220
|
|
239
221
|
def emit_method_call(e)
|
240
|
-
return emit_method_call_hash_access(e) if e.body
|
241
|
-
return emit_method_call_hash_assignment(e) if e.body
|
222
|
+
return emit_method_call_hash_access(e) if e.body.second == :[]
|
223
|
+
return emit_method_call_hash_assignment(e) if e.body.second == :[]=
|
242
224
|
|
243
225
|
emit_method_call_receiver(e) +
|
244
226
|
emit_method_call_name(e) +
|
@@ -246,37 +228,37 @@ module RubyScribe
|
|
246
228
|
end
|
247
229
|
|
248
230
|
def emit_method_call_receiver(e)
|
249
|
-
if e.body
|
250
|
-
"#{emit(e.body
|
251
|
-
elsif e.body
|
252
|
-
"#{emit(e.body
|
231
|
+
if e.body.first && syntactic_methods.include?(e.body.second.to_s)
|
232
|
+
"#{emit(e.body.first)} "
|
233
|
+
elsif e.body.first
|
234
|
+
"#{emit(e.body.first)}."
|
253
235
|
else
|
254
236
|
""
|
255
237
|
end
|
256
238
|
end
|
257
239
|
|
258
240
|
def emit_method_call_name(e)
|
259
|
-
emit(e.body
|
241
|
+
emit(e.body.second)
|
260
242
|
end
|
261
243
|
|
262
244
|
def emit_method_call_arguments(e)
|
263
|
-
if e.body
|
245
|
+
if e.body.third.body.empty?
|
264
246
|
""
|
265
|
-
elsif
|
266
|
-
" " + emit(e.body
|
267
|
-
elsif
|
268
|
-
" " + emit(e.body
|
247
|
+
elsif methods_without_parenthesis.include?(e.body.second.to_s)
|
248
|
+
" " + emit(e.body.third)
|
249
|
+
elsif syntactic_methods.include?(e.body.second.to_s)
|
250
|
+
" " + emit(e.body.third)
|
269
251
|
else
|
270
|
-
"(" + emit(e.body
|
252
|
+
"(" + emit(e.body.third) + ")"
|
271
253
|
end
|
272
254
|
end
|
273
255
|
|
274
256
|
def emit_method_call_hash_access(e)
|
275
|
-
emit(e.body
|
257
|
+
emit(e.body.first) + "[" + emit(e.body.third) + "]"
|
276
258
|
end
|
277
259
|
|
278
260
|
def emit_method_call_hash_assignment(e)
|
279
|
-
emit(e.body
|
261
|
+
emit(e.body.first) + "[" + emit(e.body.third.body.first) + "] = " + emit(e.body.third.body.second)
|
280
262
|
end
|
281
263
|
|
282
264
|
|
@@ -291,36 +273,36 @@ module RubyScribe
|
|
291
273
|
end
|
292
274
|
|
293
275
|
def emit_attribute_assignment(e)
|
294
|
-
return emit_method_call(e) if ['[]='].include?(e.body
|
276
|
+
return emit_method_call(e) if ['[]='].include?(e.body.second.to_s)
|
295
277
|
|
296
|
-
emit(e.body
|
278
|
+
emit(e.body.first) + "." + e.body.second.to_s.gsub(/=$/, "") + " = " + emit(e.body.third)
|
297
279
|
end
|
298
280
|
|
299
281
|
def emit_class_variable_assignment(e)
|
300
|
-
emit(e.body
|
282
|
+
emit(e.body.first) + " = " + emit(e.body.second)
|
301
283
|
end
|
302
284
|
|
303
285
|
def emit_multiple_assignment(e)
|
304
|
-
left = e.body
|
305
|
-
right = e.body
|
286
|
+
left = e.body.first.body
|
287
|
+
right = e.body.second.body
|
306
288
|
|
307
|
-
left.map {|c| c.body
|
289
|
+
left.map {|c| c.body.first }.join(", ") + " = " + right.map {|c| emit(c) }.join(", ")
|
308
290
|
end
|
309
291
|
|
310
292
|
def emit_constant_declaration(e)
|
311
|
-
emit(e.body
|
293
|
+
emit(e.body.first) + " = " + emit(e.body.second)
|
312
294
|
end
|
313
295
|
|
314
296
|
def determine_if_type(e)
|
315
|
-
if e.body
|
297
|
+
if e.body.second && e.body.third && e.body.first.line == e.body.second.try(:line) && e.line == e.body.third.try(:line)
|
316
298
|
:ternary
|
317
|
-
elsif e.body
|
299
|
+
elsif e.body.second && !e.body.third && e.line == e.body.second.line && e.body.second.kind != :block
|
318
300
|
:dangling_if
|
319
|
-
elsif !e.body
|
301
|
+
elsif !e.body.second && e.body.third && e.line == e.body.third.line && e.body.third.kind != :block
|
320
302
|
:dangling_unless
|
321
|
-
elsif e.body
|
303
|
+
elsif e.body.second
|
322
304
|
:block_if
|
323
|
-
elsif e.body
|
305
|
+
elsif e.body.third
|
324
306
|
:block_unless
|
325
307
|
end
|
326
308
|
end
|
@@ -328,17 +310,17 @@ module RubyScribe
|
|
328
310
|
def emit_conditional_block(e)
|
329
311
|
case determine_if_type(e)
|
330
312
|
when :ternary
|
331
|
-
"#{emit(e.body
|
313
|
+
"#{emit(e.body.first)} ? #{emit(e.body.second || s(:nil))} : #{emit(e.body.third || s(:nil))}"
|
332
314
|
when :dangling_if
|
333
|
-
"#{emit(e.body
|
315
|
+
"#{emit(e.body.second)} if #{emit(e.body.first)}"
|
334
316
|
when :dangling_unless
|
335
|
-
"#{emit(e.body
|
317
|
+
"#{emit(e.body.third)} unless #{emit(e.body.first)}"
|
336
318
|
when :block_if
|
337
|
-
"if #{emit(e.body
|
338
|
-
emit_conditional_else_block(e.body
|
319
|
+
"if #{emit(e.body.first)}" + indent { nl + emit(e.body.second) } +
|
320
|
+
emit_conditional_else_block(e.body.third) +
|
339
321
|
nl("end")
|
340
322
|
when :block_unless
|
341
|
-
"unless #{emit(e.body
|
323
|
+
"unless #{emit(e.body.first)}" + indent { nl + emit(e.body.third) } +
|
342
324
|
nl("end")
|
343
325
|
end
|
344
326
|
end
|
@@ -347,19 +329,19 @@ module RubyScribe
|
|
347
329
|
return "" unless e
|
348
330
|
|
349
331
|
if e.kind == :if
|
350
|
-
nl("elsif #{emit(e.body
|
351
|
-
emit_conditional_else_block(e.body
|
332
|
+
nl("elsif #{emit(e.body.first)}") + indent { nl + emit(e.body.second) } +
|
333
|
+
emit_conditional_else_block(e.body.third)
|
352
334
|
else
|
353
335
|
nl("else") + indent { nl + emit(e) }
|
354
336
|
end
|
355
337
|
end
|
356
338
|
|
357
339
|
def emit_case_statement(e)
|
358
|
-
"case #{emit(e.body
|
340
|
+
"case #{emit(e.body.first)}".gsub(/ $/, '') + e.body[1..-2].map {|c| emit(c) }.join + emit_case_else_statement(e.body[-1]) + nl("end")
|
359
341
|
end
|
360
342
|
|
361
343
|
def emit_case_when_statement(e)
|
362
|
-
nl("when #{emit_case_when_argument(e.body.first)}") + indent { nl + emit(e.body
|
344
|
+
nl("when #{emit_case_when_argument(e.body.first)}") + indent { nl + emit(e.body.second) }
|
363
345
|
end
|
364
346
|
|
365
347
|
def emit_case_else_statement(e)
|
@@ -375,57 +357,57 @@ module RubyScribe
|
|
375
357
|
end
|
376
358
|
|
377
359
|
def emit_loop_block(e)
|
378
|
-
"#{e.kind} #{emit(e.body
|
379
|
-
indent { nl + emit(e.body
|
360
|
+
"#{e.kind} #{emit(e.body.first)}" +
|
361
|
+
indent { nl + emit(e.body.second) } +
|
380
362
|
nl("end")
|
381
363
|
end
|
382
364
|
|
383
365
|
def emit_for_block(e)
|
384
|
-
"for #{e.body
|
385
|
-
indent { nl + emit(e.body
|
366
|
+
"for #{e.body.second.body.first} in #{emit(e.body.first)}" +
|
367
|
+
indent { nl + emit(e.body.third) } +
|
386
368
|
nl("end")
|
387
369
|
end
|
388
370
|
|
389
371
|
def emit_assignment_expression(e)
|
390
|
-
"#{e.body
|
372
|
+
"#{e.body.first} = #{emit(e.body.second)}"
|
391
373
|
end
|
392
374
|
|
393
375
|
def emit_optional_assignment_expression(e)
|
394
|
-
emit(e.body
|
376
|
+
emit(e.body.first) + "[#{emit(e.body.second)}] #{emit(e.body.third)}= " + emit(e.body[3])
|
395
377
|
end
|
396
378
|
|
397
379
|
def emit_optional_assignment_or_expression(e)
|
398
|
-
emit(e.body
|
380
|
+
emit(e.body.first) + " ||= " + emit(e.body.second.body.second)
|
399
381
|
end
|
400
382
|
|
401
383
|
def emit_optional_assignment_and_expression(e)
|
402
|
-
emit(e.body
|
384
|
+
emit(e.body.first) + " &&= " + emit(e.body.second.body.second)
|
403
385
|
end
|
404
386
|
|
405
387
|
def emit_binary_expression(e)
|
406
|
-
"(" + emit(e.body
|
388
|
+
"(" + emit(e.body.first) + " " +
|
407
389
|
(e.kind == :and ? "&&" : "||") +
|
408
|
-
" " + emit(e.body
|
390
|
+
" " + emit(e.body.second) + ")"
|
409
391
|
end
|
410
392
|
|
411
393
|
def emit_block_invocation(e)
|
412
|
-
emit(e.body
|
394
|
+
emit(e.body.first) + emit_block_invocation_body(e)
|
413
395
|
end
|
414
396
|
|
415
397
|
def emit_block_invocation_body(e)
|
416
398
|
# If it's on the same line, it should probably be shorthand form:
|
417
|
-
if e.line == e.body
|
418
|
-
" {#{emit_block_invocation_arguments(e)} #{emit(e.body
|
399
|
+
if e.line == e.body.third.try(:line)
|
400
|
+
" {#{emit_block_invocation_arguments(e)} #{emit(e.body.third)} }"
|
419
401
|
else
|
420
402
|
" do #{emit_block_invocation_arguments(e)}".gsub(/ +$/, '') +
|
421
|
-
indent { nl + emit(e.body
|
403
|
+
indent { nl + emit(e.body.third) } +
|
422
404
|
nl("end")
|
423
405
|
end
|
424
406
|
end
|
425
407
|
|
426
408
|
def emit_block_invocation_arguments(e)
|
427
|
-
if e.body
|
428
|
-
"|" + emit_assignments_as_arguments(e.body
|
409
|
+
if e.body.second
|
410
|
+
"|" + emit_assignments_as_arguments(e.body.second) + "|"
|
429
411
|
else
|
430
412
|
""
|
431
413
|
end
|
@@ -433,34 +415,34 @@ module RubyScribe
|
|
433
415
|
|
434
416
|
def emit_assignments_as_arguments(e)
|
435
417
|
if e.kind == :masgn
|
436
|
-
e.body
|
418
|
+
e.body.first.body.map {|c| emit_assignments_as_arguments(c) }.join(", ")
|
437
419
|
elsif e.kind == :lasgn
|
438
|
-
e.body
|
420
|
+
e.body.first.to_s
|
439
421
|
elsif e.kind == :splat
|
440
|
-
"*" + emit_assignments_as_arguments(e.body
|
422
|
+
"*" + emit_assignments_as_arguments(e.body.first)
|
441
423
|
end
|
442
424
|
end
|
443
425
|
|
444
426
|
def emit_defined_invocation(e)
|
445
|
-
"defined?(#{emit(e.body
|
427
|
+
"defined?(#{emit(e.body.first)})"
|
446
428
|
end
|
447
429
|
|
448
430
|
def emit_token(e)
|
449
431
|
case e.kind
|
450
432
|
when :str
|
451
|
-
'"' + e.body
|
433
|
+
'"' + e.body.first + '"'
|
452
434
|
when :lit
|
453
|
-
e.body
|
435
|
+
e.body.first.inspect
|
454
436
|
when :const
|
455
|
-
e.body
|
437
|
+
e.body.first.to_s
|
456
438
|
when :lvar
|
457
|
-
e.body
|
439
|
+
e.body.first.to_s
|
458
440
|
when :ivar
|
459
|
-
e.body
|
441
|
+
e.body.first.to_s
|
460
442
|
when :cvar
|
461
|
-
e.body
|
443
|
+
e.body.first.to_s
|
462
444
|
when :not
|
463
|
-
"!" + emit(e.body
|
445
|
+
"!" + emit(e.body.first)
|
464
446
|
when :true
|
465
447
|
"true"
|
466
448
|
when :false
|
@@ -482,41 +464,41 @@ module RubyScribe
|
|
482
464
|
when :return
|
483
465
|
"return #{emit_argument_list(e)}".strip
|
484
466
|
when :alias
|
485
|
-
"alias #{emit(e.body
|
467
|
+
"alias #{emit(e.body.first)} #{emit(e.body.second)}"
|
486
468
|
when :block_pass
|
487
|
-
"&" + emit(e.body
|
469
|
+
"&" + emit(e.body.first)
|
488
470
|
when :splat
|
489
|
-
"*" + emit(e.body
|
471
|
+
"*" + emit(e.body.first)
|
490
472
|
when :colon2
|
491
|
-
"#{emit(e.body
|
473
|
+
"#{emit(e.body.first)}::#{emit(e.body.second)}"
|
492
474
|
when :colon3
|
493
|
-
"::#{emit(e.body
|
475
|
+
"::#{emit(e.body.first)}"
|
494
476
|
when :dot2
|
495
|
-
"#{emit(e.body
|
477
|
+
"#{emit(e.body.first)}..#{emit(e.body.second)}"
|
496
478
|
when :hash
|
497
479
|
"{" + emit_hash_body(e) + "}"
|
498
480
|
when :array
|
499
481
|
"[" + e.body.map {|c| emit(c)}.join(", ") + "]"
|
500
482
|
when :nth_ref, :back_ref
|
501
|
-
"$" + e.body
|
483
|
+
"$" + e.body.first.to_s
|
502
484
|
when :gvar
|
503
|
-
e.body
|
485
|
+
e.body.first.to_s
|
504
486
|
when :dstr
|
505
487
|
'"' + literalize_strings(e.body).map {|c| emit(c) }.join + '"'
|
506
488
|
when :dregx
|
507
489
|
'/' + literalize_strings(e.body).map {|c| emit(c) }.join + '/'
|
508
490
|
when :evstr
|
509
|
-
'#{' + emit(e.body
|
491
|
+
'#{' + emit(e.body.first) + '}'
|
510
492
|
when :xstr
|
511
|
-
'`' + emit(e.body
|
493
|
+
'`' + emit(e.body.first) + '`'
|
512
494
|
when :dxstr
|
513
495
|
'`' + literalize_strings(e.body).map {|c| emit(c) }.join + '`'
|
514
496
|
when :dsym
|
515
497
|
':"' + literalize_strings(e.body).map {|c| emit(c) }.join + '"'
|
516
498
|
when :match3
|
517
|
-
emit(e.body
|
499
|
+
emit(e.body.second) + " =~ " + emit(e.body.first)
|
518
500
|
when :cvdecl
|
519
|
-
emit(e.body
|
501
|
+
emit(e.body.first.to_s) + " = " + emit(e.body.second)
|
520
502
|
else
|
521
503
|
emit_unknown_expression(e)
|
522
504
|
end
|
@@ -525,7 +507,7 @@ module RubyScribe
|
|
525
507
|
def emit_hash_body(e, force_short = false)
|
526
508
|
grouped = e.body.in_groups_of(2)
|
527
509
|
|
528
|
-
if !force_short && grouped.size >=
|
510
|
+
if !force_short && grouped.size >= long_hash_key_size
|
529
511
|
indent(2) { nl + grouped.map {|g| "#{emit(g[0])} => #{emit(g[1])}" }.join("," + nl) } + nl
|
530
512
|
else
|
531
513
|
grouped.map {|g| "#{emit(g[0])} => #{emit(g[1])}" }.join(", ")
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module RubyScribe
|
2
|
+
module EmitterConfig
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
attr_accessor :methods_without_parenthesis
|
7
|
+
attr_accessor :grouped_methods
|
8
|
+
attr_accessor :long_hash_key_size
|
9
|
+
attr_accessor :default_indent
|
10
|
+
attr_accessor :syntactic_methods
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
self.grouped_methods = %w(require attr_accessor autoload)
|
15
|
+
self.long_hash_key_size = 5
|
16
|
+
self.default_indent = 2
|
17
|
+
self.syntactic_methods = ['+', '-', '<<', '==', '===', '>', '<']
|
18
|
+
|
19
|
+
self.methods_without_parenthesis = %w(
|
20
|
+
attr_accessor attr_reader attr_writer
|
21
|
+
alias alias_method alias_attribute
|
22
|
+
gem require extend include raise
|
23
|
+
delegate autoload raise
|
24
|
+
puts
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|