pub struct SourceFile {
pub id: FileId,
pub name: String,
pub start: BytePos,
pub end: BytePos,
pub included_from: Option<Span>,
/* private fields */
}Expand description
The bytes of one file, plus where they sit in the flat space.
Contents are held behind a trait object rather than as a Vec, so that the memory mapped
input in spec/05-preprocessor.md section 5.2 can be handed over as it is instead of
being copied into one. Reading the bytes goes through one virtual call, which is fine
because it happens once per file in the lexer and once per rendered diagnostic, never in
a loop.
Fields§
§id: FileIdThis file’s own id, so that anything holding a &SourceFile can name it.
name: StringThe name to print in a diagnostic, which is the path as the user wrote it rather than
a canonical one. Somebody who typed -I../include wants to read ../include/foo.h.
start: BytePosFirst byte of this file in the flat space.
end: BytePosOne past this file’s last byte.
included_from: Option<Span>The #include that pulled this file in, or None for a file named on the command
line. This is what “in file included from” is printed from.
Implementations§
Source§impl SourceFile
impl SourceFile
The file’s contents, shared.
Sourcepub fn contains(&self, pos: BytePos) -> bool
pub fn contains(&self, pos: BytePos) -> bool
Whether pos falls in this file.
The end position is included, so a diagnostic about something missing at the end of a file still names the file rather than falling into the gap after it.
Sourcepub fn line_count(&self) -> u32
pub fn line_count(&self) -> u32
How many lines the file has, counting a trailing newline as ending the last line rather than starting another. An empty file has one line, which is empty.
Sourcepub fn line_bytes(&self, line: u32) -> Option<&[u8]>
pub fn line_bytes(&self, line: u32) -> Option<&[u8]>
The bytes of line line, counting from one, without its line terminator.
None if the file has no such line.
Sourcepub fn position(&self, pos: BytePos) -> Option<Loc>
pub fn position(&self, pos: BytePos) -> Option<Loc>
The line and column of pos, or None if pos is not in this file.
Sourcepub fn presumed_position(&self, pos: BytePos) -> Option<PresumedLoc<'_>>
pub fn presumed_position(&self, pos: BytePos) -> Option<PresumedLoc<'_>>
Where pos is presented as being, once the #line directives in front of it are
taken into account. The same as SourceFile::position for a file that has none.