Class: Ripper::CleanSexpBuilder

Inherits:
SexpBuilder
  • Object
show all
Defined in:
lib/clean_sexp_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ CleanSexpBuilder



7
8
9
10
# File 'lib/clean_sexp_builder.rb', line 7

def initialize(source)
  @source = source.split("\n")
  super
end

Class Method Details

.parse(source) ⇒ Object



6
7
8
# File 'lib/clean_ripper.rb', line 6

def CleanRipper.parse(source)
  CleanRipper.new(source).parse
end

Instance Method Details

#on_aref(*args) ⇒ Object



100
101
102
# File 'lib/clean_sexp_builder.rb', line 100

def on_aref(*args)
  [:call, args[0], :[], args[1], nil]
end

#on_aref_field(*args) ⇒ Object



317
318
319
# File 'lib/clean_sexp_builder.rb', line 317

def on_aref_field(*args)
  [:call, args[0], :[], args[1], nil]
end

#on_args_add(*args) ⇒ Object



148
149
150
# File 'lib/clean_sexp_builder.rb', line 148

def on_args_add(*args)
  args.first ? args.first + [args.last] : [args.last]
end

#on_args_add_block(*args) ⇒ Object



136
137
138
# File 'lib/clean_sexp_builder.rb', line 136

def on_args_add_block(*args)
  args.first
end

#on_args_add_star(*args) ⇒ Object



72
73
74
# File 'lib/clean_sexp_builder.rb', line 72

def on_args_add_star(*args)
  [:splat, args.last]
end

#on_args_new(*args) ⇒ Object



140
141
142
# File 'lib/clean_sexp_builder.rb', line 140

def on_args_new(*args)
  nil
end

#on_array(*args) ⇒ Object



249
250
251
252
# File 'lib/clean_sexp_builder.rb', line 249

def on_array(*args)
  el = args.first || []
  [:array] + ((el.size > 0 and Symbol === el.first) ? [el] : el)
end

#on_begin(*args) ⇒ Object



197
198
199
200
201
# File 'lib/clean_sexp_builder.rb', line 197

def on_begin(*args)
  body = args.first.shift
  body.size > 1 ? body = [:block, *body] : body = body.first
  [:begin, [:body, body], *args.first]
end

#on_block_var(*args) ⇒ Object



159
160
161
# File 'lib/clean_sexp_builder.rb', line 159

def on_block_var(*args)
  [:block] + args.first
end

#on_bodystmt(*args) ⇒ Object



227
228
229
# File 'lib/clean_sexp_builder.rb', line 227

def on_bodystmt(*args)
  args
end

#on_call(*args) ⇒ Object



128
129
130
# File 'lib/clean_sexp_builder.rb', line 128

def on_call(*args)
  [:call, args[0], args[-1][1], [], nil]
end

#on_CHAR(*args) ⇒ Object



84
85
86
# File 'lib/clean_sexp_builder.rb', line 84

def on_CHAR(*args)
  [:lit, eval(args.first)]
end

#on_command(*args) ⇒ Object



144
145
146
# File 'lib/clean_sexp_builder.rb', line 144

def on_command(*args)
  [:call, nil, args[0][1], args[1] || [], nil]
end

#on_const(*args) ⇒ Object



223
224
225
# File 'lib/clean_sexp_builder.rb', line 223

def on_const(*args)
  [:const, args.first.to_sym]
end

#on_cvar(*args) ⇒ Object



108
109
110
# File 'lib/clean_sexp_builder.rb', line 108

def on_cvar(*args)
  [:cvar, args.first.to_sym]
end

#on_do_block(*args) ⇒ Object



231
232
233
# File 'lib/clean_sexp_builder.rb', line 231

def on_do_block(*args)
  [:brace_block, args[0] ? args[0][1..-1] : [], args[1]]
end

#on_dyna_symbol(*args) ⇒ Object



336
337
338
339
340
341
342
343
344
345
# File 'lib/clean_sexp_builder.rb', line 336

def on_dyna_symbol(*args)
  s = [:symbol, *(args.first || []).compact]
  if s.size == 2 and s[1][0] == :lit
    [:lit, s[1][1].to_sym]
  elsif s.size == 1
    [:lit, "".to_sym]
  else
    s
  end
end

#on_else(*args) ⇒ Object



203
204
205
206
207
# File 'lib/clean_sexp_builder.rb', line 203

def on_else(*args)
  block = args.first
  block.size > 1 ? block = [:block, *block] : block = block.first
  [:else, block]
end

#on_ensure(*args) ⇒ Object



209
210
211
212
213
# File 'lib/clean_sexp_builder.rb', line 209

def on_ensure(*args)
  block = args.first
  block.size > 1 ? block = [:block, *block] : block = block.first
  [:ensure, block]
end

#on_fcall(*args) ⇒ Object



132
133
134
# File 'lib/clean_sexp_builder.rb', line 132

def on_fcall(*args)
  [:call, nil, args[0][1], [], nil]
end

#on_field(*args) ⇒ Object



313
314
315
# File 'lib/clean_sexp_builder.rb', line 313

def on_field(*args)
  [:call, args[0], args[2][1], [], nil]
end

#on_float(*args) ⇒ Object



80
81
82
# File 'lib/clean_sexp_builder.rb', line 80

def on_float(*args)
  [:lit, eval(args.first)]
end

#on_gvar(*args) ⇒ Object



104
105
106
# File 'lib/clean_sexp_builder.rb', line 104

def on_gvar(*args)
  [:gvar, args.first.to_sym]
end

#on_ident(*args) ⇒ Object



116
117
118
# File 'lib/clean_sexp_builder.rb', line 116

def on_ident(*args)
  [:ident, args.first.to_sym]
end

#on_if(*args) ⇒ Object



239
240
241
242
# File 'lib/clean_sexp_builder.rb', line 239

def on_if(*args)
  args[0] = [args.first] if Symbol === args.first.first
  [:if] << [:block] + args.first << ([:block, *args[1]] if args[1].first) << ([:block, *args[2][1][1..-1]] if args[2])
end

#on_if_mod(*args) ⇒ Object



235
236
237
# File 'lib/clean_sexp_builder.rb', line 235

def on_if_mod(*args)
  [:if] + args + [nil]
end

#on_int(*args) ⇒ Object



76
77
78
# File 'lib/clean_sexp_builder.rb', line 76

def on_int(*args)
  [:lit, eval(args.first)]
end

#on_ivar(*args) ⇒ Object



112
113
114
# File 'lib/clean_sexp_builder.rb', line 112

def on_ivar(*args)
  [:ivar, args.first.to_sym]
end

#on_kw(*args) ⇒ Object



193
194
195
# File 'lib/clean_sexp_builder.rb', line 193

def on_kw(*args)
  [args.first.to_sym]
end

#on_massign(*args) ⇒ Object



305
306
307
308
309
310
311
# File 'lib/clean_sexp_builder.rb', line 305

def on_massign(*args)
  if args[0][0] == :mlhs_add_star
    [:massign_splat, args[0][1] << args[0][2], args[1]]
  else
    [:massign, *args]
  end
end

#on_method_add_arg(*args) ⇒ Object



124
125
126
# File 'lib/clean_sexp_builder.rb', line 124

def on_method_add_arg(*args)
  [:call, *args.first[1..-3], (args[1][1] if args[1]) || [], nil]
end

#on_method_add_block(*args) ⇒ Object



152
153
154
155
156
157
# File 'lib/clean_sexp_builder.rb', line 152

def on_method_add_block(*args)
  brace_block = args[1]
  brace_block[2].unshift(:block)
  brace_block[2] = nil if brace_block[2].compact.size == 1
  args.first[0, 4] << brace_block
end

#on_mlhs_add(*args) ⇒ Object



40
41
42
# File 'lib/clean_sexp_builder.rb', line 40

def on_mlhs_add(*args)
  args.first ? args.first + [args.last] : [args.last]
end

#on_mlhs_new(*args) ⇒ Object



36
37
38
# File 'lib/clean_sexp_builder.rb', line 36

def on_mlhs_new(*args)
  nil
end

#on_mlhs_paren(*args) ⇒ Object



321
322
323
# File 'lib/clean_sexp_builder.rb', line 321

def on_mlhs_paren(*args)
  [:group, *args[0]]
end

#on_mrhs_add(*args) ⇒ Object



60
61
62
# File 'lib/clean_sexp_builder.rb', line 60

def on_mrhs_add(*args)
  args.first ? args.first + [args.last] : [args.last]
end

#on_mrhs_add_star(*args) ⇒ Object



32
33
34
# File 'lib/clean_sexp_builder.rb', line 32

def on_mrhs_add_star(*args)
  [:splat, args.last]
end

#on_mrhs_new(*args) ⇒ Object



24
25
26
# File 'lib/clean_sexp_builder.rb', line 24

def on_mrhs_new(*args)
  nil
end

#on_mrhs_new_from_args(*args) ⇒ Object



68
69
70
# File 'lib/clean_sexp_builder.rb', line 68

def on_mrhs_new_from_args(*args)
  args.first
end

#on_op(*args) ⇒ Object



120
121
122
# File 'lib/clean_sexp_builder.rb', line 120

def on_op(*args)
  args[0].to_sym
end

#on_param_error(*args) ⇒ Object



183
184
185
# File 'lib/clean_sexp_builder.rb', line 183

def on_param_error(*args)
  args.first
end

#on_params(*args) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/clean_sexp_builder.rb', line 163

def on_params(*args)
  params = []
  args.first.each do |p|
    p[0] = :lvar if p.first == :ident
    params << [:param, p, nil]
  end if args.first
  args[1].each do |p|
    p.first[0] = :lvar if p.first.first == :ident
    p[1] = [p[1]] if Symbol === p[1].first
    params << [:param, p.first, [:block, *p[1]]]
  end if args[1]
  params << args[2] if args[2]
  params
end

#on_paren(*args) ⇒ Object



187
188
189
190
191
# File 'lib/clean_sexp_builder.rb', line 187

def on_paren(*args)
  l = args.first
  l = [l] if Symbol === l[0]
  l.size == 1 ? l.first : [:block] + l
end

#on_program(*args) ⇒ Object



12
13
14
# File 'lib/clean_sexp_builder.rb', line 12

def on_program(*args)
  [:program] + args.first
end

#on_qwords_add(*args) ⇒ Object



258
259
260
# File 'lib/clean_sexp_builder.rb', line 258

def on_qwords_add(*args)
  args.first ? args.first + [args.last] : [args.last]
end

#on_qwords_new(*args) ⇒ Object



301
302
303
# File 'lib/clean_sexp_builder.rb', line 301

def on_qwords_new(*args)
  args
end

#on_regexp_add(*args) ⇒ Object



48
49
50
# File 'lib/clean_sexp_builder.rb', line 48

def on_regexp_add(*args)
  args.first ? args.first + [args.last] : [args.last]
end

#on_regexp_literal(*args) ⇒ Object



325
326
327
328
329
330
331
332
333
334
# File 'lib/clean_sexp_builder.rb', line 325

def on_regexp_literal(*args)
  s = [:regex, *(args.first || []).compact]
  if s.size == 2 and s[1][0] == :lit
    s[1]
  elsif s.size == 1
    [:lit, //]
  else
    s
  end
end

#on_regexp_new(*args) ⇒ Object



44
45
46
# File 'lib/clean_sexp_builder.rb', line 44

def on_regexp_new(*args)
  nil
end

#on_rescue(*args) ⇒ Object



215
216
217
218
219
220
221
# File 'lib/clean_sexp_builder.rb', line 215

def on_rescue(*args)
  err = args.shift
  var = args.shift
  block = args.first
  block.size > 1 ? block = [:block, *block] : block = block.first
  [:rescue, err, var, block]
end

#on_rescue_mod(*args) ⇒ Object



64
65
66
# File 'lib/clean_sexp_builder.rb', line 64

def on_rescue_mod(*args)
  [:begin, [:body, args[0]], [:rescue, nil, nil, args[1]], nil, nil]
end

#on_rest_param(*args) ⇒ Object



178
179
180
181
# File 'lib/clean_sexp_builder.rb', line 178

def on_rest_param(*args)
  args.first[0] = :lvar if args.first.first == :ident
  [:rest_param, args.first]
end

#on_stmts_add(*args) ⇒ Object



28
29
30
# File 'lib/clean_sexp_builder.rb', line 28

def on_stmts_add(*args)
  args.first ? args.first + [args.last] : [args.last]
end

#on_stmts_new(*args) ⇒ Object



20
21
22
# File 'lib/clean_sexp_builder.rb', line 20

def on_stmts_new(*args)
  nil
end

#on_string_add(*args) ⇒ Object



254
255
256
# File 'lib/clean_sexp_builder.rb', line 254

def on_string_add(*args)
  args.first ? args.first + [args.last] : [args.last]
end

#on_string_content(*args) ⇒ Object



262
263
264
# File 'lib/clean_sexp_builder.rb', line 262

def on_string_content(*args)
  nil
end

#on_string_embexpr(*args) ⇒ Object



295
296
297
298
299
# File 'lib/clean_sexp_builder.rb', line 295

def on_string_embexpr(*args)
  if args[0][0]
    args[0].size == 1 ? args[0][0] : [:block] + args[0]
  end
end

#on_string_literal(*args) ⇒ Object



266
267
268
269
270
271
272
273
274
275
# File 'lib/clean_sexp_builder.rb', line 266

def on_string_literal(*args)
  s = [:string, *(args.first || []).compact]
  if s.size == 2 and s[1][0] == :lit
    s[1]
  elsif s.size == 1
    [:lit, ""]
  else
    s
  end
end

#on_symbol_literal(*args) ⇒ Object



347
348
349
# File 'lib/clean_sexp_builder.rb', line 347

def on_symbol_literal(*args)
  [:lit, args[0][1][1]]
end

#on_tstring_content(*args) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/clean_sexp_builder.rb', line 277

def on_tstring_content(*args)
  str = "\""+args.first+"\""
  if @source[lineno-1][column-1, 1] == "'"
    str[0] = str[-1] = "'"
  elsif @source[lineno-1][column-1, 1] == "/"
    str[0] = str[-1] = "/"
  else
    if @source[lineno-1][column-1-2, 2] =~ /%([qQ])/
      case $1
      when "q"
        str[0] = str[-1] = "'"
      when "Q"
      end
    end
  end
  [:lit, eval(str)]
end

#on_unless(*args) ⇒ Object



244
245
246
247
# File 'lib/clean_sexp_builder.rb', line 244

def on_unless(*args)
  args[0] = [args.first] if Symbol === args.first.first
  [:if] << [:block] + args.first << ([:block, *args[2][1][1..-1]] if args[2]) << ([:block, *args[1]] if args[1].first)
end

#on_var_field(*args) ⇒ Object



88
89
90
91
92
# File 'lib/clean_sexp_builder.rb', line 88

def on_var_field(*args)
  var = args.first
  var[0] = :lvar if var.first == :ident
  var
end

#on_var_ref(*args) ⇒ Object



94
95
96
97
98
# File 'lib/clean_sexp_builder.rb', line 94

def on_var_ref(*args)
  var = args.first
  var[0] = :ident if var.first == :ident
  var
end

#on_void_stmt(*args) ⇒ Object



16
17
18
# File 'lib/clean_sexp_builder.rb', line 16

def on_void_stmt(*args)
  nil
end

#on_xstring_add(*args) ⇒ Object



56
57
58
# File 'lib/clean_sexp_builder.rb', line 56

def on_xstring_add(*args)
  args.first ? args.first + [args.last] : [args.last]
end

#on_xstring_new(*args) ⇒ Object



52
53
54
# File 'lib/clean_sexp_builder.rb', line 52

def on_xstring_new(*args)
  nil
end