Struct source_span::Span

source ·
pub struct Span { /* private fields */ }
Expand description

Span in a source file.

A span points to a range of caracters between two cursor Position.

§Span construction with the push* methods

A span can be directly created using the new method, however in the context of parsing (or lexing) it might be useful to build spans incrementally. The push* methods family will help you do that.

  • push will extend the span to include the given character located at the spans end.

  • push_column will extend the span to include the next column. Note that this does not necessarily correspond to the next character (if it is a NL, or a full-width character for instance).

  • push_line will extend the span to include the rest of the line. The end of the span will be placed at the begining of the next line.

  • The next method can finally be used to create the span to [end, end] (when a token has been read entirely for instance) and start building the next span. The clear method does the same but in place.

§Example

Here is a basic example computing the span of every word/token in a char stream.

for c in chars {
    let c = c?; // report eventual I/O errors.
    if c.is_whitespace() {
        // save the current token.
        if !current.string.is_empty() {
            tokens.push(current.clone());
        }

        // reset current token.
        current.string.clear();
        current.span.clear(); // the span here is moved to the end of itself.
    } else {
        current.string.push(c);
        current.span.push(c);
    }
}

if !current.string.is_empty() {
    tokens.push(current);
}

Implementations§

source§

impl Span

source

pub fn new(start: Position, last: Position, end: Position) -> Span

Create a new span from three positions.

If the end position is before the start position then the returned span will be [start, start]. If the last position is before start or after end it will panic. If the last position is equal to end while the span is not empty, it will panic.

source

pub fn start(&self) -> Position

Return the position of the first character in the span.

source

pub fn last(&self) -> Position

Return the last position included in the span.

source

pub fn end(&self) -> Position

Return the position of the character directly following the span.

It is not included in the span.

source

pub fn is_empty(&self) -> bool

Checks if the span is empty.

source

pub fn overlaps(&self, other: &Span) -> bool

Checks if two span overlaps.

source

pub fn line_count(&self) -> usize

The number of lines covered by the span.

It is at least one, even if the span is empty.

source

pub fn includes_line(&self, line: usize) -> bool

Checks if the span includes the given line.

source

pub fn push_column(&mut self)

Extends the span to include the next column.

Note that this does not necessarily correspond to the next character (if it is a NL, or a full-width character for instance). To do that you can use the push method.

source

pub fn push_line(&mut self)

Extends the span to include the rest of the line.

The end of the span will be placed at the begining of the next line.

source

pub fn push(&mut self, c: char)

Extend the span to include the given character located at the spans end position.

source

pub fn next(&self) -> Span

Return the next span (defined as [end, end]).

source

pub fn clear(&mut self)

Set the span to next ([end, end]).

Trait Implementations§

source§

impl Clone for Span

source§

fn clone(&self) -> Span

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Span

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Span

source§

fn default() -> Span

Returns the “default value” for a type. Read more
source§

impl Display for Span

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<Position> for Span

source§

fn from(pos: Position) -> Span

Converts to this type from the input type.
source§

impl Hash for Span

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Span

source§

fn cmp(&self, other: &Span) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Span

source§

fn eq(&self, other: &Span) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Span

source§

fn partial_cmp(&self, other: &Span) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Copy for Span

source§

impl Eq for Span

source§

impl StructuralPartialEq for Span

Auto Trait Implementations§

§

impl Freeze for Span

§

impl RefUnwindSafe for Span

§

impl Send for Span

§

impl Sync for Span

§

impl Unpin for Span

§

impl UnwindSafe for Span

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.