Skip to content

Commit

Permalink
Catch parser errors. (#38)
Browse files Browse the repository at this point in the history
This should resolve:
#36
  • Loading branch information
ArthurSonzogni authored May 8, 2022
1 parent 23d95bb commit ccb028f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
13 changes: 13 additions & 0 deletions src/translator/antlr_error_listener.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions src/translator/antlr_error_listener.h
Original file line number Diff line number Diff line change
@@ -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
16 changes: 14 additions & 2 deletions src/translator/flowchart/Flowchart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/translator/graph_planar/GraphPlanar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 10 additions & 2 deletions src/translator/math/Math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1291,7 +1292,6 @@ class Math : public Translator {
};
}

//
antlr4::ANTLRInputStream input_stream(input);

// Lexer.
Expand All @@ -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';
Expand Down
11 changes: 9 additions & 2 deletions src/translator/sequence/Sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ccb028f

Please sign in to comment.