2023-01-05 20:12:15 +00:00
|
|
|
#include <error.hh>
|
2023-01-07 07:54:53 +00:00
|
|
|
#include <exception>
|
2023-01-05 20:12:15 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace xlang {
|
|
|
|
|
2023-01-07 07:54:53 +00:00
|
|
|
void ErrorListener::printError(size_t line, size_t charPositionInLine,
|
|
|
|
std::string_view msg) {
|
|
|
|
std::cerr << file << ":" << line << ":" << charPositionInLine + 1 << ": "
|
|
|
|
<< msg << std::endl;
|
|
|
|
has_error = true;
|
|
|
|
}
|
|
|
|
|
2023-01-05 20:12:15 +00:00
|
|
|
ErrorListener::ErrorListener(std::string_view inputfile)
|
|
|
|
: file{inputfile}, has_error{false} {}
|
|
|
|
|
|
|
|
bool ErrorListener::hasError() {
|
|
|
|
return has_error;
|
|
|
|
}
|
|
|
|
|
2023-01-07 07:54:53 +00:00
|
|
|
void ErrorListener::compilerError(const std::string &file, size_t line) {
|
|
|
|
std::cerr << "compiler bug in " << file << ":" << line << std::endl;
|
|
|
|
std::terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ErrorListener::duplicateFunction(antlr4::Token *token,
|
|
|
|
const std::string &name) {
|
|
|
|
printError(token->getLine(), token->getCharPositionInLine(),
|
|
|
|
"duplicate function '" + name + "'");
|
|
|
|
}
|
|
|
|
|
2023-01-07 08:56:27 +00:00
|
|
|
void ErrorListener::loopControlWithoutLoop(antlr4::Token *token) {
|
|
|
|
printError(token->getLine(), token->getCharPositionInLine(),
|
|
|
|
"loop control statement without loop");
|
|
|
|
}
|
|
|
|
|
2023-01-07 07:54:53 +00:00
|
|
|
void ErrorListener::shadowedVariable(antlr4::Token *token,
|
|
|
|
const std::string &name) {
|
|
|
|
printError(token->getLine(), token->getCharPositionInLine(),
|
|
|
|
"definition of variable '" + name +
|
|
|
|
"' shadows previously defined variable");
|
|
|
|
}
|
|
|
|
|
2023-01-05 20:12:15 +00:00
|
|
|
void ErrorListener::syntaxError([[maybe_unused]] antlr4::Recognizer *recognizer,
|
|
|
|
[[maybe_unused]] antlr4::Token *offendingSymbol,
|
|
|
|
size_t line, size_t charPositionInLine,
|
|
|
|
const std::string &msg,
|
|
|
|
[[maybe_unused]] std::exception_ptr e) {
|
2023-01-07 07:54:53 +00:00
|
|
|
printError(line, charPositionInLine, msg);
|
2023-01-06 02:56:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 07:54:53 +00:00
|
|
|
void ErrorListener::typeMismatch(antlr4::Token *token, Type expected,
|
|
|
|
Type actual) {
|
|
|
|
printError(token->getLine(), token->getCharPositionInLine(),
|
|
|
|
"expected type '" + typeToString(expected) + "', but got '" +
|
|
|
|
typeToString(actual) + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ErrorListener::unknownFunction(antlr4::Token *token,
|
|
|
|
const std::string &name) {
|
|
|
|
printError(token->getLine(), token->getCharPositionInLine(),
|
|
|
|
"unknown function '" + name + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ErrorListener::unknownVariable(antlr4::Token *token,
|
|
|
|
const std::string &name) {
|
|
|
|
printError(token->getLine(), token->getCharPositionInLine(),
|
|
|
|
"unknown variable '" + name + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ErrorListener::wrongArgumentNumber(antlr4::Token *token,
|
|
|
|
const std::string &name,
|
|
|
|
size_t expected, size_t actual) {
|
|
|
|
printError(token->getLine(), token->getCharPositionInLine(),
|
|
|
|
"function '" + name + "' expects " + std::to_string(expected) +
|
|
|
|
" arguments, but got " + std::to_string(actual));
|
2023-01-05 20:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace xlang
|