module Byebug::Helpers::ParseHelper

Utilities to assist command parsing

Public Instance Methods

get_int(str, cmd, min = nil, max = nil) click to toggle source

Parses str of command cmd as an integer between min and max.

If either min or max is nil, that value has no bound.

@todo Remove the `cmd` parameter. It has nothing to do with the method's purpose.

# File lib/byebug/helpers/parse.rb, line 15
def get_int(str, cmd, min = nil, max = nil)
  if str !~ /\A-?[0-9]+\z/
    return nil, pr('parse.errors.int.not_number', cmd: cmd, str: str)
  end

  int = str.to_i
  if min && int < min
    err = pr('parse.errors.int.too_low', cmd: cmd, str: str, min: min)
    return nil, err
  elsif max && int > max
    err = pr('parse.errors.int.too_high', cmd: cmd, str: str, max: max)
    return nil, err
  end

  int
end
parse_steps(str, cmd) click to toggle source

@return str as an integer or 1 if str is empty.

# File lib/byebug/helpers/parse.rb, line 51
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) click to toggle source

@return true if code is syntactically correct for Ruby, false otherwise

# File lib/byebug/helpers/parse.rb, line 35
def syntax_valid?(code)
  return true unless code

  without_stderr do
    begin
      RubyVM::InstructionSequence.compile(code)
      true
    rescue SyntaxError
      false
    end
  end
end

Private Instance Methods

without_stderr() { || ... } click to toggle source

Temporarily disable output to $stderr

# File lib/byebug/helpers/parse.rb, line 65
def without_stderr
  old_stderr = $stderr
  $stderr = StringIO.new

  yield
ensure
  $stderr = old_stderr
end