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