Class: RubyScope::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_scope/scanner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScanner

Returns a new instance of Scanner.



5
6
7
8
# File 'lib/ruby_scope/scanner.rb', line 5

def initialize
  @query = nil
  @verbose = false
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



3
4
5
# File 'lib/ruby_scope/scanner.rb', line 3

def cache
  @cache
end

#verboseObject

Returns the value of attribute verbose.



2
3
4
# File 'lib/ruby_scope/scanner.rb', line 2

def verbose
  @verbose
end

Instance Method Details

#add_query(pattern) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby_scope/scanner.rb', line 10

def add_query(pattern)
  # Generate the pattern, we use a little instance_eval trickery here. 
  sexp = case pattern
    when String then SexpPath::SexpQueryBuilder.instance_eval(pattern)
    when Sexp   then pattern
    else raise ArgumentError, "Expected a String or Sexp"
  end
  
  if @query 
    @query = @query | sexp
  else
    @query = sexp
  end
  @query
  
rescue Exception=>ex
  puts "Invalid Pattern: '#{pattern}'"
  puts "Trace:"
  puts ex
  puts ex.backtrace
  exit 1    
end

#queryObject



33
34
35
# File 'lib/ruby_scope/scanner.rb', line 33

def query
  @query.clone if @query
end

#scan(path) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ruby_scope/scanner.rb', line 37

def scan(path)
  @path = path
  begin
    report_file path
    
    # Reset our cached code and split lines
    @code,@lines = nil,nil
          
    # Load the code and parse it with RubyParser
    # If we're caching pull from the cache, otherwise parse the code
    sexp = @cache[path] if @cache
    if !sexp
      sexp = RubyParser.new.parse(code, @path)
      @cache[path] = sexp if @cache
    end
 
    if sexp
      # Search it with the given pattern, printing any results
      sexp.search_each(@query) do |matching_sexp|
        report_match matching_sexp
      end 
    end
  rescue StandardError => ex
    report_exception ex
  end
end