Class: RipperRubyParser::CommentingRipperParser Private

Inherits:
Ripper::SexpBuilder
  • Object
show all
Defined in:
lib/ripper_ruby_parser/commenting_ripper_parser.rb

Overview

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

Variant of Ripper’s SexpBuilder parser class that inserts comments as Sexps into the built parse tree.

Constant Summary collapse

NUMBER_LITERAL_TYPES =

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

[:@int, :@float].freeze

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CommentingRipperParser

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 a new instance of CommentingRipperParser.



13
14
15
16
17
18
19
20
21
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 13

def initialize(*args)
  super
  @comment = ""
  @comment_stack = []
  @delimiter_stack = []
  @space_before = false
  @seen_space = false
  @in_symbol = false
end

Instance Method Details

#on_alias_error(*args) ⇒ 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.

Raises:



304
305
306
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 304

def on_alias_error(*args)
  raise SyntaxError, *args
end

#on_args_add(list, elem) ⇒ 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.



96
97
98
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 96

def on_args_add(list, elem)
  list << elem
end

#on_args_newObject

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
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 92

def on_args_new
  [:args]
end

#on_assign_error(*args) ⇒ 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.

Raises:



308
309
310
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 308

def on_assign_error(*args)
  raise SyntaxError, *args
end

#on_backtick(delimiter) ⇒ 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.



30
31
32
33
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 30

def on_backtick(delimiter)
  @delimiter_stack.push delimiter
  super
end

#on_begin(*args) ⇒ 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.



35
36
37
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 35

def on_begin(*args)
  commentize(:begin, super)
end

#on_BEGIN(*args) ⇒ 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.



288
289
290
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 288

def on_BEGIN(*args)
  commentize(:BEGIN, super)
end

#on_class(*args) ⇒ 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.



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

def on_class(*args)
  commentize(:class, super)
end

#on_class_name_error(*args) ⇒ 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.

Raises:



300
301
302
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 300

def on_class_name_error(*args)
  raise SyntaxError, *args
end

#on_comment(tok) ⇒ 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.



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

def on_comment(tok)
  @comment += tok
end

#on_def(*args) ⇒ 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
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 84

def on_def(*args)
  commentize(:def, super)
end

#on_defs(*args) ⇒ 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.



88
89
90
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 88

def on_defs(*args)
  commentize(:def, super)
end

#on_dyna_symbol(*args) ⇒ 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.



283
284
285
286
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 283

def on_dyna_symbol(*args)
  @in_symbol = false
  super
end

#on_embdoc(tok) ⇒ 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.



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

def on_embdoc(tok)
  @comment += tok
end

#on_embdoc_beg(tok) ⇒ 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.



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

def on_embdoc_beg(tok)
  @comment += tok
end

#on_embdoc_end(tok) ⇒ 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/commenting_ripper_parser.rb', line 56

def on_embdoc_end(tok)
  @comment += tok
end

#on_embexpr_beg(_delimiter) ⇒ 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.



279
280
281
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 279

def on_embexpr_beg(_delimiter)
  @in_symbol = false
end

#on_END(*args) ⇒ 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.



292
293
294
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 292

def on_END(*args)
  commentize(:END, super)
end

#on_float(_token) ⇒ 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.



248
249
250
251
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 248

def on_float(_token)
  @space_before = @seen_space
  super
end

#on_heredoc_beg(delimiter) ⇒ 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.



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

def on_heredoc_beg(delimiter)
  @delimiter_stack.push delimiter
end

#on_heredoc_end(_delimiter) ⇒ 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.



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

def on_heredoc_end(_delimiter)
  @delimiter_stack.pop
end

#on_int(_token) ⇒ 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.



243
244
245
246
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 243

def on_int(_token)
  @space_before = @seen_space
  super
end

#on_kw(tok) ⇒ 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
63
64
65
66
67
68
69
70
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 60

def on_kw(tok)
  result = super
  case tok
  when "class", "def", "module", "BEGIN", "begin", "END"
    unless @in_symbol
      @comment_stack.push [result, @comment]
      @comment = ""
    end
  end
  result
end

#on_mlhs_add(list, elem) ⇒ 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.



112
113
114
115
116
117
118
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 112

def on_mlhs_add(list, elem)
  if list.first == :mlhs
    list << elem
  else
    [:mlhs_add_post, list, elem]
  end
end

#on_mlhs_newObject

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.



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

def on_mlhs_new
  [:mlhs]
end

#on_module(*args) ⇒ 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
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 72

def on_module(*args)
  commentize(:module, super)
end

#on_mrhs_add(list, elem) ⇒ 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
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 124

def on_mrhs_add(list, elem)
  list << elem
end

#on_mrhs_newObject

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.



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

def on_mrhs_new
  [:mrhs]
end

#on_op(token) ⇒ 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.



234
235
236
237
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 234

def on_op(token)
  @seen_space = false
  super
end

#on_param_error(*args) ⇒ 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.

Raises:



312
313
314
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 312

def on_param_error(*args)
  raise SyntaxError, *args
end

#on_parse_error(*args) ⇒ 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.

Raises:



296
297
298
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 296

def on_parse_error(*args)
  raise SyntaxError, *args
end

#on_qsymbols_add(list, elem) ⇒ 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.



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

def on_qsymbols_add(list, elem)
  list << elem
end

#on_qsymbols_beg(delimiter) ⇒ 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.



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

def on_qsymbols_beg(delimiter)
  @delimiter_stack.push delimiter
end

#on_qsymbols_newObject

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.



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

def on_qsymbols_new
  [:qsymbols]
end

#on_qwords_add(list, elem) ⇒ 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.



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

def on_qwords_add(list, elem)
  list << elem
end

#on_qwords_beg(delimiter) ⇒ 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.



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

def on_qwords_beg(delimiter)
  @delimiter_stack.push delimiter
end

#on_qwords_newObject

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.



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

def on_qwords_new
  [:qwords]
end

#on_regexp_add(list, elem) ⇒ 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.



165
166
167
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 165

def on_regexp_add(list, elem)
  list << elem
end

#on_regexp_beg(delimiter) ⇒ 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.



152
153
154
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 152

def on_regexp_beg(delimiter)
  @delimiter_stack.push delimiter
end

#on_regexp_end(delimiter) ⇒ 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.



156
157
158
159
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 156

def on_regexp_end(delimiter)
  @delimiter_stack.pop
  super
end

#on_regexp_newObject

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.



161
162
163
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 161

def on_regexp_new
  [:regexp]
end

#on_sclass(*args) ⇒ 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/commenting_ripper_parser.rb', line 80

def on_sclass(*args)
  commentize(:class, super)
end

#on_sp(_token) ⇒ 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.



239
240
241
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 239

def on_sp(_token)
  @seen_space = true
end

#on_stmts_add(list, elem) ⇒ 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.



173
174
175
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 173

def on_stmts_add(list, elem)
  list << elem
end

#on_stmts_newObject

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.



169
170
171
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 169

def on_stmts_new
  [:stmts]
end

#on_string_add(list, elem) ⇒ 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.



177
178
179
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 177

def on_string_add(list, elem)
  list << elem
end

#on_symbeg(delimiter) ⇒ 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.



268
269
270
271
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 268

def on_symbeg(delimiter)
  @delimiter_stack.push delimiter
  @in_symbol = true
end

#on_symbol(*args) ⇒ 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.



273
274
275
276
277
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 273

def on_symbol(*args)
  @delimiter_stack.pop
  @in_symbol = false
  super
end

#on_symbols_add(list, elem) ⇒ 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.



189
190
191
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 189

def on_symbols_add(list, elem)
  list << elem
end

#on_symbols_beg(delimiter) ⇒ 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.



181
182
183
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 181

def on_symbols_beg(delimiter)
  @delimiter_stack.push delimiter
end

#on_symbols_newObject

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.



185
186
187
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 185

def on_symbols_new
  [:symbols]
end

#on_tstring_beg(delimiter) ⇒ 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.



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

def on_tstring_beg(delimiter)
  @delimiter_stack.push delimiter
end

#on_tstring_content(content) ⇒ 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.



197
198
199
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 197

def on_tstring_content(content)
  super(content) << @delimiter_stack.last
end

#on_tstring_end(delimiter) ⇒ 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.



201
202
203
204
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 201

def on_tstring_end(delimiter)
  @delimiter_stack.pop
  super
end

#on_unary(operator, value) ⇒ 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.



255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 255

def on_unary(operator, value)
  if !@space_before && operator == :-@ && NUMBER_LITERAL_TYPES.include?(value.first)
    type, literal, lines = value
    if literal[0] == "-"
      super
    else
      [type, "-#{literal}", lines]
    end
  else
    super
  end
end

#on_void_stmtObject

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.



39
40
41
42
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 39

def on_void_stmt
  result = super
  result << [lineno, column]
end

#on_word_add(list, elem) ⇒ 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.



210
211
212
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 210

def on_word_add(list, elem)
  list << elem
end

#on_word_newObject

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.



206
207
208
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 206

def on_word_new
  [:word]
end

#on_words_add(list, elem) ⇒ 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.



222
223
224
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 222

def on_words_add(list, elem)
  list << elem
end

#on_words_beg(delimiter) ⇒ 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.



214
215
216
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 214

def on_words_beg(delimiter)
  @delimiter_stack.push delimiter
end

#on_words_newObject

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.



218
219
220
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 218

def on_words_new
  [:words]
end

#on_xstring_add(list, elem) ⇒ 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.



230
231
232
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 230

def on_xstring_add(list, elem)
  list << elem
end

#on_xstring_newObject

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.



226
227
228
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 226

def on_xstring_new
  [:xstring]
end

#parseObject

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.



23
24
25
26
27
28
# File 'lib/ripper_ruby_parser/commenting_ripper_parser.rb', line 23

def parse
  result = super
  raise "Ripper parse failed." unless result

  Sexp.from_array(result)
end