Added builder error

This commit is contained in:
Rafał Grodziński
2025-07-05 21:58:11 +09:00
parent 1e7ecaf801
commit 48f27169d0
6 changed files with 38 additions and 10 deletions

View File

@@ -1,5 +1,6 @@
#include "ModuleBuilder.h"
#include "Error.h"
#include "Parser/ValueType.h"
#include "Parser/Expression/ExpressionGrouping.h"
@@ -474,3 +475,7 @@ void ModuleBuilder::failWithMessage(string message) {
cerr << "Error! Building module \"" << moduleName << "\" from \"" + sourceFileName + "\" failed:" << endl << message << endl;
exit(1);
}
void ModuleBuilder::markError(int line, int column, string message) {
errors.push_back(Error::builderError(line, column, message));
}

View File

@@ -11,6 +11,7 @@
#include <llvm/Support/raw_ostream.h>
#include <llvm/IR/Verifier.h>
class Error;
class ValueType;
class Expression;
@@ -41,6 +42,7 @@ typedef struct {
class ModuleBuilder {
private:
vector<shared_ptr<Error>> errors;
string moduleName;
string sourceFileName;
@@ -86,6 +88,8 @@ private:
llvm::Type *typeForValueType(shared_ptr<ValueType> valueType);
void failWithMessage(string message);
void markError(int line, int column, string message);
public:
ModuleBuilder(string moduleName, string sourceFileName, vector<shared_ptr<Statement>> statements);
shared_ptr<llvm::Module> getModule();

View File

@@ -1,11 +1,19 @@
#include "Error.h"
shared_ptr<Error> Error::builderError(int line, int column, string message) {
Error e = Error(ErrorKind::BUILDER_ERROR, line, column, {}, nullptr, {}, message);
return nullptr;
}
Error::Error(int line, int column, string lexme) :
kind(ErrorKind::LEXER_ERROR), line(line), column(column), lexme(lexme) { }
Error::Error(shared_ptr<Token> actualToken, optional<TokenKind> expectedTokenKind, optional<string> message) :
kind(ErrorKind::PARSER_ERROR), actualToken(actualToken), expectedTokenKind(expectedTokenKind), message(message) { }
Error::Error(ErrorKind kind, int line, int column, optional<string> lexme, shared_ptr<Token> actualToken, optional<TokenKind> expectedTokenKind, optional<string> message):
kind(kind), line(line), column(column), lexme(lexme), actualToken(actualToken), expectedTokenKind(expectedTokenKind), message(message) { }
ErrorKind Error::getKind() {
return kind;
}
@@ -18,7 +26,7 @@ int Error::getColumn() {
return column;
}
string Error::getLexme() {
optional<string> Error::getLexme() {
return lexme;
}

View File

@@ -10,7 +10,8 @@ using namespace std;
enum class ErrorKind {
LEXER_ERROR,
PARSER_ERROR
PARSER_ERROR,
BUILDER_ERROR
};
class Error {
@@ -19,21 +20,24 @@ private:
int line;
int column;
string lexme;
optional<string> lexme;
shared_ptr<Token> actualToken;
optional<TokenKind> expectedTokenKind;
optional<string> message;
public:
static shared_ptr<Error> builderError(int line, int column, string message);
Error(int line, int column, string lexme);
Error(shared_ptr<Token> actualToken, optional<TokenKind> expectedTokenKind, optional<string> message);
Error(ErrorKind kind, int line, int column, optional<string> lexme, shared_ptr<Token> actualToken, optional<TokenKind> expectedTokenKind, optional<string> message);
ErrorKind getKind();
int getLine();
int getColumn();
string getLexme();
optional<string> getLexme();
shared_ptr<Token> getActualToken();
optional<TokenKind> getExpectedTokenKind();

View File

@@ -2,6 +2,8 @@
#include <iostream>
#include "Error.h"
#include "Lexer/Token.h"
#include "Parser/ValueType.h"
@@ -24,8 +26,6 @@
#include "Parser/Expression/ExpressionCall.h"
#include "Parser/Expression/ExpressionBlock.h"
#include "Error.h"
string Logger::toString(shared_ptr<Token> token) {
switch (token->getKind()) {
case TokenKind::PLUS:
@@ -421,10 +421,12 @@ void Logger::print(vector<shared_ptr<Statement>> statements) {
void Logger::print(shared_ptr<Error> error) {
string message;
switch (error->getKind()) {
case ErrorKind::LEXER_ERROR:
message = format("Unexpected token \"{}\" at line: {}, column: {}", error->getLexme(), error->getLine() + 1, error->getColumn() + 1);
case ErrorKind::LEXER_ERROR: {
string lexme = error->getLexme() ? *(error->getLexme()) : "";
message = format("Unexpected token \"{}\" at line: {}, column: {}", lexme, error->getLine() + 1, error->getColumn() + 1);
break;
case ErrorKind::PARSER_ERROR:
}
case ErrorKind::PARSER_ERROR: {
shared_ptr<Token> token = error->getActualToken();
optional<TokenKind> expectedTokenKind = error->getExpectedTokenKind();
optional<string> errorMessage = error->getMessage();
@@ -443,6 +445,11 @@ void Logger::print(shared_ptr<Error> error) {
if (errorMessage)
message += format(". {}", *errorMessage);
break;
}
case ErrorKind::BUILDER_ERROR: {
message = "";
break;
}
}
cout << message << endl;
}

View File

@@ -14,9 +14,9 @@ using namespace std;
class Parser {
private:
vector<shared_ptr<Error>> errors;
vector<shared_ptr<Token>> tokens;
int currentIndex = 0;
vector<shared_ptr<Error>> errors;
shared_ptr<Statement> nextStatement();
shared_ptr<Statement> nextInBlockStatement();