added some sort of error generation when parsing

errors occur
This commit is contained in:
Karma Riuk
2025-07-07 15:02:06 +02:00
parent bbac513aa9
commit 132dc65240
4 changed files with 90 additions and 31 deletions

25
src/ast/errors/error.hpp Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#include "token/type.hpp"
namespace ast::error {
struct error : public std::runtime_error {
explicit error(const std::string& message)
: std::runtime_error(message) {}
};
struct parser_error : error {
explicit parser_error(const std::string& message): error(message) {}
};
struct expected_next : parser_error {
token::type expected_type;
explicit expected_next(
token::type expected_type, const std::string& message
)
: parser_error(message),
expected_type(expected_type) {}
};
} // namespace ast::error

View File

@@ -1,5 +1,6 @@
#include "parser.hpp"
#include "ast/errors/error.hpp"
#include "token/type.hpp"
#include <sstream>
@@ -27,7 +28,6 @@ namespace parser {
p->statements.push_back(stmt);
}
return p;
}
@@ -45,6 +45,7 @@ namespace parser {
next_token();
return true;
}
next_error(t);
return false;
}
@@ -73,6 +74,11 @@ namespace parser {
std::stringstream ss;
ss << "Expected next token to be " << t << " but instead got "
<< next.type;
errors.push_back(ss.str());
errors.push_back(new ast::error::expected_next(t, ss.str()));
}
parser::~parser() {
for (const auto& e : errors)
delete e;
}
} // namespace parser

View File

@@ -1,6 +1,7 @@
#pragma once
#include "ast/ast.hpp"
#include "ast/errors/error.hpp"
#include "ast/statements/let.hpp"
#include "lexer/lexer.hpp"
#include "token/token.hpp"
@@ -8,7 +9,8 @@
namespace parser {
struct parser {
parser(lexer::lexer& lexer);
std::vector<std::string> errors;
~parser();
std::vector<ast::error::error*> errors;
ast::program* parse_program();