sorcerer 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sorcerer/resource.rb +521 -459
- data/lib/sorcerer/version.rb +1 -1
- data/test/sorcerer/resource_test.rb +118 -71
- metadata +2 -2
data/lib/sorcerer/resource.rb
CHANGED
@@ -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
|
-
@
|
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
|
-
|
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
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
-
|
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
|
-
|
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
|
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 { |
|
197
|
-
DBG = lambda { |
|
198
|
-
NOOP = lambda { |
|
199
|
-
SPACE = lambda { |
|
200
|
-
PASS1 = lambda { |
|
201
|
-
PASS2 = lambda { |
|
202
|
-
EMIT1 = lambda { |
|
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 { |
|
208
|
-
|
209
|
-
if
|
210
|
-
|
260
|
+
:BEGIN => lambda { |sexp|
|
261
|
+
emit("BEGIN {")
|
262
|
+
if void?(sexp[1])
|
263
|
+
emit " }"
|
211
264
|
else
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
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 { |
|
219
|
-
|
220
|
-
if
|
221
|
-
|
273
|
+
:END => lambda { |sexp|
|
274
|
+
emit("END {")
|
275
|
+
if void?(sexp[1])
|
276
|
+
emit(" }")
|
222
277
|
else
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
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 { |
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
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 { |
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
},
|
242
|
-
:aref_field => lambda { |
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
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 { |
|
250
|
-
|
251
|
-
|
252
|
-
|
306
|
+
:arg_paren => lambda { |sexp|
|
307
|
+
emit("(")
|
308
|
+
resource(sexp[1]) if sexp[1]
|
309
|
+
emit(")")
|
253
310
|
},
|
254
|
-
:args_add => lambda { |
|
255
|
-
|
311
|
+
:args_add => lambda { |sexp|
|
312
|
+
resource(sexp[1])
|
256
313
|
if sexp[1].first != :args_new
|
257
|
-
|
314
|
+
emit(", ")
|
258
315
|
end
|
259
|
-
|
316
|
+
resource(sexp[2])
|
260
317
|
},
|
261
|
-
:args_add_block => lambda { |
|
262
|
-
|
318
|
+
:args_add_block => lambda { |sexp|
|
319
|
+
resource(sexp[1])
|
263
320
|
if sexp[2]
|
264
321
|
if sexp[1].first != :args_new
|
265
|
-
|
322
|
+
emit(", ")
|
266
323
|
end
|
267
324
|
if sexp[2]
|
268
|
-
|
269
|
-
|
325
|
+
emit("&")
|
326
|
+
resource(sexp[2])
|
270
327
|
end
|
271
328
|
end
|
272
329
|
},
|
273
|
-
:args_add_star => lambda { |
|
274
|
-
|
330
|
+
:args_add_star => lambda { |sexp|
|
331
|
+
resource(sexp[1])
|
275
332
|
if sexp[1].first != :args_new
|
276
|
-
|
333
|
+
emit(", ")
|
277
334
|
end
|
278
|
-
|
279
|
-
|
335
|
+
emit("*")
|
336
|
+
resource(sexp[2])
|
280
337
|
},
|
281
338
|
:args_new => NOOP,
|
282
339
|
:args_prepend => NYI,
|
283
|
-
:array => lambda { |
|
284
|
-
|
285
|
-
|
286
|
-
|
340
|
+
:array => lambda { |sexp|
|
341
|
+
emit("[")
|
342
|
+
resource(sexp[1]) if sexp[1]
|
343
|
+
emit("]")
|
287
344
|
},
|
288
|
-
:assign => lambda { |
|
289
|
-
|
290
|
-
|
291
|
-
|
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 { |
|
295
|
-
|
296
|
-
|
297
|
-
|
351
|
+
:assoc_new => lambda { |sexp|
|
352
|
+
resource(sexp[1])
|
353
|
+
emit(" => ")
|
354
|
+
resource(sexp[2])
|
298
355
|
},
|
299
|
-
:assoclist_from_args => lambda { |
|
356
|
+
:assoclist_from_args => lambda { |sexp|
|
300
357
|
first = true
|
301
358
|
sexp[1].each do |sx|
|
302
|
-
|
359
|
+
emit(", ") unless first
|
303
360
|
first = false
|
304
|
-
|
361
|
+
resource(sx)
|
305
362
|
end
|
306
363
|
},
|
307
|
-
:bare_assoc_hash => lambda { |
|
364
|
+
:bare_assoc_hash => lambda { |sexp|
|
308
365
|
first = true
|
309
366
|
sexp[1].each do |sx|
|
310
|
-
|
367
|
+
emit(", ") unless first
|
311
368
|
first = false
|
312
|
-
|
369
|
+
resource(sx)
|
313
370
|
end
|
314
371
|
},
|
315
|
-
:begin => lambda { |
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
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
|
-
|
382
|
+
emit("end")
|
324
383
|
},
|
325
|
-
:binary => lambda { |
|
326
|
-
|
327
|
-
|
328
|
-
|
384
|
+
:binary => lambda { |sexp|
|
385
|
+
resource(sexp[1])
|
386
|
+
emit(" #{sexp[2]} ")
|
387
|
+
resource(sexp[3])
|
329
388
|
},
|
330
|
-
:block_var => lambda { |
|
331
|
-
|
332
|
-
|
333
|
-
|
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 { |
|
338
|
-
|
339
|
-
|
340
|
-
},
|
341
|
-
:body_stmt => lambda { |
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
},
|
347
|
-
:brace_block => lambda { |
|
348
|
-
|
349
|
-
},
|
350
|
-
:break => lambda { |
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
},
|
355
|
-
:call => lambda { |
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
},
|
360
|
-
:case => lambda { |
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
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
|
-
|
376
|
-
|
377
|
-
|
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 { |
|
381
|
-
|
382
|
-
|
383
|
-
|
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 { |
|
387
|
-
|
388
|
-
|
389
|
-
|
449
|
+
:const_path_field => lambda { |sexp|
|
450
|
+
resource(sexp[1])
|
451
|
+
emit("::")
|
452
|
+
resource(sexp[2])
|
390
453
|
},
|
391
|
-
:const_path_ref => lambda { |
|
392
|
-
|
393
|
-
|
394
|
-
|
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 { |
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
},
|
405
|
-
:defined => lambda { |
|
406
|
-
|
407
|
-
|
408
|
-
|
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 { |
|
412
|
-
|
413
|
-
},
|
414
|
-
:dot2 => lambda { |
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
},
|
419
|
-
:dot3 => lambda { |
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
},
|
424
|
-
:dyna_symbol => lambda { |
|
425
|
-
|
426
|
-
|
427
|
-
|
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
|
-
:
|
448
|
-
|
449
|
-
|
450
|
-
|
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
|
-
|
453
|
-
|
454
|
-
|
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 { |
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
},
|
464
|
-
:for => lambda { |
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
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
|
-
|
475
|
-
},
|
476
|
-
:hash => lambda { |
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
},
|
481
|
-
:if => lambda { |
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
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
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
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
|
-
|
515
|
-
|
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 { |
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
},
|
523
|
-
:method_add_arg => lambda { |
|
524
|
-
|
525
|
-
|
526
|
-
},
|
527
|
-
:method_add_block => lambda { |
|
528
|
-
|
529
|
-
|
530
|
-
},
|
531
|
-
:mlhs_add => lambda { |
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
},
|
536
|
-
:mlhs_add_star => lambda { |
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
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 { |
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
},
|
548
|
-
:module => lambda { |
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
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
|
-
|
618
|
+
emit("end")
|
557
619
|
},
|
558
|
-
:mrhs_add => lambda { |
|
559
|
-
|
560
|
-
|
561
|
-
|
620
|
+
:mrhs_add => lambda { |sexp|
|
621
|
+
resource(sexp[1])
|
622
|
+
emit(", ")
|
623
|
+
resource(sexp[2])
|
562
624
|
},
|
563
|
-
:mrhs_add_star => lambda { |
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
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 { |
|
572
|
-
|
633
|
+
:next => lambda { |sexp|
|
634
|
+
emit("next")
|
573
635
|
},
|
574
|
-
:opassign => lambda { |
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
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 { |
|
583
|
-
|
644
|
+
:params => lambda { |sexp|
|
645
|
+
params(sexp[1], sexp[2], sexp[3], sexp[4], sexp[5])
|
584
646
|
},
|
585
|
-
:paren => lambda { |
|
586
|
-
|
587
|
-
|
588
|
-
|
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 { |
|
593
|
-
|
654
|
+
:qwords_add => lambda { |sexp|
|
655
|
+
words("w", sexp)
|
594
656
|
},
|
595
657
|
:qwords_new => NOOP,
|
596
|
-
:redo => lambda { |
|
597
|
-
|
658
|
+
:redo => lambda { |sexp|
|
659
|
+
emit("redo")
|
598
660
|
},
|
599
661
|
:regexp_add => PASS2,
|
600
|
-
:regexp_literal => lambda { |
|
662
|
+
:regexp_literal => lambda { |sexp|
|
601
663
|
delims = determine_regexp_delimiters(sexp[2])
|
602
|
-
|
603
|
-
|
604
|
-
|
664
|
+
emit(delims[0])
|
665
|
+
resource(sexp[1])
|
666
|
+
emit(delims[1])
|
605
667
|
},
|
606
|
-
:rescue => lambda { |
|
607
|
-
|
668
|
+
:rescue => lambda { |sexp|
|
669
|
+
outdent do emit("rescue") end
|
608
670
|
if sexp[1] # Exception list
|
609
|
-
|
671
|
+
emit(" ")
|
610
672
|
if sexp[1].first.kind_of?(Symbol)
|
611
|
-
|
673
|
+
resource(sexp[1])
|
612
674
|
else
|
613
|
-
|
675
|
+
resource(sexp[1].first)
|
614
676
|
end
|
615
|
-
|
616
|
-
|
677
|
+
emit(" => ")
|
678
|
+
resource(sexp[2])
|
617
679
|
end
|
618
|
-
|
619
|
-
if sexp[3] && !
|
620
|
-
|
621
|
-
|
680
|
+
newline
|
681
|
+
if sexp[3] && ! void?(sexp[3])
|
682
|
+
resource(sexp[3])
|
683
|
+
newline
|
622
684
|
end
|
623
685
|
},
|
624
|
-
:rescue_mod => lambda { |
|
625
|
-
|
626
|
-
|
627
|
-
|
686
|
+
:rescue_mod => lambda { |sexp|
|
687
|
+
resource(sexp[2])
|
688
|
+
emit(" rescue ")
|
689
|
+
resource(sexp[1])
|
628
690
|
},
|
629
|
-
:rest_param => lambda { |
|
630
|
-
|
631
|
-
|
691
|
+
:rest_param => lambda { |sexp|
|
692
|
+
emit("*")
|
693
|
+
resource(sexp[1])
|
632
694
|
},
|
633
|
-
:retry => lambda { |
|
634
|
-
|
695
|
+
:retry => lambda { |sexp|
|
696
|
+
emit("retry")
|
635
697
|
},
|
636
|
-
:return => lambda { |
|
637
|
-
|
638
|
-
|
698
|
+
:return => lambda { |sexp|
|
699
|
+
emit("return")
|
700
|
+
opt_parens(sexp[1])
|
639
701
|
},
|
640
|
-
:return0 => lambda { |
|
641
|
-
|
702
|
+
:return0 => lambda { |sexp|
|
703
|
+
emit("return")
|
642
704
|
},
|
643
705
|
:sclass => NYI,
|
644
|
-
:stmts_add => lambda { |
|
645
|
-
if sexp[1] != [:stmts_new] && !
|
646
|
-
|
647
|
-
|
706
|
+
:stmts_add => lambda { |sexp|
|
707
|
+
if sexp[1] != [:stmts_new] && ! void?(sexp[1])
|
708
|
+
resource(sexp[1])
|
709
|
+
newline
|
648
710
|
end
|
649
|
-
|
711
|
+
resource(sexp[2]) if sexp[2]
|
650
712
|
},
|
651
713
|
:stmts_new => NOOP,
|
652
|
-
:string_add => lambda { |
|
653
|
-
|
654
|
-
|
714
|
+
:string_add => lambda { |sexp|
|
715
|
+
resource(sexp[1])
|
716
|
+
resource(sexp[2])
|
655
717
|
},
|
656
|
-
:string_concat => lambda { |
|
657
|
-
|
658
|
-
|
659
|
-
|
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 { |
|
664
|
-
|
665
|
-
|
666
|
-
|
725
|
+
:string_embexpr => lambda { |sexp|
|
726
|
+
emit('#{')
|
727
|
+
resource(sexp[1])
|
728
|
+
emit('}')
|
667
729
|
},
|
668
|
-
:string_literal => lambda { |
|
669
|
-
|
670
|
-
|
671
|
-
|
730
|
+
:string_literal => lambda { |sexp|
|
731
|
+
emit('"')
|
732
|
+
resource(sexp[1])
|
733
|
+
emit('"')
|
672
734
|
},
|
673
|
-
:super => lambda { |
|
674
|
-
|
675
|
-
|
735
|
+
:super => lambda { |sexp|
|
736
|
+
emit("super")
|
737
|
+
opt_parens(sexp[1])
|
676
738
|
},
|
677
|
-
:symbol => lambda { |
|
678
|
-
|
679
|
-
|
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 { |
|
685
|
-
|
686
|
-
|
687
|
-
},
|
688
|
-
:undef => lambda { |
|
689
|
-
|
690
|
-
|
691
|
-
},
|
692
|
-
:unless => lambda { |
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
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
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
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 { |
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
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
|
-
|
792
|
+
emit(" ")
|
733
793
|
end
|
734
|
-
|
735
|
-
},
|
736
|
-
:while => lambda { |
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
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
|
-
|
806
|
+
emit("end")
|
745
807
|
},
|
746
|
-
:while_mod => lambda { |
|
747
|
-
|
748
|
-
|
749
|
-
|
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 { |
|
754
|
-
|
815
|
+
:words_add => lambda { |sexp|
|
816
|
+
words("W", sexp)
|
755
817
|
},
|
756
818
|
:words_new => NOOP,
|
757
|
-
:xstring_add => lambda { |
|
758
|
-
|
759
|
-
|
819
|
+
:xstring_add => lambda { |sexp|
|
820
|
+
resource(sexp[1])
|
821
|
+
resource(sexp[2])
|
760
822
|
},
|
761
|
-
:xstring_literal => lambda { |
|
762
|
-
|
763
|
-
|
764
|
-
|
823
|
+
:xstring_literal => lambda { |sexp|
|
824
|
+
emit('"')
|
825
|
+
resource(sexp[1])
|
826
|
+
emit('"')
|
765
827
|
},
|
766
828
|
:xstring_new => NOOP,
|
767
|
-
:yield => lambda { |
|
768
|
-
|
769
|
-
|
829
|
+
:yield => lambda { |sexp|
|
830
|
+
emit("yield")
|
831
|
+
opt_parens(sexp[1])
|
770
832
|
},
|
771
|
-
:yield0 => lambda { |
|
772
|
-
|
833
|
+
:yield0 => lambda { |sexp|
|
834
|
+
emit("yield")
|
773
835
|
},
|
774
|
-
:zsuper => lambda { |
|
775
|
-
|
836
|
+
:zsuper => lambda { |sexp|
|
837
|
+
emit("super")
|
776
838
|
},
|
777
839
|
|
778
840
|
# Scanner keywords
|