Class: Masamune::AbstractSyntaxTree

Inherits:
Object
  • Object
show all
Defined in:
lib/masamune/abstract_syntax_tree.rb,
lib/masamune/abstract_syntax_tree/visitors/strings_visitor.rb,
lib/masamune/abstract_syntax_tree/visitors/symbols_visitor.rb,
lib/masamune/abstract_syntax_tree/visitors/variables_visitor.rb,
lib/masamune/abstract_syntax_tree/visitors/parameters_visitor.rb,
lib/masamune/abstract_syntax_tree/visitors/method_calls_visitor.rb,
lib/masamune/abstract_syntax_tree/visitors/block_parameters_visitor.rb,
lib/masamune/abstract_syntax_tree/visitors/method_definitions_visitor.rb

Defined Under Namespace

Classes: BlockParametersVisitors, MethodCallsVisitor, MethodDefinitionsVisitor, ParametersVisitor, StringsVisitor, SymbolsVisitor, VariablesVisitor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ AbstractSyntaxTree

Returns a new instance of AbstractSyntaxTree.



8
9
10
11
12
13
14
15
16
# File 'lib/masamune/abstract_syntax_tree.rb', line 8

def initialize(code)
  @code = code
  @prism = Prism.parse(code)
  @ripper = Ripper.sexp(code)
  raw_lex_nodes = Ripper.lex(code)
  @lex_nodes = raw_lex_nodes.map do |lex_node|
    LexNode.new(raw_lex_nodes.index(lex_node), lex_node, self.__id__)
  end
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/masamune/abstract_syntax_tree.rb', line 5

def code
  @code
end

#lex_nodesObject

Returns the value of attribute lex_nodes.



6
7
8
# File 'lib/masamune/abstract_syntax_tree.rb', line 6

def lex_nodes
  @lex_nodes
end

#prismObject (readonly)

Returns the value of attribute prism.



5
6
7
# File 'lib/masamune/abstract_syntax_tree.rb', line 5

def prism
  @prism
end

#ripperObject (readonly)

Returns the value of attribute ripper.



5
6
7
# File 'lib/masamune/abstract_syntax_tree.rb', line 5

def ripper
  @ripper
end

Instance Method Details

#all_methods(token_value: nil) ⇒ Object



30
31
32
# File 'lib/masamune/abstract_syntax_tree.rb', line 30

def all_methods(token_value: nil)
  order_nodes(method_definitions(token_value: token_value) + method_calls(token_value: token_value))
end

#block_parametersObject

Retrieves all parameters within pipes (i.e. - |x, y, z|).



67
68
69
70
71
# File 'lib/masamune/abstract_syntax_tree.rb', line 67

def block_parameters
  visitor = BlockParametersVisitor.new
  @prism.value.accept(visitor)
  visitor.results
end

#commentsObject

TODO: Search by token_value if necessary.



80
81
82
# File 'lib/masamune/abstract_syntax_tree.rb', line 80

def comments
  @prism.comments
end

#method_calls(token_value: nil) ⇒ Object



40
41
42
43
44
# File 'lib/masamune/abstract_syntax_tree.rb', line 40

def method_calls(token_value: nil)
  visitor = MethodCallsVisitor.new(token_value)
  @prism.value.accept(visitor)
  visitor.results
end

#method_definitions(token_value: nil) ⇒ Object



34
35
36
37
38
# File 'lib/masamune/abstract_syntax_tree.rb', line 34

def method_definitions(token_value: nil)
  visitor = MethodDefinitionsVisitor.new(token_value)
  @prism.value.accept(visitor)
  visitor.results
end

#parameters(token_value: nil) ⇒ Object



73
74
75
76
77
# File 'lib/masamune/abstract_syntax_tree.rb', line 73

def parameters(token_value: nil)
  visitor = ParametersVisitor.new(token_value)
  @prism.value.accept(visitor)
  visitor.results
end

#replace(type:, old_token_value:, new_token_value:) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/masamune/abstract_syntax_tree.rb', line 84

def replace(type:, old_token_value:, new_token_value:)
  Slasher.replace(
    type: type,
    old_token_value: old_token_value,
    new_token_value: new_token_value,
    code: @code,
    ast: self
  )
end

#string_symbols(token_value: nil) ⇒ Object



59
60
61
62
63
64
# File 'lib/masamune/abstract_syntax_tree.rb', line 59

def string_symbols(token_value: nil)
  result = symbols(token_value: token_value)

  # TODO: Describe why closing_loc has to happen.
  result.reject{|node| node.closing_loc.nil?}
end

#strings(token_value: nil) ⇒ Object



24
25
26
27
28
# File 'lib/masamune/abstract_syntax_tree.rb', line 24

def strings(token_value: nil)
  visitor = StringsVisitor.new(token_value)
  @prism.value.accept(visitor)
  visitor.results
end

#symbol_literals(token_value: nil) ⇒ Object



52
53
54
55
56
57
# File 'lib/masamune/abstract_syntax_tree.rb', line 52

def symbol_literals(token_value: nil)
  result = symbols(token_value: token_value)

  # TODO: Describe why closing_loc has to happen.
  result.select{|node| node.closing_loc.nil?}
end

#symbols(token_value: nil) ⇒ Object



46
47
48
49
50
# File 'lib/masamune/abstract_syntax_tree.rb', line 46

def symbols(token_value: nil)
  visitor = SymbolsVisitor.new(token_value)
  @prism.value.accept(visitor)
  visitor.results
end

#variables(token_value: nil) ⇒ Object



18
19
20
21
22
# File 'lib/masamune/abstract_syntax_tree.rb', line 18

def variables(token_value: nil)
  visitor = VariablesVisitor.new(token_value)
  @prism.value.accept(visitor)
  visitor.results
end