Class: Sourcify::Proc::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sourcify/lib/sourcify/proc/parser.rb,
lib/sourcify/lib/sourcify/proc/parser/scanner.rb,
lib/sourcify/lib/sourcify/proc/parser/converter.rb,
lib/sourcify/lib/sourcify/proc/parser/normalizer.rb,
lib/sourcify/lib/sourcify/proc/parser/raw_scanner.rb,
lib/sourcify/lib/sourcify/proc/parser/source_code.rb,
lib/sourcify/lib/sourcify/proc/parser/raw_scanner_extensions.rb

Overview

:nodoc:all

Defined Under Namespace

Modules: RawScanner Classes: Normalizer, Scanner

Constant Summary collapse

Converter =
Common::Parser::Converter
SourceCode =
Common::Parser::SourceCode

Instance Method Summary collapse

Constructor Details

#initialize(_proc) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
13
14
# File 'lib/sourcify/lib/sourcify/proc/parser.rb', line 9

def initialize(_proc)
  @arity, @source_code = _proc.arity, SourceCode.new(*_proc.source_location(false))
  raise CannotHandleCreatedOnTheFlyProcError unless @source_code.file
  raise CannotParseEvalCodeError if @source_code.file == '(eval)'
  @binding = _proc.binding # this must come after the above check
end

Instance Method Details

#raw_source(opts) ⇒ Object



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

def raw_source(opts)
  raw_code = extracted_source(opts)[0].strip
  opts[:strip_enclosure] ?
    raw_code.sub(/^proc\s*(\{|do)\s*(\|[^\|]+\|)?(.*)(\}|end)$/m, '\3').strip : raw_code
end

#sexp(opts) ⇒ Object



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

def sexp(opts)
  (@sexps ||= {})[opts.hash] ||= (
    extracted = extracted_source(opts)[1]
    raw_code = (("\n" * @source_code.line) + extracted).same_encoding_as(extracted)

    raw_sexp = Converter.to_sexp(raw_code, @source_code.file)
    sexp = Normalizer.process(raw_sexp, @binding)
    opts[:strip_enclosure] ? Sexp.from_array(sexp.to_a.last) : sexp
  )
end

#source(opts) ⇒ Object



16
17
18
# File 'lib/sourcify/lib/sourcify/proc/parser.rb', line 16

def source(opts)
  (@sources ||= {})[opts.hash] ||= Converter.to_code(sexp(opts))
end