module

Logos::Source(T)

Trait for types the Lexer can read from.

Most notably this is implemented for String and Bytes. It is unlikely you will ever want to use this Trait yourself, unless implementing a new Source the Lexer can use.

The correctness of unsafe operations depends on the correct implementation of the length and find_boundary functions so generated code does not request out-of-bounds access.

Instance methods

boundary?(index : Int32) : Bool

Check if index is valid for this Source, that is:

  • It's not larger than the byte length of the Source.
  • (String only) It doesn't land in the middle of a UTF-8 code point.
Source
find_boundary(index : Int32) : Int32

For String sources attempts to find the closest Char boundary at which source can be sliced, starting from index.

For binary sources (Bytes) this should just return index back.

Source
length

Length of the source

Source
read_bytes(bytes : Int32, offset : Int32) : Bytes | Nil

Read bytes bytes starting at offset. Returns nil when reading out of bounds would occur.

Source
read_u8(offset : Int32) : UInt8 | Nil

Read a single byte at offset. Returns nil when reading out of bounds would occur.

This is very useful for matching fixed-size byte arrays, and tends to be very fast at it too, since the compiler knows the byte length.

Source
slice(range : Range(Int32, Int32)) : T | Nil

Get a slice of the source at given range. This is analogous to slice[range]?.

foo = "It was the year when they finally immanentized the Eschaton."
foo.byte_slice?(51, 8) # => "Eschaton"
Source
slice_unchecked(range : Range(Int32, Int32)) : T

Get a slice of the source at given range. This is analogous to slice[range] without bounds checking.

Unsafe: Range should not exceed bounds.

foo = "It was the year when they finally immanentized the Eschaton."

unsafe do
  foo.slice_unchecked(51..59) # => "Eschaton"
end
Source