Struct 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.

use source_span::{Span, DEFAULT_METRICS};

#[derive(Clone, Default)]
pub struct Token {
	string: String,
	span: Span,
}

let string = "This is an example String.".to_string();
let mut tokens = Vec::new();
let mut current = Token::default();
let metrics = &DEFAULT_METRICS;

for c in string.chars() {
	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, metrics);
	}
}

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

Implementations§

Source§

impl Span

Source

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

Create a new span from three positions.

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

Source

pub fn of_string<M: Metrics>(str: &str, metrics: &M) -> Self

Source

pub const fn start(&self) -> Position

Return the position of the first character in the span.

Source

pub const fn last(&self) -> Position

Return the last position included in the span.

Source

pub const 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 includes(&self, other: &Span) -> bool

Checks if the given span is included it this span.

Source

pub const 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<M: Metrics>(&mut self, c: char, metrics: &M)

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

Source

pub fn union(&self, other: Self) -> Self

Compute the union of two spans.

If the two spans do not overlap, all positions in between will be included in the resulting span.

Source

pub fn inter(&self, other: Self) -> Self

Computes the intersection of the two spans.

If the two spans do not overlap, then the empty span located at the start of the most advanced span (maximum of the start of the two spans) is returned.

Source

pub fn append(&mut self, other: Self)

Extend the span to the end of the given span.

This is the in-place version of union, except that nothing happens if the input span finishes before the end of self.

Source

pub const fn next(&self) -> Self

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

Source

pub fn clear(&mut self)

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

Source

pub const fn aligned(&self) -> Self

Return the span aligned on line boundaries.

This will compute the smallest span including self such that

  • start is at the begining of a line (column 0),
  • end is at the end of a line (column std::usize::MAX),
  • last points to the last character of a line (column std::usize::MAX - 1).

Trait Implementations§

Source§

impl Clone for Span

Source§

fn clone(&self) -> Span

Returns a duplicate 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) -> Self

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: &Self) -> 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,

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

impl PartialEq for Span

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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: &Self) -> 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

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

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

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

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§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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§

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>,

Source§

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>,

Source§

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.