Class: RBS::Environment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnvironment

Returns a new instance of Environment.



12
13
14
15
16
17
18
19
20
21
# File 'lib/rbs/environment.rb', line 12

def initialize
  @buffers = []
  @declarations = []

  @name_to_decl = {}
  @name_to_extensions = {}
  @name_to_constant = {}
  @name_to_global = {}
  @name_to_alias = {}
end

Instance Attribute Details

#buffersObject (readonly)

Returns the value of attribute buffers.



3
4
5
# File 'lib/rbs/environment.rb', line 3

def buffers
  @buffers
end

#declarationsObject (readonly)

Returns the value of attribute declarations.



4
5
6
# File 'lib/rbs/environment.rb', line 4

def declarations
  @declarations
end

#name_to_aliasObject (readonly)

Returns the value of attribute name_to_alias.



10
11
12
# File 'lib/rbs/environment.rb', line 10

def name_to_alias
  @name_to_alias
end

#name_to_constantObject (readonly)

Returns the value of attribute name_to_constant.



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

def name_to_constant
  @name_to_constant
end

#name_to_declObject (readonly)

Returns the value of attribute name_to_decl.



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

def name_to_decl
  @name_to_decl
end

#name_to_extensionsObject (readonly)

Returns the value of attribute name_to_extensions.



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

def name_to_extensions
  @name_to_extensions
end

#name_to_globalObject (readonly)

Returns the value of attribute name_to_global.



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

def name_to_global
  @name_to_global
end

Instance Method Details

#<<(decl) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rbs/environment.rb', line 41

def <<(decl)
  declarations << decl
  case decl
  when AST::Declarations::Class, AST::Declarations::Module, AST::Declarations::Interface
    cache_name name_to_decl, name: decl.name.absolute!, decl: decl
  when AST::Declarations::Extension
    yield_self do
      name = decl.name.absolute!
      exts = name_to_extensions.fetch(name) do
        name_to_extensions[name] = []
      end
      exts << decl
    end
  when AST::Declarations::Alias
    cache_name name_to_alias, name: decl.name.absolute!, decl: decl
  when AST::Declarations::Constant
    cache_name name_to_constant, name: decl.name.absolute!, decl: decl
  when AST::Declarations::Global
    cache_name name_to_global, name: decl.name, decl: decl
  end
end

#absolute_alias_name(name, namespace:) ⇒ Object



166
167
168
169
# File 'lib/rbs/environment.rb', line 166

def absolute_alias_name(name, namespace:)
  raise "Alias name expected: #{name}" unless name.alias?
  absolute_type_name_in name_to_alias, name: name, namespace: namespace
end

#absolute_class_name(name, namespace:) ⇒ Object



156
157
158
159
# File 'lib/rbs/environment.rb', line 156

def absolute_class_name(name, namespace:)
  raise "Class name expected: #{name}" unless name.class?
  absolute_type_name_in name_to_decl, name: name, namespace: namespace
end

#absolute_interface_name(name, namespace:) ⇒ Object



161
162
163
164
# File 'lib/rbs/environment.rb', line 161

def absolute_interface_name(name, namespace:)
  raise "Interface name expected: #{name}" unless name.interface?
  absolute_type_name_in name_to_decl, name: name, namespace: namespace
end

#absolute_name_or(name, type) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/rbs/environment.rb', line 184

def absolute_name_or(name, type)
  if name.absolute?
    type
  else
    yield
  end
end

#absolute_type(type, namespace:, &block) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/rbs/environment.rb', line 192

def absolute_type(type, namespace:, &block)
  case type
  when Types::ClassSingleton
    absolute_name_or(type.name, type) do
      absolute_name = absolute_type_name(type.name, namespace: namespace) { yield(type) }
      Types::ClassSingleton.new(name: absolute_name, location: type.location)
    end
  when Types::ClassInstance
    absolute_name = absolute_type_name(type.name, namespace: namespace) { yield(type) }
    Types::ClassInstance.new(name: absolute_name,
                             args: type.args.map {|ty|
                               absolute_type(ty, namespace: namespace, &block)
                             },
                             location: type.location)
  when Types::Interface
    absolute_name = absolute_type_name(type.name, namespace: namespace) { yield(type) }
    Types::Interface.new(name: absolute_name,
                         args: type.args.map {|ty|
                           absolute_type(ty, namespace: namespace, &block)
                         },
                         location: type.location)
  when Types::Alias
    absolute_name_or(type.name, type) do
      absolute_name = absolute_type_name(type.name, namespace: namespace) { yield(type) }
      Types::Alias.new(name: absolute_name, location: type.location)
    end
  when Types::Tuple
    Types::Tuple.new(
      types: type.types.map {|ty| absolute_type(ty, namespace: namespace, &block) },
      location: type.location
    )
  when Types::Record
    Types::Record.new(
      fields: type.fields.transform_values {|ty| absolute_type(ty, namespace: namespace, &block) },
      location: type.location
    )
  when Types::Union
    Types::Union.new(
      types: type.types.map {|ty| absolute_type(ty, namespace: namespace, &block) },
      location: type.location
    )
  when Types::Intersection
    Types::Intersection.new(
      types: type.types.map {|ty| absolute_type(ty, namespace: namespace, &block) },
      location: type.location
    )
  when Types::Optional
    Types::Optional.new(
      type: absolute_type(type.type, namespace: namespace, &block),
      location: type.location
    )
  when Types::Proc
    Types::Proc.new(
      type: type.type.map_type {|ty| absolute_type(ty, namespace: namespace, &block) },
      location: type.location
    )
  else
    type
  end
end

#absolute_type_name(type_name, namespace:) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rbs/environment.rb', line 171

def absolute_type_name(type_name, namespace:)
  absolute_name = case
                  when type_name.class?
                    absolute_class_name(type_name, namespace: namespace)
                  when type_name.alias?
                    absolute_alias_name(type_name, namespace: namespace)
                  when type_name.interface?
                    absolute_interface_name(type_name, namespace: namespace)
                  end

  absolute_name || yield(type_name)
end

#absolute_type_name_in(environment, name:, namespace:) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rbs/environment.rb', line 135

def absolute_type_name_in(environment, name:, namespace:)
  raise "Namespace should be absolute: #{namespace}" unless namespace.absolute?

  if name.absolute?
    name if environment.key?(name)
  else
    absolute_name = name.with_prefix(namespace)

    if environment.key?(absolute_name)
      absolute_name
    else
      if namespace.empty?
        nil
      else
        parent = namespace.parent
        absolute_type_name_in environment, name: name, namespace: parent
      end
    end
  end
end

#cache_name(cache, name:, decl:) ⇒ Object



34
35
36
37
38
39
# File 'lib/rbs/environment.rb', line 34

def cache_name(cache, name:, decl:)
  if cache.key?(name)
    raise DuplicatedDeclarationError.new(name, decl, cache[name])
  end
  cache[name] = decl
end

#class?(type_name) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
# File 'lib/rbs/environment.rb', line 109

def class?(type_name)
  find_class(type_name)&.yield_self do |decl|
    decl.is_a?(AST::Declarations::Class) || decl.is_a?(AST::Declarations::Module)
  end
end

#each_alias(&block) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/rbs/environment.rb', line 97

def each_alias(&block)
  if block_given?
    name_to_alias.each(&block)
  else
    enum_for :each_alias
  end
end

#each_class_name(&block) ⇒ Object



105
106
107
# File 'lib/rbs/environment.rb', line 105

def each_class_name(&block)
  each_decl.select {|name,| class?(name) }.each(&block)
end

#each_constantObject



77
78
79
80
81
82
83
84
85
# File 'lib/rbs/environment.rb', line 77

def each_constant
  if block_given?
    name_to_constant.each do |name, decl|
      yield name, decl
    end
  else
    enum_for :each_constant
  end
end

#each_declObject



67
68
69
70
71
72
73
74
75
# File 'lib/rbs/environment.rb', line 67

def each_decl
  if block_given?
    name_to_decl.each do |name, decl|
      yield name, decl
    end
  else
    enum_for :each_decl
  end
end

#each_extension(type_name, &block) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/rbs/environment.rb', line 127

def each_extension(type_name, &block)
  if block_given?
    (name_to_extensions[type_name] || []).each(&block)
  else
    enum_for :each_extension, type_name
  end
end

#each_globalObject



87
88
89
90
91
92
93
94
95
# File 'lib/rbs/environment.rb', line 87

def each_global
  if block_given?
    name_to_global.each do |name, global|
      yield name, global
    end
  else
    enum_for :each_global
  end
end

#find_alias(type_name) ⇒ Object



123
124
125
# File 'lib/rbs/environment.rb', line 123

def find_alias(type_name)
  name_to_alias[type_name]
end

#find_class(type_name) ⇒ Object



63
64
65
# File 'lib/rbs/environment.rb', line 63

def find_class(type_name)
  name_to_decl[type_name]
end

#find_extensions(type_name) ⇒ Object



119
120
121
# File 'lib/rbs/environment.rb', line 119

def find_extensions(type_name)
  name_to_extensions[type_name] || []
end

#find_type_decl(type_name) ⇒ Object



115
116
117
# File 'lib/rbs/environment.rb', line 115

def find_type_decl(type_name)
  name_to_decl[type_name]
end

#initialize_copy(other) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/rbs/environment.rb', line 23

def initialize_copy(other)
  @buffers = other.buffers.dup
  @declarations = other.declarations.dup

  @name_to_decl = other.name_to_decl.dup
  @name_to_extensions = other.name_to_extensions.dup
  @name_to_constant = other.name_to_constant.dup
  @name_to_global = other.name_to_global.dup
  @name_to_alias = other.name_to_alias.dup
end

#validate(type, namespace:) ⇒ Object

Validates presence of the relative type, and application arity match.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/rbs/environment.rb', line 254

def validate(type, namespace:)
  case type
  when Types::ClassInstance, Types::Interface
    if type.name.namespace.relative?
      type = absolute_type(type, namespace: namespace) do |type|
        NoTypeFoundError.check!(type.name.absolute!, env: self, location: type.location)
      end
    end

    decl = find_class(type.name)
    unless decl
      raise NoTypeFoundError.new(type_name: type.name, location: type.location)
    end

    InvalidTypeApplicationError.check!(
      type_name: type.name,
      args: type.args,
      params: decl.type_params,
      location: type.location
    )
  end

  type.each_type do |type_|
    validate(type_, namespace: namespace)
  end
end