sorcerer 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,11 +18,16 @@ module Sorcerer
18
18
 
19
19
  def initialize(sexp, options={})
20
20
  @sexp = sexp
21
- @source = ''
22
21
  @debug = options[:debug]
23
- @multiline = options[:multiline]
22
+ @indent = options[:indent] || 0
23
+ @indent = 2 if @indent && ! @indent.is_a?(Integer)
24
+ @multiline = options[:multiline] || indenting?
25
+
26
+ @source = ''
24
27
  @word_level = 0
25
28
  @stack = []
29
+ @level = 0
30
+ @virgin_line = true
26
31
  end
27
32
 
28
33
  def source
@@ -31,6 +36,32 @@ module Sorcerer
31
36
  @source
32
37
  end
33
38
 
39
+ private
40
+
41
+ def multiline?
42
+ @multiline
43
+ end
44
+
45
+ def indenting?
46
+ @indent > 0
47
+ end
48
+
49
+ def indent
50
+ old_level = @level
51
+ @level += 1
52
+ yield
53
+ ensure
54
+ @level = old_level
55
+ end
56
+
57
+ def outdent
58
+ old_level = @level
59
+ @level -= 1
60
+ yield
61
+ ensure
62
+ @level = old_level
63
+ end
64
+
34
65
  def sexp?(obj)
35
66
  obj && obj.respond_to?(:each) && obj.first.is_a?(Symbol)
36
67
  end
@@ -48,8 +79,13 @@ module Sorcerer
48
79
  puts "----------------------------------------------------------"
49
80
  pp sexp
50
81
  end
82
+ apply_handler(sexp, handler)
83
+ end
84
+
85
+ def apply_handler(sexp, handler)
51
86
  @stack.push(sexp.first)
52
- handler.call(self, sexp)
87
+ instance_exec(sexp, &handler)
88
+ ensure
53
89
  @stack.pop
54
90
  end
55
91
 
@@ -57,14 +93,16 @@ module Sorcerer
57
93
  emit(" ")
58
94
  emit(do_word)
59
95
  resource(sexp[1]) if sexp[1] # Arguments
60
- if ! void?(sexp[2])
61
- soft_newline
62
- resource(sexp[2]) # Statements
63
- end
64
- if !void?(sexp[2])
65
- soft_newline
66
- else
67
- emit(" ")
96
+ indent do
97
+ if ! void?(sexp[2])
98
+ soft_newline
99
+ resource(sexp[2]) # Statements
100
+ end
101
+ if !void?(sexp[2])
102
+ soft_newline
103
+ else
104
+ emit(" ")
105
+ end
68
106
  end
69
107
  emit(end_word)
70
108
  end
@@ -87,7 +125,13 @@ module Sorcerer
87
125
  end
88
126
 
89
127
  def emit(string)
90
- puts "EMITTING '#{string}' (#{last_handler})" if @debug
128
+ emit_raw(" " * @level) if indenting? && virgin_line?
129
+ @virgin_line = false
130
+ emit_raw(string.to_s)
131
+ end
132
+
133
+ def emit_raw(string)
134
+ puts "EMITTING '#{string}' (#{last_handler}) [#{@level}]" if @debug
91
135
  @source << string.to_s
92
136
  end
93
137
 
@@ -95,6 +139,14 @@ module Sorcerer
95
139
  raise "Handler for #{sexp.first} not implemented (#{sexp.inspect})"
96
140
  end
97
141
 
142
+ def emit_then
143
+ if multiline?
144
+ soft_newline
145
+ else
146
+ emit(" then ")
147
+ end
148
+ end
149
+
98
150
  def emit_separator(sep, first)
99
151
  emit(sep) unless first
100
152
  false
@@ -151,17 +203,18 @@ module Sorcerer
151
203
  sexp == VOID_BODY2
152
204
  end
153
205
 
154
- def multiline?
155
- @multiline
156
- end
157
-
158
206
  def last_handler
159
207
  @stack.last
160
208
  end
161
209
 
210
+ def virgin_line?
211
+ @virgin_line
212
+ end
213
+
162
214
  def newline
163
215
  if multiline?
164
216
  emit("\n")
217
+ @virgin_line = true
165
218
  else
166
219
  emit("; ")
167
220
  end
@@ -169,7 +222,7 @@ module Sorcerer
169
222
 
170
223
  def soft_newline
171
224
  if multiline?
172
- emit("\n")
225
+ newline
173
226
  else
174
227
  emit(" ")
175
228
  end
@@ -182,7 +235,7 @@ module Sorcerer
182
235
  ']' => '[',
183
236
  }
184
237
 
185
- def self.determine_regexp_delimiters(sexp)
238
+ def determine_regexp_delimiters(sexp)
186
239
  sym, end_delim, other = sexp
187
240
  fail UnexpectedSexpError, "Expected :@regexp_end, got #{sym.inspect}" unless sym == :@regexp_end
188
241
  end_delim_char = end_delim[0]
@@ -193,586 +246,595 @@ module Sorcerer
193
246
  [first_delim, end_delim]
194
247
  end
195
248
 
196
- NYI = lambda { |src, sexp| src.nyi(sexp) }
197
- DBG = lambda { |src, sexp| pp(sexp) }
198
- NOOP = lambda { |src, sexp| }
199
- SPACE = lambda { |src, sexp| src.emit(" ") }
200
- PASS1 = lambda { |src, sexp| src.resource(sexp[1]) }
201
- PASS2 = lambda { |src, sexp| src.resource(sexp[2]) }
202
- EMIT1 = lambda { |src, sexp| src.emit(sexp[1]) }
249
+ NYI = lambda { |sexp| nyi(sexp) }
250
+ DBG = lambda { |sexp| pp(sexp) }
251
+ NOOP = lambda { |sexp| }
252
+ SPACE = lambda { |sexp| emit(" ") }
253
+ PASS1 = lambda { |sexp| resource(sexp[1]) }
254
+ PASS2 = lambda { |sexp| resource(sexp[2]) }
255
+ EMIT1 = lambda { |sexp| emit(sexp[1]) }
203
256
 
204
257
  HANDLERS = {
205
258
  # parser keywords
206
259
 
207
- :BEGIN => lambda { |src, sexp|
208
- src.emit("BEGIN {")
209
- if src.void?(sexp[1])
210
- src.emit " }"
260
+ :BEGIN => lambda { |sexp|
261
+ emit("BEGIN {")
262
+ if void?(sexp[1])
263
+ emit " }"
211
264
  else
212
- src.soft_newline
213
- src.resource(sexp[1])
214
- src.soft_newline
215
- src.emit("}")
265
+ soft_newline
266
+ indent do
267
+ resource(sexp[1])
268
+ soft_newline
269
+ end
270
+ emit("}")
216
271
  end
217
272
  },
218
- :END => lambda { |src, sexp|
219
- src.emit("END {")
220
- if src.void?(sexp[1])
221
- src.emit(" }")
273
+ :END => lambda { |sexp|
274
+ emit("END {")
275
+ if void?(sexp[1])
276
+ emit(" }")
222
277
  else
223
- src.soft_newline
224
- src.resource(sexp[1])
225
- src.soft_newline
226
- src.emit("}")
278
+ soft_newline
279
+ indent do
280
+ resource(sexp[1])
281
+ soft_newline
282
+ end
283
+ emit("}")
227
284
  end
228
285
  },
229
- :alias => lambda { |src, sexp|
230
- src.emit("alias ")
231
- src.resource(sexp[1])
232
- src.emit(" ")
233
- src.resource(sexp[2])
286
+ :alias => lambda { |sexp|
287
+ emit("alias ")
288
+ resource(sexp[1])
289
+ emit(" ")
290
+ resource(sexp[2])
234
291
  },
235
292
  :alias_error => NYI,
236
- :aref => lambda { |src, sexp|
237
- src.resource(sexp[1])
238
- src.emit("[")
239
- src.resource(sexp[2])
240
- src.emit("]")
241
- },
242
- :aref_field => lambda { |src, sexp|
243
- src.resource(sexp[1])
244
- src.emit("[")
245
- src.resource(sexp[2])
246
- src.emit("]")
293
+ :aref => lambda { |sexp|
294
+ resource(sexp[1])
295
+ emit("[")
296
+ resource(sexp[2])
297
+ emit("]")
298
+ },
299
+ :aref_field => lambda { |sexp|
300
+ resource(sexp[1])
301
+ emit("[")
302
+ resource(sexp[2])
303
+ emit("]")
247
304
  },
248
305
  :arg_ambiguous => NYI,
249
- :arg_paren => lambda { |src, sexp|
250
- src.emit("(")
251
- src.resource(sexp[1]) if sexp[1]
252
- src.emit(")")
306
+ :arg_paren => lambda { |sexp|
307
+ emit("(")
308
+ resource(sexp[1]) if sexp[1]
309
+ emit(")")
253
310
  },
254
- :args_add => lambda { |src, sexp|
255
- src.resource(sexp[1])
311
+ :args_add => lambda { |sexp|
312
+ resource(sexp[1])
256
313
  if sexp[1].first != :args_new
257
- src.emit(", ")
314
+ emit(", ")
258
315
  end
259
- src.resource(sexp[2])
316
+ resource(sexp[2])
260
317
  },
261
- :args_add_block => lambda { |src, sexp|
262
- src.resource(sexp[1])
318
+ :args_add_block => lambda { |sexp|
319
+ resource(sexp[1])
263
320
  if sexp[2]
264
321
  if sexp[1].first != :args_new
265
- src.emit(", ")
322
+ emit(", ")
266
323
  end
267
324
  if sexp[2]
268
- src.emit("&")
269
- src.resource(sexp[2])
325
+ emit("&")
326
+ resource(sexp[2])
270
327
  end
271
328
  end
272
329
  },
273
- :args_add_star => lambda { |src, sexp|
274
- src.resource(sexp[1])
330
+ :args_add_star => lambda { |sexp|
331
+ resource(sexp[1])
275
332
  if sexp[1].first != :args_new
276
- src.emit(", ")
333
+ emit(", ")
277
334
  end
278
- src.emit("*")
279
- src.resource(sexp[2])
335
+ emit("*")
336
+ resource(sexp[2])
280
337
  },
281
338
  :args_new => NOOP,
282
339
  :args_prepend => NYI,
283
- :array => lambda { |src, sexp|
284
- src.emit("[")
285
- src.resource(sexp[1]) if sexp[1]
286
- src.emit("]")
340
+ :array => lambda { |sexp|
341
+ emit("[")
342
+ resource(sexp[1]) if sexp[1]
343
+ emit("]")
287
344
  },
288
- :assign => lambda { |src, sexp|
289
- src.resource(sexp[1])
290
- src.emit(" = ")
291
- src.resource(sexp[2])
345
+ :assign => lambda { |sexp|
346
+ resource(sexp[1])
347
+ emit(" = ")
348
+ resource(sexp[2])
292
349
  },
293
350
  :assign_error => NYI,
294
- :assoc_new => lambda { |src, sexp|
295
- src.resource(sexp[1])
296
- src.emit(" => ")
297
- src.resource(sexp[2])
351
+ :assoc_new => lambda { |sexp|
352
+ resource(sexp[1])
353
+ emit(" => ")
354
+ resource(sexp[2])
298
355
  },
299
- :assoclist_from_args => lambda { |src, sexp|
356
+ :assoclist_from_args => lambda { |sexp|
300
357
  first = true
301
358
  sexp[1].each do |sx|
302
- src.emit(", ") unless first
359
+ emit(", ") unless first
303
360
  first = false
304
- src.resource(sx)
361
+ resource(sx)
305
362
  end
306
363
  },
307
- :bare_assoc_hash => lambda { |src, sexp|
364
+ :bare_assoc_hash => lambda { |sexp|
308
365
  first = true
309
366
  sexp[1].each do |sx|
310
- src.emit(", ") unless first
367
+ emit(", ") unless first
311
368
  first = false
312
- src.resource(sx)
369
+ resource(sx)
313
370
  end
314
371
  },
315
- :begin => lambda { |src, sexp|
316
- src.emit("begin")
317
- if src.void?(sexp[1])
318
- src.emit(" ")
319
- else
320
- src.soft_newline
321
- src.resource(sexp[1])
372
+ :begin => lambda { |sexp|
373
+ emit("begin")
374
+ indent do
375
+ if void?(sexp[1])
376
+ emit(" ")
377
+ else
378
+ soft_newline
379
+ resource(sexp[1])
380
+ end
322
381
  end
323
- src.emit("end")
382
+ emit("end")
324
383
  },
325
- :binary => lambda { |src, sexp|
326
- src.resource(sexp[1])
327
- src.emit(" #{sexp[2]} ")
328
- src.resource(sexp[3])
384
+ :binary => lambda { |sexp|
385
+ resource(sexp[1])
386
+ emit(" #{sexp[2]} ")
387
+ resource(sexp[3])
329
388
  },
330
- :block_var => lambda { |src, sexp|
331
- src.emit(" |")
332
- src.resource(sexp[1])
333
- src.emit("|")
389
+ :block_var => lambda { |sexp|
390
+ emit(" |")
391
+ resource(sexp[1])
392
+ emit("|")
334
393
  },
335
394
  :block_var_add_block => NYI,
336
395
  :block_var_add_star => NYI,
337
- :blockarg => lambda { |src, sexp|
338
- src.emit("&")
339
- src.resource(sexp[1])
340
- },
341
- :body_stmt => lambda { |src, sexp|
342
- src.resource(sexp[1]) # Main Body
343
- src.newline unless src.void?(sexp[1])
344
- src.resource(sexp[2]) if sexp[2] # Rescue
345
- src.resource(sexp[4]) if sexp[4] # Ensure
346
- },
347
- :brace_block => lambda { |src, sexp|
348
- src.emit_block(sexp, "{", "}")
349
- },
350
- :break => lambda { |src, sexp|
351
- src.emit("break")
352
- src.emit(" ") unless sexp[1] == [:args_new]
353
- src.resource(sexp[1])
354
- },
355
- :call => lambda { |src, sexp|
356
- src.resource(sexp[1])
357
- src.emit(sexp[2])
358
- src.resource(sexp[3]) unless sexp[3] == :call
359
- },
360
- :case => lambda { |src, sexp|
361
- src.emit("case ")
362
- src.resource(sexp[1])
363
- src.soft_newline
364
- src.resource(sexp[2])
365
- src.newline
366
- src.emit("end")
367
- },
368
- :class => lambda { |src, sexp|
369
- src.emit("class ")
370
- src.resource(sexp[1])
371
- if ! src.void?(sexp[2])
372
- src.emit " < "
373
- src.resource(sexp[2])
396
+ :blockarg => lambda { |sexp|
397
+ emit("&")
398
+ resource(sexp[1])
399
+ },
400
+ :body_stmt => lambda { |sexp|
401
+ resource(sexp[1]) # Main Body
402
+ newline unless void?(sexp[1])
403
+ resource(sexp[2]) if sexp[2] # Rescue
404
+ resource(sexp[4]) if sexp[4] # Ensure
405
+ },
406
+ :brace_block => lambda { |sexp|
407
+ emit_block(sexp, "{", "}")
408
+ },
409
+ :break => lambda { |sexp|
410
+ emit("break")
411
+ emit(" ") unless sexp[1] == [:args_new]
412
+ resource(sexp[1])
413
+ },
414
+ :call => lambda { |sexp|
415
+ resource(sexp[1])
416
+ emit(sexp[2])
417
+ resource(sexp[3]) unless sexp[3] == :call
418
+ },
419
+ :case => lambda { |sexp|
420
+ emit("case ")
421
+ resource(sexp[1])
422
+ soft_newline
423
+ indent do
424
+ resource(sexp[2])
425
+ newline
374
426
  end
375
- src.newline
376
- src.resource(sexp[3]) unless src.void?(sexp[3])
377
- src.emit("end")
427
+ emit("end")
428
+ },
429
+ :class => lambda { |sexp|
430
+ emit("class ")
431
+ resource(sexp[1])
432
+ if ! void?(sexp[2])
433
+ emit " < "
434
+ resource(sexp[2])
435
+ end
436
+ newline
437
+ indent do
438
+ resource(sexp[3]) unless void?(sexp[3])
439
+ end
440
+ emit("end")
378
441
  },
379
442
  :class_name_error => NYI,
380
- :command => lambda { |src, sexp|
381
- src.resource(sexp[1])
382
- src.emit(" ")
383
- src.resource(sexp[2])
443
+ :command => lambda { |sexp|
444
+ resource(sexp[1])
445
+ emit(" ")
446
+ resource(sexp[2])
384
447
  },
385
448
  :command_call => NYI,
386
- :const_path_field => lambda { |src, sexp|
387
- src.resource(sexp[1])
388
- src.emit("::")
389
- src.resource(sexp[2])
449
+ :const_path_field => lambda { |sexp|
450
+ resource(sexp[1])
451
+ emit("::")
452
+ resource(sexp[2])
390
453
  },
391
- :const_path_ref => lambda { |src, sexp|
392
- src.resource(sexp[1])
393
- src.emit("::")
394
- src.resource(sexp[2])
454
+ :const_path_ref => lambda { |sexp|
455
+ resource(sexp[1])
456
+ emit("::")
457
+ resource(sexp[2])
395
458
  },
396
459
  :const_ref => PASS1,
397
- :def => lambda { |src, sexp|
398
- src.emit("def ")
399
- src.resource(sexp[1])
400
- src.opt_parens(sexp[2])
401
- src.newline
402
- src.resource(sexp[3])
403
- src.emit("end")
404
- },
405
- :defined => lambda { |src, sexp|
406
- src.emit("defined?(")
407
- src.resource(sexp[1])
408
- src.emit(")")
460
+ :def => lambda { |sexp|
461
+ emit("def ")
462
+ resource(sexp[1])
463
+ opt_parens(sexp[2])
464
+ newline
465
+ indent do resource(sexp[3]) end
466
+ emit("end")
467
+ },
468
+ :defined => lambda { |sexp|
469
+ emit("defined?(")
470
+ resource(sexp[1])
471
+ emit(")")
409
472
  },
410
473
  :defs => NYI,
411
- :do_block => lambda { |src, sexp|
412
- src.emit_block(sexp, "do", "end")
413
- },
414
- :dot2 => lambda { |src, sexp|
415
- src.resource(sexp[1])
416
- src.emit("..")
417
- src.resource(sexp[2])
418
- },
419
- :dot3 => lambda { |src, sexp|
420
- src.resource(sexp[1])
421
- src.emit("...")
422
- src.resource(sexp[2])
423
- },
424
- :dyna_symbol => lambda { |src, sexp|
425
- src.emit(':"')
426
- src.resource(sexp[1])
427
- src.emit('"')
428
- },
429
- :else => lambda { |src, sexp|
430
- src.soft_newline
431
- src.emit("else")
432
- src.soft_newline
433
- src.resource(sexp[1])
434
- },
435
- :elsif => lambda { |src, sexp|
436
- src.soft_newline
437
- src.emit("elsif ")
438
- src.resource(sexp[1])
439
- if src.multiline?
440
- src.soft_newline
441
- else
442
- src.emit(" then ")
443
- end
444
- src.resource(sexp[2])
445
- src.resource(sexp[3]) if sexp[3]
474
+ :do_block => lambda { |sexp|
475
+ emit_block(sexp, "do", "end")
476
+ },
477
+ :dot2 => lambda { |sexp|
478
+ resource(sexp[1])
479
+ emit("..")
480
+ resource(sexp[2])
481
+ },
482
+ :dot3 => lambda { |sexp|
483
+ resource(sexp[1])
484
+ emit("...")
485
+ resource(sexp[2])
486
+ },
487
+ :dyna_symbol => lambda { |sexp|
488
+ emit(':"')
489
+ resource(sexp[1])
490
+ emit('"')
446
491
  },
447
- :ensure => lambda { |src, sexp|
448
- src.emit("ensure")
449
- if src.void?(sexp[1])
450
- src.soft_newline
492
+ :else => lambda { |sexp|
493
+ soft_newline
494
+ outdent do emit("else") end
495
+ soft_newline
496
+ resource(sexp[1])
497
+ },
498
+ :elsif => lambda { |sexp|
499
+ soft_newline
500
+ outdent do emit("elsif ") end
501
+ resource(sexp[1])
502
+ emit_then
503
+ resource(sexp[2])
504
+ resource(sexp[3]) if sexp[3]
505
+ },
506
+ :ensure => lambda { |sexp|
507
+ outdent do emit("ensure") end
508
+ if void?(sexp[1])
509
+ soft_newline
451
510
  else
452
- src.soft_newline
453
- src.resource(sexp[1])
454
- src.newline unless src.void?(sexp[1])
511
+ soft_newline
512
+ resource(sexp[1])
513
+ newline unless void?(sexp[1])
455
514
  end
456
515
  },
457
516
  :excessed_comma => NYI,
458
517
  :fcall => PASS1,
459
- :field => lambda { |src, sexp|
460
- src.resource(sexp[1])
461
- src.emit(sexp[2])
462
- src.resource(sexp[3])
463
- },
464
- :for => lambda { |src, sexp|
465
- src.emit("for ")
466
- src.resource(sexp[1])
467
- src.emit(" in ")
468
- src.resource(sexp[2])
469
- src.emit(" do ")
470
- unless src.void?(sexp[3])
471
- src.resource(sexp[3])
472
- src.emit(" ")
518
+ :field => lambda { |sexp|
519
+ resource(sexp[1])
520
+ emit(sexp[2])
521
+ resource(sexp[3])
522
+ },
523
+ :for => lambda { |sexp|
524
+ emit("for ")
525
+ resource(sexp[1])
526
+ emit(" in ")
527
+ resource(sexp[2])
528
+ newline
529
+ indent do
530
+ unless void?(sexp[3])
531
+ resource(sexp[3])
532
+ soft_newline
533
+ end
473
534
  end
474
- src.emit("end")
475
- },
476
- :hash => lambda { |src, sexp|
477
- src.emit("{")
478
- src.resource(sexp[1]) if sexp[1]
479
- src.emit("}")
480
- },
481
- :if => lambda { |src, sexp|
482
- src.emit("if ")
483
- src.resource(sexp[1])
484
- if src.multiline?
485
- src.newline
486
- else
487
- src.emit(" then ")
535
+ emit("end")
536
+ },
537
+ :hash => lambda { |sexp|
538
+ emit("{")
539
+ resource(sexp[1]) if sexp[1]
540
+ emit("}")
541
+ },
542
+ :if => lambda { |sexp|
543
+ emit("if ")
544
+ resource(sexp[1])
545
+ emit_then
546
+ indent do
547
+ resource(sexp[2])
548
+ resource(sexp[3]) if sexp[3]
549
+ soft_newline
488
550
  end
489
- src.resource(sexp[2])
490
- src.resource(sexp[3]) if sexp[3]
491
- src.soft_newline
492
- src.emit("end")
493
- },
494
- :if_mod => lambda { |src, sexp|
495
- src.resource(sexp[2])
496
- src.emit(" if ")
497
- src.resource(sexp[1])
498
- },
499
- :ifop => lambda { |src, sexp|
500
- src.resource(sexp[1])
501
- src.emit(" ? ")
502
- src.resource(sexp[2])
503
- src.emit(" : ")
504
- src.resource(sexp[3])
505
- },
506
- :lambda => lambda { |src, sexp|
507
- src.emit("->")
508
- src.resource(sexp[1])
509
- src.emit(" {")
510
- if ! src.void?(sexp[2])
511
- src.emit(" ")
512
- src.resource(sexp[2])
551
+ emit("end")
552
+ },
553
+ :if_mod => lambda { |sexp|
554
+ resource(sexp[2])
555
+ emit(" if ")
556
+ resource(sexp[1])
557
+ },
558
+ :ifop => lambda { |sexp|
559
+ resource(sexp[1])
560
+ emit(" ? ")
561
+ resource(sexp[2])
562
+ emit(" : ")
563
+ resource(sexp[3])
564
+ },
565
+ :lambda => lambda { |sexp|
566
+ emit("->")
567
+ resource(sexp[1])
568
+ emit(" {")
569
+ if ! void?(sexp[2])
570
+ soft_newline
571
+ resource(sexp[2])
513
572
  end
514
- src.emit(" ")
515
- src.emit("}")
573
+ if void?(sexp[2])
574
+ emit(" ")
575
+ else
576
+ soft_newline
577
+ end
578
+ emit("}")
516
579
  },
517
580
  :magic_comment => NYI,
518
- :massign => lambda { |src, sexp|
519
- src.resource(sexp[1])
520
- src.emit(" = ")
521
- src.resource(sexp[2])
522
- },
523
- :method_add_arg => lambda { |src, sexp|
524
- src.resource(sexp[1])
525
- src.resource(sexp[2])
526
- },
527
- :method_add_block => lambda { |src, sexp|
528
- src.resource(sexp[1])
529
- src.resource(sexp[2])
530
- },
531
- :mlhs_add => lambda { |src, sexp|
532
- src.resource(sexp[1])
533
- src.emit(", ") unless sexp[1] == [:mlhs_new]
534
- src.resource(sexp[2])
535
- },
536
- :mlhs_add_star => lambda { |src, sexp|
537
- src.resource(sexp[1])
538
- src.emit(", ") unless sexp[1] == [:mlhs_new]
539
- src.emit("*")
540
- src.resource(sexp[2])
581
+ :massign => lambda { |sexp|
582
+ resource(sexp[1])
583
+ emit(" = ")
584
+ resource(sexp[2])
585
+ },
586
+ :method_add_arg => lambda { |sexp|
587
+ resource(sexp[1])
588
+ resource(sexp[2])
589
+ },
590
+ :method_add_block => lambda { |sexp|
591
+ resource(sexp[1])
592
+ resource(sexp[2])
593
+ },
594
+ :mlhs_add => lambda { |sexp|
595
+ resource(sexp[1])
596
+ emit(", ") unless sexp[1] == [:mlhs_new]
597
+ resource(sexp[2])
598
+ },
599
+ :mlhs_add_star => lambda { |sexp|
600
+ resource(sexp[1])
601
+ emit(", ") unless sexp[1] == [:mlhs_new]
602
+ emit("*")
603
+ resource(sexp[2])
541
604
  },
542
605
  :mlhs_new => NOOP,
543
- :mlhs_paren => lambda { |src, sexp|
544
- src.emit("(")
545
- src.resource(sexp[1])
546
- src.emit(")")
547
- },
548
- :module => lambda { |src, sexp|
549
- src.emit("module ")
550
- src.resource(sexp[1])
551
- src.newline
552
- if src.void?(sexp[2])
553
- else
554
- src.resource(sexp[2])
606
+ :mlhs_paren => lambda { |sexp|
607
+ emit("(")
608
+ resource(sexp[1])
609
+ emit(")")
610
+ },
611
+ :module => lambda { |sexp|
612
+ emit("module ")
613
+ resource(sexp[1])
614
+ newline
615
+ unless void?(sexp[2])
616
+ indent do resource(sexp[2]) end
555
617
  end
556
- src.emit("end")
618
+ emit("end")
557
619
  },
558
- :mrhs_add => lambda { |src, sexp|
559
- src.resource(sexp[1])
560
- src.emit(", ")
561
- src.resource(sexp[2])
620
+ :mrhs_add => lambda { |sexp|
621
+ resource(sexp[1])
622
+ emit(", ")
623
+ resource(sexp[2])
562
624
  },
563
- :mrhs_add_star => lambda { |src, sexp|
564
- src.resource(sexp[1])
565
- src.emit(", ")
566
- src.emit("*")
567
- src.resource(sexp[2])
625
+ :mrhs_add_star => lambda { |sexp|
626
+ resource(sexp[1])
627
+ emit(", ")
628
+ emit("*")
629
+ resource(sexp[2])
568
630
  },
569
631
  :mrhs_new => NYI,
570
632
  :mrhs_new_from_args => PASS1,
571
- :next => lambda { |src, sexp|
572
- src.emit("next")
633
+ :next => lambda { |sexp|
634
+ emit("next")
573
635
  },
574
- :opassign => lambda { |src, sexp|
575
- src.resource(sexp[1])
576
- src.emit(" ")
577
- src.resource(sexp[2])
578
- src.emit(" ")
579
- src.resource(sexp[3])
636
+ :opassign => lambda { |sexp|
637
+ resource(sexp[1])
638
+ emit(" ")
639
+ resource(sexp[2])
640
+ emit(" ")
641
+ resource(sexp[3])
580
642
  },
581
643
  :param_error => NYI,
582
- :params => lambda { |src, sexp|
583
- src.params(sexp[1], sexp[2], sexp[3], sexp[4], sexp[5])
644
+ :params => lambda { |sexp|
645
+ params(sexp[1], sexp[2], sexp[3], sexp[4], sexp[5])
584
646
  },
585
- :paren => lambda { |src, sexp|
586
- src.emit("(")
587
- src.resource(sexp[1])
588
- src.emit(")")
647
+ :paren => lambda { |sexp|
648
+ emit("(")
649
+ resource(sexp[1])
650
+ emit(")")
589
651
  },
590
652
  :parse_error => NYI,
591
653
  :program => PASS1,
592
- :qwords_add => lambda { |src, sexp|
593
- src.words("w", sexp)
654
+ :qwords_add => lambda { |sexp|
655
+ words("w", sexp)
594
656
  },
595
657
  :qwords_new => NOOP,
596
- :redo => lambda { |src, sexp|
597
- src.emit("redo")
658
+ :redo => lambda { |sexp|
659
+ emit("redo")
598
660
  },
599
661
  :regexp_add => PASS2,
600
- :regexp_literal => lambda { |src, sexp|
662
+ :regexp_literal => lambda { |sexp|
601
663
  delims = determine_regexp_delimiters(sexp[2])
602
- src.emit(delims[0])
603
- src.resource(sexp[1])
604
- src.emit(delims[1])
664
+ emit(delims[0])
665
+ resource(sexp[1])
666
+ emit(delims[1])
605
667
  },
606
- :rescue => lambda { |src, sexp|
607
- src.emit("rescue")
668
+ :rescue => lambda { |sexp|
669
+ outdent do emit("rescue") end
608
670
  if sexp[1] # Exception list
609
- src.emit(" ")
671
+ emit(" ")
610
672
  if sexp[1].first.kind_of?(Symbol)
611
- src.resource(sexp[1])
673
+ resource(sexp[1])
612
674
  else
613
- src.resource(sexp[1].first)
675
+ resource(sexp[1].first)
614
676
  end
615
- src.emit(" => ")
616
- src.resource(sexp[2])
677
+ emit(" => ")
678
+ resource(sexp[2])
617
679
  end
618
- src.newline
619
- if sexp[3] && ! src.void?(sexp[3])
620
- src.resource(sexp[3])
621
- src.newline
680
+ newline
681
+ if sexp[3] && ! void?(sexp[3])
682
+ resource(sexp[3])
683
+ newline
622
684
  end
623
685
  },
624
- :rescue_mod => lambda { |src, sexp|
625
- src.resource(sexp[2])
626
- src.emit(" rescue ")
627
- src.resource(sexp[1])
686
+ :rescue_mod => lambda { |sexp|
687
+ resource(sexp[2])
688
+ emit(" rescue ")
689
+ resource(sexp[1])
628
690
  },
629
- :rest_param => lambda { |src, sexp|
630
- src.emit("*")
631
- src.resource(sexp[1])
691
+ :rest_param => lambda { |sexp|
692
+ emit("*")
693
+ resource(sexp[1])
632
694
  },
633
- :retry => lambda { |src, sexp|
634
- src.emit("retry")
695
+ :retry => lambda { |sexp|
696
+ emit("retry")
635
697
  },
636
- :return => lambda { |src, sexp|
637
- src.emit("return")
638
- src.opt_parens(sexp[1])
698
+ :return => lambda { |sexp|
699
+ emit("return")
700
+ opt_parens(sexp[1])
639
701
  },
640
- :return0 => lambda { |src, sexp|
641
- src.emit("return")
702
+ :return0 => lambda { |sexp|
703
+ emit("return")
642
704
  },
643
705
  :sclass => NYI,
644
- :stmts_add => lambda { |src, sexp|
645
- if sexp[1] != [:stmts_new] && ! src.void?(sexp[1])
646
- src.resource(sexp[1])
647
- src.newline
706
+ :stmts_add => lambda { |sexp|
707
+ if sexp[1] != [:stmts_new] && ! void?(sexp[1])
708
+ resource(sexp[1])
709
+ newline
648
710
  end
649
- src.resource(sexp[2]) if sexp[2]
711
+ resource(sexp[2]) if sexp[2]
650
712
  },
651
713
  :stmts_new => NOOP,
652
- :string_add => lambda { |src, sexp|
653
- src.resource(sexp[1])
654
- src.resource(sexp[2])
714
+ :string_add => lambda { |sexp|
715
+ resource(sexp[1])
716
+ resource(sexp[2])
655
717
  },
656
- :string_concat => lambda { |src, sexp|
657
- src.resource(sexp[1])
658
- src.emit(" ")
659
- src.resource(sexp[2])
718
+ :string_concat => lambda { |sexp|
719
+ resource(sexp[1])
720
+ emit(" ")
721
+ resource(sexp[2])
660
722
  },
661
723
  :string_content => NOOP,
662
724
  :string_dvar => NYI,
663
- :string_embexpr => lambda { |src, sexp|
664
- src.emit('#{')
665
- src.resource(sexp[1])
666
- src.emit('}')
725
+ :string_embexpr => lambda { |sexp|
726
+ emit('#{')
727
+ resource(sexp[1])
728
+ emit('}')
667
729
  },
668
- :string_literal => lambda { |src, sexp|
669
- src.emit('"')
670
- src.resource(sexp[1])
671
- src.emit('"')
730
+ :string_literal => lambda { |sexp|
731
+ emit('"')
732
+ resource(sexp[1])
733
+ emit('"')
672
734
  },
673
- :super => lambda { |src, sexp|
674
- src.emit("super")
675
- src.opt_parens(sexp[1])
735
+ :super => lambda { |sexp|
736
+ emit("super")
737
+ opt_parens(sexp[1])
676
738
  },
677
- :symbol => lambda { |src, sexp|
678
- src.emit(":")
679
- src.resource(sexp[1])
739
+ :symbol => lambda { |sexp|
740
+ emit(":")
741
+ resource(sexp[1])
680
742
  },
681
743
  :symbol_literal => PASS1,
682
744
  :top_const_field => NYI,
683
745
  :top_const_ref => NYI,
684
- :unary => lambda { |src, sexp|
685
- src.emit(sexp[1].to_s[0,1])
686
- src.resource(sexp[2])
687
- },
688
- :undef => lambda { |src, sexp|
689
- src.emit("undef ")
690
- src.resource(sexp[1].first)
691
- },
692
- :unless => lambda { |src, sexp|
693
- src.emit("unless ")
694
- src.resource(sexp[1])
695
- if src.multiline?
696
- src.newline
697
- else
698
- src.emit(" then ")
746
+ :unary => lambda { |sexp|
747
+ emit(sexp[1].to_s[0,1])
748
+ resource(sexp[2])
749
+ },
750
+ :undef => lambda { |sexp|
751
+ emit("undef ")
752
+ resource(sexp[1].first)
753
+ },
754
+ :unless => lambda { |sexp|
755
+ emit("unless ")
756
+ resource(sexp[1])
757
+ emit_then
758
+ indent do
759
+ resource(sexp[2])
760
+ resource(sexp[3]) if sexp[3]
761
+ soft_newline
699
762
  end
700
- src.resource(sexp[2])
701
- src.resource(sexp[3]) if sexp[3]
702
- src.soft_newline
703
- src.emit("end")
704
- },
705
- :unless_mod => lambda { |src, sexp|
706
- src.resource(sexp[2])
707
- src.emit(" unless ")
708
- src.resource(sexp[1])
709
- },
710
- :until => lambda { |src, sexp|
711
- src.emit("until ")
712
- src.resource(sexp[1])
713
- src.emit(" do ")
714
- src.resource(sexp[2])
715
- src.emit(" end")
716
- },
717
- :until_mod => lambda { |src, sexp|
718
- src.resource(sexp[2])
719
- src.emit(" until ")
720
- src.resource(sexp[1])
763
+ emit("end")
764
+ },
765
+ :unless_mod => lambda { |sexp|
766
+ resource(sexp[2])
767
+ emit(" unless ")
768
+ resource(sexp[1])
769
+ },
770
+ :until => lambda { |sexp|
771
+ emit("until ")
772
+ resource(sexp[1])
773
+ newline
774
+ indent do resource(sexp[2]) end
775
+ emit(" end")
776
+ },
777
+ :until_mod => lambda { |sexp|
778
+ resource(sexp[2])
779
+ emit(" until ")
780
+ resource(sexp[1])
721
781
  },
722
782
  :var_alias => NYI,
723
783
  :var_field => PASS1,
724
784
  :var_ref => PASS1,
725
785
  :void_stmt => NOOP,
726
- :when => lambda { |src, sexp|
727
- src.emit("when ")
728
- src.resource(sexp[1])
729
- src.newline
730
- src.resource(sexp[2])
786
+ :when => lambda { |sexp|
787
+ outdent do emit("when ") end
788
+ resource(sexp[1])
789
+ newline
790
+ resource(sexp[2])
731
791
  if sexp[3] && sexp[3].first == :when
732
- src.emit(" ")
792
+ emit(" ")
733
793
  end
734
- src.resource(sexp[3]) if sexp[3]
735
- },
736
- :while => lambda { |src, sexp|
737
- src.emit("while ")
738
- src.resource(sexp[1])
739
- src.newline
740
- unless src.void?(sexp[2])
741
- src.resource(sexp[2])
742
- src.newline
794
+ resource(sexp[3]) if sexp[3]
795
+ },
796
+ :while => lambda { |sexp|
797
+ emit("while ")
798
+ resource(sexp[1])
799
+ newline
800
+ indent do
801
+ unless void?(sexp[2])
802
+ resource(sexp[2])
803
+ newline
804
+ end
743
805
  end
744
- src.emit("end")
806
+ emit("end")
745
807
  },
746
- :while_mod => lambda { |src, sexp|
747
- src.resource(sexp[2])
748
- src.emit(" while ")
749
- src.resource(sexp[1])
808
+ :while_mod => lambda { |sexp|
809
+ resource(sexp[2])
810
+ emit(" while ")
811
+ resource(sexp[1])
750
812
  },
751
813
  :word_add => PASS2,
752
814
  :word_new => NOOP,
753
- :words_add => lambda { |src, sexp|
754
- src.words("W", sexp)
815
+ :words_add => lambda { |sexp|
816
+ words("W", sexp)
755
817
  },
756
818
  :words_new => NOOP,
757
- :xstring_add => lambda { |src, sexp|
758
- src.resource(sexp[1])
759
- src.resource(sexp[2])
819
+ :xstring_add => lambda { |sexp|
820
+ resource(sexp[1])
821
+ resource(sexp[2])
760
822
  },
761
- :xstring_literal => lambda { |src, sexp|
762
- src.emit('"')
763
- src.resource(sexp[1])
764
- src.emit('"')
823
+ :xstring_literal => lambda { |sexp|
824
+ emit('"')
825
+ resource(sexp[1])
826
+ emit('"')
765
827
  },
766
828
  :xstring_new => NOOP,
767
- :yield => lambda { |src, sexp|
768
- src.emit("yield")
769
- src.opt_parens(sexp[1])
829
+ :yield => lambda { |sexp|
830
+ emit("yield")
831
+ opt_parens(sexp[1])
770
832
  },
771
- :yield0 => lambda { |src, sexp|
772
- src.emit("yield")
833
+ :yield0 => lambda { |sexp|
834
+ emit("yield")
773
835
  },
774
- :zsuper => lambda { |src, sexp|
775
- src.emit("super")
836
+ :zsuper => lambda { |sexp|
837
+ emit("super")
776
838
  },
777
839
 
778
840
  # Scanner keywords