13 void makeContext(std::ostream& os,
const std::string& code, std::size_t line, std::size_t col_start, std::size_t sym_size)
15 os << termcolor::colorize;
18 std::size_t col_end = std::min<std::size_t>(col_start + sym_size, ctx[line].size());
19 std::size_t first = line >= 3 ? line - 3 : 0;
20 std::size_t last = (line + 3) <= ctx.size() ? line + 3 : ctx.size();
23 for (std::size_t loop = first; loop < last; ++loop)
25 std::string current_line =
colorizeLine(ctx[loop], line_color_context_counts);
26 os << termcolor::green << std::setw(5) << (loop + 1) << termcolor::reset <<
" | " << current_line <<
"\n";
33 for (std::size_t i = 0; i < col_start; ++i)
38 for (std::size_t i = col_start; i < col_end; ++i)
41 os << termcolor::reset <<
"\n";
48 constexpr std::array<std::ostream& (*)(std::ostream & stream), 3> pairing_color {
49 termcolor::bright_blue,
50 termcolor::bright_green,
51 termcolor::bright_yellow
53 std::size_t pairing_color_size = pairing_color.size();
55 std::stringstream colorized_line;
56 colorized_line << termcolor::colorize;
58 for (
const char& c : line)
62 std::size_t pairing_color_index = 0;
67 pairing_color_index = std::abs(line_color_context_counts.
open_parentheses) % pairing_color_size;
72 pairing_color_index = std::abs(line_color_context_counts.
open_parentheses) % pairing_color_size;
75 pairing_color_index = std::abs(line_color_context_counts.
open_square_braces) % pairing_color_size;
80 pairing_color_index = std::abs(line_color_context_counts.
open_square_braces) % pairing_color_size;
83 pairing_color_index = std::abs(line_color_context_counts.
open_curly_braces) % pairing_color_size;
88 pairing_color_index = std::abs(line_color_context_counts.
open_curly_braces) % pairing_color_size;
92 colorized_line << pairing_color[pairing_color_index] << c << termcolor::reset;
98 return colorized_line.str();
103 std::stringstream ss;
104 ss << message <<
"\n\n";
106 ss <<
"In file " << node.
filename() <<
"\n";
107 ss <<
"On line " << (node.
line() + 1) <<
":" << node.
col() <<
", got `" << node <<
"'\n";
109 std::size_t ssize = 1;
111 ssize = node.
string().size();
121 std::stringstream ss;
122 ss <<
"On line " << (line + 1) <<
":" << col <<
"\n";
Constants used by ArkScript.
Lots of utilities about the filesystem.
Lots of utilities about string, filesystem and more.
A node of an Abstract Syntax Tree for ArkScript.
NodeType nodeType() const noexcept
Return the node type.
const std::string & filename() const noexcept
Return the filename in which this node was created.
const std::string & string() const noexcept
Return the string held by the value (if the node type allows it)
std::size_t col() const noexcept
Get the column at which this node was created.
std::size_t line() const noexcept
Get the line at which this node was created.
Create string error context for AST errors.
std::string readFile(const std::string &name)
Helper to read a file.
std::vector< std::string > splitString(const std::string &source, char sep)
Cut a string into pieces, given a character separator.
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.
void makeContext(std::ostream &os, const std::string &code, std::size_t line, std::size_t col_start, std::size_t sym_size)
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.