pgLab/core/SqlParser.h

72 lines
1.7 KiB
C++

#ifndef SQLPARSER_H
#define SQLPARSER_H
#include "SqlLexer.h"
#include <memory>
#include <optional>
enum class Keyword {
NotAKeyword,
As,
By,
Delete,
From,
Group,
Insert,
Order,
Select,
Update,
Where,
};
namespace SqlAst {
class Select;
class SelectList;
}
// The parsing works by calling functions that know either a global part
// or a smaller specific part and is thus recursive. At certain points a function parsing something specific
// will reach a point where it is in a valid terminal state and it encounters something that cannot be a continuation
// of what it is parsing and thus returns succesfully or it is in a non terminal state and encounters something unexpected
// In both cases it will return hoping that one of the functions above can continue (and recover from the error if needed)
class SqlParser
{
public:
explicit SqlParser(SqlLexer &lexer);
void parse();
/** Checks to see if the next token is the expected keyword.
*
* If it is the token is consumed and the function returns true.
* Otherwise false is returned
*/
std::optional<SqlToken> expectKeyword(Keyword kw);
std::optional<SqlToken> expectSymbol();
std::optional<SqlToken> expectToken(BasicTokenType tt);
/** If the next token is Keyword kw consume it otherwise do nothing.
* In some cases the return value is unimportant as the keyword is completely optional
* in other cases the optional keywords presence might force the next token to be something specific
*
* \return true if the token was found
*/
bool consumeOptionalKeyword(Keyword kw);
private:
//using TokenStack = std::stack<SqlToken>;
//TokenStack tokenStack;
SqlLexer &lexer;
//bool try_reduce(SqkToken token);
};
#endif // SQLPARSER_H