Class: Sourcify::Method::Parser::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/sourcify/lib/sourcify/method/parser/scanner.rb

Class Method Summary collapse

Class Method Details

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sourcify/lib/sourcify/method/parser/scanner.rb', line 9

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,
  }).select{|(raw, normalized)| matcher.call(raw) }

  case results.size
  when 0 then raise NoMatchingMethodError
  when 1 then results[0]
  else raise MultipleMatchingMethodsPerLineError
  end
end

.rscan(str, opts) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sourcify/lib/sourcify/method/parser/scanner.rb', line 33

def rscan(str, opts)
  results = RawScanner.process(str, opts) || []
  inner_opts = opts.merge(:stop_on_newline => true)
  return results if opts[:ignore_nested]
  results.map do |outer|
    [
      outer,
      *rscan(
        outer[0].sub(/^def(.*)end$/,'\1').sub(/^(?:.*?)(def.*)$/,'\1'),
        inner_opts
      )
    ]
  end.flatten(1)
end

.scan_pattern_hint(val) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/sourcify/lib/sourcify/method/parser/scanner.rb', line 24

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