fail on parsing errors

development
Thomas Lindner 2023-01-05 21:12:15 +01:00
parent d46f7fb91f
commit 2d0c688aa0
5 changed files with 55 additions and 0 deletions

View File

@ -81,6 +81,8 @@ IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<BaseErrorListener\.h>$'
Priority: 3
- Regex: '^<xlang.*\.h>$'
Priority: 3
- Regex: '^<sys/.*\.h>$'

23
bootstrap/error.cc Normal file
View File

@ -0,0 +1,23 @@
#include <error.hh>
#include <iostream>
namespace xlang {
ErrorListener::ErrorListener(std::string_view inputfile)
: file{inputfile}, has_error{false} {}
bool ErrorListener::hasError() {
return has_error;
}
void ErrorListener::syntaxError([[maybe_unused]] antlr4::Recognizer *recognizer,
[[maybe_unused]] antlr4::Token *offendingSymbol,
size_t line, size_t charPositionInLine,
const std::string &msg,
[[maybe_unused]] std::exception_ptr e) {
std::cerr << file << ":" << line << ":" << charPositionInLine << ": " << msg
<< std::endl;
has_error = true;
}
} // namespace xlang

21
bootstrap/error.hh Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include <BaseErrorListener.h>
#include <string_view>
namespace xlang {
class ErrorListener : public antlr4::BaseErrorListener {
std::string_view file;
bool has_error;
public:
ErrorListener(std::string_view inputfile);
bool hasError();
void syntaxError(antlr4::Recognizer *recognizer,
antlr4::Token *offendingSymbol, size_t line,
size_t charPositionInLine, const std::string &msg,
std::exception_ptr e) override;
};
} // namespace xlang

View File

@ -2,6 +2,7 @@
#include <unistd.h>
#include <emit.hh>
#include <error.hh>
#include <iostream>
#include <string>
#include <xlangLexer.h>
@ -41,7 +42,14 @@ int main(int argc, char **argv) {
xlang::xlangLexer lexer{&input};
antlr4::CommonTokenStream tokens{&lexer};
xlang::xlangParser parser{&tokens};
xlang::ErrorListener errorlistener{inputfile};
parser.removeErrorListeners();
parser.addErrorListener(&errorlistener);
auto *tree = parser.file();
if (errorlistener.hasError()) {
exit(1);
}
xlang::EmitVisitor emit{outputfile};
emit.visitFile(tree);

View File

@ -2,6 +2,7 @@ xc_exe = executable('xc',
sources : [
'main.cc',
'emit.cc',
'error.cc',
antlr4.process('xlang.g4'),
],
dependencies : [