ArkScript
A small, lisp-inspired, functional scripting language
Ark::Diagnostics::Printer Class Reference

Source printer for diagnostics. More...

#include <PrettyPrinting.hpp>

Collaboration diagram for Ark::Diagnostics::Printer:
[legend]

Public Member Functions

 Printer (const std::string &filename, std::size_t target_line, std::optional< std::size_t > end_target_line, bool colorize)
 Create a new Printer object.
 
std::string sliceCode (internal::FilePos start, const std::optional< internal::FilePos > &end) const
 Slice the source code to get code between two cursors.
 
void extendWindow (std::size_t line_to_include)
 Extend the window of lines to show, to include a given line. Useful to display the origin of an error.
 
void printLine (std::ostream &os)
 Print the current line and advance by one.
 
bool isTargetLine () const
 Check if we printed the target line.
 
bool hasContent () const
 Check if there are lines to print.
 
bool coversLine (std::size_t line_number) const
 
const Windowwindow () const
 
std::size_t current () const
 
const std::string & currentLine () const
 

Static Public Attributes

static const std::string GhostLinePrefix = " |"
 

Private Attributes

bool m_should_colorize
 
std::vector< std::string > m_source
 
Window m_window
 
std::size_t m_current_line
 
LineColorContextCounts m_color_ctx
 

Detailed Description

Source printer for diagnostics.

Definition at line 61 of file PrettyPrinting.hpp.

Constructor & Destructor Documentation

◆ Printer()

Ark::Diagnostics::Printer::Printer ( const std::string & filename,
std::size_t target_line,
std::optional< std::size_t > end_target_line,
bool colorize )

Create a new Printer object.

Parameters
filenamepath to the file that has an error
target_lineline of the error (0-indexed)
end_target_lineoptional end line for the error (0-indexed)
colorizeif we should colorize the output or not

Definition at line 78 of file PrettyPrinting.cpp.

References ARK_NO_NAME_FILE, m_current_line, m_source, m_window, Ark::Utils::readFile(), Ark::Utils::splitString(), and Ark::Diagnostics::Window::start.

Member Function Documentation

◆ coversLine()

bool Ark::Diagnostics::Printer::coversLine ( std::size_t line_number) const
nodiscard

◆ current()

std::size_t Ark::Diagnostics::Printer::current ( ) const
inlinenodiscard

Definition at line 122 of file PrettyPrinting.hpp.

Referenced by Ark::Diagnostics::makeContext().

◆ currentLine()

const std::string & Ark::Diagnostics::Printer::currentLine ( ) const
inlinenodiscard

Definition at line 127 of file PrettyPrinting.hpp.

Referenced by Ark::Diagnostics::makeContext().

◆ extendWindow()

void Ark::Diagnostics::Printer::extendWindow ( std::size_t line_to_include)

Extend the window of lines to show, to include a given line. Useful to display the origin of an error.

Parameters
line_to_includeline to include (0-indexed)

Definition at line 121 of file PrettyPrinting.cpp.

References m_current_line, m_window, Ark::Diagnostics::Window::resume_at, Ark::Diagnostics::Window::skip_start_at, and Ark::Diagnostics::Window::start.

Referenced by Ark::Diagnostics::makeContext().

◆ hasContent()

bool Ark::Diagnostics::Printer::hasContent ( ) const
nodiscard

Check if there are lines to print.

Returns
true while the window isn't exhausted
false when there is nothing more to print

Definition at line 171 of file PrettyPrinting.cpp.

References Ark::Diagnostics::Window::end, m_current_line, m_source, m_window, and Ark::Diagnostics::Window::target.

Referenced by Ark::Diagnostics::makeContext(), and printLine().

◆ isTargetLine()

bool Ark::Diagnostics::Printer::isTargetLine ( ) const
nodiscard

Check if we printed the target line.

Returns
true if the last displayed line is the target line
false otherwise

Definition at line 166 of file PrettyPrinting.cpp.

References m_current_line, m_window, Ark::Diagnostics::Window::target, and Ark::Diagnostics::Window::target_end.

Referenced by Ark::Diagnostics::makeContext().

◆ printLine()

void Ark::Diagnostics::Printer::printLine ( std::ostream & os)

◆ sliceCode()

std::string Ark::Diagnostics::Printer::sliceCode ( internal::FilePos start,
const std::optional< internal::FilePos > & end ) const
nodiscard

Slice the source code to get code between two cursors.

Parameters
start
end
Returns
std::string

Definition at line 90 of file PrettyPrinting.cpp.

References Ark::internal::FilePos::column, Ark::internal::FilePos::line, Ark::Utils::ltrim(), m_source, and Ark::Utils::rtrim().

◆ window()

const Window & Ark::Diagnostics::Printer::window ( ) const
inlinenodiscard

Definition at line 116 of file PrettyPrinting.hpp.

Member Data Documentation

◆ GhostLinePrefix

const std::string Ark::Diagnostics::Printer::GhostLinePrefix = " |"
inlinestatic

◆ m_color_ctx

LineColorContextCounts Ark::Diagnostics::Printer::m_color_ctx
private

Definition at line 139 of file PrettyPrinting.hpp.

Referenced by printLine().

◆ m_current_line

std::size_t Ark::Diagnostics::Printer::m_current_line
private

Definition at line 138 of file PrettyPrinting.hpp.

Referenced by extendWindow(), hasContent(), isTargetLine(), Printer(), and printLine().

◆ m_should_colorize

bool Ark::Diagnostics::Printer::m_should_colorize
private

Definition at line 135 of file PrettyPrinting.hpp.

Referenced by printLine().

◆ m_source

std::vector<std::string> Ark::Diagnostics::Printer::m_source
private

Definition at line 136 of file PrettyPrinting.hpp.

Referenced by hasContent(), Printer(), printLine(), and sliceCode().

◆ m_window

Window Ark::Diagnostics::Printer::m_window
private

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