Catch parser errors. (#38)

This should resolve:
https://github.com/ArthurSonzogni/Diagon/issues/36
This commit is contained in:
Arthur Sonzogni
2022-05-08 08:35:11 +02:00
committed by GitHub
parent 23d95bb09f
commit ccb028fc8a
7 changed files with 78 additions and 7 deletions
+5
View File
@@ -105,6 +105,11 @@ configure_file(
add_library(diagon_base STATIC
src/translator/Translator.cpp
src/translator/Translator.h
src/translator/antlr_error_listener.cpp
src/translator/antlr_error_listener.h
)
target_link_libraries(diagon_base
PRIVATE antlr4_static
)
target_set_common(diagon_base)
+13
View File
@@ -0,0 +1,13 @@
#include "translator/antlr_error_listener.h"
#include <sstream>
void AntlrErrorListener::syntaxError(antlr4::Recognizer* recognizer,
antlr4::Token* offendingSymbol,
size_t line,
size_t charPositionInLine,
const std::string& msg,
std::exception_ptr e) {
std::stringstream s;
s << "Line(" << line << ":" << charPositionInLine << ") Error(" << msg << ")";
throw std::invalid_argument(s.str());
}
+15
View File
@@ -0,0 +1,15 @@
#ifndef TRANSLATOR_ANTLR_ERROR_LISTENER_HPP
#define TRANSLATOR_ANTLR_ERROR_LISTENER_HPP
#include <antlr4-runtime.h>
class AntlrErrorListener : public antlr4::BaseErrorListener {
void syntaxError(antlr4::Recognizer* recognizer,
antlr4::Token* offendingSymbol,
size_t line,
size_t charPositionInLine,
const std::string& msg,
std::exception_ptr e) final;
};
#endif // TRANSLATOR_ANTLR_ERROR_LISTENER_HPP
+14 -2
View File
@@ -7,8 +7,8 @@
#include <string_view>
#include <vector>
#include "screen/Screen.h"
#include "translator/Translator.h"
#include "translator/antlr_error_listener.h"
#include "translator/flowchart/FlowchartLexer.h"
#include "translator/flowchart/FlowchartParser.h"
#include "util.hpp"
@@ -636,8 +636,20 @@ std::string Flowchart::Translate(const std::string& input,
antlr4::CommonTokenStream tokens(&lexer);
tokens.fill();
// Parser:
FlowchartParser parser(&tokens);
return Parse(parser.program(), true).screen.ToString();
AntlrErrorListener error_listener;
parser.addErrorListener(&error_listener);
FlowchartParser::ProgramContext* context = nullptr;
try {
context = parser.program();
} catch (...) {
return "";
}
return Parse(context, true).screen.ToString();
}
} // namespace
+12 -1
View File
@@ -10,6 +10,7 @@
#include <vector>
#include "screen/Screen.h"
#include "translator/Translator.h"
#include "translator/antlr_error_listener.h"
#include "translator/graph_planar/GraphPlanarLexer.h"
#include "translator/graph_planar/GraphPlanarParser.h"
@@ -201,8 +202,18 @@ void GraphPlanar::Read(const std::string& input) {
antlr4::CommonTokenStream tokens(&lexer);
tokens.fill();
// Parser:
AntlrErrorListener error_listener;
GraphPlanarParser parser(&tokens);
ReadGraph(parser.graph());
parser.addErrorListener(&error_listener);
GraphPlanarParser::GraphContext* context = nullptr;
try {
context = parser.graph();
} catch (...) {
return;
}
ReadGraph(context);
}
void GraphPlanar::ReadGraph(GraphPlanarParser::GraphContext* graph) {
+10 -2
View File
@@ -8,6 +8,7 @@
#include "translator/Translator.h"
#include "translator/math/MathLexer.h"
#include "translator/math/MathParser.h"
#include "translator/antlr_error_listener.h"
class Screen;
@@ -1291,7 +1292,6 @@ class Math : public Translator {
};
}
//
antlr4::ANTLRInputStream input_stream(input);
// Lexer.
@@ -1300,8 +1300,16 @@ class Math : public Translator {
tokens.fill();
// Parser.
AntlrErrorListener error_listener;
MathParser parser(&tokens);
auto* content = parser.multilineEquation();
parser.addErrorListener(&error_listener);
MathParser::MultilineEquationContext* content = nullptr;
try {
content = parser.multilineEquation();
} catch (...) {
return "";
}
if (options["style"] == "Latex")
return to_string(ParseLatex(content, &style)) + '\n';
+9 -2
View File
@@ -11,6 +11,7 @@
#include <string>
#include <vector>
#include "screen/Screen.h"
#include "translator/antlr_error_listener.h"
#include "translator/sequence/Graph.hpp"
void Actor::Draw(Screen& screen, int height) {
@@ -269,10 +270,16 @@ void Sequence::ComputeInternalRepresentation(const std::string& input) {
tokens.fill();
// Parser.
AntlrErrorListener error_listener;
SequenceParser parser(&tokens);
parser.addErrorListener(&error_listener);
// Print the tree.
auto program = parser.program();
SequenceParser::ProgramContext* program = nullptr;
try {
program = parser.program();
} catch (...) {
return;
}
for (SequenceParser::CommandContext* command : program->command()) {
AddCommand(command);