Class: Sourcify::Proc::Parser

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

Overview

:nodoc:all

Defined Under Namespace

Classes: CodeScanner, Converter, Normalizer, SourceCode

Constant Summary collapse

IS_19x =
RUBY_VERSION.include?('1.9.')

Instance Method Summary collapse

Constructor Details

#initialize(_proc) ⇒ Parser

Returns a new instance of Parser.



12
13
14
15
16
17
# File 'lib/sourcify/proc/parser.rb', line 12

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



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

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

#sexp(opts) ⇒ Object



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

def sexp(opts)
  (@sexps ||= {})[opts.hash] ||= (
    raw_code = ("\n" * @source_code.line) + extracted_source(opts)
    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



19
20
21
# File 'lib/sourcify/proc/parser.rb', line 19

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