fix(frontend): 补充不支持语法的警告输出
This commit is contained in:
@@ -4,15 +4,30 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include "SysYLexer.h"
|
||||
#include "SysYParser.h"
|
||||
#include "antlr4-runtime.h"
|
||||
|
||||
namespace {
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
AntlrResult ParseFileWithAntlr(const std::string& path) {
|
||||
std::ifstream fin(path);
|
||||
if (!fin.is_open()) {
|
||||
throw std::runtime_error("无法打开输入文件: " + path);
|
||||
throw std::runtime_error("[parse] 无法打开输入文件: " + path);
|
||||
}
|
||||
std::ostringstream ss;
|
||||
ss << fin.rdbuf();
|
||||
@@ -21,8 +36,29 @@ AntlrResult ParseFileWithAntlr(const std::string& path) {
|
||||
auto lexer = std::make_unique<SysYLexer>(input.get());
|
||||
auto tokens = std::make_unique<antlr4::CommonTokenStream>(lexer.get());
|
||||
auto parser = std::make_unique<SysYParser>(tokens.get());
|
||||
|
||||
ParseErrorListener error_listener;
|
||||
lexer->removeErrorListeners();
|
||||
lexer->addErrorListener(&error_listener);
|
||||
parser->removeErrorListeners();
|
||||
auto tree = parser->compUnit();
|
||||
parser->addErrorListener(&error_listener);
|
||||
parser->setErrorHandler(std::make_shared<antlr4::BailErrorStrategy>());
|
||||
antlr4::tree::ParseTree* tree = nullptr;
|
||||
try {
|
||||
tree = parser->compUnit();
|
||||
} catch (const std::exception& ex) {
|
||||
const std::string msg = ex.what();
|
||||
if (!msg.empty()) {
|
||||
throw std::runtime_error("[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("[parse] 暂不支持的语法/词法");
|
||||
}
|
||||
|
||||
AntlrResult result;
|
||||
result.input = std::move(input);
|
||||
|
||||
Reference in New Issue
Block a user