ArkScript
A small, lisp-inspired, functional scripting language
Diagnostics.hpp
Go to the documentation of this file.
1/**
2 * @file Diagnostics.hpp
3 * @author Lexy Plateau (lexplt.dev@gmail.com)
4 * @brief Tools to report code errors nicely to the user
5 * @date 2025-08-16
6 *
7 * @copyright Copyright (c) 2025-2026
8 *
9 */
10
11#ifndef ARK_ERROR_DIAGNOSTICS_HPP
12#define ARK_ERROR_DIAGNOSTICS_HPP
13
14#include <string>
15#include <optional>
16#include <iostream>
17
21
23{
25 {
26 std::string filename; ///< Complete path to the file where the error is
28 std::optional<internal::FilePos> end;
29 std::optional<std::string> maybe_content;
30
31 [[nodiscard]] bool wholeLineIsError() const
32 {
33 return !end.has_value();
34 }
35
36 [[nodiscard]] std::optional<decltype(internal::FilePos::line)> maybeEndLine() const noexcept
37 {
38 if (end)
39 return end->line;
40 return std::nullopt;
41 }
42 };
43
44 /**
45 * @brief Helper to create a colorized context to report errors to the user
46 *
47 * @param loc error location
48 * @param os stream in which the error will be written
49 * @param maybe_context optional context, parent of the error
50 * @param colorize generate colors or not
51 */
53 const ErrorLocation& loc,
54 std::ostream& os,
55 const std::optional<CodeErrorContext>& maybe_context,
56 bool colorize);
57
58 /**
59 * @brief Helper used by the compiler to generate a colorized context from a node
60 *
61 * @param message error message to be included in the context
62 * @param node AST node with the error
63 * @param colorize toggle context colors (default: true)
64 * @return std::string
65 */
66 std::string makeContextWithNode(const std::string& message, const internal::Node& node, bool colorize = true);
67
68 ARK_API void generateWithCode(const CodeError& e, const std::string& code, std::ostream& os = std::cerr, bool colorize = true);
69
70 /**
71 * @brief Generate a diagnostic from an error and print it to the standard error output
72 *
73 * @param e code error
74 * @param os output stream
75 * @param colorize generate colors or not
76 */
77 ARK_API void generate(const CodeError& e, std::ostream& os = std::cerr, bool colorize = true);
78}
79
80#endif // ARK_ERROR_DIAGNOSTICS_HPP
ArkScript homemade exceptions.
#define ARK_API
Definition Module.hpp:22
ArkScript configuration macros.
Defines position utilities (for text in a file) for the parser, formatter, diagnostics.
A node of an Abstract Syntax Tree for ArkScript.
Definition Node.hpp:32
ARK_API void generateWithCode(const CodeError &e, const std::string &code, std::ostream &os=std::cerr, bool colorize=true)
std::string makeContextWithNode(const std::string &message, const internal::Node &node, bool colorize=true)
Helper used by the compiler to generate a colorized context from a node.
ARK_API void generate(const CodeError &e, std::ostream &os=std::cerr, bool colorize=true)
Generate a diagnostic from an error and print it to the standard error output.
ARK_API void makeContext(const ErrorLocation &loc, std::ostream &os, const std::optional< CodeErrorContext > &maybe_context, bool colorize)
Helper to create a colorized context to report errors to the user.
CodeError thrown by the compiler (parser, macro processor, optimizer, and compiler itself)
std::optional< internal::FilePos > end
std::string filename
Complete path to the file where the error is.
std::optional< std::string > maybe_content
std::optional< decltype(internal::FilePos::line)> maybeEndLine() const noexcept
std::size_t line
0-indexed line number
Definition Position.hpp:22