From 5ef1e1917e036e6753e4b4a1daa74546efb9d423 Mon Sep 17 00:00:00 2001 From: Marc Auberer Date: Sun, 12 Nov 2023 00:24:33 +0100 Subject: [PATCH 1/2] Include filepath to lexer and parser error messages --- .github/workflows/publish.yml | 4 ++-- .run/spice.run.xml | 2 +- .run/spicetest.run.xml | 2 +- docs/mkdocs.yml | 2 -- src/SourceFile.cpp | 4 ++-- src/exception/AntlrThrowingErrorListener.cpp | 7 ++++--- src/exception/AntlrThrowingErrorListener.h | 18 +++++++++++++++--- 7 files changed, 25 insertions(+), 14 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index fd8846c74..5de8ef6fb 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -245,7 +245,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: '1.21' + go-version: '1.22' - name: Restore Go modules cache uses: actions/cache@v3 @@ -283,7 +283,7 @@ jobs: - name: Run GoReleaser uses: goreleaser/goreleaser-action@v5 with: - version: v1.21.2 + version: v1.22.1 args: release --clean env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.run/spice.run.xml b/.run/spice.run.xml index 003094b8a..4fd067aa3 100644 --- a/.run/spice.run.xml +++ b/.run/spice.run.xml @@ -1,5 +1,5 @@ - + diff --git a/.run/spicetest.run.xml b/.run/spicetest.run.xml index 8a437d5c1..2b9329852 100644 --- a/.run/spicetest.run.xml +++ b/.run/spicetest.run.xml @@ -1,5 +1,5 @@ - + diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 8c58566d3..a8ff60ba5 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -52,8 +52,6 @@ plugins: - en - minify: minify_html: true - - git-revision-date-localized: - enable_creation_date: true - offline extra: diff --git a/src/SourceFile.cpp b/src/SourceFile.cpp index 2d2aaf7dc..1cdb90f4d 100644 --- a/src/SourceFile.cpp +++ b/src/SourceFile.cpp @@ -52,8 +52,8 @@ void SourceFile::runLexer() { throw CompilerError(SOURCE_FILE_NOT_FOUND, "Source file at path '" + filePath.string() + "' does not exist."); // Create error handlers for lexer and parser - antlrCtx.lexerErrorHandler = std::make_unique(MODE_LEXER); - antlrCtx.parserErrorHandler = std::make_unique(MODE_PARSER); + antlrCtx.lexerErrorHandler = std::make_unique(ThrowingErrorListenerMode::LEXER, filePath); + antlrCtx.parserErrorHandler = std::make_unique(ThrowingErrorListenerMode::PARSER, filePath); // Tokenize input antlrCtx.inputStream = std::make_unique(fileInputStream); diff --git a/src/exception/AntlrThrowingErrorListener.cpp b/src/exception/AntlrThrowingErrorListener.cpp index e8634fe6b..ecf130111 100644 --- a/src/exception/AntlrThrowingErrorListener.cpp +++ b/src/exception/AntlrThrowingErrorListener.cpp @@ -10,10 +10,11 @@ namespace spice::compiler { void AntlrThrowingErrorListener::syntaxError(antlr4::Recognizer *recognizer, antlr4::Token *offendingSymbol, size_t line, size_t charPositionInLine, const std::string &msg, std::exception_ptr e) { - if (mode == MODE_LEXER) - throw LexerError(CodeLoc(line, charPositionInLine), TOKENIZING_FAILED, msg); + const CodeLoc codeLoc(line, charPositionInLine, filePath); + if (mode == ThrowingErrorListenerMode::LEXER) + throw LexerError(codeLoc, TOKENIZING_FAILED, msg); else - throw ParserError(CodeLoc(line, charPositionInLine), PARSING_FAILED, msg); + throw ParserError(codeLoc, PARSING_FAILED, msg); } } // namespace spice::compiler \ No newline at end of file diff --git a/src/exception/AntlrThrowingErrorListener.h b/src/exception/AntlrThrowingErrorListener.h index 8739ee4f0..3e6affa9d 100644 --- a/src/exception/AntlrThrowingErrorListener.h +++ b/src/exception/AntlrThrowingErrorListener.h @@ -2,20 +2,32 @@ #pragma once +#include +#include + #include namespace spice::compiler { -enum Mode { MODE_LEXER, MODE_PARSER }; +enum class ThrowingErrorListenerMode { + LEXER, + PARSER, +}; class AntlrThrowingErrorListener : public antlr4::BaseErrorListener { public: - explicit AntlrThrowingErrorListener(Mode mode) : mode(mode){}; // GCOV_EXCL_LINE + // Constructors + AntlrThrowingErrorListener(ThrowingErrorListenerMode mode, std::filesystem::path filePath) + : mode(mode), filePath(std::move(filePath)){}; + + // Public methods void syntaxError(antlr4::Recognizer *recognizer, antlr4::Token *offendingSymbol, size_t line, size_t charPositionInLine, const std::string &msg, std::exception_ptr e) override; private: - Mode mode; + // Private members + ThrowingErrorListenerMode mode; + std::filesystem::path filePath; }; } // namespace spice::compiler \ No newline at end of file From 0c803417e0ce86ea241bcd37f694ffb93cc7ef91 Mon Sep 17 00:00:00 2001 From: Marc Auberer Date: Sun, 12 Nov 2023 00:30:26 +0100 Subject: [PATCH 2/2] Update refs --- test/test-files/lexer/error-lexing-error/exception.out | 2 +- test/test-files/parser/error-parsing-error/exception.out | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test-files/lexer/error-lexing-error/exception.out b/test/test-files/lexer/error-lexing-error/exception.out index 20f2334b0..5937e7751 100644 --- a/test/test-files/lexer/error-lexing-error/exception.out +++ b/test/test-files/lexer/error-lexing-error/exception.out @@ -1 +1 @@ -[Error|Lexer] 1:0: Tokenizing failed: token recognition error at: '"test' \ No newline at end of file +[Error|Lexer] ./test-files/lexer/error-lexing-error/source.spice:1:0: Tokenizing failed: token recognition error at: '"test' \ No newline at end of file diff --git a/test/test-files/parser/error-parsing-error/exception.out b/test/test-files/parser/error-parsing-error/exception.out index 20ca9afda..584778127 100644 --- a/test/test-files/parser/error-parsing-error/exception.out +++ b/test/test-files/parser/error-parsing-error/exception.out @@ -1 +1 @@ -[Error|Parser] 1:1: Parsing failed: no viable alternative at input 'f' \ No newline at end of file +[Error|Parser] ./test-files/parser/error-parsing-error/source.spice:1:1: Parsing failed: no viable alternative at input 'f' \ No newline at end of file