Class: RubyLexUtils

Inherits:
Object show all
Defined in:
lib/openc3/utilities/ruby_lex_utils.rb

Constant Summary collapse

BLANK_LINE_REGEX =

Regular expression to detect blank lines

/^\s*$/
LONELY_ELSE_REGEX =

Regular expression to detect lines containing only ‘else’

/^\s*else\s*$/
KEY_KEYWORDS =
[
  'class'.freeze,
  'module'.freeze,
  'def'.freeze,
  'undef'.freeze,
  'begin'.freeze,
  'rescue'.freeze,
  'ensure'.freeze,
  'end'.freeze,
  'if'.freeze,
  'unless'.freeze,
  'then'.freeze,
  'elsif'.freeze,
  'else'.freeze,
  'case'.freeze,
  'when'.freeze,
  'while'.freeze,
  'until'.freeze,
  'for'.freeze,
  'break'.freeze,
  'next'.freeze,
  'redo'.freeze,
  'retry'.freeze,
  'in'.freeze,
  'do'.freeze,
  'return'.freeze,
  'alias'.freeze
]

Instance Method Summary collapse

Constructor Details

#initializeRubyLexUtils

Create a new RubyLex and StringIO to hold the text to operate on



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 83

def initialize
  # Taken from https://github.com/ruby/ruby/blob/master/test/irb/test_ruby_lex.rb#L827
  IRB.init_config(nil)
  IRB.conf[:VERBOSE] = false
  # IRB.setup doesn't work because the command line options are passed
  # and it doesn't recognize --warnings when we run rspec (see spec.rake)
  # IRB.setup(__FILE__)
  workspace = IRB::WorkSpace.new(binding)
  @context = IRB::Context.new(nil, workspace)

  @lex    = RubyLex.new
  @lex_io = StringIO.new('')
end

Instance Method Details

#contains_begin?(text) ⇒ Boolean

Returns Whether the text contains the ‘begin’ keyword.

Parameters:

Returns:

  • (Boolean)

    Whether the text contains the 'begin' keyword



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 103

def contains_begin?(text)
  @lex.reinitialize
  @lex_io.string = text
  @lex.set_input(@lex_io, context: @context)
  tokens = ripper_lex_without_warning(text)
  tokens.each do |token|
    if token[1] == :on_kw and token[2] == 'begin'
      return true
    end
  end
  return false
end

#contains_block_beginning?(text) ⇒ Boolean

Returns Whether the text contains a keyword which starts a block. i.e. ‘do’, ‘{’, or ‘begin’.

Parameters:

Returns:

  • (Boolean)

    Whether the text contains a keyword which starts a block. i.e. 'do', '{', or 'begin'



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 153

def contains_block_beginning?(text)
  @lex.reinitialize
  @lex_io.string = text
  @lex.set_input(@lex_io, context: @context)
  tokens = ripper_lex_without_warning(text)
  tokens.each do |token|
    if token[1] == :on_kw
      if token[2] == 'begin' || token[2] == 'do'
        return true
      end
    elsif token[1] == :on_lbrace
      return true
    end
  end
  return false
end

#contains_end?(text) ⇒ Boolean

Returns Whether the text contains the ‘end’ keyword.

Parameters:

Returns:

  • (Boolean)

    Whether the text contains the 'end' keyword



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 118

def contains_end?(text)
  @lex.reinitialize
  @lex_io.string = text
  @lex.set_input(@lex_io, context: @context)
  tokens = ripper_lex_without_warning(text)
  tokens.each do |token|
    if token[1] == :on_kw and token[2] == 'end'
      return true
    end
  end
  return false
end

#contains_keyword?(text) ⇒ Boolean

Returns Whether the text contains a Ruby keyword.

Parameters:

Returns:

  • (Boolean)

    Whether the text contains a Ruby keyword



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 133

def contains_keyword?(text)
  @lex.reinitialize
  @lex_io.string = text
  @lex.set_input(@lex_io, context: @context)
  tokens = ripper_lex_without_warning(text)
  tokens.each do |token|
    if token[1] == :on_kw
      if KEY_KEYWORDS.include?(token[2])
        return true
      end
    elsif token[1] == :on_lbrace and !token[3].allbits?(Ripper::EXPR_BEG | Ripper::EXPR_LABEL)
      return true
    end
  end
  return false
end

#continue_block?(text) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 170

def continue_block?(text)
  @lex.reinitialize
  @lex_io.string = text
  @lex.set_input(@lex_io, context: @context)
  tokens = RubyLex.ripper_lex_without_warning(text)
  index = tokens.length - 1
  while index > 0
    token = tokens[index]
    return true if token[1] == :on_kw and token[2] == "do"
    index -= 1
  end
  return false
end

#each_lexed_segment(text) {|line, instrumentable, inside_begin, line_no| ... } ⇒ Object

Yields each lexed segment and if the segment is instrumentable

Parameters:

Yield Parameters:

  • line (String)

    The entire line

  • instrumentable (Boolean)

    Whether the line is instrumentable

  • inside_begin (Integer)

    The level of indentation

  • line_no (Integer)

    The current line number



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 221

def each_lexed_segment(text)
  inside_begin = false
  lex = RubyLex.new
  lex_io = StringIO.new(text)
  lex.set_input(lex_io, context: @context)
  lex.line = ''
  line = ''
  indent = 0
  continue_indent = nil
  begin_indent = nil
  previous_line_indent = 0

  while lexed = lex.lex(@context)
    lex.line_no += lexed.count("\n")
    lex.line.concat lexed
    line.concat lexed

    if continue_indent
      indent = previous_line_indent + lex.indent
    else
      indent += lex.indent
      lex.indent = 0
    end

    if inside_begin and indent < begin_indent
      begin_indent = nil
      inside_begin = false
    end

    # Uncomment the following to help with debugging
    #puts
    #puts '*' * 80
    #puts lex.line
    #puts "lexed = #{lexed.chomp}, indent (of next line) = #{indent}, actual lex.indent = #{lex.indent}, continue = #{lex.continue}, ltype = #{lex.ltype.inspect}, code_block_open = #{lex.code_block_open}, continue_indent = #{continue_indent.inspect}, begin_indent = #{begin_indent.inspect}"

    # These lines put multiple lines together that are really one line
    if lex.continue or lex.ltype
      if not continue_block?(lexed)
        # Set the indent we should stop at
        unless continue_indent
          if (indent - previous_line_indent) > 1
            continue_indent = indent - 1
          else
            continue_indent = previous_line_indent
          end
        end
        next
      end
    elsif continue_indent
      if indent > continue_indent
        # Still have more content
        next
      else
        # Ready to yield this combined line
        yield line, !contains_keyword?(line), inside_begin, lex.exp_line_no
        line = ''
        lex.exp_line_no = lex.line_no
        # puts "clear line 1"
        lex.line = ''
        previous_line_indent = indent
        continue_indent = nil
        next
      end
    end
    previous_line_indent = indent
    continue_indent = nil

    # Detect the beginning and end of begin blocks so we can not catch exceptions there
    if contains_begin?(line)
      if contains_end?(line)
        # Assume the user is being fancy with a single line begin; end;
        # Ignore
      else
        inside_begin = true
        begin_indent = indent unless begin_indent # Don't restart for nested begins
      end
    end

    # The following code does not care about indent

    loop do # loop to allow restarting for nested conditions
      # Yield blank lines and lonely else lines before the actual line
      while (index = line.index("\n"))
        one_line = line[0..index]
        if BLANK_LINE_REGEX.match?(one_line)
          yield one_line, true, inside_begin, lex.exp_line_no
          lex.exp_line_no += 1
          line = line[(index + 1)..-1]
        elsif LONELY_ELSE_REGEX.match?(one_line)
          yield one_line, false, inside_begin, lex.exp_line_no
          lex.exp_line_no += 1
          line = line[(index + 1)..-1]
        else
          break
        end
      end

      if contains_keyword?(line)
        yield line, false, inside_begin, lex.exp_line_no
      elsif !line.empty?
        yield line, true, inside_begin, lex.exp_line_no
      end
      line = ''
      lex.exp_line_no = lex.line_no
      # puts "clear line 2"
      lex.line = ''
      break
    end # loop do
  end # while lexed
end

#remove_comments(text, progress_dialog = nil) ⇒ String

Returns The text with all comments removed.

Parameters:

  • text (String)
  • progress_dialog (OpenC3::ProgressDialog) (defaults to: nil)

    If this is set, the overall progress will be set as the processing progresses

Returns:

  • (String)

    The text with all comments removed



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 188

def remove_comments(text, progress_dialog = nil)
  @lex.reinitialize
  @lex_io.string = text
  @lex.set_input(@lex_io, context: @context)
  comments_removed = ""
  token_count = 0
  progress = 0.0
  tokens = ripper_lex_without_warning(text)
  tokens.each do |token|
    token_count += 1
    if token[1] != :on_comment
      comments_removed << token[2]
    else
      newline_count = token[2].count("\n")
      comments_removed << ("\n" * newline_count)
    end
    if progress_dialog and token_count % 10000 == 0
      progress += 0.01
      progress = 0.0 if progress >= 0.99
      progress_dialog.set_overall_progress(progress)
    end
  end

  return comments_removed
end

#ripper_lex_without_warning(code) ⇒ Object



97
98
99
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 97

def ripper_lex_without_warning(code)
  RubyLex.ripper_lex_without_warning(code)
end