Class: BioSyntax::Span

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

Overview

A highlighted byte range within one input line.

Offsets are byte offsets into the original line, not character indexes. This matches the native C API and keeps slicing correct for arbitrary encodings.

Examples:

Extract the text covered by a span

text = line.byteslice(span.start, span.length)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, length, kind_id) ⇒ Span

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Span.



193
194
195
196
197
198
# File 'lib/biosyntax.rb', line 193

def initialize(start, length, kind_id)
  @start = Integer(start)
  @length = Integer(length)
  @kind_id = Integer(kind_id)
  freeze
end

Instance Attribute Details

#kind_idInteger (readonly)

Returns:

  • (Integer)

    byte offset at the start of the span

  • (Integer)

    byte length of the span

  • (Integer)

    native kind id for this span



190
191
192
# File 'lib/biosyntax.rb', line 190

def kind_id
  @kind_id
end

#lengthInteger (readonly)

Returns:

  • (Integer)

    byte offset at the start of the span

  • (Integer)

    byte length of the span

  • (Integer)

    native kind id for this span



190
191
192
# File 'lib/biosyntax.rb', line 190

def length
  @length
end

#startInteger (readonly)

Returns:

  • (Integer)

    byte offset at the start of the span

  • (Integer)

    byte length of the span

  • (Integer)

    native kind id for this span



190
191
192
# File 'lib/biosyntax.rb', line 190

def start
  @start
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


251
252
253
254
255
256
# File 'lib/biosyntax.rb', line 251

def ==(other)
  other.is_a?(Span) &&
    other.start == @start &&
    other.length == @length &&
    other.kind_id == @kind_id
end

#deconstructArray(Integer, Integer, Symbol)

Pattern matching support.

Returns:

  • (Array(Integer, Integer, Symbol))


233
234
235
# File 'lib/biosyntax.rb', line 233

def deconstruct
  to_a
end

#endInteger

Returns byte offset just after the span.

Returns:

  • (Integer)

    byte offset just after the span



201
202
203
# File 'lib/biosyntax.rb', line 201

def end
  @start + @length
end

#hashInteger

Returns:

  • (Integer)


260
261
262
# File 'lib/biosyntax.rb', line 260

def hash
  [self.class, @start, @length, @kind_id].hash
end

#inspectString

Returns:

  • (String)


265
266
267
# File 'lib/biosyntax.rb', line 265

def inspect
  "#<#{self.class} start=#{@start} end=#{self.end} kind=#{kind.name.inspect}>"
end

#kindKind

Returns token kind metadata for this span.

Returns:

  • (Kind)

    token kind metadata for this span



206
207
208
# File 'lib/biosyntax.rb', line 206

def kind
  BioSyntax.kind(@kind_id)
end

#kind_nameSymbol

Returns token kind name.

Returns:

  • (Symbol)

    token kind name



211
212
213
# File 'lib/biosyntax.rb', line 211

def kind_name
  kind.name
end

#rangeRange<Integer>

Returns byte range covered by this span.

Returns:

  • (Range<Integer>)

    byte range covered by this span



221
222
223
# File 'lib/biosyntax.rb', line 221

def range
  @start...self.end
end

#scopeString

Returns semantic scope for this span.

Returns:

  • (String)

    semantic scope for this span



216
217
218
# File 'lib/biosyntax.rb', line 216

def scope
  kind.scope
end

#to_aArray(Integer, Integer, Symbol)

Returns start offset, end offset, and kind name.

Returns:

  • (Array(Integer, Integer, Symbol))

    start offset, end offset, and kind name



226
227
228
# File 'lib/biosyntax.rb', line 226

def to_a
  [@start, self.end, kind.name]
end

#to_hHash

Returns serializable metadata for this span.

Returns:

  • (Hash)

    serializable metadata for this span



238
239
240
241
242
243
244
245
246
247
# File 'lib/biosyntax.rb', line 238

def to_h
  {
    start: @start,
    end: self.end,
    length: @length,
    kind: kind.name,
    kind_id: @kind_id,
    scope: kind.scope
  }
end