refactor(dev): unify compiler error logging
This commit is contained in:
@@ -9,20 +9,17 @@
|
||||
#include "SysYLexer.h"
|
||||
#include "SysYParser.h"
|
||||
#include "antlr4-runtime.h"
|
||||
#include "utils/Log.h"
|
||||
|
||||
namespace {
|
||||
|
||||
bool HasParsePrefix(const std::string& msg) {
|
||||
return msg.rfind("[parse]", 0) == 0;
|
||||
}
|
||||
|
||||
class ParseErrorListener : public antlr4::BaseErrorListener {
|
||||
public:
|
||||
void syntaxError(antlr4::Recognizer* /*recognizer*/, antlr4::Token* /*offendingSymbol*/,
|
||||
size_t line, size_t charPositionInLine,
|
||||
const std::string& msg, std::exception_ptr /*e*/) override {
|
||||
throw std::runtime_error("[parse] 暂不支持的语法/词法 @" + std::to_string(line) + ":" +
|
||||
std::to_string(charPositionInLine) + " - " + msg);
|
||||
throw std::runtime_error(FormatErrorAt("parse", line, charPositionInLine,
|
||||
"暂不支持的语法/词法 - " + msg));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -31,7 +28,7 @@ class ParseErrorListener : public antlr4::BaseErrorListener {
|
||||
AntlrResult ParseFileWithAntlr(const std::string& path) {
|
||||
std::ifstream fin(path);
|
||||
if (!fin.is_open()) {
|
||||
throw std::runtime_error("[parse] 无法打开输入文件: " + path);
|
||||
throw std::runtime_error(FormatError("parse", "无法打开输入文件: " + path));
|
||||
}
|
||||
std::ostringstream ss;
|
||||
ss << fin.rdbuf();
|
||||
@@ -53,18 +50,18 @@ AntlrResult ParseFileWithAntlr(const std::string& path) {
|
||||
} catch (const std::exception& ex) {
|
||||
const std::string msg = ex.what();
|
||||
if (!msg.empty()) {
|
||||
if (HasParsePrefix(msg)) {
|
||||
if (HasErrorPrefix(msg, "parse")) {
|
||||
throw;
|
||||
}
|
||||
throw std::runtime_error("[parse] 暂不支持的语法/词法 - " + msg);
|
||||
throw std::runtime_error(
|
||||
FormatError("parse", "暂不支持的语法/词法 - " + msg));
|
||||
}
|
||||
if (auto* tok = parser->getCurrentToken()) {
|
||||
throw std::runtime_error("[parse] 暂不支持的语法/词法 @" +
|
||||
std::to_string(tok->getLine()) + ":" +
|
||||
std::to_string(tok->getCharPositionInLine()) +
|
||||
" near token '" + tok->getText() + "'");
|
||||
throw std::runtime_error(
|
||||
FormatErrorAt("parse", tok->getLine(), tok->getCharPositionInLine(),
|
||||
"暂不支持的语法/词法 near token '" + tok->getText() + "'"));
|
||||
}
|
||||
throw std::runtime_error("[parse] 暂不支持的语法/词法");
|
||||
throw std::runtime_error(FormatError("parse", "暂不支持的语法/词法"));
|
||||
}
|
||||
|
||||
AntlrResult result;
|
||||
|
||||
Reference in New Issue
Block a user