Struct SourceMap

Source
pub struct SourceMap { /* private fields */ }
Expand description

A storage for source files.

§Example

use span::{Span, source::{FileName, SourceMap, SourceFile}};

fn process_file(f: &SourceFile) -> Span {
    Span {
        offset: 2 + f.offset(),
        len: 3,
    }
}

let mut source = SourceMap::default();

let f = source.add_file(FileName::Annon, "ABCDEFG".into());
let span1 = process_file(f);

let f = source.add_file(FileName::Annon, "DEFGHIJK".into());
let span2 = process_file(f);

let slice1 = source.slice(&span1);
let slice2 = source.slice(&span2);

assert_eq!(slice1, Some("CDE"));
assert_eq!(slice2, Some("FGH"));

Implementations§

Source§

impl SourceMap

Source

pub const fn new() -> Self

Creates a new empty SourceMap

Source

pub fn add_file(&mut self, fname: FileName, contents: Rc<str>) -> &SourceFile

Adds a new SourceFile to the SourceMap

Returns a reference to the newly created file

Source

pub fn add_file_fs(&mut self, path: PathBuf) -> Result<&SourceFile>

Reads the given path and creates a SourceFile with it’s contents

Source

pub fn add_file_annon(&mut self, contents: Rc<str>) -> &SourceFile

Adds a new annonymous SourceFile (without a filename) to this SourceMap

Source

pub fn get_file_of_span(&self, span: &Span) -> Option<&SourceFile>

Gets the corresponding SourceFile for the given span

§Example
use span::{Span, source::{SourceMap, FileName}};

let mut sm = SourceMap::new();
let (input1, offset1) =
    sm.add_file(
        FileName::from("file1.txt"),
        "Hello world!".into()
    )
    .into_parts();
let (input2, offset2) =
    sm.add_file(
        FileName::from("file2.txt"),
        "Another file :)".into()
    )
    .into_parts();

let span1 = Span {
    offset: 3 + offset1,
    len: 3,
};

let span2 = Span {
    offset: 1 + offset2,
    len: 4,
};

let file1 = sm.get_file_of_span(&span1).unwrap();
assert_eq!(file1.filename().unwrap(), "file1.txt");

let file2 = sm.get_file_of_span(&span2).unwrap();
assert_eq!(file2.filename().unwrap(), "file2.txt");
Source

pub fn get_file_for_offset(&self, offset: usize) -> Option<&SourceFile>

Returns the file for a given offset

Source

pub fn get_file_for_id(&self, id: &FileId) -> Option<&SourceFile>

Source

pub fn get_base_offset_of_span(&self, span: &Span) -> Option<usize>

Returns base offset for a Span. This is: the offset of the file it belongs to.

If no file is found, returns 0

Source

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

Slices the given Span

This function will compute the SourceFile for the span and slice it

Source

pub fn file_position(&self, span: &Span) -> Option<FilePosition>

Returns the FilePosition for this Span

This function will compute the SourceFile for the span and slice it

Source

pub fn files(&self) -> &[SourceFile]

Source

pub fn is_valid(&self, span: &Span) -> bool

Returns true if this is a valid span, this is, if it finds a matching SourceFile for it

Trait Implementations§

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.