ruby_scribe 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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, :unless
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[0] == s(:nil)
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[0]) if e.body.size == 1 and e.body[0].kind == :rescue
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.kind == :if && [:block_if, :block_unless].include?(determine_if_type(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.kind == :if && [:block_if, :block_unless].include?(determine_if_type(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.kind == :call && self.class.grouped_methods.include?(previous_member.body[1].to_s) && (current_member.kind != :call || (current_member.kind == :call && current_member.body[1] != previous_member.body[1]))
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.kind == :call && self.class.grouped_methods.include?(current_member.body[1].to_s) && (previous_member.kind != :call || (previous_member.kind == :call && previous_member.body[1] != current_member.body[1]))
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[0])
128
+ emit(e.body.first)
147
129
  end
148
130
 
149
131
  def emit_rescue_ensure_wrapper(e)
150
- rescue_sexp = e.body[0]
151
- block = rescue_sexp.body.size == 1 ? nil : rescue_sexp.body[0]
152
- resbody = rescue_sexp.body.size == 1 ? rescue_sexp.body[0] : rescue_sexp.body[1]
153
- ensure_sexp = e.body[1]
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[0]
164
- resbody = e.body.size == 1 ? e.body[0] : e.body[1]
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[1] && resbody.body[1].kind != :block
167
- "#{emit(block)} rescue #{emit(resbody.body[1])}"
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[1]) }
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[0]
182
- resbody = e.body.size == 1 ? e.body[0] : e.body[1]
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[0])}" +
191
- (e.body[1] ? " < #{emit(e.body[1])}" : "") +
192
- indent { nl + emit(e.body[2]) } +
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[0])}" +
198
- indent { nl + emit(e.body[1]) } +
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[0])}" +
205
- indent { nl + emit(e.body[1]) } +
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[0]}" +
212
- (e.body[1].body.empty? ? "" : "(#{emit(e.body[1])})") +
213
- indent { nl + emit(e.body[2]) } +
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[0])}.#{e.body[1]}" +
220
- (e.body[2].body.empty? ? "" : "(#{emit(e.body[2])})") +
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[0])] = emit(body_child)
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[1] == :[]
241
- return emit_method_call_hash_assignment(e) if e.body[1] == :[]=
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[0] && SYNTACTIC_METHODS.include?(e.body[1].to_s)
250
- "#{emit(e.body[0])} "
251
- elsif e.body[0]
252
- "#{emit(e.body[0])}."
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[1])
241
+ emit(e.body.second)
260
242
  end
261
243
 
262
244
  def emit_method_call_arguments(e)
263
- if e.body[2].body.empty?
245
+ if e.body.third.body.empty?
264
246
  ""
265
- elsif self.class.methods_without_parenthesis.include?(e.body[1].to_s)
266
- " " + emit(e.body[2])
267
- elsif SYNTACTIC_METHODS.include?(e.body[1].to_s)
268
- " " + emit(e.body[2])
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[2]) + ")"
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[0]) + "[" + emit(e.body[2]) + "]"
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[0]) + "[" + emit(e.body[2].body[0]) + "] = " + emit(e.body[2].body[1])
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[1].to_s)
276
+ return emit_method_call(e) if ['[]='].include?(e.body.second.to_s)
295
277
 
296
- emit(e.body[0]) + "." + e.body[1].to_s.gsub(/=$/, "") + " = " + emit(e.body[2])
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[0]) + " = " + emit(e.body[1])
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[0].body
305
- right = e.body[1].body
286
+ left = e.body.first.body
287
+ right = e.body.second.body
306
288
 
307
- left.map {|c| c.body[0] }.join(", ") + " = " + right.map {|c| emit(c) }.join(", ")
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[0]) + " = " + emit(e.body[1])
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[1] && e.body[2] && e.body[0].line == e.body[1].try(:line) && e.line == e.body[2].try(:line)
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[1] && !e.body[2] && e.line == e.body[1].line && e.body[1].kind != :block
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[1] && e.body[2] && e.line == e.body[2].line && e.body[2].kind != :block
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[1]
303
+ elsif e.body.second
322
304
  :block_if
323
- elsif e.body[2]
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[0])} ? #{emit(e.body[1] || s(:nil))} : #{emit(e.body[2] || s(:nil))}"
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[1])} if #{emit(e.body[0])}"
315
+ "#{emit(e.body.second)} if #{emit(e.body.first)}"
334
316
  when :dangling_unless
335
- "#{emit(e.body[2])} unless #{emit(e.body[0])}"
317
+ "#{emit(e.body.third)} unless #{emit(e.body.first)}"
336
318
  when :block_if
337
- "if #{emit(e.body[0])}" + indent { nl + emit(e.body[1]) } +
338
- emit_conditional_else_block(e.body[2]) +
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[0])}" + indent { nl + emit(e.body[2]) } +
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[0])}") + indent { nl + emit(e.body[1]) } +
351
- emit_conditional_else_block(e.body[2])
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[0])}".gsub(/ $/, '') + e.body[1..-2].map {|c| emit(c) }.join + emit_case_else_statement(e.body[-1]) + nl("end")
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[1]) }
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[0])}" +
379
- indent { nl + emit(e.body[1]) } +
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[1].body[0]} in #{emit(e.body[0])}" +
385
- indent { nl + emit(e.body[2]) } +
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[0]} = #{emit(e.body[1])}"
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[0]) + "[#{emit(e.body[1])}] #{emit(e.body[2])}= " + emit(e.body[3])
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[0]) + " ||= " + emit(e.body[1].body[1])
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[0]) + " &&= " + emit(e.body[1].body[1])
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[0]) + " " +
388
+ "(" + emit(e.body.first) + " " +
407
389
  (e.kind == :and ? "&&" : "||") +
408
- " " + emit(e.body[1]) + ")"
390
+ " " + emit(e.body.second) + ")"
409
391
  end
410
392
 
411
393
  def emit_block_invocation(e)
412
- emit(e.body[0]) + emit_block_invocation_body(e)
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[2].try(:line)
418
- " {#{emit_block_invocation_arguments(e)} #{emit(e.body[2])} }"
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[2]) } +
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[1]
428
- "|" + emit_assignments_as_arguments(e.body[1]) + "|"
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[0].body.map {|c| emit_assignments_as_arguments(c) }.join(", ")
418
+ e.body.first.body.map {|c| emit_assignments_as_arguments(c) }.join(", ")
437
419
  elsif e.kind == :lasgn
438
- e.body[0].to_s
420
+ e.body.first.to_s
439
421
  elsif e.kind == :splat
440
- "*" + emit_assignments_as_arguments(e.body[0])
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[0])})"
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[0] + '"'
433
+ '"' + e.body.first + '"'
452
434
  when :lit
453
- e.body[0].inspect
435
+ e.body.first.inspect
454
436
  when :const
455
- e.body[0].to_s
437
+ e.body.first.to_s
456
438
  when :lvar
457
- e.body[0].to_s
439
+ e.body.first.to_s
458
440
  when :ivar
459
- e.body[0].to_s
441
+ e.body.first.to_s
460
442
  when :cvar
461
- e.body[0].to_s
443
+ e.body.first.to_s
462
444
  when :not
463
- "!" + emit(e.body[0])
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[0])} #{emit(e.body[1])}"
467
+ "alias #{emit(e.body.first)} #{emit(e.body.second)}"
486
468
  when :block_pass
487
- "&" + emit(e.body[0])
469
+ "&" + emit(e.body.first)
488
470
  when :splat
489
- "*" + emit(e.body[0])
471
+ "*" + emit(e.body.first)
490
472
  when :colon2
491
- "#{emit(e.body[0])}::#{emit(e.body[1])}"
473
+ "#{emit(e.body.first)}::#{emit(e.body.second)}"
492
474
  when :colon3
493
- "::#{emit(e.body[0])}"
475
+ "::#{emit(e.body.first)}"
494
476
  when :dot2
495
- "#{emit(e.body[0])}..#{emit(e.body[1])}"
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[0].to_s
483
+ "$" + e.body.first.to_s
502
484
  when :gvar
503
- e.body[0].to_s
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[0]) + '}'
491
+ '#{' + emit(e.body.first) + '}'
510
492
  when :xstr
511
- '`' + emit(e.body[0]) + '`'
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[1]) + " =~ " + emit(e.body[0])
499
+ emit(e.body.second) + " =~ " + emit(e.body.first)
518
500
  when :cvdecl
519
- emit(e.body[0].to_s) + " = " + emit(e.body[1])
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 >= self.class.long_hash_key_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
@@ -9,7 +9,7 @@ module RubyScribe
9
9
  end
10
10
 
11
11
  def indent(level = nil)
12
- level = self.class.default_indent if level.nil?
12
+ level = default_indent if level.nil?
13
13
 
14
14
  indents.push(level)
15
15
  output = yield
@@ -1,9 +1,3 @@
1
1
  class Sexp
2
- def kind
3
- sexp_type.to_sym
4
- end
5
-
6
- def body
7
- sexp_body
8
- end
2
+ include RubyScribe::SexpHelpers
9
3
  end