pub struct SourceMap<'a> { /* private fields */ }Expand description
Maps virtual byte offsets to source files.
SourceMap places all source files into a virtual byte address space,
enabling Span values from different files to coexist without
modification. Files are separated by 1-byte gaps to prevent spans
from accidentally straddling file boundaries.
§Example
# use orrery_parser::source_map::SourceMap;
let mut map = SourceMap::new();
let base = map.add_file("main.orr", "diagram component;\nbox: Rectangle;", None);
let file = map.lookup_file(base).unwrap();
assert_eq!(file.name(), "main.orr");Implementations§
Source§impl<'a> SourceMap<'a>
impl<'a> SourceMap<'a>
Sourcepub fn add_file(
&mut self,
name: impl Into<String>,
source: &'a str,
imported_at: Option<Span>,
) -> usize
pub fn add_file( &mut self, name: impl Into<String>, source: &'a str, imported_at: Option<Span>, ) -> usize
Registers a source file in the virtual address space.
The file is placed at the next available offset, separated from the previous file by a 1-byte gap.
§Arguments
name- Human-readable name for the file (typically its path).source- The full source text.imported_at- The span of theimportdeclaration that triggered loading this file, orNonefor the root file.
§Returns
The base_offset at which this file starts in the virtual address space.
Sourcepub fn lookup_file(&self, offset: usize) -> Option<&SourceFile<'a>>
pub fn lookup_file(&self, offset: usize) -> Option<&SourceFile<'a>>
Looks up the source file containing the given virtual offset.
Uses binary search for O(log n) performance.
§Arguments
offset- A byte offset in the virtual address space.
§Returns
The SourceFile containing offset, or None if the offset falls
in a gap between files or is out of range.
Sourcepub fn lookup_file_by_span(&self, span: Span) -> Option<&SourceFile<'a>>
pub fn lookup_file_by_span(&self, span: Span) -> Option<&SourceFile<'a>>
Looks up the source file containing the given Span.
This verifies that the entire span (not just its start offset) falls within a single file.
§Arguments
span- ASpanin the virtual address space.
§Returns
The SourceFile containing the full span, or None if the span
crosses a file boundary, falls in a gap, or is out of range.
Sourcepub fn source_slice(&self, span: Span) -> Option<&str>
pub fn source_slice(&self, span: Span) -> Option<&str>
Sourcepub fn file_count(&self) -> usize
pub fn file_count(&self) -> usize
Returns the number of registered source files.
Sourcepub fn files(&self) -> &[SourceFile<'a>]
pub fn files(&self) -> &[SourceFile<'a>]
Returns all registered source files.
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for SourceMap<'a>
impl<'a> RefUnwindSafe for SourceMap<'a>
impl<'a> Send for SourceMap<'a>
impl<'a> Sync for SourceMap<'a>
impl<'a> Unpin for SourceMap<'a>
impl<'a> UnsafeUnpin for SourceMap<'a>
impl<'a> UnwindSafe for SourceMap<'a>
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more