ArkScript
A small, fast, functional and scripting language for video games
makeErrorCtx.hpp
Go to the documentation of this file.
1 /**
2  * @file makeErrorCtx.hpp
3  * @author Alexandre Plateau (lexplt.dev@gmail.com)
4  * @brief Create string error context for AST errors
5  * @version 0.2
6  * @date 2022-02-19
7  *
8  * @copyright Copyright (c) 2020-2022
9  *
10  */
11 
12 #ifndef COMPILER_AST_MAKEERRORCTX_HPP
13 #define COMPILER_AST_MAKEERRORCTX_HPP
14 
15 #include <sstream>
16 #include <string>
17 
19 
20 namespace Ark::internal
21 {
23  {
27  };
28 
29  /**
30  * @brief Construct an error message based on a given node
31  * @details It opens the related file at the line and column of the node,
32  * and display context, plus underline the problem with a serie of ^.
33  *
34  * @param message
35  * @param node
36  * @return std::string the complete generated error message
37  */
38  std::string makeNodeBasedErrorCtx(const std::string& message, const Node& node);
39 
40  /**
41  * @brief Construct an error message based on a given match in the code
42  * @details Mostly used by the Lexer and Parser since they don't have Nodes to work on
43  *
44  * @param match the identified token, causing a problem
45  * @param line line of the token
46  * @param col starting column of the token
47  * @param code the whole code of the file
48  * @return std::string the complete generated error message
49  */
50  std::string makeTokenBasedErrorCtx(const std::string& match, std::size_t line, std::size_t col, const std::string& code);
51 
52  /**
53  * @brief Add colors to highlight matching parentheses/curly braces/square braces on a line
54  *
55  * @param line the line of code to colorize
56  * @param line_color_context_counts a LineColorContextCounts to manipulate the running counts of open pairings
57  * @return std::string a colorized line of code
58  */
59  std::string colorizeLine(const std::string& line, LineColorContextCounts& line_color_context_counts);
60 
61  /**
62  * @brief Check if the character passed in can be paired (parentheses, curly braces, or square braces)
63  *
64  * @param c
65  * @return bool
66  */
67  inline bool isPairableChar(const char c)
68  {
69  return c == '(' || c == ')' || c == '[' || c == ']' || c == '{' || c == '}';
70  }
71 }
72 
73 #endif
AST node used by the parser, optimizer and compiler.
A node of an Abstract Syntax Tree for ArkScript.
Definition: Node.hpp:29
bool isPairableChar(const char c)
Check if the character passed in can be paired (parentheses, curly braces, or square braces)
std::string colorizeLine(const std::string &line, LineColorContextCounts &line_color_context_counts)
Add colors to highlight matching parentheses/curly braces/square braces on a line.
std::string makeTokenBasedErrorCtx(const std::string &match, std::size_t line, std::size_t col, const std::string &code)
Construct an error message based on a given match in the code.
std::string makeNodeBasedErrorCtx(const std::string &message, const Node &node)
Construct an error message based on a given node.