Porytiles
Loading...
Searching...
No Matches
porytiles::CParserContext Class Reference

Context object providing rich error formatting for C/C++ parsing. More...

#include <c_parser_context.hpp>

Public Member Functions

 CParserContext (gsl::not_null< const std::vector< std::string > * > file_lines, gsl::not_null< const TextFormatter * > format, std::string file_path="")
 Constructs a parser context with formatting dependencies.
 
FormattableError make_error (SourcePosition pos, const std::string &message) const
 Creates a FormattableError with source context.
 
const std::vector< std::string > * file_lines () const
 Returns the file lines held by this context.
 
const TextFormatterformatter () const
 Returns the text formatter used by this context.
 
const std::string & file_path () const
 Returns the file path associated with this context.
 

Detailed Description

Context object providing rich error formatting for C/C++ parsing.

CParserContext holds the dependencies needed to create FormattableError messages with source file context. It provides a make_error() factory method that creates multi-line error messages including the relevant source code snippet with highlighted error location via FileHighlightPrinter.

The context does not own the file_lines - callers must ensure the file_lines outlive the context. This design allows CParserFacade to own the file content while passing non-owning access to Lexer and Parser.

Example usage:

std::vector<std::string> file_lines = {"#define FOO 1", "#define BAR UNDEFINED"};
CParserContext context{&file_lines, &formatter, "header.h"};
// Create error at line 2, column 13 (UNDEFINED token)
auto error = context.make_error({2, 13}, "unknown identifier 'UNDEFINED'");
// error.details() produces:
// header.h:2:13: unknown identifier 'UNDEFINED'
// 1: #define FOO 1
// ➞ 2: #define BAR UNDEFINED
// ^
const TextFormatter * formatter() const
Returns the text formatter used by this context.
const std::vector< std::string > * file_lines() const
Returns the file lines held by this context.
TextFormatter implementation that strips all styling from text.
@ error
Emit a formatted error and fail decompilation.

Definition at line 41 of file c_parser_context.hpp.

Constructor & Destructor Documentation

◆ CParserContext()

porytiles::CParserContext::CParserContext ( gsl::not_null< const std::vector< std::string > * >  file_lines,
gsl::not_null< const TextFormatter * >  format,
std::string  file_path = "" 
)

Constructs a parser context with formatting dependencies.

Parameters
file_linesPointer to cached file lines (non-owning, must outlive context)
formatText formatter for styled output
file_pathOptional file path for error message headers (empty if unknown)
Precondition
file_lines must not be nullptr

Definition at line 21 of file c_parser_context.cpp.

Member Function Documentation

◆ file_lines()

const std::vector< std::string > * porytiles::CParserContext::file_lines ( ) const

Returns the file lines held by this context.

Returns
Pointer to the file lines vector

Definition at line 72 of file c_parser_context.cpp.

◆ file_path()

const std::string & porytiles::CParserContext::file_path ( ) const

Returns the file path associated with this context.

Returns
The file path string (empty if no path was provided)

Definition at line 82 of file c_parser_context.cpp.

◆ formatter()

const TextFormatter * porytiles::CParserContext::formatter ( ) const

Returns the text formatter used by this context.

Returns
Pointer to the text formatter

Definition at line 77 of file c_parser_context.cpp.

◆ make_error()

FormattableError porytiles::CParserContext::make_error ( SourcePosition  pos,
const std::string &  message 
) const

Creates a FormattableError with source context.

Creates a multi-line FormattableError including:

  • Header line: "file:line:col: message" (or "line:col: message" if no file path)
  • Source context from FileHighlightPrinter showing surrounding lines
  • Highlighted error line with arrow prefix and styled content
  • Caret indicator pointing to the exact column

If the position is out of bounds (line beyond file, column beyond line), the error is created without source context, showing only the header line with position information.

Parameters
posSource position (1-based line and column)
messageError message describing the problem
Returns
FormattableError with rich source context

Definition at line 29 of file c_parser_context.cpp.


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