Class: Computering::CodeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/computering/code_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ CodeParser

Returns a new instance of CodeParser.



8
9
10
11
12
# File 'lib/computering/code_parser.rb', line 8

def initialize(code)
  @sexp   = Ripper::SexpBuilder.new(code).parse
  @blocks = {}
  walk(@sexp)
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



6
7
8
# File 'lib/computering/code_parser.rb', line 6

def blocks
  @blocks
end

Instance Method Details

#walk(sexp, last = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/computering/code_parser.rb', line 14

def walk(sexp, last=nil)
  sexp.each do |item|
    if item.is_a? Array
      walk(item, sexp)
    elsif item == :command
      next_item = sexp[1]
      token, name, _ = next_item
      if token == :@ident && name == "spec"
        block_sexp = last.last.last
        code = Sorcerer.source(block_sexp, multiline: true, indent: true)
        name = sexp.last[1].last.last.last[1]
        @blocks[name] = code
      end
    end
  end
end