2022-10-17 22:26:36 -03:00
|
|
|
#ifndef OBELISK_PARSER_H
|
|
|
|
#define OBELISK_PARSER_H
|
|
|
|
|
|
|
|
#include "ast/expression_ast.h"
|
|
|
|
#include "ast/function_ast.h"
|
|
|
|
#include "ast/prototype_ast.h"
|
2022-11-26 00:32:06 -03:00
|
|
|
#include "knowledge_base.h"
|
2022-10-17 22:26:36 -03:00
|
|
|
#include "lexer.h"
|
2023-02-16 00:35:29 -03:00
|
|
|
#include "models/action.h"
|
|
|
|
#include "models/entity.h"
|
2022-11-26 00:32:06 -03:00
|
|
|
#include "models/fact.h"
|
2023-02-16 00:35:29 -03:00
|
|
|
#include "models/rule.h"
|
|
|
|
#include "models/suggest_action.h"
|
|
|
|
#include "models/verb.h"
|
2022-10-17 22:26:36 -03:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace obelisk
|
|
|
|
{
|
2023-02-20 09:11:10 -03:00
|
|
|
/**
|
2023-02-20 22:01:14 -03:00
|
|
|
* @brief The Parser is responsible for analyzing the language's key words and taking action based on its analysis.
|
2023-02-20 09:11:10 -03:00
|
|
|
*
|
|
|
|
*/
|
2022-10-17 22:26:36 -03:00
|
|
|
class Parser
|
|
|
|
{
|
|
|
|
private:
|
2023-02-20 09:11:10 -03:00
|
|
|
/**
|
2023-02-20 22:01:14 -03:00
|
|
|
* @brief The Lexer object that the Parser is using to Parse a specific source file.
|
2023-02-20 09:11:10 -03:00
|
|
|
*
|
|
|
|
*/
|
2023-02-13 23:03:31 -03:00
|
|
|
std::shared_ptr<obelisk::Lexer> lexer_;
|
2023-02-20 09:11:10 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The current token that the lexer has retrieved.
|
|
|
|
*
|
|
|
|
*/
|
2023-02-13 23:03:31 -03:00
|
|
|
int currentToken_ = 0;
|
2022-10-17 22:26:36 -03:00
|
|
|
|
2023-02-20 09:11:10 -03:00
|
|
|
/**
|
|
|
|
* @brief Set the current token.
|
|
|
|
*
|
|
|
|
* @param[in] currentToken The token should be ASCII character.
|
|
|
|
*/
|
2022-10-17 22:26:36 -03:00
|
|
|
void setCurrentToken(int currentToken);
|
|
|
|
|
2023-02-20 22:01:14 -03:00
|
|
|
/**
|
|
|
|
* @brief Log errors from the LLVM parsing.
|
|
|
|
*
|
|
|
|
* @param[in] str The error message.
|
|
|
|
* @return std::unique_ptr<obelisk::ExpressionAST> Returns the AST expression that caused the error.
|
|
|
|
*/
|
2022-10-17 22:26:36 -03:00
|
|
|
std::unique_ptr<obelisk::ExpressionAST> logError(const char* str);
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Log errors from the LLVM parsing involving the prototypes.
|
|
|
|
*
|
|
|
|
* @param[in] str The error message.
|
|
|
|
* @return std::unique_ptr<obelisk::PrototypeAST> Returns the AST for the prototype.
|
|
|
|
*/
|
2022-12-13 17:35:14 -03:00
|
|
|
std::unique_ptr<obelisk::PrototypeAST> logErrorPrototype(const char* str);
|
2022-10-17 22:26:36 -03:00
|
|
|
|
2023-02-20 22:01:14 -03:00
|
|
|
/**
|
|
|
|
* @brief The AST expression parser.
|
|
|
|
*
|
|
|
|
* @return std::unique_ptr<obelisk::ExpressionAST> Returns the parsed AST expression.
|
|
|
|
*/
|
2022-10-17 22:26:36 -03:00
|
|
|
std::unique_ptr<obelisk::ExpressionAST> parseExpression();
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The AST number expression parser.
|
|
|
|
*
|
|
|
|
* @return std::unique_ptr<obelisk::ExpressionAST> Returns the parsed AST expression.
|
|
|
|
*/
|
2022-10-17 22:26:36 -03:00
|
|
|
std::unique_ptr<obelisk::ExpressionAST> parseNumberExpression();
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The AST parenthesis expression parser.
|
|
|
|
*
|
|
|
|
* @return std::unique_ptr<obelisk::ExpressionAST> Returns the parsed AST expression.
|
|
|
|
*/
|
2022-12-13 17:35:14 -03:00
|
|
|
std::unique_ptr<obelisk::ExpressionAST> parseParenthesisExpression();
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The AST identifier expression parser.
|
|
|
|
*
|
|
|
|
* @return std::unique_ptr<obelisk::ExpressionAST> Returns the parsed AST expression.
|
|
|
|
*/
|
2022-10-17 22:26:36 -03:00
|
|
|
std::unique_ptr<obelisk::ExpressionAST> parseIdentifierExpression();
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The AST primary expression parser.
|
|
|
|
*
|
|
|
|
* @return std::unique_ptr<obelisk::ExpressionAST> Returns the parsed AST expression.
|
|
|
|
*/
|
2022-10-17 22:26:36 -03:00
|
|
|
std::unique_ptr<obelisk::ExpressionAST> parsePrimary();
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The AST prototype parser.
|
|
|
|
*
|
|
|
|
* @return std::unique_ptr<obelisk::PrototypeAST> Returns the parsed AST prototype expression.
|
|
|
|
*/
|
2022-10-17 22:26:36 -03:00
|
|
|
std::unique_ptr<obelisk::PrototypeAST> parsePrototype();
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The AST definition parser.
|
|
|
|
*
|
|
|
|
* @return std::unique_ptr<obelisk::FunctionAST> Returns the parsed AST definition expression.
|
|
|
|
*/
|
2022-10-17 22:26:36 -03:00
|
|
|
std::unique_ptr<obelisk::FunctionAST> parseDefinition();
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The AST top level expression parser.
|
|
|
|
*
|
|
|
|
* @return std::unique_ptr<obelisk::FunctionAST> Returns the parsed AST top level expression.
|
|
|
|
*/
|
2022-10-17 22:26:36 -03:00
|
|
|
std::unique_ptr<obelisk::FunctionAST> parseTopLevelExpression();
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The AST external definition parser.
|
|
|
|
*
|
|
|
|
* @return std::unique_ptr<obelisk::PrototypeAST> Returns the parsed AST external definition.
|
|
|
|
*/
|
2022-10-17 22:26:36 -03:00
|
|
|
std::unique_ptr<obelisk::PrototypeAST> parseExtern();
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Parse a SuggestAction.
|
|
|
|
*
|
|
|
|
* @param[out] suggestAction The parsed SuggestAction.
|
|
|
|
*/
|
2023-02-16 00:35:29 -03:00
|
|
|
void parseAction(obelisk::SuggestAction& suggestAction);
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Parse a Rule.
|
|
|
|
*
|
|
|
|
* @param[out] rule The parsed Rule.
|
|
|
|
*/
|
2023-02-19 01:03:35 -03:00
|
|
|
void parseRule(obelisk::Rule& rule);
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Parse Facts.
|
|
|
|
*
|
|
|
|
* @param[out] facts The parsed Facts.
|
|
|
|
*/
|
2022-11-26 00:32:06 -03:00
|
|
|
void parseFact(std::vector<obelisk::Fact>& facts);
|
2022-10-17 22:26:36 -03:00
|
|
|
|
|
|
|
public:
|
2023-02-20 22:01:14 -03:00
|
|
|
/**
|
|
|
|
* @brief Construct a new Parser object.
|
|
|
|
*
|
|
|
|
* @param[in] lexer The lexer the parser uses to retrieve parts of the language.
|
|
|
|
*/
|
2023-02-13 23:03:31 -03:00
|
|
|
Parser(std::shared_ptr<obelisk::Lexer> lexer) :
|
|
|
|
lexer_(lexer)
|
|
|
|
{
|
|
|
|
}
|
2022-10-17 22:26:36 -03:00
|
|
|
|
2023-02-20 22:01:14 -03:00
|
|
|
/**
|
|
|
|
* @brief Get the Lexer.
|
|
|
|
*
|
|
|
|
* @return std::shared_ptr<obelisk::Lexer> Returns the current Lexer in use by the Parser.
|
|
|
|
*/
|
2023-02-13 23:03:31 -03:00
|
|
|
std::shared_ptr<obelisk::Lexer> getLexer();
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the Lexer to use during the parsing phase.
|
|
|
|
*
|
|
|
|
* @param[in] lexer The Lexer.
|
|
|
|
*/
|
2023-02-13 23:03:31 -03:00
|
|
|
void setLexer(std::shared_ptr<obelisk::Lexer> lexer);
|
2022-10-17 22:26:36 -03:00
|
|
|
|
2023-02-20 22:01:14 -03:00
|
|
|
/**
|
|
|
|
* @brief Gets the current token held inside the Lexer.
|
|
|
|
*
|
|
|
|
* @return int Returns the current token.
|
|
|
|
*/
|
2022-10-17 22:26:36 -03:00
|
|
|
int getCurrentToken();
|
|
|
|
|
2023-02-20 22:01:14 -03:00
|
|
|
/**
|
|
|
|
* @brief Instructs the Lexer to retrieve a new token.
|
|
|
|
*
|
|
|
|
* @return int Returns the next token.
|
|
|
|
*/
|
2022-10-17 22:26:36 -03:00
|
|
|
int getNextToken();
|
|
|
|
|
2023-02-20 22:01:14 -03:00
|
|
|
/**
|
|
|
|
* @brief Parse the SuggestAction and then insert it into the KnowledgeBase.
|
|
|
|
*
|
|
|
|
* @param[in] kb The KnowledgeBase to insert the SuggestAction into.
|
|
|
|
*/
|
2022-11-26 00:32:06 -03:00
|
|
|
void handleAction(std::unique_ptr<obelisk::KnowledgeBase>& kb);
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Parse the Rule and then insert it into the KnowledgeBase.
|
|
|
|
*
|
|
|
|
* @param[in] kb The KnowledgeBase to insert the Rule into.
|
|
|
|
*/
|
2022-11-26 00:32:06 -03:00
|
|
|
void handleRule(std::unique_ptr<obelisk::KnowledgeBase>& kb);
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Parse the Fact and then insert it into the KnowledgeBase.
|
|
|
|
*
|
|
|
|
* @param[in] kb The KnowledgeBase to insert the Fact into.
|
|
|
|
*/
|
2022-11-26 00:32:06 -03:00
|
|
|
void handleFact(std::unique_ptr<obelisk::KnowledgeBase>& kb);
|
2023-02-16 00:35:29 -03:00
|
|
|
|
2023-02-20 22:01:14 -03:00
|
|
|
/**
|
|
|
|
* @brief Helper used to insert an Entity into the KnowledgeBase.
|
|
|
|
*
|
|
|
|
* @param[in] kb The KnowledgeBase to use.
|
|
|
|
* @param[in,out] entity The Entity to insert. It will contain the ID of the Entity after inserting it.
|
|
|
|
*/
|
2023-02-16 00:35:29 -03:00
|
|
|
void insertEntity(std::unique_ptr<obelisk::KnowledgeBase>& kb, obelisk::Entity& entity);
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Helper used to insert a Verb into the KnowledgeBase.
|
|
|
|
*
|
|
|
|
* @param[in] kb The KnowledegeBase to use.
|
|
|
|
* @param[in,out] verb The Verb to insert. It will contain the ID of the Verb after inserting it.
|
|
|
|
*/
|
2023-02-16 00:35:29 -03:00
|
|
|
void insertVerb(std::unique_ptr<obelisk::KnowledgeBase>& kb, obelisk::Verb& verb);
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Helper used to insert an Action into the KnowledgeBase.
|
|
|
|
*
|
|
|
|
* @param[in] kb The KnowledgeBase to use.
|
|
|
|
* @param[in,out] action The Action to insert. It will contain the ID of the Action after inserting it.
|
|
|
|
*/
|
2023-02-18 21:06:42 -03:00
|
|
|
void insertAction(std::unique_ptr<obelisk::KnowledgeBase>& kb, obelisk::Action& action);
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Helper used to insert a Fact into the KnowledgeBase.
|
|
|
|
*
|
|
|
|
* @param[in] kb The KnowledgeBase to use.
|
|
|
|
* @param[in,out] fact The Fact to insert. It will contain the ID of the Fact after inserting it.
|
|
|
|
* @param[in] updateIsTrue If true, it will update the value of is_true in the KnowledgeBase if the Fact
|
|
|
|
* already exists.
|
|
|
|
*/
|
2023-02-20 09:11:10 -03:00
|
|
|
void insertFact(std::unique_ptr<obelisk::KnowledgeBase>& kb,
|
|
|
|
obelisk::Fact& fact,
|
|
|
|
bool updateIsTrue = false);
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Helper used to insert a SuggestAction into the KnowledgeBase.
|
|
|
|
*
|
|
|
|
* @param[in] kb The KnowledgeBase to use.
|
|
|
|
* @param[in,out] suggestAction The SuggestAction to insert. It will contain the ID of the SuggestAction
|
|
|
|
* after inserting it.
|
|
|
|
*/
|
2023-02-18 21:32:05 -03:00
|
|
|
void insertSuggestAction(std::unique_ptr<obelisk::KnowledgeBase>& kb,
|
|
|
|
obelisk::SuggestAction& suggestAction);
|
2023-02-20 22:01:14 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Helper usedto insert a Rule into the KnowledgeBase.
|
|
|
|
*
|
|
|
|
* @param[in] kb The KnowledgeBase to use.
|
|
|
|
* @param[in,out] rule The Rule to insert. It will contain the ID of the Rule after inserting it.
|
|
|
|
*/
|
2023-02-19 01:03:35 -03:00
|
|
|
void insertRule(std::unique_ptr<obelisk::KnowledgeBase>& kb, obelisk::Rule& rule);
|
2022-10-17 22:26:36 -03:00
|
|
|
};
|
2022-11-22 11:34:07 -03:00
|
|
|
|
2023-02-20 22:01:14 -03:00
|
|
|
/**
|
|
|
|
* @brief The exceptions thrown by the Parser.
|
|
|
|
*
|
|
|
|
*/
|
2022-11-22 11:34:07 -03:00
|
|
|
class ParserException : public std::exception
|
|
|
|
{
|
|
|
|
private:
|
2023-02-20 22:01:14 -03:00
|
|
|
/**
|
|
|
|
* @brief The error message.
|
|
|
|
*
|
|
|
|
*/
|
2022-11-22 11:34:07 -03:00
|
|
|
const std::string errorMessage_;
|
|
|
|
|
|
|
|
public:
|
2023-02-20 22:01:14 -03:00
|
|
|
/**
|
|
|
|
* @brief Construct a new ParserException object.
|
|
|
|
*
|
|
|
|
*/
|
2022-11-22 11:34:07 -03:00
|
|
|
ParserException() :
|
|
|
|
errorMessage_("an unknown error ocurred")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-20 22:01:14 -03:00
|
|
|
/**
|
|
|
|
* @brief Construct a new ParserException object.
|
|
|
|
*
|
|
|
|
* @param[in] errorMessage The error message.
|
|
|
|
*/
|
2022-11-22 11:34:07 -03:00
|
|
|
ParserException(const std::string& errorMessage) :
|
|
|
|
errorMessage_(errorMessage)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-20 22:01:14 -03:00
|
|
|
/**
|
|
|
|
* @brief Return the error message as a C style string.
|
|
|
|
*
|
|
|
|
* @return const char* Returns the error message.
|
|
|
|
*/
|
2022-11-22 11:34:07 -03:00
|
|
|
const char* what() const noexcept
|
|
|
|
{
|
|
|
|
return errorMessage_.c_str();
|
|
|
|
}
|
|
|
|
};
|
2022-10-17 22:26:36 -03:00
|
|
|
} // namespace obelisk
|
|
|
|
|
|
|
|
#endif
|