Porytiles
Loading...
Searching...
No Matches
c_parser_context.cpp
Go to the documentation of this file.
2
3#include <format>
4#include <string>
5#include <vector>
6
7namespace porytiles {
8
9namespace {
10
11std::string format_header(const std::string &file_path, SourcePosition pos, const std::string &message)
12{
13 if (!file_path.empty()) {
14 return std::format("{}:{}:{}: {}", file_path, pos.line, pos.column, message);
15 }
16 return std::format("{}:{}: {}", pos.line, pos.column, message);
17}
18
19} // namespace
20
22 gsl::not_null<const std::vector<std::string> *> file_lines,
23 gsl::not_null<const TextFormatter *> format,
24 std::string file_path)
25 : file_lines_{file_lines.get()}, format_{format.get()}, file_path_{std::move(file_path)}
26{
27}
28
29FormattableError CParserContext::make_error(SourcePosition pos, const std::string &message) const
30{
31 std::vector<std::string> lines;
32
33 // Always add the header line with position
34 lines.push_back(format_header(file_path_, pos, message));
35
36 // Add source context if position is valid
37 if (!file_lines_->empty() && pos.line > 0 && pos.line <= file_lines_->size()) {
38 // Convert 1-based SourcePosition to 0-based indices for FileHighlightPrinter
39 const std::size_t line_idx = pos.line - 1;
40 const std::string &target_line = (*file_lines_)[line_idx];
41
42 const FileHighlightPrinter printer{format_};
43
44 // Determine column index for highlighting
45 if (pos.column > 0 && pos.column <= target_line.size()) {
46 // Valid column position - show column highlight with caret
47 const std::size_t col_idx = pos.column - 1;
48 auto highlight_lines = printer.print(*file_lines_, line_idx, col_idx);
49 for (auto &hl : highlight_lines) {
50 lines.push_back(std::move(hl));
51 }
52 }
53 else if (!target_line.empty()) {
54 // Column out of bounds but line is valid - highlight the line without column caret
55 auto highlight_lines = printer.print(*file_lines_, std::vector{line_idx});
56 for (auto &hl : highlight_lines) {
57 lines.push_back(std::move(hl));
58 }
59 }
60 else {
61 // Empty line - just highlight the line
62 auto highlight_lines = printer.print(*file_lines_, std::vector{line_idx});
63 for (auto &hl : highlight_lines) {
64 lines.push_back(std::move(hl));
65 }
66 }
67 }
68
69 return FormattableError{std::move(lines)};
70}
71
72const std::vector<std::string> *CParserContext::file_lines() const
73{
74 return file_lines_;
75}
76
78{
79 return format_;
80}
81
82const std::string &CParserContext::file_path() const
83{
84 return file_path_;
85}
86
87} // namespace porytiles
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 TextFormatter * formatter() const
Returns the text formatter used by this context.
const std::string & file_path() const
Returns the file path associated with this context.
const std::vector< std::string > * file_lines() const
Returns the file lines held by this context.
A service for printing file lines with highlighted lines and line numbers.
General-purpose error implementation with formatted message support.
Definition error.hpp:63
Abstract base class for applying text styling with context-aware formatting.
Represents a position within source content.
std::size_t line
1-based line number
std::size_t column
1-based column number