Module: RipperRubyParser::SexpHandlers::HelperMethods Private

Defined in:
lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Utility methods used in several of the sexp handler modules

Instance Method Summary collapse

Instance Method Details

#convert_empty_to_nil_symbol(block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



124
125
126
127
128
129
130
131
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 124

def convert_empty_to_nil_symbol(block)
  case block.length
  when 0
    s(:nil)
  else
    block
  end
end

#convert_void_stmt_to_nil_symbol(block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



115
116
117
118
119
120
121
122
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 115

def convert_void_stmt_to_nil_symbol(block)
  case block.sexp_type
  when :void_stmt
    s(:nil)
  else
    block
  end
end

#extract_node_symbol(exp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



7
8
9
10
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 7

def extract_node_symbol(exp)
  ident, = extract_node_symbol_with_position(exp)
  ident
end

#extract_node_symbol_with_position(exp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



12
13
14
15
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 12

def extract_node_symbol_with_position(exp)
  _, ident, pos = exp.shift 3
  return ident.to_sym, pos
end

#generic_add_star(exp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



41
42
43
44
45
46
47
48
49
50
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 41

def generic_add_star(exp)
  _, args, splatarg, *rest = shift_all exp
  items = process args
  if splatarg
    items.push s(:splat, unwrap_begin(process(splatarg)))
  else
    items.push s(:splat)
  end
  items.push(*map_process_list(rest))
end

#handle_return_argument_list(arglist) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 133

def handle_return_argument_list(arglist)
  args = process(arglist).sexp_body

  case args.length
  when 0
    args
  when 1
    arg = args.first
    if arg.sexp_type == :splat
      s(:svalue, arg)
    else
      arg
    end
  else
    s(:array, *args)
  end
end

#integer_literal?(exp) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


52
53
54
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 52

def integer_literal?(exp)
  exp && exp.sexp_type == :lit && exp[1].is_a?(Integer)
end

#make_symbol(exp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
20
21
22
23
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 17

def make_symbol(exp)
  return nil if exp.nil?
  raise "Unexpected number of children: #{exp.length}" if exp.length != 2

  _, ident = exp.shift 2
  ident.to_sym
end

#map_process_list(list) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



64
65
66
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 64

def map_process_list(list)
  list.map { |exp| process(exp) }
end

#map_process_list_compact(list) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



60
61
62
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 60

def map_process_list_compact(list)
  reject_void_stmt map_unwrap_begin_list map_process_list list
end

#map_unwrap_begin_list(list) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



68
69
70
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 68

def map_unwrap_begin_list(list)
  list.map { |exp| unwrap_begin(exp) }
end

#reject_void_stmt(body) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 56

def reject_void_stmt(body)
  body.reject { |sub_exp| sub_exp.sexp_type == :void_stmt }
end

#safe_unwrap_void_stmt(exp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



80
81
82
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 80

def safe_unwrap_void_stmt(exp)
  unwrap_nil(exp) || s()
end

#shift_all(exp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



151
152
153
154
155
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 151

def shift_all(exp)
  [].tap do |result|
    result << exp.shift until exp.empty?
  end
end

#unwrap_begin(exp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



84
85
86
87
88
89
90
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 84

def unwrap_begin(exp)
  if exp.sexp_type == :begin
    exp[1]
  else
    exp
  end
end

#unwrap_block(exp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



92
93
94
95
96
97
98
99
100
101
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 92

def unwrap_block(exp)
  case exp.sexp_type
  when :block
    exp.sexp_body
  when :void_stmt
    [nil]
  else
    [exp]
  end
end

#unwrap_nil(exp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



72
73
74
75
76
77
78
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 72

def unwrap_nil(exp)
  if exp.sexp_type == :void_stmt
    nil
  else
    exp
  end
end

#with_line_number(line, exp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



31
32
33
34
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 31

def with_line_number(line, exp)
  exp.line = line
  exp
end

#with_position(pos, exp = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
28
29
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 25

def with_position(pos, exp = nil)
  (line,) = pos
  exp = yield if exp.nil?
  with_line_number line, exp
end

#with_position_from_node_symbol(exp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
39
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 36

def with_position_from_node_symbol(exp)
  sym, pos = extract_node_symbol_with_position exp
  with_position(pos, yield(sym))
end

#wrap_in_block(statements, line) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb', line 103

def wrap_in_block(statements, line)
  case statements.length
  when 0
    s(:void_stmt).line(line)
  when 1
    statements.first
  else
    first = statements.shift
    s(:block, *unwrap_block(first), *statements)
  end
end