Class: Mago::SexpProcessor

Inherits:
SexpProcessor
  • Object
show all
Defined in:
lib/mago/sexp_processor.rb

Overview

The core of Mago. Iterates through AST returned by RubyParser and finds numeric literals.

Instance Method Summary collapse

Constructor Details

#initialize(file, ignore = []) ⇒ SexpProcessor

Returns a new instance of SexpProcessor.

Parameters:

  • file (Mago::File)

    file where found numbers will be stored

  • ignore (Array<Numeric>) (defaults to: [])

    array of numbers to ignore



7
8
9
10
11
12
13
14
# File 'lib/mago/sexp_processor.rb', line 7

def initialize(file, ignore = [])
  super()
  self.warn_on_default = false
  self.strict = false

  @file   = file
  @ignore = ignore
end

Instance Method Details

#process_cdecl(exp) ⇒ Sexp

Process constant declaration node. It’s the case where we numeric literals, because they are not magic numbers.

Parameters:

  • exp (Sexp)

Returns:

  • (Sexp)


22
23
24
# File 'lib/mago/sexp_processor.rb', line 22

def process_cdecl(exp)
  process_default(exp)
end

#process_default(exp) ⇒ Sexp

Handler for all other nodes. They doesn’t interest us, so we just skip.

Parameters:

  • exp (Sexp)

Returns:

  • (Sexp)


48
49
50
51
52
53
# File 'lib/mago/sexp_processor.rb', line 48

def process_default(exp)
  until exp.size == 0
    exp.shift
  end
  s()
end

#process_lit(exp) ⇒ Sexp

Process literal node. If a literal is a number and add it to the collection of magic numbers.

Parameters:

  • exp (Sexp)

Returns:

  • (Sexp)


32
33
34
35
36
37
38
39
40
41
# File 'lib/mago/sexp_processor.rb', line 32

def process_lit(exp)
  exp.shift
  value = exp.shift

  if value.is_a?(Numeric) && !@ignore.include?(value)
    @file.magic_numbers << MagicNumber.new(:value => value, :line => exp.line)
  end

  s()
end