Start of new ANTLR4 based parser.
Very simple tests pass.
This commit is contained in:
parent
03b4194193
commit
fbbe832a05
44 changed files with 860 additions and 8 deletions
22
pglablib/sqlparser/ErrorListener.cpp
Normal file
22
pglablib/sqlparser/ErrorListener.cpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include "ErrorListener.h"
|
||||
|
||||
|
||||
void ErrorListener::syntaxError(antlr4::Recognizer *recognizer, antlr4::Token *offendingSymbol, size_t line, size_t charPositionInLine, const std::string &msg, std::exception_ptr e)
|
||||
{
|
||||
++errors;
|
||||
}
|
||||
|
||||
void ErrorListener::reportAmbiguity(antlr4::Parser *recognizer, const antlr4::dfa::DFA &dfa, size_t startIndex, size_t stopIndex, bool exact, const antlrcpp::BitSet &ambigAlts, antlr4::atn::ATNConfigSet *configs)
|
||||
{
|
||||
++errors;
|
||||
}
|
||||
|
||||
void ErrorListener::reportAttemptingFullContext(antlr4::Parser *recognizer, const antlr4::dfa::DFA &dfa, size_t startIndex, size_t stopIndex, const antlrcpp::BitSet &conflictingAlts, antlr4::atn::ATNConfigSet *configs)
|
||||
{
|
||||
++errors;
|
||||
}
|
||||
|
||||
void ErrorListener::reportContextSensitivity(antlr4::Parser *recognizer, const antlr4::dfa::DFA &dfa, size_t startIndex, size_t stopIndex, size_t prediction, antlr4::atn::ATNConfigSet *configs)
|
||||
{
|
||||
++errors;
|
||||
}
|
||||
21
pglablib/sqlparser/ErrorListener.h
Normal file
21
pglablib/sqlparser/ErrorListener.h
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#pragma once
|
||||
#include "antlr4-runtime.h"
|
||||
|
||||
class ErrorListener : public antlr4::ANTLRErrorListener
|
||||
{
|
||||
// ANTLRErrorListener interface
|
||||
public:
|
||||
virtual void syntaxError(antlr4::Recognizer *recognizer, antlr4::Token *offendingSymbol, size_t line, size_t charPositionInLine, const std::string &msg, std::exception_ptr e) override;
|
||||
virtual void reportAmbiguity(antlr4::Parser *recognizer, const antlr4::dfa::DFA &dfa, size_t startIndex, size_t stopIndex, bool exact, const antlrcpp::BitSet &ambigAlts, antlr4::atn::ATNConfigSet *configs) override;
|
||||
virtual void reportAttemptingFullContext(antlr4::Parser *recognizer, const antlr4::dfa::DFA &dfa, size_t startIndex, size_t stopIndex, const antlrcpp::BitSet &conflictingAlts, antlr4::atn::ATNConfigSet *configs) override;
|
||||
virtual void reportContextSensitivity(antlr4::Parser *recognizer, const antlr4::dfa::DFA &dfa, size_t startIndex, size_t stopIndex, size_t prediction, antlr4::atn::ATNConfigSet *configs) override;
|
||||
|
||||
public:
|
||||
int errorCount() const
|
||||
{
|
||||
return errors;
|
||||
}
|
||||
|
||||
private:
|
||||
int errors = 0;
|
||||
};
|
||||
21
pglablib/sqlparser/Parser.cpp
Normal file
21
pglablib/sqlparser/Parser.cpp
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include "Parser.h"
|
||||
#include "antlr4-runtime.h"
|
||||
|
||||
|
||||
Parser::Parser(const std::string &input_string)
|
||||
: InputStream(std::make_unique<antlr4::ANTLRInputStream>(input_string))
|
||||
, CaseFilter(InputStream.get(), true)
|
||||
, Lexer(&CaseFilter)
|
||||
, TokenStream(&Lexer)
|
||||
, AParser(&TokenStream)
|
||||
{
|
||||
AParser.removeErrorListeners();
|
||||
AParser.addErrorListener(&Errors);
|
||||
}
|
||||
|
||||
std::unique_ptr<sqlast::StatementList> Parser::Parse()
|
||||
{
|
||||
auto context = AParser.main();
|
||||
return std::move(context->program);
|
||||
}
|
||||
|
||||
27
pglablib/sqlparser/Parser.h
Normal file
27
pglablib/sqlparser/Parser.h
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
#include ".generated/PgsqlLexer.h"
|
||||
#include ".generated/PgsqlParser.h"
|
||||
#include "CaseChangingCharStream.h"
|
||||
#include "ErrorListener.h"
|
||||
|
||||
class Parser
|
||||
{
|
||||
public:
|
||||
Parser(const std::string &input_string);
|
||||
|
||||
std::unique_ptr<sqlast::StatementList> Parse();
|
||||
|
||||
int errorCount() const
|
||||
{
|
||||
return Errors.errorCount();
|
||||
}
|
||||
private:
|
||||
std::unique_ptr<antlr4::CharStream> InputStream;
|
||||
CaseChangingCharStream CaseFilter;
|
||||
PgsqlLexer Lexer;
|
||||
antlr4::CommonTokenStream TokenStream;
|
||||
PgsqlParser AParser;
|
||||
ErrorListener Errors;
|
||||
};
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue