Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Source_Manager Class Reference

Stores source files and resolves offsets into human-readable data. More...

#include <ah-source.H>

Collaboration diagram for Aleph::Source_Manager:
[legend]

Public Member Functions

Source_File_Id add_virtual_file (const std::string &name, const std::string &text)
 Registers an in-memory source file.
 
Source_File_Id load_file (const std::string &path)
 Loads a file from disk and registers it.
 
const Source_File & file (const Source_File_Id id) const
 Returns the registered file record for id.
 
const std::string & file_name (const Source_File_Id id) const
 Returns the display name of file id.
 
const std::string & file_text (const Source_File_Id id) const
 Returns the full text of file id.
 
size_t file_size (const Source_File_Id id) const
 Returns the size in bytes of file id.
 
size_t num_files () const noexcept
 Returns the number of registered files.
 
Source_Span span (const Source_File_Id id, const Source_Offset begin, const Source_Offset end) const
 Creates a validated span in file id.
 
Source_Position position (const Source_File_Id id, const Source_Offset off) const
 Resolves off to file/offset/line/column information.
 
Source_Position begin_position (const Source_Span &span) const
 Resolves the begin of span to a concrete source position.
 
Source_Position end_position (const Source_Span &span) const
 Resolves the end of span to a concrete source position.
 
std::string slice (const Source_Span &span) const
 Returns the exact text covered by span.
 
Source_Snippet snippet (const Source_Span &span) const
 Builds a single-line diagnostic snippet from span.
 

Private Member Functions

size_t checked_index (const Source_File_Id id) const
 
const Source_File & checked_file (const Source_File_Id id) const
 
size_t line_index_from_offset (const Source_File &file, const Source_Offset off) const
 
size_t line_end_offset (const Source_File &file, const size_t line_idx) const
 

Static Private Member Functions

static DynArray< Source_Offset > compute_line_starts (const std::string &text)
 

Private Attributes

DynArray< Source_File > files
 

Detailed Description

Stores source files and resolves offsets into human-readable data.

Source_Manager owns source texts and offers the core conversions required by lexer/parser infrastructure:

  • file registration from memory or disk
  • line/column resolution from byte offsets
  • span validation and slicing
  • single-line snippets for diagnostics

Definition at line 183 of file ah-source.H.

Member Function Documentation

◆ add_virtual_file()

Source_File_Id Aleph::Source_Manager::add_virtual_file ( const std::string &  name,
const std::string &  text 
)
inline

◆ begin_position()

Source_Position Aleph::Source_Manager::begin_position ( const Source_Span &  span) const
inline

◆ checked_file()

const Source_File & Aleph::Source_Manager::checked_file ( const Source_File_Id  id) const
inlineprivate

Definition at line 207 of file ah-source.H.

References checked_index(), and files.

Referenced by file(), file_name(), file_size(), file_text(), position(), slice(), snippet(), and span().

◆ checked_index()

size_t Aleph::Source_Manager::checked_index ( const Source_File_Id  id) const
inlineprivate

Definition at line 200 of file ah-source.H.

References ah_out_of_range_error_unless, Aleph::and, and files.

Referenced by checked_file().

◆ compute_line_starts()

static DynArray< Source_Offset > Aleph::Source_Manager::compute_line_starts ( const std::string &  text)
inlinestaticprivate

◆ end_position()

Source_Position Aleph::Source_Manager::end_position ( const Source_Span &  span) const
inline

Resolves the end of span to a concrete source position.

Definition at line 355 of file ah-source.H.

References ah_invalid_argument_unless, Aleph::Source_Span::end, Aleph::Source_Span::file_id, Aleph::Source_Span::is_valid(), position(), and span().

◆ file()

const Source_File & Aleph::Source_Manager::file ( const Source_File_Id  id) const
inline

Returns the registered file record for id.

Definition at line 288 of file ah-source.H.

References checked_file().

◆ file_name()

const std::string & Aleph::Source_Manager::file_name ( const Source_File_Id  id) const
inline

Returns the display name of file id.

Definition at line 294 of file ah-source.H.

References checked_file(), and Aleph::Source_File::name.

◆ file_size()

size_t Aleph::Source_Manager::file_size ( const Source_File_Id  id) const
inline

Returns the size in bytes of file id.

Definition at line 306 of file ah-source.H.

References checked_file(), and Aleph::Source_File::text.

◆ file_text()

const std::string & Aleph::Source_Manager::file_text ( const Source_File_Id  id) const
inline

Returns the full text of file id.

Definition at line 300 of file ah-source.H.

References checked_file(), and Aleph::Source_File::text.

Referenced by Aleph::Compiler_Line_Frontend::parse_source_file().

◆ line_end_offset()

size_t Aleph::Source_Manager::line_end_offset ( const Source_File &  file,
const size_t  line_idx 
) const
inlineprivate

◆ line_index_from_offset()

size_t Aleph::Source_Manager::line_index_from_offset ( const Source_File &  file,
const Source_Offset  off 
) const
inlineprivate

Definition at line 212 of file ah-source.H.

References ah_out_of_range_error_unless, Aleph::blossom_maximum_cardinality_matching(), and file.

Referenced by position().

◆ load_file()

Source_File_Id Aleph::Source_Manager::load_file ( const std::string &  path)
inline

Loads a file from disk and registers it.

Parameters
pathPath to the file to load.
Returns
Stable file identifier greater than zero.
Exceptions
std::runtime_errorIf the file cannot be opened.

Definition at line 276 of file ah-source.H.

References add_virtual_file(), ah_runtime_error_unless, Aleph::blossom_maximum_cardinality_matching(), and out.

Referenced by Aleph::Compiler_Driver::execute_files(), and TEST().

◆ num_files()

size_t Aleph::Source_Manager::num_files ( ) const
inlinenoexcept

Returns the number of registered files.

Definition at line 312 of file ah-source.H.

References files.

◆ position()

Source_Position Aleph::Source_Manager::position ( const Source_File_Id  id,
const Source_Offset  off 
) const
inline

Resolves off to file/offset/line/column information.

Definition at line 338 of file ah-source.H.

References Aleph::blossom_maximum_cardinality_matching(), checked_file(), and line_index_from_offset().

Referenced by begin_position(), and end_position().

◆ slice()

◆ snippet()

Source_Snippet Aleph::Source_Manager::snippet ( const Source_Span &  span) const
inline

Builds a single-line diagnostic snippet from span.

Multi-line spans are clipped to the first line because plain-text diagnostics in the MVP render a single highlighted line.

Parameters
spanSpan to inspect.
Returns
Render-ready snippet.

Definition at line 383 of file ah-source.H.

References ah_invalid_argument_unless, Aleph::Source_Span::begin, begin_position(), Aleph::blossom_maximum_cardinality_matching(), checked_file(), Aleph::Source_Span::end, Aleph::Source_Span::file_id, Aleph::Source_Span::is_valid(), line_end_offset(), and span().

Referenced by Aleph::Diagnostic_Engine::render_label_snippet().

◆ span()

Source_Span Aleph::Source_Manager::span ( const Source_File_Id  id,
const Source_Offset  begin,
const Source_Offset  end 
) const
inline

Creates a validated span in file id.

Parameters
idFile id.
beginInclusive begin offset.
endExclusive end offset.
Returns
Valid span referencing id.
Exceptions
std::out_of_rangeIf offsets are invalid.

Definition at line 325 of file ah-source.H.

References ah_out_of_range_error_unless, Aleph::and, and checked_file().

Referenced by begin_position(), end_position(), Aleph::Compiler_Lexer::make_span(), slice(), and snippet().

Member Data Documentation

◆ files

DynArray<Source_File> Aleph::Source_Manager::files
private

Definition at line 185 of file ah-source.H.

Referenced by add_virtual_file(), checked_file(), checked_index(), and num_files().


The documentation for this class was generated from the following file: