This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
parser.rb
85 lines (73 loc) · 2.03 KB
/
parser.rb
1
2
3
4
5
6
7
8
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
85
module RubyLint
##
# {RubyLint::Parser} provides a small wrapper around the Parser Gem and
# allows for the use of a custom AST builder.
#
# @!attribute [r] internal_parser
# @return [Parser::Parser]
#
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 # Parser
end # RubyLint