Class: LiveAST::RubyParser

Inherits:
Object
  • Object
show all
Defined in:
lib/live_ast/ruby_parser.rb

Defined Under Namespace

Modules: Test, Unparser

Instance Method Summary collapse

Instance Method Details

#parse(source) ⇒ Object

Returns a line-to-sexp hash where sexp corresponds to the method or block defined at the given line.

This method is the only requirement of a LiveAST parser plugin.



11
12
13
14
15
# File 'lib/live_ast/ruby_parser.rb', line 11

def parse(source)
  @defs = {}
  process RubyParser.new.parse(source)
  @defs
end

#process(sexp) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/live_ast/ruby_parser.rb', line 17

def process(sexp)
  case sexp.first
  when :defn, :defs, :iter
    store_sexp(sexp, sexp.line)
  end

  sexp.each do |elem|
    process(elem) if elem.is_a? Sexp
  end
end

#store_sexp(sexp, line) ⇒ Object



28
29
30
# File 'lib/live_ast/ruby_parser.rb', line 28

def store_sexp(sexp, line)
  @defs[line] = @defs.has_key?(line) ? :multiple : sexp
end