Struct source_span::Span[][src]

pub struct Span { /* fields omitted */ }
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

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.

Return the position of the first character in the span.

Return the last position included in the span.

Return the position of the character directly following the span.

It is not included in the span.

Checks if the span is empty.

Checks if two span overlaps.

Checks if the given span is included it this span.

The number of lines covered by the span.

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

Checks if the span includes the given line.

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.

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.

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

Compute the union of two spans.

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

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.

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.

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

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Formats the value using the given formatter. Read more

Performs the conversion.

Feeds this value into the given Hasher. Read more

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

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

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

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

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.