Class: RBS::Location::WithChildren

Inherits:
RBS::Location show all
Defined in:
lib/rbs/location.rb

Instance Attribute Summary collapse

Attributes inherited from RBS::Location

#buffer, #end_pos, #start_pos

Instance Method Summary collapse

Methods inherited from RBS::Location

#+, #<<, #==, #concat, #end_column, #end_line, #end_loc, #inspect, #name, #pred?, #range, #source, #start_column, #start_line, #start_loc, #to_json, #to_s, to_string, #with_children

Constructor Details

#initialize(buffer:, start_pos:, end_pos:) ⇒ WithChildren

Returns a new instance of WithChildren.



150
151
152
153
154
155
# File 'lib/rbs/location.rb', line 150

def initialize(buffer:, start_pos:, end_pos:)
  super(buffer: buffer, start_pos: start_pos, end_pos: end_pos)

  @optional_children = {}
  @required_children = {}
end

Instance Attribute Details

#optional_childrenObject (readonly)

Returns the value of attribute optional_children.



148
149
150
# File 'lib/rbs/location.rb', line 148

def optional_children
  @optional_children
end

#required_childrenObject (readonly)

Returns the value of attribute required_children.



148
149
150
# File 'lib/rbs/location.rb', line 148

def required_children
  @required_children
end

Instance Method Details

#[](key) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rbs/location.rb', line 163

def [](key)
  case
  when required_children.key?(_ = key)
    range = required_children[_ = key]
    Location.new(buffer: buffer, start_pos: range.begin, end_pos: range.end)
  when optional_children.key?(_ = key)
    range = required_children[_ = key] || optional_children[_ = key]
    if range
      Location.new(buffer: buffer, start_pos: range.begin, end_pos: range.end)
    end
  else
    raise "Unknown key given: `#{key}`"
  end
end

#initialize_copy(from) ⇒ Object



157
158
159
160
161
# File 'lib/rbs/location.rb', line 157

def initialize_copy(from)
  required_children.merge!(from.required_children)
  optional_children.merge!(from.optional_children)
  self
end

#merge_optional(hash) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rbs/location.rb', line 197

def merge_optional(hash)
  this = dup

  h = hash.transform_values do |value|
    case value
    when Range
      value
    when Location
      value.range
    else
      nil
    end
  end

  this.optional_children.merge!(h)

  this
end

#merge_required(hash) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rbs/location.rb', line 178

def merge_required(hash)
  this = dup

  h = hash.transform_values do |value|
    case value
    when Range
      value
    when Location
      value.range
    else
      raise
    end
  end

  this.required_children.merge!(h)

  this
end