Bash++
Bash++ compiler internal documentation
SyntaxError.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <string>
10#include <memory>
11#include <cstdint>
12#include <stdexcept>
13#include <bpp_include/bpp.h>
14#include <error/detail.h>
15#include <error/ParserError.h>
16
17namespace bpp::ErrorHandling {
18
30 const std::string& source_file,
31 uint32_t line,
32 uint32_t column,
33 uint32_t text_length,
34 const std::string& msg,
35 const std::vector<std::string>& include_chain,
36 std::shared_ptr<bpp::bpp_program> program,
37 bool lsp_mode,
38 bool is_warning = false);
39
41 const std::vector<AST::ParserError>& errors,
42 const std::string& source_file,
43 const std::vector<std::string>& include_chain,
44 std::shared_ptr<bpp::bpp_program> program,
45 bool lsp_mode
46);
47
48class ErrorOrWarning : public std::runtime_error {
49 protected:
50 std::string source_file;
51 uint32_t line = 0;
52 uint32_t column = 0;
53 uint32_t text_length = 0;
54 std::vector<std::string> include_chain;
55 std::shared_ptr<bpp::bpp_program> program;
56 bool lsp_mode = false;
57 bool is_warning = false;
58
59 template <bpp::detail::ErrorReportableListener Listener, bpp::detail::ASTNodePtrORToken T>
60 void set_from_listener(Listener* listener, T error_ctx) {
61 source_file = listener->get_source_file();
62 include_chain = listener->get_include_stack();
63 program = listener->get_program();
64 lsp_mode = listener->get_lsp_mode();
65
66 text_length = 1;
67 if constexpr (bpp::detail::ASTNodePtrType<T>) {
68 line = error_ctx->getPosition().line;
69 column = error_ctx->getPosition().column;
70 if (error_ctx->getEndPosition().line == error_ctx->getPosition().line) {
71 text_length = error_ctx->getEndPosition().column - error_ctx->getPosition().column;
72 }
73 } else if constexpr(bpp::detail::ASTStringToken<T>) {
74 line = error_ctx.getLine();
75 column = error_ctx.getCharPositionInLine();
76 text_length = static_cast<uint32_t>(error_ctx.getValue().length());
77 } else if constexpr(bpp::detail::ASTParameterToken<T>) {
78 // Special case: Error reporting on a declared method parameter
79 // TODO(@rail5): Kind of hacky to handle special cases. Would prefer a general solution.
80 line = error_ctx.getLine();
81 column = error_ctx.getCharPositionInLine();
82 auto param = error_ctx.getValue();
83 text_length = 0;
84 if (param.type.has_value()) {
85 text_length += 1 + static_cast<uint32_t>(param.type.value().getValue().length()); // '@Type'
86 if (param.pointer) text_length += 1; // '*'
87 text_length += 1 + static_cast<uint32_t>(param.name.getValue().length()); // ' Name'
88 } else {
89 text_length = static_cast<uint32_t>(param.name.getValue().length()); // 'Name'
90 }
91 }
92 }
93 public:
94 ErrorOrWarning() = delete;
95 explicit ErrorOrWarning(const std::string& msg) = delete;
96
97 template <bpp::detail::ErrorReportableListener Listener, bpp::detail::ASTNodePtrORToken T>
98 ErrorOrWarning(Listener* listener, const T& error_ctx, const std::string& msg) : std::runtime_error(msg) {
99 set_from_listener(listener, error_ctx);
100 }
101
102 void print() const {
105 line,
106 column,
108 this->what(),
110 program,
111 lsp_mode,
113 );
114 }
115};
116
126 public:
127 SyntaxError() = delete;
128 explicit SyntaxError(const std::string& msg) = delete;
129
130 template <bpp::detail::ErrorReportableListener Listener, bpp::detail::ASTNodePtrORToken T>
131 SyntaxError(Listener* listener, const T& error_ctx, const std::string& msg) : ErrorOrWarning(listener, error_ctx, msg) {
132 is_warning = false;
133 }
134};
135
145class Warning : public ErrorOrWarning {
146 public:
147 Warning() = delete;
148 explicit Warning(const std::string& msg) = delete;
149
150 template <bpp::detail::ErrorReportableListener Listener, bpp::detail::ASTNodePtrORToken T>
151 Warning(Listener* listener, const T& error_ctx, const std::string& msg) : ErrorOrWarning(listener, error_ctx, msg) {
152 is_warning = true;
153 }
154};
155
156// Helper functions
157// Should probably be moved to a separate file or somehow better organized
158std::string utf8_substr(const std::string& str, uint32_t start, uint32_t length);
159uint32_t utf8_length(const std::string& str);
160std::string equal_width_padding(const std::string& str, char padding_char = ' ');
161
162} // namespace bpp::ErrorHandling
Definition SyntaxError.h:48
uint32_t text_length
Definition SyntaxError.h:53
uint32_t line
Definition SyntaxError.h:51
bool lsp_mode
Definition SyntaxError.h:56
ErrorOrWarning(const std::string &msg)=delete
void set_from_listener(Listener *listener, T error_ctx)
Definition SyntaxError.h:60
std::shared_ptr< bpp::bpp_program > program
Definition SyntaxError.h:55
ErrorOrWarning(Listener *listener, const T &error_ctx, const std::string &msg)
Definition SyntaxError.h:98
uint32_t column
Definition SyntaxError.h:52
bool is_warning
Definition SyntaxError.h:57
void print() const
Definition SyntaxError.h:102
std::vector< std::string > include_chain
Definition SyntaxError.h:54
std::string source_file
Definition SyntaxError.h:50
An exception thrown when a syntax error is encountered This exception can be constructed from any lis...
Definition SyntaxError.h:125
SyntaxError(Listener *listener, const T &error_ctx, const std::string &msg)
Definition SyntaxError.h:131
SyntaxError(const std::string &msg)=delete
A compiler warning that is not fatal to compilation This type should never be thrown....
Definition SyntaxError.h:145
Warning(const std::string &msg)=delete
Warning(Listener *listener, const T &error_ctx, const std::string &msg)
Definition SyntaxError.h:151
Definition detail.h:18
Definition detail.h:25
Definition detail.h:22
Definition InternalError.h:11
uint32_t utf8_length(const std::string &str)
Definition SyntaxError.cpp:173
std::string equal_width_padding(const std::string &str, char padding_char)
Definition SyntaxError.cpp:187
void print_parser_errors(const std::vector< AST::ParserError > &errors, const std::string &source_file, const std::vector< std::string > &include_chain, std::shared_ptr< bpp::bpp_program > program, bool lsp_mode)
Definition SyntaxError.cpp:117
void print_syntax_error_or_warning(const std::string &source_file, uint32_t line, uint32_t column, uint32_t text_length, const std::string &msg, const std::vector< std::string > &include_chain, std::shared_ptr< bpp::bpp_program > program, bool lsp_mode, bool is_warning)
Print a syntax error or warning message to stderr.
Definition SyntaxError.cpp:22
std::string utf8_substr(const std::string &str, uint32_t start, uint32_t length)
Definition SyntaxError.cpp:144