Class: RubyLexUtils

Inherits:
Object show all
Defined in:
lib/cosmos/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*$/
KEYWORD_TOKENS =

Ruby keywords

[RubyToken::TkCLASS,
RubyToken::TkMODULE,
RubyToken::TkDEF,
RubyToken::TkUNDEF,
RubyToken::TkBEGIN,
RubyToken::TkRESCUE,
RubyToken::TkENSURE,
RubyToken::TkEND,
RubyToken::TkIF,
RubyToken::TkUNLESS,
RubyToken::TkTHEN,
RubyToken::TkELSIF,
RubyToken::TkELSE,
RubyToken::TkCASE,
RubyToken::TkWHEN,
RubyToken::TkWHILE,
RubyToken::TkUNTIL,
RubyToken::TkFOR,
RubyToken::TkBREAK,
RubyToken::TkNEXT,
RubyToken::TkREDO,
RubyToken::TkRETRY,
RubyToken::TkIN,
RubyToken::TkDO,
RubyToken::TkRETURN,
RubyToken::TkIF_MOD,
RubyToken::TkUNLESS_MOD,
RubyToken::TkWHILE_MOD,
RubyToken::TkUNTIL_MOD,
RubyToken::TkALIAS,
RubyToken::TklBEGIN,
RubyToken::TklEND,
RubyToken::TkfLBRACE]
BLOCK_BEGINNING_TOKENS =

Ruby keywords which define the beginning of a block: do, {, begin

[RubyToken::TkDO,
RubyToken::TkfLBRACE,
RubyToken::TkBEGIN]

Instance Method Summary collapse

Constructor Details

#initializeRubyLexUtils

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



137
138
139
140
# File 'lib/cosmos/utilities/ruby_lex_utils.rb', line 137

def initialize
  @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:

  • Whether the text contains the 'begin' keyword



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cosmos/utilities/ruby_lex_utils.rb', line 144

def contains_begin?(text)
  @lex.reinitialize
  @lex.exception_on_syntax_error = false
  @lex_io.string = text
  @lex.set_input(@lex_io)
  while token = @lex.token
    if token.class == RubyToken::TkBEGIN
      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:

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



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/cosmos/utilities/ruby_lex_utils.rb', line 175

def contains_block_beginning?(text)
  @lex.reinitialize
  @lex.exception_on_syntax_error = false
  @lex_io.string = text
  @lex.set_input(@lex_io)
  while token = @lex.token
    if BLOCK_BEGINNING_TOKENS.include?(token.class)
      return true
    end
  end
  return false
end

#contains_keyword?(text) ⇒ Boolean

Returns Whether the text contains a Ruby keyword.

Parameters:

Returns:

  • Whether the text contains a Ruby keyword



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/cosmos/utilities/ruby_lex_utils.rb', line 159

def contains_keyword?(text)
  @lex.reinitialize
  @lex.exception_on_syntax_error = false
  @lex_io.string = text
  @lex.set_input(@lex_io)
  while token = @lex.token
    if KEYWORD_TOKENS.include?(token.class)
      return true
    end
  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

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

Parameters:



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
# File 'lib/cosmos/utilities/ruby_lex_utils.rb', line 244

def each_lexed_segment(text)
  lex = RubyLex.new
  lex.exception_on_syntax_error = false
  lex_io = StringIO.new(text)
  lex.set_input(lex_io)

  while lexed = lex.lex
    line_no = lex.exp_line_no

    if contains_begin?(lexed)
      inside_begin = lex.indent - 1
    end

    if lex.indent == inside_begin
      inside_begin = nil
    end

    loop do # loop to allow restarting for nested conditions

      # Yield blank lines and lonely else lines before the actual line
      while (index = lexed.index("\n"))
        line = lexed[0..index]
        if line =~ BLANK_LINE_REGEX
          yield line, true, inside_begin, line_no
          line_no += 1
          lexed = lexed[(index + 1)..-1]
        elsif line =~ LONELY_ELSE_REGEX
          yield line, false, inside_begin, line_no
          line_no += 1
          lexed = lexed[(index + 1)..-1]
        else
          break
        end
      end

      if contains_keyword?(lexed)
        if contains_block_beginning?(lexed)
          section = ''
          lexed.each_line do |lexed_part|
            section << lexed_part
            if contains_block_beginning?(section)
              yield section, false, inside_begin, line_no
              break
            end
            line_no += 1
          end
          line_no += 1
          remainder = lexed[(section.length)..-1]
          lexed = remainder
          next unless remainder.empty?
        else
          yield lexed, false, inside_begin, line_no
        end
      elsif !lexed.empty?
        num_left_brackets  = lexed.count('{')
        num_right_brackets = lexed.count('}')
        if num_left_brackets != num_right_brackets
          # Don't instrument lines with unequal numbers of { and } brackets
          yield lexed, false, inside_begin, line_no
        else
          yield lexed, true, inside_begin, line_no
        end
      end
      lex.exp_line_no = lex.line_no
      break
    end # loop do
  end # while lexed
end

#remove_comments(text, progress_dialog = nil) ⇒ String

Returns The text with all comments removed.

Parameters:

  • (defaults to: nil)

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

Returns:

  • The text with all comments removed



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/cosmos/utilities/ruby_lex_utils.rb', line 192

def remove_comments(text, progress_dialog = nil)
  comments_removed = text.clone
  @lex.reinitialize
  @lex.exception_on_syntax_error = false
  @lex_io.string = text
  @lex.set_input(@lex_io)
  need_remove = nil
  delete_ranges = []
  token_count = 0
  progress = 0.0
  while token = @lex.token
    token_count += 1
    if need_remove
      delete_ranges << (need_remove..(token.seek - 1))
      need_remove = nil
    end
    if token.class == RubyToken::TkCOMMENT
      need_remove = token.seek
    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

  if need_remove
    delete_ranges << (need_remove..(text.length - 1))
    need_remove = nil
  end

  delete_count = 0
  delete_ranges.reverse_each do |range|
    delete_count += 1
    comments_removed[range] = ''
    if progress_dialog and delete_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