[−][src]Struct source_span::Span
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 spansend
. -
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. Theclear
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; #[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(); 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); } } if !current.string.is_empty() { tokens.push(current); }
Methods
impl Span
[src]
#[must_use]
pub fn new(start: Position, last: Position, end: Position) -> Self
[src]
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.
#[must_use]
pub const fn start(&self) -> Position
[src]
Return the position of the first character in the span.
#[must_use]
pub const fn last(&self) -> Position
[src]
Return the last position included in the span.
#[must_use]
pub const fn end(&self) -> Position
[src]
Return the position of the character directly following the span.
It is not included in the span.
#[must_use]
pub fn is_empty(&self) -> bool
[src]
Checks if the span is empty.
#[must_use]
pub fn overlaps(&self, other: &Self) -> bool
[src]
Checks if two span overlaps.
#[must_use]
pub const fn line_count(&self) -> usize
[src]
The number of lines covered by the span.
It is at least one, even if the span is empty.
#[must_use]
pub fn includes_line(&self, line: usize) -> bool
[src]
Checks if the span includes the given line.
pub fn push_column(&mut self)
[src]
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.
pub fn push_line(&mut self)
[src]
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.
pub fn push(&mut self, c: char)
[src]
Extend the span to include the given character located at the spans
end
position.
#[must_use]
pub fn union(&self, other: Self) -> Self
[src]
Compute the union of two spans.
If the two spans do not overlap, all positions in between will be included in the resulting span.
#[must_use]
pub fn inter(&self, other: Self) -> Self
[src]
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.
pub fn append(&mut self, other: Self)
[src]
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
.
#[must_use]
pub const fn next(&self) -> Self
[src]
Return the next span (defined as [end, end]
).
pub fn clear(&mut self)
[src]
Set the span to next
([end, end]
).
#[must_use]
pub const fn aligned(&self) -> Self
[src]
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 (columnstd::usize::MAX
),last
points to the last character of a line (columnstd::usize::MAX - 1
).
Trait Implementations
impl Clone for Span
[src]
impl Copy for Span
[src]
impl Debug for Span
[src]
impl Default for Span
[src]
impl Display for Span
[src]
impl Eq for Span
[src]
impl From<Position> for Span
[src]
impl Hash for Span
[src]
fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl Ord for Span
[src]
fn cmp(&self, other: &Self) -> Ordering
[src]
#[must_use]
fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]
fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]
fn clamp(self, min: Self, max: Self) -> Self
[src]
impl PartialEq<Span> for Span
[src]
impl PartialOrd<Span> for Span
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl StructuralEq for Span
[src]
impl StructuralPartialEq for Span
[src]
Auto Trait Implementations
impl RefUnwindSafe for Span
impl Send for Span
impl Sync for Span
impl Unpin for Span
impl UnwindSafe for Span
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,