Class: RubyLint::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-lint/parser.rb

Overview

Parser provides a small wrapper around the Parser Gem and allows for the use of a custom AST builder.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser



12
13
14
15
16
17
# File 'lib/ruby-lint/parser.rb', line 12

def initialize
  builder          = AST::Builder.new
  @internal_parser = ::Parser::CurrentRuby.new(builder)

  internal_parser.diagnostics.all_errors_are_fatal = false
end

Instance Attribute Details

#internal_parserParser::Parser (readonly)

Returns:

  • (Parser::Parser)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
57
58
59
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/ruby-lint/parser.rb', line 9

class Parser
  attr_reader :internal_parser

  def initialize
    builder          = AST::Builder.new
    @internal_parser = ::Parser::CurrentRuby.new(builder)

    internal_parser.diagnostics.all_errors_are_fatal = false
  end

  ##
  # Registers the consumer with the internal diagnostics handler.
  #
  # @param [#call] consumer
  #
  def consumer=(consumer)
    internal_parser.diagnostics.consumer = consumer
  end

  ##
  # Parses a block of Ruby code and returns the AST and a mapping of each AST
  # node and their comments (if there are any). This mapping is returned as a
  # Hash.
  #
  # @param [String] code
  # @param [String] file
  # @param [Numeric] line
  # @return [Array]
  #
  def parse(code, file = '(ruby-lint)', line = 1)
    buffer        = ::Parser::Source::Buffer.new(file, line)
    buffer.source = code
    ast, comments = internal_parser.parse_with_comments(buffer)

    internal_parser.reset

    associated = associate_comments(ast, comments)

    return create_root_node(ast), associated
  end

  private

  ##
  # @param [RubyLint::AST::Node|NilClass] ast
  # @return [RubyLint::AST::Node]
  #
  def create_root_node(ast)
    if ast
      children = [ast]
      location = ast.location
    # empty input.
    else
      children = []
      location = nil
    end

    return AST::Node.new(:root, children, :location => location)
  end

  ##
  # @param [RubyLint::AST::Node|NilClass] ast
  # @param [Mixed] comments
  # @return [Hash]
  #
  def associate_comments(ast, comments)
    if ast
      associator = ::Parser::Source::Comment::Associator.new(ast, comments)
      associated = associator.associate
    else
      associated = {}
    end

    return associated
  end
end

Instance Method Details

#associate_comments(ast, comments) ⇒ Hash (private)

Parameters:

Returns:

  • (Hash)


74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby-lint/parser.rb', line 74

def associate_comments(ast, comments)
  if ast
    associator = ::Parser::Source::Comment::Associator.new(ast, comments)
    associated = associator.associate
  else
    associated = {}
  end

  return associated
end

#consumer=(consumer) ⇒ Object

Registers the consumer with the internal diagnostics handler.

Parameters:

  • consumer (#call)


24
25
26
# File 'lib/ruby-lint/parser.rb', line 24

def consumer=(consumer)
  internal_parser.diagnostics.consumer = consumer
end

#create_root_node(ast) ⇒ RubyLint::AST::Node (private)

Parameters:

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby-lint/parser.rb', line 56

def create_root_node(ast)
  if ast
    children = [ast]
    location = ast.location
  # empty input.
  else
    children = []
    location = nil
  end

  return AST::Node.new(:root, children, :location => location)
end

#parse(code, file = '(ruby-lint)', line = 1) ⇒ Array

Parses a block of Ruby code and returns the AST and a mapping of each AST node and their comments (if there are any). This mapping is returned as a Hash.

Parameters:

  • code (String)
  • file (String) (defaults to: '(ruby-lint)')
  • line (Numeric) (defaults to: 1)

Returns:

  • (Array)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby-lint/parser.rb', line 38

def parse(code, file = '(ruby-lint)', line = 1)
  buffer        = ::Parser::Source::Buffer.new(file, line)
  buffer.source = code
  ast, comments = internal_parser.parse_with_comments(buffer)

  internal_parser.reset

  associated = associate_comments(ast, comments)

  return create_root_node(ast), associated
end