[][src]Trait logos::source::Source

pub trait Source<'source> {
    type Slice: Slice<'source>;
    fn len(&self) -> usize;
fn read<Chunk>(&self, offset: usize) -> Option<Chunk>
    where
        Chunk: Chunk<'source>
;
fn slice(&self, range: Range<usize>) -> Option<Self::Slice>;
unsafe fn slice_unchecked(&self, range: Range<usize>) -> Self::Slice; fn find_boundary(&self, index: usize) -> usize { ... } }

Trait for types the Lexer can read from.

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

Associated Types

type Slice: Slice<'source>

A type this Source can be sliced into.

Loading content...

Required methods

fn len(&self) -> usize

Length of the source

fn read<Chunk>(&self, offset: usize) -> Option<Chunk> where
    Chunk: Chunk<'source>, 

Read a chunk of bytes into an array. Returns None 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 lengths.

use logos::Source;

fn main() {
    let foo = "foo";

    assert_eq!(foo.read(0), Some(b"foo"));     // Option<&[u8; 3]>
    assert_eq!(foo.read(0), Some(b"fo"));      // Option<&[u8; 2]>
    assert_eq!(foo.read(2), Some(b'o'));       // Option<u8>
    assert_eq!(foo.read::<&[u8; 4]>(0), None); // Out of bounds
    assert_eq!(foo.read::<&[u8; 2]>(2), None); // Out of bounds
}

fn slice(&self, range: Range<usize>) -> Option<Self::Slice>

Get a slice of the source at given range. This is analogous to slice::get(range).

use logos::Source;

fn main() {
    let foo = "It was the year when they finally immanentized the Eschaton.";

    assert_eq!(Source::slice(&foo, 51..59), Some("Eschaton"));
}

unsafe fn slice_unchecked(&self, range: Range<usize>) -> Self::Slice

Get a slice of the source at given range. This is analogous to slice::get_unchecked(range).

Using this method with range out of bounds is undefined behavior!

use logos::Source;

fn main() {
    let foo = "It was the year when they finally immanentized the Eschaton.";

    unsafe {
        assert_eq!(Source::slice_unchecked(&foo, 51..59), "Eschaton");
    }
}
Loading content...

Provided methods

fn find_boundary(&self, index: usize) -> usize

For &str sources attempts to find the closest char boundary at which source can be sliced, starting from index.

For binary sources (&[u8]) this should just return index back.

Loading content...

Implementations on Foreign Types

impl<'source> Source<'source> for &'source str[src]

type Slice = &'source str

impl<'source> Source<'source> for &'source [u8][src]

type Slice = &'source [u8]

Loading content...

Implementors

Loading content...