Module: Solargraph::Parser::Legacy::ClassMethods

Defined in:
lib/solargraph/parser/legacy/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#chain(*args) ⇒ Object



77
78
79
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 77

def chain *args
  NodeChainer.chain *args
end

#chain_string(*args) ⇒ Object



81
82
83
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 81

def chain_string *args
  NodeChainer.load_string *args
end

#infer_literal_node_type(node) ⇒ Object



89
90
91
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 89

def infer_literal_node_type node
  NodeMethods.infer_literal_node_type node
end

#inner_node_references(name, top) ⇒ Array<AST::Node>

Parameters:

  • name (String)
  • top (AST::Node)

Returns:



68
69
70
71
72
73
74
75
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 68

def inner_node_references name, top
  result = []
  if top.is_a?(AST::Node) && top.to_s.include?(":#{name}")
    result.push top if top.children.any? { |c| c.to_s == name }
    top.children.each { |c| result.concat inner_node_references(name, c) }
  end
  result
end

#is_ast_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 97

def is_ast_node? node
  node.is_a?(::Parser::AST::Node)
end

#map(source) ⇒ Object



42
43
44
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 42

def map source
  NodeProcessor.process(source.node, Region.new(source: source))
end

#node_range(node) ⇒ Object



101
102
103
104
105
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 101

def node_range node
  st = Position.new(node.loc.line, node.loc.column)
  en = Position.new(node.loc.last_line, node.loc.last_column)
  Range.new(st, en)
end

#parse(code, filename = nil, line = 0) ⇒ Parser::AST::Node

Parameters:

  • code (String)
  • filename (String, nil) (defaults to: nil)
  • line (Integer) (defaults to: 0)

Returns:

  • (Parser::AST::Node)


24
25
26
27
28
29
30
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 24

def parse code, filename = nil, line = 0
  buffer = ::Parser::Source::Buffer.new(filename, line)
  buffer.source = code
  parser.parse(buffer)
rescue ::Parser::SyntaxError => e
  raise Parser::SyntaxError, e.message
end

#parse_with_comments(code, filename = nil) ⇒ Array(Parser::AST::Node, Array<Parser::Source::Comment>)

Parameters:

  • code (String)
  • filename (String) (defaults to: nil)

Returns:

  • (Array(Parser::AST::Node, Array<Parser::Source::Comment>))


10
11
12
13
14
15
16
17
18
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 10

def parse_with_comments code, filename = nil
  buffer = ::Parser::Source::Buffer.new(filename, 0)
  buffer.source = code
  node = parser.parse(buffer)
  comments = CommentRipper.new(code, filename, 0).parse
  [node, comments]
rescue ::Parser::SyntaxError => e
  raise Parser::SyntaxError, e.message
end

#parserParser::Base

Returns:

  • (Parser::Base)


33
34
35
36
37
38
39
40
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 33

def parser
  # @todo Consider setting an instance variable. We might not need to
  #   recreate the parser every time we use it.
  parser = ::Parser::CurrentRuby.new(FlawedBuilder.new)
  parser.diagnostics.all_errors_are_fatal = true
  parser.diagnostics.ignore_warnings      = true
  parser
end

#process_node(*args) ⇒ Object



85
86
87
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 85

def process_node *args
  Solargraph::Parser::NodeProcessor.process *args
end

#references(source, name) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 50

def references source, name
  inner_node_references(name, source.node).map do |n|
    offset = Position.to_offset(source.code, NodeMethods.get_node_start_position(n))
    soff = source.code.index(name, offset)
    eoff = soff + name.length
    Location.new(
      source.filename,
      Range.new(
        Position.from_offset(source.code, soff),
        Position.from_offset(source.code, eoff)
      )
    )
  end
end

#returns_from(node) ⇒ Object



46
47
48
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 46

def returns_from node
  NodeMethods.returns_from(node)
end

#versionObject



93
94
95
# File 'lib/solargraph/parser/legacy/class_methods.rb', line 93

def version
  parser.version
end