Sketched rough parser code construction including some SQL AST classes.
This commit is contained in:
parent
f3898599fd
commit
5b20f900fc
15 changed files with 459 additions and 4 deletions
75
core/SqlParser.cpp
Normal file
75
core/SqlParser.cpp
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#include "SqlParser.h"
|
||||
#include "SqlAstSelect.h"
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace SqlAst;
|
||||
|
||||
Keyword isKeyword(QString symbol)
|
||||
{
|
||||
static std::unordered_map<std::string, Keyword> lookup_map = {
|
||||
{ "as", Keyword::As },
|
||||
{ "by", Keyword::By },
|
||||
{ "delete", Keyword::Delete },
|
||||
{ "from", Keyword::From },
|
||||
{ "group", Keyword::Group },
|
||||
{ "insert", Keyword::Insert },
|
||||
{ "order", Keyword::Order },
|
||||
{ "select", Keyword::Select },
|
||||
{ "update", Keyword::Update },
|
||||
{ "where", Keyword::Where }
|
||||
};
|
||||
|
||||
auto res = lookup_map.find(symbol.toLower().toUtf8().data());
|
||||
if (res != lookup_map.end())
|
||||
return res->second;
|
||||
else
|
||||
return Keyword::NotAKeyword;
|
||||
}
|
||||
|
||||
|
||||
SqlParser::SqlParser(SqlLexer &lexer)
|
||||
: lexer(lexer)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void SqlParser::parse()
|
||||
{
|
||||
// Basic algo:
|
||||
// LOOP
|
||||
// GET token
|
||||
// IF NOT try_reduce(token)
|
||||
// THEN SHIFT
|
||||
// END LOOP
|
||||
while (1) {
|
||||
SqlToken token = lexer.nextBasicToken();
|
||||
if (token.ok) {
|
||||
if (token.tokenType == BasicTokenType::Symbol) {
|
||||
Keyword kw = isKeyword(token.out);
|
||||
switch (kw) {
|
||||
case Keyword::Select:
|
||||
parseSelect(*this);
|
||||
break;
|
||||
|
||||
case Keyword::NotAKeyword:
|
||||
default:
|
||||
// unexpected
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// error during lexical analysis, need to recover
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//bool try_reduce(SqkToken token)
|
||||
//{
|
||||
// // what state are we in? what are we expecting
|
||||
|
||||
//}
|
||||
Loading…
Add table
Add a link
Reference in a new issue