Class: Sourcify::Proc::Parser::CodeScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/sourcify/proc/parser/code_scanner.rb

Class Method Summary collapse

Class Method Details

.process(source_code, opts, &matcher) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sourcify/proc/parser/code_scanner.rb', line 7

def process(source_code, opts, &matcher)
  results = rscan(source_code.to_s, {
    :start_pattern => scan_pattern_hint(opts[:attached_to]),
    :body_matcher => opts[:body_matcher],
    :ignore_nested => opts[:ignore_nested],
    :stop_on_newline => false,
  }).flatten.select(&matcher)
  case results.size
  when 0 then raise NoMatchingProcError
  when 1 then results[0]
  else raise MultipleMatchingProcsPerLineError
  end
end

.rscan(str, opts) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/sourcify/proc/parser/code_scanner.rb', line 30

def rscan(str, opts)
results = Scanner.process(str, opts) || []
return results if opts[:ignore_nested]
results.map do |outer|
  inner = rscan(outer.sub(/^proc\s*(do|\{)/,''), opts.merge(:stop_on_newline => true))
    [outer, inner]
  end
end

.scan_pattern_hint(val) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/sourcify/proc/parser/code_scanner.rb', line 21

def scan_pattern_hint(val)
  case val
  when Regexp then val
  when String, Symbol then /^(?:.*?\W|)#{val}(?:\W)/
  when nil then /.*/
  else raise TypeError
  end
end