clean_ripper 0.0.0 → 0.0.1
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/clean_ripper.rb +1 -1
- data/lib/clean_ripper_doc.rb +51 -0
- data/lib/clean_sexp_builder.rb +161 -9
- data/test/clean_ripper_test.rb +633 -3
- metadata +5 -3
data/lib/clean_ripper.rb
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
|
|
2
|
+
warn "Don't include this file! ('clean_ripper_doc.rb')"
|
|
3
|
+
|
|
4
|
+
# Show all sexp that CleanRipper can output.
|
|
5
|
+
class CleanRipperDocumentation
|
|
6
|
+
|
|
7
|
+
# A literal object.
|
|
8
|
+
# @example
|
|
9
|
+
# 1 #=> [:lit, 1]
|
|
10
|
+
# "aa" #=> [:lit, "aa"]
|
|
11
|
+
# @param [Opject] value The literal object.
|
|
12
|
+
def lit(value)
|
|
13
|
+
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# A binary operation.
|
|
17
|
+
# @example
|
|
18
|
+
# 1+2 #=> [:binary, [:lit, 1], :+, [:lit, 2]]
|
|
19
|
+
# a and b #=> [:binary, [lvar, :a], :and, [:lvar, :a]]
|
|
20
|
+
# @param [Sexp] right The right side of the operation.
|
|
21
|
+
# @param [Sexp] left The left side of the operation.
|
|
22
|
+
# @param [Symbol] operator The operator.
|
|
23
|
+
def binary(right, operator, left)
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# A unary operation.
|
|
28
|
+
# @example
|
|
29
|
+
# -a #=> [:unary, :-, [:lvar, :a]]
|
|
30
|
+
# !b #=> [:unary, :!, [:lvar, :b]]
|
|
31
|
+
# @param [Sexp] receiver The receiver of the operation.
|
|
32
|
+
# @param [Symbol] operator The operator.
|
|
33
|
+
def unary(operator, receiver)
|
|
34
|
+
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# A method call.
|
|
38
|
+
# @example
|
|
39
|
+
# Array.new(5, nil)
|
|
40
|
+
# #=> [:call, [:const, :Array], :new, [[:lit, 5], [:nil]], nil]
|
|
41
|
+
# print var
|
|
42
|
+
# #=> [:call, nil, :print, [[:ident, :var]], nil]
|
|
43
|
+
# @param [Sexp, nil] receiver How calls the method.
|
|
44
|
+
# @param [Symbol] method The method name.
|
|
45
|
+
# @param [Array] arguments Array of arguments.
|
|
46
|
+
# @param [Sexp, nil] block Block passed to the method.
|
|
47
|
+
def call(receiver, method, arguments, block)
|
|
48
|
+
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
end
|
data/lib/clean_sexp_builder.rb
CHANGED
|
@@ -3,7 +3,7 @@ require 'ripper'
|
|
|
3
3
|
|
|
4
4
|
class Ripper
|
|
5
5
|
class CleanSexpBuilder < SexpBuilder
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
def initialize(source)
|
|
8
8
|
@source = source.split("\n")
|
|
9
9
|
super
|
|
@@ -33,6 +33,42 @@ class Ripper
|
|
|
33
33
|
[:splat, args.last]
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
+
def on_mlhs_new(*args)
|
|
37
|
+
nil
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def on_mlhs_add(*args)
|
|
41
|
+
args.first ? args.first + [args.last] : [args.last]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def on_regexp_new(*args)
|
|
45
|
+
nil
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def on_regexp_add(*args)
|
|
49
|
+
args.first ? args.first + [args.last] : [args.last]
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def on_xstring_new(*args)
|
|
53
|
+
nil
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def on_xstring_add(*args)
|
|
57
|
+
args.first ? args.first + [args.last] : [args.last]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def on_mrhs_add(*args)
|
|
61
|
+
args.first ? args.first + [args.last] : [args.last]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def on_rescue_mod(*args)
|
|
65
|
+
[:begin, [:body, args[0]], [:rescue, nil, nil, args[1]], nil, nil]
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def on_mrhs_new_from_args(*args)
|
|
69
|
+
args.first
|
|
70
|
+
end
|
|
71
|
+
|
|
36
72
|
def on_args_add_star(*args)
|
|
37
73
|
[:splat, args.last]
|
|
38
74
|
end
|
|
@@ -72,7 +108,7 @@ class Ripper
|
|
|
72
108
|
def on_ivar(*args)
|
|
73
109
|
[:ivar, args.first.to_sym]
|
|
74
110
|
end
|
|
75
|
-
|
|
111
|
+
|
|
76
112
|
def on_ident(*args)
|
|
77
113
|
[:ident, args.first.to_sym]
|
|
78
114
|
end
|
|
@@ -82,17 +118,29 @@ class Ripper
|
|
|
82
118
|
end
|
|
83
119
|
|
|
84
120
|
def on_method_add_arg(*args)
|
|
85
|
-
[:call, args.first
|
|
121
|
+
[:call, *args.first, (args[1][1] if args[1]) || [], nil]
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def on_call(*args)
|
|
125
|
+
[args[0], args[-1][1]]
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def on_fcall(*args)
|
|
129
|
+
[nil, args[0][1]]
|
|
86
130
|
end
|
|
87
131
|
|
|
88
132
|
def on_args_add_block(*args)
|
|
89
|
-
|
|
133
|
+
args.first
|
|
90
134
|
end
|
|
91
135
|
|
|
92
136
|
def on_args_new(*args)
|
|
93
137
|
nil
|
|
94
138
|
end
|
|
95
139
|
|
|
140
|
+
def on_command(*args)
|
|
141
|
+
[:call, nil, args[0][1], args[1] || [], nil]
|
|
142
|
+
end
|
|
143
|
+
|
|
96
144
|
def on_args_add(*args)
|
|
97
145
|
args.first ? args.first + [args.last] : [args.last]
|
|
98
146
|
end
|
|
@@ -101,7 +149,7 @@ class Ripper
|
|
|
101
149
|
brace_block = args[1]
|
|
102
150
|
brace_block[2].unshift(:block)
|
|
103
151
|
brace_block[2] = nil if brace_block[2].compact.size == 1
|
|
104
|
-
args.first[0
|
|
152
|
+
args.first[0, 4] << brace_block
|
|
105
153
|
end
|
|
106
154
|
|
|
107
155
|
def on_block_var(*args)
|
|
@@ -134,6 +182,7 @@ class Ripper
|
|
|
134
182
|
|
|
135
183
|
def on_paren(*args)
|
|
136
184
|
l = args.first
|
|
185
|
+
l = [l] if Symbol === l[0]
|
|
137
186
|
l.size == 1 ? l.first : [:block] + l
|
|
138
187
|
end
|
|
139
188
|
|
|
@@ -141,8 +190,42 @@ class Ripper
|
|
|
141
190
|
[args.first.to_sym]
|
|
142
191
|
end
|
|
143
192
|
|
|
193
|
+
def on_begin(*args)
|
|
194
|
+
body = args.first.shift
|
|
195
|
+
body.size > 1 ? body = [:block, *body] : body = body.first
|
|
196
|
+
[:begin, [:body, body], *args.first]
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def on_else(*args)
|
|
200
|
+
block = args.first
|
|
201
|
+
block.size > 1 ? block = [:block, *block] : block = block.first
|
|
202
|
+
[:else, block]
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def on_ensure(*args)
|
|
206
|
+
block = args.first
|
|
207
|
+
block.size > 1 ? block = [:block, *block] : block = block.first
|
|
208
|
+
[:ensure, block]
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def on_rescue(*args)
|
|
212
|
+
err = args.shift
|
|
213
|
+
var = args.shift
|
|
214
|
+
block = args.first
|
|
215
|
+
block.size > 1 ? block = [:block, *block] : block = block.first
|
|
216
|
+
[:rescue, err, var, block]
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def on_const(*args)
|
|
220
|
+
[:const, args.first.to_sym]
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def on_bodystmt(*args)
|
|
224
|
+
args
|
|
225
|
+
end
|
|
226
|
+
|
|
144
227
|
def on_do_block(*args)
|
|
145
|
-
[:brace_block]
|
|
228
|
+
[:brace_block, args[0] ? args[0][1..-1] : [], args[1]]
|
|
146
229
|
end
|
|
147
230
|
|
|
148
231
|
def on_if_mod(*args)
|
|
@@ -177,19 +260,88 @@ class Ripper
|
|
|
177
260
|
end
|
|
178
261
|
|
|
179
262
|
def on_string_literal(*args)
|
|
180
|
-
[:string, *(args.first || []).compact]
|
|
263
|
+
s = [:string, *(args.first || []).compact]
|
|
264
|
+
if s.size == 2 and s[1][0] == :lit
|
|
265
|
+
s[1]
|
|
266
|
+
elsif s.size == 1
|
|
267
|
+
[:lit, ""]
|
|
268
|
+
else
|
|
269
|
+
s
|
|
270
|
+
end
|
|
181
271
|
end
|
|
182
272
|
|
|
183
273
|
def on_tstring_content(*args)
|
|
184
274
|
str = "\""+args.first+"\""
|
|
185
275
|
if @source[lineno-1][column-1, 1] == "'"
|
|
186
276
|
str[0] = str[-1] = "'"
|
|
277
|
+
elsif @source[lineno-1][column-1, 1] == "/"
|
|
278
|
+
str[0] = str[-1] = "/"
|
|
279
|
+
else
|
|
280
|
+
if @source[lineno-1][column-1-2, 2] =~ /%([qQ])/
|
|
281
|
+
case $1
|
|
282
|
+
when "q"
|
|
283
|
+
str[0] = str[-1] = "'"
|
|
284
|
+
when "Q"
|
|
285
|
+
end
|
|
286
|
+
end
|
|
187
287
|
end
|
|
188
|
-
[:
|
|
288
|
+
[:lit, eval(str)]
|
|
189
289
|
end
|
|
190
290
|
|
|
191
291
|
def on_string_embexpr(*args)
|
|
192
|
-
|
|
292
|
+
if args[0][0]
|
|
293
|
+
args[0].size == 1 ? args[0][0] : [:block] + args[0]
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
def on_qwords_new(*args)
|
|
298
|
+
args
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def on_massign(*args)
|
|
302
|
+
if args[0][0] == :mlhs_add_star
|
|
303
|
+
[:massign_splat, args[0][1] << args[0][2], args[1]]
|
|
304
|
+
else
|
|
305
|
+
[:massign, *args]
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
def on_field(*args)
|
|
310
|
+
[:field, args[0], args[2][1], []]
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
def on_aref_field(*args)
|
|
314
|
+
[:field, args[0], :[], args[1]]
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def on_mlhs_paren(*args)
|
|
318
|
+
[:group, *args[0]]
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def on_regexp_literal(*args)
|
|
322
|
+
s = [:regex, *(args.first || []).compact]
|
|
323
|
+
if s.size == 2 and s[1][0] == :lit
|
|
324
|
+
s[1]
|
|
325
|
+
elsif s.size == 1
|
|
326
|
+
[:lit, //]
|
|
327
|
+
else
|
|
328
|
+
s
|
|
329
|
+
end
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
def on_dyna_symbol(*args)
|
|
333
|
+
s = [:symbol, *(args.first || []).compact]
|
|
334
|
+
if s.size == 2 and s[1][0] == :lit
|
|
335
|
+
[:lit, s[1][1].to_sym]
|
|
336
|
+
elsif s.size == 1
|
|
337
|
+
[:lit, "".to_sym]
|
|
338
|
+
else
|
|
339
|
+
s
|
|
340
|
+
end
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def on_symbol_literal(*args)
|
|
344
|
+
[:lit, args[0][1][1]]
|
|
193
345
|
end
|
|
194
346
|
|
|
195
347
|
end
|
data/test/clean_ripper_test.rb
CHANGED
|
@@ -160,9 +160,11 @@ class CleanRipperTest < Test::Unit::TestCase
|
|
|
160
160
|
clean_ripper = [:program,
|
|
161
161
|
[:assign,
|
|
162
162
|
[:lvar, :a],
|
|
163
|
-
[:
|
|
164
|
-
[:ident, :oops],
|
|
165
|
-
[:lit, 10]
|
|
163
|
+
[:begin,
|
|
164
|
+
[:body, [:ident, :oops]],
|
|
165
|
+
[:rescue, nil, nil, [:lit, 10]],
|
|
166
|
+
nil,
|
|
167
|
+
nil
|
|
166
168
|
]
|
|
167
169
|
]
|
|
168
170
|
]
|
|
@@ -414,5 +416,633 @@ class CleanRipperTest < Test::Unit::TestCase
|
|
|
414
416
|
assert_equal(clean_ripper, CleanRipper.parse(code))
|
|
415
417
|
assert_equal(ripper, Ripper::SexpBuilder.new(code).parse)
|
|
416
418
|
end
|
|
419
|
+
def test_011
|
|
420
|
+
code = <<-END
|
|
421
|
+
a,b = 1,2
|
|
422
|
+
END
|
|
423
|
+
|
|
424
|
+
# CleanRipper output
|
|
425
|
+
clean_ripper = [:program,
|
|
426
|
+
[:massign,
|
|
427
|
+
[[:ident, :a], [:ident, :b]],
|
|
428
|
+
[[:lit, 1], [:lit, 2]]
|
|
429
|
+
]
|
|
430
|
+
]
|
|
431
|
+
|
|
432
|
+
# Ripper output
|
|
433
|
+
ripper = [:program,
|
|
434
|
+
[:stmts_add,
|
|
435
|
+
[:stmts_new],
|
|
436
|
+
[:massign,
|
|
437
|
+
[:mlhs_add,
|
|
438
|
+
[:mlhs_add,
|
|
439
|
+
[:mlhs_new],
|
|
440
|
+
[:@ident, "a", [1, 4]]
|
|
441
|
+
],
|
|
442
|
+
[:@ident, "b", [1, 6]]
|
|
443
|
+
],
|
|
444
|
+
[:mrhs_add,
|
|
445
|
+
[:mrhs_new_from_args,
|
|
446
|
+
[:args_add,
|
|
447
|
+
[:args_new],
|
|
448
|
+
[:@int, "1", [1, 10]]
|
|
449
|
+
]
|
|
450
|
+
],
|
|
451
|
+
[:@int, "2", [1, 12]]
|
|
452
|
+
]
|
|
453
|
+
]
|
|
454
|
+
]
|
|
455
|
+
]
|
|
456
|
+
|
|
457
|
+
assert_equal(clean_ripper, CleanRipper.parse(code))
|
|
458
|
+
assert_equal(ripper, Ripper::SexpBuilder.new(code).parse)
|
|
459
|
+
end
|
|
460
|
+
def test_012
|
|
461
|
+
code = <<-END
|
|
462
|
+
begin
|
|
463
|
+
a
|
|
464
|
+
rescue
|
|
465
|
+
b
|
|
466
|
+
else
|
|
467
|
+
c
|
|
468
|
+
ensure
|
|
469
|
+
d
|
|
470
|
+
end
|
|
471
|
+
END
|
|
472
|
+
|
|
473
|
+
# CleanRipper output
|
|
474
|
+
clean_ripper = [:program,
|
|
475
|
+
[:begin,
|
|
476
|
+
[:body, [:ident, :a]],
|
|
477
|
+
[:rescue, nil, nil, [:ident, :b]],
|
|
478
|
+
[:else, [:ident, :c]],
|
|
479
|
+
[:ensure, [:ident, :d]]
|
|
480
|
+
]
|
|
481
|
+
]
|
|
482
|
+
|
|
483
|
+
# Ripper output
|
|
484
|
+
ripper = [:program,
|
|
485
|
+
[:stmts_add,
|
|
486
|
+
[:stmts_new],
|
|
487
|
+
[:begin,
|
|
488
|
+
[:bodystmt,
|
|
489
|
+
[:stmts_add,
|
|
490
|
+
[:stmts_new],
|
|
491
|
+
[:var_ref, [:@ident, "a", [2, 6]]]
|
|
492
|
+
],
|
|
493
|
+
[:rescue, nil, nil,
|
|
494
|
+
[:stmts_add,
|
|
495
|
+
[:stmts_new],
|
|
496
|
+
[:var_ref, [:@ident, "b", [4, 6]]]
|
|
497
|
+
],
|
|
498
|
+
nil
|
|
499
|
+
],
|
|
500
|
+
[:else,
|
|
501
|
+
[:stmts_add,
|
|
502
|
+
[:stmts_new],
|
|
503
|
+
[:var_ref, [:@ident, "c", [6, 6]]]
|
|
504
|
+
]
|
|
505
|
+
],
|
|
506
|
+
[:ensure,
|
|
507
|
+
[:stmts_add,
|
|
508
|
+
[:stmts_new],
|
|
509
|
+
[:var_ref, [:@ident, "d", [8, 6]]]
|
|
510
|
+
]
|
|
511
|
+
]
|
|
512
|
+
]
|
|
513
|
+
]
|
|
514
|
+
]
|
|
515
|
+
]
|
|
516
|
+
|
|
517
|
+
assert_equal(clean_ripper, CleanRipper.parse(code))
|
|
518
|
+
assert_equal(ripper, Ripper::SexpBuilder.new(code).parse)
|
|
519
|
+
end
|
|
520
|
+
def test_013
|
|
521
|
+
code = <<-END
|
|
522
|
+
print var
|
|
523
|
+
print(var)
|
|
524
|
+
print (var)
|
|
525
|
+
END
|
|
526
|
+
|
|
527
|
+
# CleanRipper output
|
|
528
|
+
clean_ripper = [:program,
|
|
529
|
+
[:call, nil, :print, [[:ident, :var]], nil],
|
|
530
|
+
[:call, nil, :print, [[:ident, :var]], nil],
|
|
531
|
+
[:call, nil, :print, [[:ident, :var]], nil]
|
|
532
|
+
]
|
|
533
|
+
|
|
534
|
+
# Ripper output
|
|
535
|
+
ripper = [:program,
|
|
536
|
+
[:stmts_add,
|
|
537
|
+
[:stmts_add,
|
|
538
|
+
[:stmts_add,
|
|
539
|
+
[:stmts_new],
|
|
540
|
+
[:command,
|
|
541
|
+
[:@ident, "print", [1, 6]],
|
|
542
|
+
[:args_add_block,
|
|
543
|
+
[:args_add,
|
|
544
|
+
[:args_new],
|
|
545
|
+
[:var_ref, [:@ident, "var", [1, 12]]]
|
|
546
|
+
],
|
|
547
|
+
false
|
|
548
|
+
]
|
|
549
|
+
]
|
|
550
|
+
],
|
|
551
|
+
[:method_add_arg,
|
|
552
|
+
[:fcall, [:@ident, "print", [2, 6]]],
|
|
553
|
+
[:arg_paren,
|
|
554
|
+
[:args_add_block,
|
|
555
|
+
[:args_add,
|
|
556
|
+
[:args_new],
|
|
557
|
+
[:var_ref, [:@ident, "var", [2, 12]]]
|
|
558
|
+
],
|
|
559
|
+
false
|
|
560
|
+
]
|
|
561
|
+
]
|
|
562
|
+
]
|
|
563
|
+
],
|
|
564
|
+
[:command,
|
|
565
|
+
[:@ident, "print", [3, 6]],
|
|
566
|
+
[:args_add_block,
|
|
567
|
+
[:args_add,
|
|
568
|
+
[:args_new],
|
|
569
|
+
[:paren,
|
|
570
|
+
[:var_ref, [:@ident, "var", [3, 13]]]
|
|
571
|
+
]
|
|
572
|
+
],
|
|
573
|
+
false
|
|
574
|
+
]
|
|
575
|
+
]
|
|
576
|
+
]
|
|
577
|
+
]
|
|
578
|
+
|
|
579
|
+
assert_equal(clean_ripper, CleanRipper.parse(code))
|
|
580
|
+
assert_equal(ripper, Ripper::SexpBuilder.new(code).parse)
|
|
581
|
+
end
|
|
582
|
+
def test_014
|
|
583
|
+
code = <<-END
|
|
584
|
+
Array.new(5, nil)
|
|
585
|
+
END
|
|
586
|
+
|
|
587
|
+
# CleanRipper output
|
|
588
|
+
clean_ripper = [:program,
|
|
589
|
+
[:call, [:const, :Array], :new, [[:lit, 5], [:nil]], nil]
|
|
590
|
+
]
|
|
591
|
+
|
|
592
|
+
# Ripper output
|
|
593
|
+
ripper = [:program,
|
|
594
|
+
[:stmts_add,
|
|
595
|
+
[:stmts_new],
|
|
596
|
+
[:method_add_arg,
|
|
597
|
+
[:call,
|
|
598
|
+
[:var_ref, [:@const, "Array", [1, 4]]],
|
|
599
|
+
:".",
|
|
600
|
+
[:@ident, "new", [1, 10]]
|
|
601
|
+
],
|
|
602
|
+
[:arg_paren,
|
|
603
|
+
[:args_add_block,
|
|
604
|
+
[:args_add,
|
|
605
|
+
[:args_add,
|
|
606
|
+
[:args_new],
|
|
607
|
+
[:@int, "5", [1, 14]]
|
|
608
|
+
],
|
|
609
|
+
[:var_ref, [:@kw, "nil", [1, 17]]]
|
|
610
|
+
],
|
|
611
|
+
false
|
|
612
|
+
]
|
|
613
|
+
]
|
|
614
|
+
]
|
|
615
|
+
]
|
|
616
|
+
]
|
|
617
|
+
|
|
618
|
+
assert_equal(clean_ripper, CleanRipper.parse(code))
|
|
619
|
+
assert_equal(ripper, Ripper::SexpBuilder.new(code).parse)
|
|
620
|
+
end
|
|
621
|
+
def test_015
|
|
622
|
+
code = <<-END
|
|
623
|
+
eval do
|
|
624
|
+
print "Hello World!"
|
|
625
|
+
end
|
|
626
|
+
END
|
|
627
|
+
|
|
628
|
+
# CleanRipper output
|
|
629
|
+
clean_ripper = [:program,
|
|
630
|
+
[:call, nil, :eval, [],
|
|
631
|
+
[:brace_block,
|
|
632
|
+
[],
|
|
633
|
+
[:block,
|
|
634
|
+
[:call, nil, :print, [[:lit, "Hello World!"]], nil]
|
|
635
|
+
]
|
|
636
|
+
]
|
|
637
|
+
]
|
|
638
|
+
]
|
|
639
|
+
|
|
640
|
+
# Ripper output
|
|
641
|
+
ripper = [:program,
|
|
642
|
+
[:stmts_add,
|
|
643
|
+
[:stmts_new],
|
|
644
|
+
[:method_add_block,
|
|
645
|
+
[:method_add_arg,
|
|
646
|
+
[:fcall, [:@ident, "eval", [1, 4]]],
|
|
647
|
+
[:args_new]
|
|
648
|
+
],
|
|
649
|
+
[:do_block, nil,
|
|
650
|
+
[:stmts_add,
|
|
651
|
+
[:stmts_new],
|
|
652
|
+
[:command,
|
|
653
|
+
[:@ident, "print", [2, 6]],
|
|
654
|
+
[:args_add_block,
|
|
655
|
+
[:args_add,
|
|
656
|
+
[:args_new],
|
|
657
|
+
[:string_literal,
|
|
658
|
+
[:string_add,
|
|
659
|
+
[:string_content],
|
|
660
|
+
[:@tstring_content, "Hello World!", [2, 13]]
|
|
661
|
+
]
|
|
662
|
+
]
|
|
663
|
+
],
|
|
664
|
+
false
|
|
665
|
+
]
|
|
666
|
+
]
|
|
667
|
+
]
|
|
668
|
+
]
|
|
669
|
+
]
|
|
670
|
+
]
|
|
671
|
+
]
|
|
672
|
+
|
|
673
|
+
assert_equal(clean_ripper, CleanRipper.parse(code))
|
|
674
|
+
assert_equal(ripper, Ripper::SexpBuilder.new(code).parse)
|
|
675
|
+
end
|
|
676
|
+
def test_016
|
|
677
|
+
code = <<-'END'
|
|
678
|
+
a = "hello"
|
|
679
|
+
b = /world/
|
|
680
|
+
"a = #{a}" =~ /(.)\s=\s#{a}/
|
|
681
|
+
END
|
|
682
|
+
|
|
683
|
+
# CleanRipper output
|
|
684
|
+
clean_ripper = [:program,
|
|
685
|
+
[:assign, [:lvar, :a], [:lit, "hello"]],
|
|
686
|
+
[:assign, [:lvar, :b], [:lit, /world/]],
|
|
687
|
+
[:binary,
|
|
688
|
+
[:string, [:lit, "a = "], [:ident, :a]],
|
|
689
|
+
:=~,
|
|
690
|
+
[:regex, [:lit, /(.)\s=\s/], [:ident, :a]]
|
|
691
|
+
]
|
|
692
|
+
]
|
|
693
|
+
|
|
694
|
+
# Ripper output
|
|
695
|
+
ripper = [:program,
|
|
696
|
+
[:stmts_add,
|
|
697
|
+
[:stmts_add,
|
|
698
|
+
[:stmts_add,
|
|
699
|
+
[:stmts_new],
|
|
700
|
+
[:assign,
|
|
701
|
+
[:var_field, [:@ident, "a", [1, 4]]],
|
|
702
|
+
[:string_literal,
|
|
703
|
+
[:string_add,
|
|
704
|
+
[:string_content],
|
|
705
|
+
[:@tstring_content, "hello", [1, 9]]
|
|
706
|
+
]
|
|
707
|
+
]
|
|
708
|
+
]
|
|
709
|
+
],
|
|
710
|
+
[:assign,
|
|
711
|
+
[:var_field, [:@ident, "b", [2, 4]]],
|
|
712
|
+
[:regexp_literal,
|
|
713
|
+
[:regexp_add,
|
|
714
|
+
[:regexp_new],
|
|
715
|
+
[:@tstring_content, "world", [2, 9]]
|
|
716
|
+
],
|
|
717
|
+
[:@regexp_end, "/", [2, 14]]
|
|
718
|
+
]
|
|
719
|
+
]
|
|
720
|
+
],
|
|
721
|
+
[:binary,
|
|
722
|
+
[:string_literal,
|
|
723
|
+
[:string_add,
|
|
724
|
+
[:string_add,
|
|
725
|
+
[:string_content],
|
|
726
|
+
[:@tstring_content, "a = ", [3, 5]]
|
|
727
|
+
],
|
|
728
|
+
[:string_embexpr,
|
|
729
|
+
[:stmts_add,
|
|
730
|
+
[:stmts_new],
|
|
731
|
+
[:var_ref, [:@ident, "a", [3, 11]]]
|
|
732
|
+
]
|
|
733
|
+
]
|
|
734
|
+
]
|
|
735
|
+
],
|
|
736
|
+
:=~,
|
|
737
|
+
[:regexp_literal,
|
|
738
|
+
[:regexp_add,
|
|
739
|
+
[:regexp_add,
|
|
740
|
+
[:regexp_new],
|
|
741
|
+
[:@tstring_content, "(.)\\s=\\s", [3, 19]]
|
|
742
|
+
],
|
|
743
|
+
[:string_embexpr,
|
|
744
|
+
[:stmts_add,
|
|
745
|
+
[:stmts_new],
|
|
746
|
+
[:var_ref, [:@ident, "a", [3, 29]]]
|
|
747
|
+
]
|
|
748
|
+
]
|
|
749
|
+
],
|
|
750
|
+
[:@regexp_end, "/", [3, 31]]
|
|
751
|
+
]
|
|
752
|
+
]
|
|
753
|
+
]
|
|
754
|
+
]
|
|
755
|
+
|
|
756
|
+
assert_equal(clean_ripper, CleanRipper.parse(code))
|
|
757
|
+
assert_equal(ripper, Ripper::SexpBuilder.new(code).parse)
|
|
758
|
+
end
|
|
759
|
+
def test_017
|
|
760
|
+
code = <<-'END'
|
|
761
|
+
%q{Hello\sWorld}
|
|
762
|
+
%Q{Hello\sWorld}
|
|
763
|
+
%w{Hello World}
|
|
764
|
+
%s{Hello}
|
|
765
|
+
END
|
|
766
|
+
|
|
767
|
+
# CleanRipper output
|
|
768
|
+
clean_ripper = [:program,
|
|
769
|
+
[:lit, "Hello\\sWorld"],
|
|
770
|
+
[:lit, "Hello World"],
|
|
771
|
+
[:array, [:lit, "Hello"], [:lit, "World"]],
|
|
772
|
+
[:lit, :Hello]
|
|
773
|
+
]
|
|
774
|
+
|
|
775
|
+
# Ripper output
|
|
776
|
+
ripper = [:program,
|
|
777
|
+
[:stmts_add,
|
|
778
|
+
[:stmts_add,
|
|
779
|
+
[:stmts_add,
|
|
780
|
+
[:stmts_add,
|
|
781
|
+
[:stmts_new],
|
|
782
|
+
[:string_literal,
|
|
783
|
+
[:string_add,
|
|
784
|
+
[:string_content],
|
|
785
|
+
[:@tstring_content, "Hello\\sWorld", [1, 7]]
|
|
786
|
+
]
|
|
787
|
+
]
|
|
788
|
+
],
|
|
789
|
+
[:string_literal,
|
|
790
|
+
[:string_add,
|
|
791
|
+
[:string_content],
|
|
792
|
+
[:@tstring_content, "Hello\\sWorld", [2, 7]]
|
|
793
|
+
]
|
|
794
|
+
]
|
|
795
|
+
],
|
|
796
|
+
[:array,
|
|
797
|
+
[:qwords_add,
|
|
798
|
+
[:qwords_add,
|
|
799
|
+
[:qwords_new],
|
|
800
|
+
[:@tstring_content, "Hello", [3, 7]]
|
|
801
|
+
],
|
|
802
|
+
[:@tstring_content, "World", [3, 13]]
|
|
803
|
+
]
|
|
804
|
+
]
|
|
805
|
+
],
|
|
806
|
+
[:dyna_symbol,
|
|
807
|
+
[:xstring_add,
|
|
808
|
+
[:xstring_new],
|
|
809
|
+
[:@tstring_content, "Hello", [4, 7]]
|
|
810
|
+
]
|
|
811
|
+
]
|
|
812
|
+
]
|
|
813
|
+
]
|
|
814
|
+
|
|
815
|
+
assert_equal(clean_ripper, CleanRipper.parse(code))
|
|
816
|
+
assert_equal(ripper, Ripper::SexpBuilder.new(code).parse)
|
|
817
|
+
end
|
|
818
|
+
def test_018
|
|
819
|
+
code = <<-'END'
|
|
820
|
+
a, b, c = []
|
|
821
|
+
a, b, *c = []
|
|
822
|
+
a, b, c = *[]
|
|
823
|
+
END
|
|
824
|
+
|
|
825
|
+
# CleanRipper output
|
|
826
|
+
clean_ripper = [:program,
|
|
827
|
+
[:massign,
|
|
828
|
+
[[:ident, :a], [:ident, :b], [:ident, :c]],
|
|
829
|
+
[:array]
|
|
830
|
+
],
|
|
831
|
+
[:massign_splat,
|
|
832
|
+
[[:ident, :a], [:ident, :b], [:ident, :c]],
|
|
833
|
+
[:array]
|
|
834
|
+
],
|
|
835
|
+
[:massign,
|
|
836
|
+
[[:ident, :a], [:ident, :b], [:ident, :c]],
|
|
837
|
+
[:splat, [:array]]
|
|
838
|
+
]
|
|
839
|
+
]
|
|
840
|
+
|
|
841
|
+
# Ripper output
|
|
842
|
+
ripper = [:program,
|
|
843
|
+
[:stmts_add,
|
|
844
|
+
[:stmts_add,
|
|
845
|
+
[:stmts_add,
|
|
846
|
+
[:stmts_new],
|
|
847
|
+
[:massign,
|
|
848
|
+
[:mlhs_add,
|
|
849
|
+
[:mlhs_add,
|
|
850
|
+
[:mlhs_add,
|
|
851
|
+
[:mlhs_new],
|
|
852
|
+
[:@ident, "a", [1, 4]]
|
|
853
|
+
],
|
|
854
|
+
[:@ident, "b", [1, 7]]
|
|
855
|
+
],
|
|
856
|
+
[:@ident, "c", [1, 10]]
|
|
857
|
+
],
|
|
858
|
+
[:array, nil]
|
|
859
|
+
]
|
|
860
|
+
],
|
|
861
|
+
[:massign,
|
|
862
|
+
[:mlhs_add_star,
|
|
863
|
+
[:mlhs_add,
|
|
864
|
+
[:mlhs_add,
|
|
865
|
+
[:mlhs_new],
|
|
866
|
+
[:@ident, "a", [2, 4]]
|
|
867
|
+
],
|
|
868
|
+
[:@ident, "b", [2, 7]]
|
|
869
|
+
],
|
|
870
|
+
[:@ident, "c", [2, 11]]
|
|
871
|
+
],
|
|
872
|
+
[:array, nil]
|
|
873
|
+
]
|
|
874
|
+
],
|
|
875
|
+
[:massign,
|
|
876
|
+
[:mlhs_add,
|
|
877
|
+
[:mlhs_add,
|
|
878
|
+
[:mlhs_add,
|
|
879
|
+
[:mlhs_new],
|
|
880
|
+
[:@ident, "a", [3, 4]]
|
|
881
|
+
],
|
|
882
|
+
[:@ident, "b", [3, 7]]
|
|
883
|
+
],
|
|
884
|
+
[:@ident, "c", [3, 10]]
|
|
885
|
+
],
|
|
886
|
+
[:mrhs_add_star,
|
|
887
|
+
[:mrhs_new],
|
|
888
|
+
[:array, nil]
|
|
889
|
+
]
|
|
890
|
+
]
|
|
891
|
+
]
|
|
892
|
+
]
|
|
893
|
+
|
|
894
|
+
assert_equal(clean_ripper, CleanRipper.parse(code))
|
|
895
|
+
assert_equal(ripper, Ripper::SexpBuilder.new(code).parse)
|
|
896
|
+
end
|
|
897
|
+
def test_019
|
|
898
|
+
code = <<-'END'
|
|
899
|
+
a.x, b = 1, 2
|
|
900
|
+
a[2], b = 1, 2
|
|
901
|
+
END
|
|
902
|
+
|
|
903
|
+
# CleanRipper output
|
|
904
|
+
clean_ripper = [:program,
|
|
905
|
+
[:massign,
|
|
906
|
+
[[:field, [:ident, :a], :x, []], [:ident, :b]],
|
|
907
|
+
[[:lit, 1], [:lit, 2]]
|
|
908
|
+
],
|
|
909
|
+
[:massign,
|
|
910
|
+
[[:field, [:ident, :a], :[], [[:lit, 2]]], [:ident, :b]],
|
|
911
|
+
[[:lit, 1], [:lit, 2]]
|
|
912
|
+
]
|
|
913
|
+
]
|
|
914
|
+
|
|
915
|
+
# Ripper output
|
|
916
|
+
ripper = [:program,
|
|
917
|
+
[:stmts_add,
|
|
918
|
+
[:stmts_add,
|
|
919
|
+
[:stmts_new],
|
|
920
|
+
[:massign,
|
|
921
|
+
[:mlhs_add,
|
|
922
|
+
[:mlhs_add,
|
|
923
|
+
[:mlhs_new],
|
|
924
|
+
[:field,
|
|
925
|
+
[:var_ref, [:@ident, "a", [1, 4]]],
|
|
926
|
+
:".",
|
|
927
|
+
[:@ident, "x", [1, 6]]
|
|
928
|
+
]
|
|
929
|
+
],
|
|
930
|
+
[:@ident, "b", [1, 9]]
|
|
931
|
+
],
|
|
932
|
+
[:mrhs_add,
|
|
933
|
+
[:mrhs_new_from_args,
|
|
934
|
+
[:args_add,
|
|
935
|
+
[:args_new],
|
|
936
|
+
[:@int, "1", [1, 13]]
|
|
937
|
+
]
|
|
938
|
+
],
|
|
939
|
+
[:@int, "2", [1, 16]]
|
|
940
|
+
]
|
|
941
|
+
]
|
|
942
|
+
],
|
|
943
|
+
[:massign,
|
|
944
|
+
[:mlhs_add,
|
|
945
|
+
[:mlhs_add,
|
|
946
|
+
[:mlhs_new],
|
|
947
|
+
[:aref_field,
|
|
948
|
+
[:var_ref, [:@ident, "a", [2, 4]]],
|
|
949
|
+
[:args_add_block,
|
|
950
|
+
[:args_add,
|
|
951
|
+
[:args_new],
|
|
952
|
+
[:@int, "2", [2, 6]]
|
|
953
|
+
],
|
|
954
|
+
false
|
|
955
|
+
]
|
|
956
|
+
]
|
|
957
|
+
],
|
|
958
|
+
[:@ident, "b", [2, 10]]
|
|
959
|
+
],
|
|
960
|
+
[:mrhs_add,
|
|
961
|
+
[:mrhs_new_from_args,
|
|
962
|
+
[:args_add,
|
|
963
|
+
[:args_new],
|
|
964
|
+
[:@int, "1", [2, 14]]
|
|
965
|
+
]
|
|
966
|
+
],
|
|
967
|
+
[:@int, "2", [2, 17]]
|
|
968
|
+
]
|
|
969
|
+
]
|
|
970
|
+
]
|
|
971
|
+
]
|
|
972
|
+
|
|
973
|
+
assert_equal(clean_ripper, CleanRipper.parse(code))
|
|
974
|
+
assert_equal(ripper, Ripper::SexpBuilder.new(code).parse)
|
|
975
|
+
end
|
|
976
|
+
def test_020
|
|
977
|
+
code = <<-'END'
|
|
978
|
+
a, (b, c), d = 1, [2, 3], 4
|
|
979
|
+
END
|
|
980
|
+
|
|
981
|
+
# CleanRipper output
|
|
982
|
+
clean_ripper = [:program,
|
|
983
|
+
[:massign,
|
|
984
|
+
[
|
|
985
|
+
[:ident, :a],
|
|
986
|
+
[:group, [:ident, :b], [:ident, :c]],
|
|
987
|
+
[:ident, :d]
|
|
988
|
+
],
|
|
989
|
+
[
|
|
990
|
+
[:lit, 1],
|
|
991
|
+
[:array, [:lit, 2], [:lit, 3]],
|
|
992
|
+
[:lit, 4]
|
|
993
|
+
]
|
|
994
|
+
]
|
|
995
|
+
]
|
|
996
|
+
|
|
997
|
+
# Ripper output
|
|
998
|
+
ripper = [:program,
|
|
999
|
+
[:stmts_add,
|
|
1000
|
+
[:stmts_new],
|
|
1001
|
+
[:massign,
|
|
1002
|
+
[:mlhs_add,
|
|
1003
|
+
[:mlhs_add,
|
|
1004
|
+
[:mlhs_add,
|
|
1005
|
+
[:mlhs_new],
|
|
1006
|
+
[:@ident, "a", [1, 4]]
|
|
1007
|
+
],
|
|
1008
|
+
[:mlhs_paren,
|
|
1009
|
+
[:mlhs_add,
|
|
1010
|
+
[:mlhs_add,
|
|
1011
|
+
[:mlhs_new],
|
|
1012
|
+
[:@ident, "b", [1, 8]]
|
|
1013
|
+
],
|
|
1014
|
+
[:@ident, "c", [1, 11]]
|
|
1015
|
+
]
|
|
1016
|
+
]
|
|
1017
|
+
],
|
|
1018
|
+
[:@ident, "d", [1, 15]]
|
|
1019
|
+
],
|
|
1020
|
+
[:mrhs_add,
|
|
1021
|
+
[:mrhs_new_from_args,
|
|
1022
|
+
[:args_add,
|
|
1023
|
+
[:args_add,
|
|
1024
|
+
[:args_new],
|
|
1025
|
+
[:@int, "1", [1, 19]]
|
|
1026
|
+
],
|
|
1027
|
+
[:array,
|
|
1028
|
+
[:args_add,
|
|
1029
|
+
[:args_add,
|
|
1030
|
+
[:args_new],
|
|
1031
|
+
[:@int, "2", [1, 23]]
|
|
1032
|
+
],
|
|
1033
|
+
[:@int, "3", [1, 26]]
|
|
1034
|
+
]
|
|
1035
|
+
]
|
|
1036
|
+
]
|
|
1037
|
+
],
|
|
1038
|
+
[:@int, "4", [1, 30]]
|
|
1039
|
+
]
|
|
1040
|
+
]
|
|
1041
|
+
]
|
|
1042
|
+
]
|
|
1043
|
+
|
|
1044
|
+
assert_equal(clean_ripper, CleanRipper.parse(code))
|
|
1045
|
+
assert_equal(ripper, Ripper::SexpBuilder.new(code).parse)
|
|
1046
|
+
end
|
|
417
1047
|
|
|
418
1048
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: clean_ripper
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.1
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -12,7 +12,7 @@ cert_chain: []
|
|
|
12
12
|
date: 2011-09-10 00:00:00.000000000Z
|
|
13
13
|
dependencies: []
|
|
14
14
|
description: Produces a clean output for Ripper, making the output tree usable and
|
|
15
|
-
|
|
15
|
+
readable.
|
|
16
16
|
email: lb-guilherme@live.com
|
|
17
17
|
executables: []
|
|
18
18
|
extensions: []
|
|
@@ -20,6 +20,7 @@ extra_rdoc_files: []
|
|
|
20
20
|
files:
|
|
21
21
|
- lib/clean_ripper.rb
|
|
22
22
|
- lib/clean_sexp_builder.rb
|
|
23
|
+
- lib/clean_ripper_doc.rb
|
|
23
24
|
- test/clean_ripper_test.rb
|
|
24
25
|
homepage: https://github.com/LBg/CleanRipper
|
|
25
26
|
licenses: []
|
|
@@ -41,9 +42,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
41
42
|
version: '0'
|
|
42
43
|
requirements: []
|
|
43
44
|
rubyforge_project:
|
|
44
|
-
rubygems_version: 1.
|
|
45
|
+
rubygems_version: 1.7.2
|
|
45
46
|
signing_key:
|
|
46
47
|
specification_version: 3
|
|
47
48
|
summary: Produces a clean output for Ripper.
|
|
48
49
|
test_files:
|
|
49
50
|
- test/clean_ripper_test.rb
|
|
51
|
+
has_rdoc:
|