Skip to main content

SourceMap

Struct SourceMap 

Source
pub struct SourceMap {
    pub sources: Vec<SourceInfo>,
    pub next_id: u32,
}
Expand description

The central registry that owns all source fragments.

SourceMap assigns each registered fragment a unique SourceId and provides utilities for converting between byte offsets and line/column positions.

Fields§

§sources: Vec<SourceInfo>

All registered source fragments, indexed by their SourceId.

§next_id: u32

The next SourceId to assign.

Implementations§

Source§

impl SourceMap

Source

pub fn new() -> Self

Create an empty SourceMap.

Source

pub fn add_file(&mut self, name: &str, content: String) -> SourceId

Register a source file and return its SourceId.

Source

pub fn add_macro_expansion( &mut self, macro_name: &str, site: Span, content: String, ) -> SourceId

Register a macro expansion and return its SourceId.

Source

pub fn get(&self, id: SourceId) -> Option<&SourceInfo>

Look up a source by its identifier.

Source

pub fn span_to_position(&self, span: &Span) -> Option<LineColumn>

Convert a byte-offset span to a LineColumn.

Returns the position of the start of the span. Returns None if the source is unknown or the offset is out of range.

Source

pub fn position_to_offset(&self, lc: &LineColumn) -> Option<usize>

Convert a LineColumn back to a byte offset.

Returns None if the source is unknown or the position is out of range.

Source

pub fn span_text(&self, span: &Span) -> Option<&str>

Return the text slice covered by span.

Returns None if the source is unknown or the span is out of range.

Source

pub fn chain_origin(&self, span: &Span) -> Vec<Span>

Walk back through macro expansion sites and collect origin spans.

If span belongs to a macro-expanded source, this function follows the expansion chain until it reaches a non-expanded source. The returned vector goes from span (innermost) to the original user-written span (outermost).

Trait Implementations§

Source§

impl Debug for SourceMap

Source§

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

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

impl Default for SourceMap

Source§

fn default() -> SourceMap

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

Auto Trait Implementations§

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