Skip to content

Commit

Permalink
Move antlr visitor derived classes into private namespace and dedupli…
Browse files Browse the repository at this point in the history
…cate ErrorListener
  • Loading branch information
gibber9809 committed Aug 9, 2024
1 parent b5f55d3 commit d96505e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
30 changes: 30 additions & 0 deletions components/core/src/clp_s/search/antlr_common/ErrorListener.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef CLP_S_SEARCH_ANTLRCOMMON_ERRORLISTENER_HPP
#define CLP_S_SEARCH_ANTLRCOMMON_ERRORLISTENER_HPP
#include <antlr4-runtime.h>

namespace clp_s::search::antlr_common {
class ErrorListener : public antlr4::BaseErrorListener {
public:
void syntaxError(
antlr4::Recognizer* recognizer,
antlr4::Token* offending_symbol,
size_t line,
size_t char_position_in_line,
std::string const& msg,
std::exception_ptr e
) override {
m_error = true;
m_error_message = msg;
}

bool error() const { return m_error; }

std::string const& message() const { return m_error_message; }

private:
bool m_error{false};
std::string m_error_message;
};
} // namespace clp_s::search::antlr_common

#endif // CLP_S_SEARCH_ANTLRCOMMON_ERRORLISTENER_HPP
1 change: 1 addition & 0 deletions components/core/src/clp_s/search/kql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ANTLR_TARGET(
add_library(
kql
../../Utils.hpp
../antlr_common/ErrorListener.hpp
../AndExpr.hpp
../BooleanLiteral.hpp
../ColumnDescriptor.hpp
Expand Down
27 changes: 4 additions & 23 deletions components/core/src/clp_s/search/kql/kql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "../../Utils.hpp"
#include "../AndExpr.hpp"
#include "../antlr_common/ErrorListener.hpp"
#include "../BooleanLiteral.hpp"
#include "../ColumnDescriptor.hpp"
#include "../DateLiteral.hpp"
Expand All @@ -25,31 +26,10 @@

using namespace antlr4;
using namespace kql;
using clp_s::search::antlr_common::ErrorListener;

namespace clp_s::search::kql {
class ErrorListener : public BaseErrorListener {
public:
void syntaxError(
Recognizer* recognizer,
Token* offending_symbol,
size_t line,
size_t char_position_in_line,
std::string const& msg,
std::exception_ptr e
) override {
m_error = true;
m_error_message = msg;
}

bool error() const { return m_error; }

std::string const& message() const { return m_error_message; }

private:
bool m_error{false};
std::string m_error_message;
};

namespace {
class ParseTreeVisitor : public KqlBaseVisitor {
private:
static void
Expand Down Expand Up @@ -224,6 +204,7 @@ class ParseTreeVisitor : public KqlBaseVisitor {
return base;
}
};
} // namespace

std::shared_ptr<Expression> parse_kql_expression(std::istream& in) {
ErrorListener lexer_error_listener;
Expand Down
1 change: 1 addition & 0 deletions components/core/src/clp_s/search/sql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ANTLR_TARGET(
add_library(
sql
../../Utils.hpp
../antlr_common/ErrorListener.hpp
../AndExpr.hpp
../BooleanLiteral.hpp
../ColumnDescriptor.hpp
Expand Down
4 changes: 4 additions & 0 deletions components/core/src/clp_s/search/sql/sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
#include <antlr4-runtime.h>
#include <spdlog/spdlog.h>

#include "../antlr_common/ErrorListener.hpp"
#include "../EmptyExpr.hpp"
#include "SqlBaseVisitor.h"
#include "SqlLexer.h"
#include "SqlParser.h"

using namespace antlr4;
using namespace sql;
using clp_s::search::antlr_common::ErrorListener;

namespace clp_s::search::sql {
class ErrorListener : public BaseErrorListener {
Expand All @@ -37,10 +39,12 @@ class ErrorListener : public BaseErrorListener {
std::string m_error_message;
};

namespace {
class ParseTreeVisitor : public SqlBaseVisitor {
public:
std::any visitStart(SqlParser::StartContext* ctx) override { return EmptyExpr::create(); }
};
} // namespace

std::shared_ptr<Expression> parse_sql_expression(std::istream& in) {
ErrorListener lexer_error_listener;
Expand Down

0 comments on commit d96505e

Please sign in to comment.