Class: RBS::Location
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/rbs/wasm/location.rb, ext/rbs_extension/legacy_location.c, lib/rbs/location_aux.rb |
Overview
Pure-Ruby implementation of the primitives that back Location.
On CRuby these come from the C extension (ext/rbs_extension/legacy_location.c).
JRuby loads this instead, before rbs/location_aux.rb layers the public API on
top, so Location behaves identically without the native extension.
Constant Summary
-
WithChildren =
# File 'lib/rbs/location_aux.rb', line 29self
Class Method Summary
Instance Attribute Summary
- #buffer readonly
Instance Method Summary
- #==(other)
- #[](name)
- #_add_optional_child(name, start_pos, end_pos)
- #_add_optional_no_child(name)
- #_add_required_child(name, start_pos, end_pos)
- #_end_pos
- #_optional_keys
- #_required_keys
- #_start_pos
- #add_optional_child(name, range)
- #add_required_child(name, range)
- #aref
- #each_optional_key(&block)
- #each_required_key(&block)
- #end_column
- #end_line
- #end_loc
- #end_pos
- #initialize(buffer, start_pos, end_pos) ⇒ Location constructor
- #inspect
- #key?(name) ⇒ Boolean
- #local_location
- #local_source
- #name
- #optional_key?(name) ⇒ Boolean
- #range
- #required_key?(name) ⇒ Boolean
- #source
- #start_column
- #start_line
- #start_loc
- #start_pos
- #to_json(state = nil)
- #to_s
- #initialize_copy(other) private
Constructor Details
.new(buffer_ = nil, start_pos_ = nil, end_pos_ = nil, buffer: nil, start_pos: nil, end_pos: nil) ⇒ Location
#initialize(buffer, start_pos, end_pos) ⇒ Location
Class Method Details
.to_string(location, default: "*:*:*...*:*")
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 102
def self.to_string(location, default: "*:*:*...*:*") location&.to_s || default end
Instance Attribute Details
#buffer (readonly)
[ GitHub ]# File 'lib/rbs/wasm/location.rb', line 10
attr_reader :buffer
Instance Method Details
#==(other)
[ GitHub ]#[](name)
[ GitHub ]# File 'lib/rbs/wasm/location.rb', line 48
def [](name) if (range = @required_children[name]) return Location.new(@buffer, range[0], range[1]) end if @optional_children.key?(name) range = @optional_children[name] return range && Location.new(@buffer, range[0], range[1]) end raise "Unknown child name given: #{name}" end
#_add_optional_child(name, start_pos, end_pos)
[ GitHub ]#_add_optional_no_child(name)
[ GitHub ]#_add_required_child(name, start_pos, end_pos)
[ GitHub ]#_end_pos
[ GitHub ]# File 'lib/rbs/wasm/location.rb', line 24
def _end_pos @end_pos end
#_optional_keys
[ GitHub ]# File 'lib/rbs/wasm/location.rb', line 44
def _optional_keys @optional_children.keys end
#_required_keys
[ GitHub ]# File 'lib/rbs/wasm/location.rb', line 40
def _required_keys @required_children.keys end
#_start_pos
[ GitHub ]# File 'lib/rbs/wasm/location.rb', line 20
def _start_pos @start_pos end
#add_optional_child(name, range)
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 110
def add_optional_child(name, range) if range _add_optional_child(name, range.begin, range.end) else _add_optional_no_child(name); end end
#add_required_child(name, range)
[ GitHub ]#aref
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 27
alias aref []
#each_optional_key(&block)
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 118
def each_optional_key(&block) if block _optional_keys.uniq.each(&block) else enum_for(:each_optional_key) end end
#each_required_key(&block)
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 126
def each_required_key(&block) if block _required_keys.uniq.each(&block) else enum_for(:each_required_key) end end
#end_column
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 55
def end_column end_loc[1] end
#end_line
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 51
def end_line end_loc[0] end
#end_loc
[ GitHub ]#end_pos
[ GitHub ]#initialize_copy(other) (private)
[ GitHub ]# File 'ext/rbs_extension/legacy_location.c', line 126
static VALUE location_initialize_copy(VALUE self, VALUE other) {
rbs_loc *self_loc = rbs_check_location(self);
rbs_loc *other_loc = rbs_check_location(other);
*self_loc = (rbs_loc) {
.buffer = Qnil,
.rg = other_loc->rg,
.children = NULL,
};
RB_OBJ_WRITE(self, &self_loc->buffer, other_loc->buffer);
if (other_loc->children != NULL) {
rbs_loc_legacy_alloc_children(self_loc, other_loc->children->cap);
memcpy(self_loc->children, other_loc->children, RBS_LOC_CHILDREN_SIZE(other_loc->children->cap));
}
return Qnil;
}
#inspect
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 5
def inspect rks = each_required_key.to_a ops = each_optional_key.to_a.map {|x| "?#{x}" } src = if source.length <= 1 source.inspect else source.each_line.first&.chomp&.inspect end "#<#{self.class}:#{self.__id__} buffer=#{buffer.name}, start=#{start_line}:#{start_column}, pos=#{start_pos}...#{end_pos}, children=#{(rks + ops).join(",")} source=#{src}>" end
#key?(name) ⇒ Boolean
# File 'lib/rbs/location_aux.rb', line 134
def key?(name) optional_key?(name) || required_key?(name) end
#local_location
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 146
def local_location loc = Location.new(buffer.detach, _start_pos, _end_pos) each_optional_key do |key| value = self[key] if value loc.add_optional_child(key, value._start_pos...value._end_pos) else loc.add_optional_child(key, nil) end end each_required_key do |key| value = self[key] or raise loc.add_required_child(key, value._start_pos...value._end_pos) end loc #: self end
#local_source
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 166
def local_source local_location.source end
#name
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 39
def name buffer.name end
#optional_key?(name) ⇒ Boolean
# File 'lib/rbs/location_aux.rb', line 138
def optional_key?(name) _optional_keys.include?(name) end
#range
[ GitHub ]
#required_key?(name) ⇒ Boolean
# File 'lib/rbs/location_aux.rb', line 142
def required_key?(name) _required_keys.include?(name) end
#source
[ GitHub ]#start_column
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 47
def start_column start_loc[1] end
#start_line
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 43
def start_line start_loc[0] end
#start_loc
[ GitHub ]#start_pos
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 31
def start_pos buffer.absolute_position(_start_pos) || raise end
#to_json(state = nil)
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 86
def to_json(state = nil) { start: { line: start_line, column: start_column }, end: { line: end_line, column: end_column }, buffer: { name: name&.to_s } }.to_json(state) end
#to_s
[ GitHub ]# File 'lib/rbs/location_aux.rb', line 75
def to_s "#{name || "-"}:#{start_line}:#{start_column}...#{end_line}:#{end_column}" end