Module: Byebug::ParseFunctions

Included in:
CommandProcessor
Defined in:
lib/byebug/helper.rb

Overview

Utilities to assist command parsing

Instance Method Summary collapse

Instance Method Details

#get_int(str, cmd, min = nil, max = nil) ⇒ Object

Parse ‘str’ of command ‘cmd’ as an integer between min and max. If either min or max is nil, that value has no bound.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/byebug/helper.rb', line 33

def get_int(str, cmd, min = nil, max = nil)
  if str !~ /\A[0-9]+\z/
    return nil, "\"#{cmd}\" argument \"#{str}\" needs to be a number"
  end

  int = str.to_i
  if min && int < min
    return min, "\"#{cmd}\" argument \"#{str}\" needs to be at least #{min}"
  elsif max && int > max
    return max, "\"#{cmd}\" argument \"#{str}\" needs to be at most #{max}"
  end

  int
end

#get_line(filename, lineno) ⇒ Object

Gets a single line in a source code file



67
68
69
70
71
72
# File 'lib/byebug/helper.rb', line 67

def get_line(filename, lineno)
  lines = get_lines(filename)
  return nil unless lines

  lines[lineno - 1]
end

#get_lines(filename) ⇒ Object

Gets all lines in a source code file



58
59
60
61
62
# File 'lib/byebug/helper.rb', line 58

def get_lines(filename)
  return nil unless File.exist?(filename)

  lines(filename)
end

#lines(filename) ⇒ Object

Fills SCRIPT_LINES__ entry for <filename> if not already filled.



51
52
53
# File 'lib/byebug/helper.rb', line 51

def lines(filename)
  SCRIPT_LINES__[filename] ||= File.readlines(filename)
end

#parse_steps(str, cmd) ⇒ Object

Returns the number of steps specified in <str> as an integer or 1 if <str> is empty.



87
88
89
90
91
92
93
94
# File 'lib/byebug/helper.rb', line 87

def parse_steps(str, cmd)
  return 1 unless str

  steps, err = get_int(str, cmd, 1)
  return nil, err unless steps

  steps
end

#syntax_valid?(code) ⇒ Boolean

Returns true if code is syntactically correct for Ruby.

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/byebug/helper.rb', line 77

def syntax_valid?(code)
  eval("BEGIN {return true}\n#{code}", nil, '', 0)
rescue SyntaxError
  false
end