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 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.
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
impl Span
sourcepub fn new(start: Position, last: Position, end: Position) -> Span
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.
sourcepub fn end(&self) -> Position
pub fn end(&self) -> Position
Return the position of the character directly following the span.
It is not included in the span.
sourcepub fn line_count(&self) -> usize
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.
sourcepub fn includes_line(&self, line: usize) -> bool
pub fn includes_line(&self, line: usize) -> bool
Checks if the span includes the given line.
sourcepub fn push_column(&mut self)
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.
sourcepub fn push_line(&mut self)
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.
Trait Implementations§
source§impl Ord for Span
impl Ord for Span
source§impl PartialEq for Span
impl PartialEq for Span
source§impl PartialOrd for Span
impl PartialOrd for Span
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moreimpl Copy for Span
impl Eq for Span
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)