Class: RBS::AncestorGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/ancestor_graph.rb

Constant Summary collapse

InstanceNode =
_ = Struct.new(:type_name, keyword_init: true)
SingletonNode =
_ = Struct.new(:type_name, keyword_init: true)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:, ancestor_builder: DefinitionBuilder::AncestorBuilder.new(env: env)) ⇒ AncestorGraph

Returns a new instance of AncestorGraph.



11
12
13
14
15
# File 'lib/rbs/ancestor_graph.rb', line 11

def initialize(env:, ancestor_builder: DefinitionBuilder::AncestorBuilder.new(env: env))
  @env = env
  @ancestor_builder = ancestor_builder
  build()
end

Instance Attribute Details

#ancestor_builderObject (readonly)

Returns the value of attribute ancestor_builder.



7
8
9
# File 'lib/rbs/ancestor_graph.rb', line 7

def ancestor_builder
  @ancestor_builder
end

#childrenObject (readonly)

Returns the value of attribute children.



9
10
11
# File 'lib/rbs/ancestor_graph.rb', line 9

def children
  @children
end

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/rbs/ancestor_graph.rb', line 6

def env
  @env
end

#parentsObject (readonly)

Returns the value of attribute parents.



8
9
10
# File 'lib/rbs/ancestor_graph.rb', line 8

def parents
  @parents
end

Instance Method Details

#buildObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rbs/ancestor_graph.rb', line 17

def build()
  @parents = {}
  @children = {}

  env.class_decls.each_key do |type_name|
    build_ancestors(InstanceNode.new(type_name: type_name), ancestor_builder.one_instance_ancestors(type_name))
    build_ancestors(SingletonNode.new(type_name: type_name), ancestor_builder.one_singleton_ancestors(type_name))
  end
  env.interface_decls.each_key do |type_name|
    build_ancestors(InstanceNode.new(type_name: type_name), ancestor_builder.one_interface_ancestors(type_name))
  end
end

#build_ancestors(node, ancestors) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/rbs/ancestor_graph.rb', line 30

def build_ancestors(node, ancestors)
  ancestors.each_ancestor do |ancestor|
    case ancestor
    when Definition::Ancestor::Instance
      register(child: node, parent: InstanceNode.new(type_name: ancestor.name))
    when Definition::Ancestor::Singleton
      register(child: node, parent: SingletonNode.new(type_name: ancestor.name))
    end
  end
end

#each_ancestor(node, yielded: , &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rbs/ancestor_graph.rb', line 62

def each_ancestor(node, yielded: Set[], &block)
  if block
    each_parent(node) do |parent|
      unless yielded.member?(parent)
        yielded << parent
        yield parent
        each_ancestor(parent, yielded: yielded, &block)
      end
    end
  else
    enum_for :each_ancestor, node
  end
end

#each_child(node, &block) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/rbs/ancestor_graph.rb', line 54

def each_child(node, &block)
  if block
    children[node]&.each(&block)
  else
    enum_for :each_child, node
  end
end

#each_descendant(node, yielded: , &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rbs/ancestor_graph.rb', line 76

def each_descendant(node, yielded: Set[], &block)
  if block
    each_child(node) do |child|
      unless yielded.member?(child)
        yielded << child
        yield child
        each_descendant(child, yielded: yielded, &block)
      end
    end
  else
    enum_for :each_descendant, node
  end
end

#each_parent(node, &block) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/rbs/ancestor_graph.rb', line 46

def each_parent(node, &block)
  if block
    parents[node]&.each(&block)
  else
    enum_for :each_parent, node
  end
end

#register(parent:, child:) ⇒ Object



41
42
43
44
# File 'lib/rbs/ancestor_graph.rb', line 41

def register(parent:, child:)
  (parents[child] ||= Set[]) << parent
  (children[parent] ||= Set[]) << child
end