Class: HamlLint::RubyParser

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

Overview

Parser for the Ruby language.

This provides a convenient wrapper around the `parser` gem and the `astrolabe` integration to go with it. It is intended to be used for linter checks that require deep inspection of Ruby code.

Instance Method Summary collapse

Constructor Details

#initializeRubyParser

Creates a reusable parser.


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

def initialize
  @builder = ::Astrolabe::Builder.new
  @parser = ::Parser::CurrentRuby.new(@builder)
end

Instance Method Details

#parse(source) ⇒ Array

Parse the given Ruby source into an abstract syntax tree.


21
22
23
24
25
26
27
# File 'lib/haml_lint/ruby_parser.rb', line 21

def parse(source)
  buffer = ::Parser::Source::Buffer.new('(string)')
  buffer.source = source

  @parser.reset
  @parser.parse(buffer)
end