Class: Pure::Parser::Ripper::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/pure/parser/impl/ripper.rb

Constant Summary collapse

FUN_RE =
%r!\Afun(_map)?\Z!

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Processor

Returns a new instance of Processor.



15
16
17
18
# File 'lib/pure/parser/impl/ripper.rb', line 15

def initialize(file)
  @file = file
  @defs = Hash.new
end

Instance Method Details

#process(sexp) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/pure/parser/impl/ripper.rb', line 86

def process(sexp)
  if sexp.is_a? Array
    process_def(sexp)
    process_fun(sexp)
    sexp.each { |sub_sexp|
      process(sub_sexp)
    }
  end
end

#process_def(sexp) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pure/parser/impl/ripper.rb', line 25

def process_def(sexp)
  if sexp[0] == :def
    name = sexp[1][1].to_sym
    line = sexp[1][2][0]
    params = (
      case sexp[2].first
      when :params
        sexp[2]
      when :paren
        sexp[2][1]
      #else
      #  raise ParseError.new(@file, line, "unforeseen `def' syntax"
      end
    )
    args = (
      if params[1].nil?
        []
      else
        params[1].map { |t| t[1].to_sym }
      end
    )
    splat = params.any? { |t| t.is_a?(Array) and t[0] == :rest_param }
    default = !!params[2]
    @defs[line] = {
      :name => name,
      :args => args,
      :code => sexp,
      :splat => splat,
      :default => default,
    }
  end
end

#process_fun(sexp) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pure/parser/impl/ripper.rb', line 60

def process_fun(sexp)
  if sexp[0] == :method_add_block and sexp[1].is_a?(Array)
    line = (
      if sexp[1][0] == :command and
          sexp[1][1].is_a?(Array) and
          sexp[1][1][1] =~ FUN_RE
        sexp[1][1][2][0]
      elsif sexp[1][0] == :method_add_arg and
          sexp[1][1].is_a?(Array) and
          sexp[1][1][0] == :fcall and
          sexp[1][1][1].is_a?(Array) and
          sexp[1][1][1][1] =~ FUN_RE
        sexp[1][1][1][2][0]
      else
        nil
      end
    )
    if line
      @defs[line] = {
        :name => :__fun,
        :code => sexp[2],
      }
    end
  end
end

#runObject



20
21
22
23
# File 'lib/pure/parser/impl/ripper.rb', line 20

def run
  process(::Ripper.sexp(File.read(@file)))
  @defs
end