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