Skip to content

Commit

Permalink
Include filepath in lexer and parser error messages (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer authored Nov 11, 2023
1 parent 0055ab4 commit f810298
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion .run/spice.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="test -O0 ../../media/test-project/test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="run -O2 ../../src-bootstrap/main.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<envs>
<env name="SPICE_STD_DIR" value="$PROJECT_DIR$/std" />
</envs>
Expand Down
2 changes: 1 addition & 1 deletion .run/spicetest.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="spicetest" type="CMakeGoogleTestRunConfigurationType" factoryName="Google Test" PROGRAM_PARAMS="--update-refs=true" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spicetest" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spicetest" TEST_CLASS="IRGeneratorTests" TEST_MODE="SUITE_TEST">
<configuration default="false" name="spicetest" type="CMakeGoogleTestRunConfigurationType" factoryName="Google Test" PROGRAM_PARAMS="--update-refs=false" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spicetest" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spicetest" TEST_MODE="SUITE_TEST">
<envs>
<env name="RUN_TESTS" value="ON" />
<env name="SPICE_STD_DIR" value="$PROJECT_DIR$/std" />
Expand Down
2 changes: 0 additions & 2 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ plugins:
- en
- minify:
minify_html: true
- git-revision-date-localized:
enable_creation_date: true
- offline

extra:
Expand Down
4 changes: 2 additions & 2 deletions src/SourceFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<AntlrThrowingErrorListener>(MODE_LEXER);
antlrCtx.parserErrorHandler = std::make_unique<AntlrThrowingErrorListener>(MODE_PARSER);
antlrCtx.lexerErrorHandler = std::make_unique<AntlrThrowingErrorListener>(ThrowingErrorListenerMode::LEXER, filePath);
antlrCtx.parserErrorHandler = std::make_unique<AntlrThrowingErrorListener>(ThrowingErrorListenerMode::PARSER, filePath);

// Tokenize input
antlrCtx.inputStream = std::make_unique<antlr4::ANTLRInputStream>(fileInputStream);
Expand Down
7 changes: 4 additions & 3 deletions src/exception/AntlrThrowingErrorListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 15 additions & 3 deletions src/exception/AntlrThrowingErrorListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@

#pragma once

#include <filesystem>
#include <utility>

#include <BaseErrorListener.h>

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
2 changes: 1 addition & 1 deletion test/test-files/lexer/error-lexing-error/exception.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[Error|Lexer] 1:0: Tokenizing failed: token recognition error at: '"test'
[Error|Lexer] ./test-files/lexer/error-lexing-error/source.spice:1:0: Tokenizing failed: token recognition error at: '"test'
2 changes: 1 addition & 1 deletion test/test-files/parser/error-parsing-error/exception.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[Error|Parser] 1:1: Parsing failed: no viable alternative at input 'f'
[Error|Parser] ./test-files/parser/error-parsing-error/source.spice:1:1: Parsing failed: no viable alternative at input 'f'

0 comments on commit f810298

Please sign in to comment.