Commit f5314807 authored by Simon Zünd's avatar Simon Zünd Committed by Commit Bot

[torque] Include file name in lexer and parser errors.

This CL replaces the default ConsoleErrorListener with a custom one.
The only difference is that the error message now also includes
the file name where the lexer/parser error happened.

R=tebbi@chromium.org

Change-Id: Ifa22501a55066b82b32234c76df180db41ee8b62
Reviewed-on: https://chromium-review.googlesource.com/1069137
Commit-Queue: Simon Zünd <szuend@google.com>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53309}
parent 1d682e6c
......@@ -38,6 +38,19 @@ class FailedParseErrorStrategy : public antlr4::DefaultErrorStrategy {
bool failed_;
};
class TorqueErrorListener : public antlr4::BaseErrorListener {
public:
TorqueErrorListener() : BaseErrorListener() {}
void syntaxError(antlr4::Recognizer* recognizer,
antlr4::Token* /*offendingSymbol*/, size_t line,
size_t charPositionInLine, const std::string& msg,
std::exception_ptr /*e*/) {
std::cerr << recognizer->getInputStream()->getSourceName() << ": " << line
<< ":" << charPositionInLine << " " << msg << "\n";
}
};
int WrappedMain(int argc, const char** argv) {
std::string output_directory;
std::vector<SourceFileContext> file_contexts;
......@@ -45,6 +58,7 @@ int WrappedMain(int argc, const char** argv) {
SourceFileContext context;
size_t lexer_errors = 0;
auto error_strategy = std::make_shared<FailedParseErrorStrategy>();
TorqueErrorListener error_listener;
bool verbose = false;
SourceFileMap::Scope scope;
for (int i = 1; i < argc; ++i) {
......@@ -66,6 +80,8 @@ int WrappedMain(int argc, const char** argv) {
new antlr4::ANTLRFileStream(context.name.c_str()));
context.lexer =
std::unique_ptr<TorqueLexer>(new TorqueLexer(context.stream.get()));
context.lexer->removeErrorListeners();
context.lexer->addErrorListener(&error_listener);
context.tokens = std::unique_ptr<antlr4::CommonTokenStream>(
new antlr4::CommonTokenStream(context.lexer.get()));
context.tokens->fill();
......@@ -73,6 +89,8 @@ int WrappedMain(int argc, const char** argv) {
context.parser =
std::unique_ptr<TorqueParser>(new TorqueParser(context.tokens.get()));
context.parser->setErrorHandler(error_strategy);
context.parser->removeErrorListeners();
context.parser->addErrorListener(&error_listener);
context.file = context.parser->file();
ast_generator.visitSourceFile(&context);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment