ArkScript
A small, lisp-inspired, functional scripting language
Diagnostics.hpp
Go to the documentation of this file.
1/**
2 * @file Diagnostics.hpp
3 * @author Lex 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
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
30 [[nodiscard]] bool wholeLineIsError() const
31 {
32 return !end.has_value();
33 }
34
35 [[nodiscard]] std::optional<decltype(internal::FilePos::line)> maybeEndLine() const noexcept
36 {
37 if (end)
38 return end->line;
39 return std::nullopt;
40 }
41 };
42
43 /**
44 * @brief Helper to create a colorized context to report errors to the user
45 *
46 * @param loc error location
47 * @param os stream in which the error will be written
48 * @param maybe_context optional context, parent of the error
49 * @param colorize generate colors or not
50 */
52 const ErrorLocation& loc,
53 std::ostream& os,
54 const std::optional<CodeErrorContext>& maybe_context,
55 bool colorize);
56
57 /**
58 * @brief Helper used by the compiler to generate a colorized context from a node
59 *
60 * @param message error message to be included in the context
61 * @param node AST node with the error
62 * @return std::string
63 */
64 std::string makeContextWithNode(const std::string& message, const internal::Node& node);
65
66 /**
67 * @brief Generate a diagnostic from an error and print it to the standard output
68 *
69 * @param e code error
70 * @param os output stream
71 * @param colorize generate colors or not
72 */
73 ARK_API void generate(const CodeError& e, std::ostream& os = std::cout, bool colorize = true);
74}
75
76#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 generate(const CodeError &e, std::ostream &os=std::cout, bool colorize=true)
Generate a diagnostic from an error and print it to the standard output.
std::string makeContextWithNode(const std::string &message, const internal::Node &node)
Helper used by the compiler to generate a colorized context from a node.
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< decltype(internal::FilePos::line)> maybeEndLine() const noexcept
std::size_t line
0-indexed line number
Definition Position.hpp:22