From adc5042c6c57333338f26adab1fd2425dbb9a703 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Mon, 20 Feb 2023 22:05:57 -0300 Subject: [PATCH] format to 80 columns for terminal display --- .clang-format | 4 +- src/ast/call_expression_ast.h | 9 ++- src/ast/error.h | 3 +- src/ast/expression_ast.h | 3 +- src/ast/function_ast.cpp | 3 +- src/ast/function_ast.h | 3 +- src/ast/prototype_ast.cpp | 13 +++- src/ast/prototype_ast.h | 3 +- src/lexer.cpp | 3 +- src/lexer.h | 21 +++-- src/lib/include/obelisk.h | 6 +- src/lib/knowledge_base.cpp | 32 ++++++-- src/lib/knowledge_base.h | 74 ++++++++++-------- src/lib/models/action.cpp | 18 ++++- src/lib/models/action.h | 9 ++- src/lib/models/entity.cpp | 18 ++++- src/lib/models/entity.h | 9 ++- src/lib/models/error.h | 18 +++-- src/lib/models/fact.cpp | 15 +++- src/lib/models/fact.h | 29 ++++--- src/lib/models/rule.cpp | 21 +++-- src/lib/models/rule.h | 7 +- src/lib/models/suggest_action.cpp | 3 +- src/lib/models/suggest_action.h | 15 +++- src/lib/models/verb.cpp | 18 ++++- src/lib/models/verb.h | 3 +- src/main.cpp | 21 +++-- src/main.h | 6 +- src/parser.cpp | 124 ++++++++++++++++++++---------- src/parser.h | 95 +++++++++++++++-------- 30 files changed, 412 insertions(+), 194 deletions(-) diff --git a/.clang-format b/.clang-format index c46124e..7233005 100644 --- a/.clang-format +++ b/.clang-format @@ -63,7 +63,7 @@ BreakBeforeTernaryOperators: true BreakConstructorInitializers: AfterColon BreakInheritanceList: AfterColon BreakStringLiterals: false -ColumnLimit: 120 +ColumnLimit: 80 CommentPragmas: "^ IWYU pragma:" CompactNamespaces: false ConstructorInitializerAllOnOneLineOrOnePerLine: true @@ -167,7 +167,7 @@ RawStringFormats: - PROTO CanonicalDelimiter: pb ReferenceAlignment: Left -ReflowComments: false +ReflowComments: true RemoveBracesLLVM: false SeparateDefinitionBlocks: Always ShortNamespaceLines: 0 diff --git a/src/ast/call_expression_ast.h b/src/ast/call_expression_ast.h index 4913bd9..55c5800 100644 --- a/src/ast/call_expression_ast.h +++ b/src/ast/call_expression_ast.h @@ -31,7 +31,8 @@ namespace obelisk /** * @brief Get the callee. * - * @return std::string Returns the name of the function being called. + * @return std::string Returns the name of the function being + * called. */ std::string getCallee(); @@ -45,7 +46,8 @@ namespace obelisk /** * @brief Get the arguments being used by the function. * - * @return std::vector> Returns an AST expression containing the args. + * @return std::vector> Returns an + * AST expression containing the args. */ std::vector> getArgs(); @@ -63,7 +65,8 @@ namespace obelisk * @param[in] callee The function to call. * @param[in] args The args to pass into the function. */ - CallExpressionAST(const std::string &callee, std::vector> args) : + CallExpressionAST(const std::string &callee, + std::vector> args) : callee_(callee), args_(std::move(args)) { diff --git a/src/ast/error.h b/src/ast/error.h index dd990dc..78d1770 100644 --- a/src/ast/error.h +++ b/src/ast/error.h @@ -11,7 +11,8 @@ namespace obelisk * @brief Log an AST expression error. * * @param[in] str The error message. - * @return std::unique_ptr Returns the AST expression that caused the error. + * @return std::unique_ptr Returns the AST expression that + * caused the error. */ std::unique_ptr LogError(const char *str); diff --git a/src/ast/expression_ast.h b/src/ast/expression_ast.h index c1201a8..d80c378 100644 --- a/src/ast/expression_ast.h +++ b/src/ast/expression_ast.h @@ -21,7 +21,8 @@ namespace obelisk /** * @brief Generate LLVM IR code based on the AST expression. * - * @return llvm::Value* Returns the LLVM code value from the expression. + * @return llvm::Value* Returns the LLVM code value from the + * expression. */ virtual llvm::Value *codegen() = 0; }; diff --git a/src/ast/function_ast.cpp b/src/ast/function_ast.cpp index 021e9bb..1bdad9d 100644 --- a/src/ast/function_ast.cpp +++ b/src/ast/function_ast.cpp @@ -17,7 +17,8 @@ llvm::Function *obelisk::FunctionAST::codegen() return nullptr; } - llvm::BasicBlock *bB = llvm::BasicBlock::Create(*TheContext, "entry", theFunction); + llvm::BasicBlock *bB + = llvm::BasicBlock::Create(*TheContext, "entry", theFunction); Builder->SetInsertPoint(bB); NamedValues.clear(); diff --git a/src/ast/function_ast.h b/src/ast/function_ast.h index d4e28b1..8f3e0ac 100644 --- a/src/ast/function_ast.h +++ b/src/ast/function_ast.h @@ -48,7 +48,8 @@ namespace obelisk * @param[in] prototype The prototype of the function. * @param[in] body The body of the function. */ - FunctionAST(std::unique_ptr prototype, std::unique_ptr body) : + FunctionAST(std::unique_ptr prototype, + std::unique_ptr body) : prototype_(std::move(prototype)), body_(std::move(body)) { diff --git a/src/ast/prototype_ast.cpp b/src/ast/prototype_ast.cpp index 72e096a..f7caba4 100644 --- a/src/ast/prototype_ast.cpp +++ b/src/ast/prototype_ast.cpp @@ -3,10 +3,17 @@ llvm::Function *obelisk::PrototypeAST::codegen() { - std::vector doubles(args_.size(), llvm::Type::getDoubleTy(*TheContext)); - llvm::FunctionType *FT = llvm::FunctionType::get(llvm::Type::getDoubleTy(*TheContext), doubles, false); + std::vector doubles(args_.size(), + llvm::Type::getDoubleTy(*TheContext)); + llvm::FunctionType *FT + = llvm::FunctionType::get(llvm::Type::getDoubleTy(*TheContext), + doubles, + false); - llvm::Function *F = llvm::Function::Create(FT, llvm::Function::ExternalLinkage, name_, obelisk::TheModule.get()); + llvm::Function *F = llvm::Function::Create(FT, + llvm::Function::ExternalLinkage, + name_, + obelisk::TheModule.get()); unsigned idx = 0; for (auto &arg : F->args()) diff --git a/src/ast/prototype_ast.h b/src/ast/prototype_ast.h index 2df8225..ec1e7bc 100644 --- a/src/ast/prototype_ast.h +++ b/src/ast/prototype_ast.h @@ -54,7 +54,8 @@ namespace obelisk * @param[in] name The name of the prototype. * @param[in] args The arguments the prototype accepts. */ - PrototypeAST(const std::string& name, std::vector args) : + PrototypeAST(const std::string& name, + std::vector args) : name_(name), args_(std::move(args)) { diff --git a/src/lexer.cpp b/src/lexer.cpp index c941103..91eb20d 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -7,7 +7,8 @@ obelisk::Lexer::Lexer(const std::string& sourceFile) fileStream_.open(sourceFile, std::ifstream::in); if (!fileStream_) { - throw obelisk::LexerException("could not open source file " + sourceFile); + throw obelisk::LexerException( + "could not open source file " + sourceFile); } } diff --git a/src/lexer.h b/src/lexer.h index 30741ca..4562611 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -56,19 +56,22 @@ namespace obelisk /** * @brief Comment the rest of the line. * - * @param[in] lastChar The char to check to see if it in the end of the line. + * @param[in] lastChar The char to check to see if it in the end of + * the line. */ void commentLine(int* lastChar); public: /** - * @brief These token represent recognized language keywords and language functionality. + * @brief These token represent recognized language keywords and + * language functionality. * */ enum Token { /** - * @brief End of file is returned when the source code is finished. + * @brief End of file is returned when the source code is + * finished. * */ kTokenEof = -1, @@ -79,7 +82,8 @@ namespace obelisk */ kTokenFact = -2, /** - * @brief A rule which is a relationship between a new fact a existing fact. + * @brief A rule which is a relationship between a new fact a + * existing fact. * */ kTokenRule = -3, @@ -133,14 +137,16 @@ namespace obelisk * @brief Gets the next token in the source code. * * @throws LexerException when an invalid token is found. - * @return int Returns a Token value or char if no known token was found. + * @return int Returns a Token value or char if no known token was + * found. */ int getToken(); /** * @brief Get the last identifier. * - * @return const std::string& Returns a string that contains the last found identifier. + * @return const std::string& Returns a string that contains the + * last found identifier. */ const std::string& getIdentifier(); @@ -188,7 +194,8 @@ namespace obelisk /** * @brief Return the exception's error message. * - * @return const char* Returns a string containing the error message. + * @return const char* Returns a string containing the error + * message. */ const char* what() const noexcept { diff --git a/src/lib/include/obelisk.h b/src/lib/include/obelisk.h index a73dc4c..b06eb8b 100644 --- a/src/lib/include/obelisk.h +++ b/src/lib/include/obelisk.h @@ -1,13 +1,15 @@ #include /** - * @brief The obelisk namespace contains everything needed to compile obelisk code. + * @brief The obelisk namespace contains everything needed to compile obelisk + * code. * */ namespace obelisk { /** - * @brief The obelisk library provides everything needed to consult the KnowledgeBase. + * @brief The obelisk library provides everything needed to consult the + * KnowledgeBase. * */ class Obelisk diff --git a/src/lib/knowledge_base.cpp b/src/lib/knowledge_base.cpp index f5f4170..52fa521 100644 --- a/src/lib/knowledge_base.cpp +++ b/src/lib/knowledge_base.cpp @@ -44,7 +44,11 @@ obelisk::KnowledgeBase::~KnowledgeBase() void obelisk::KnowledgeBase::enableForeignKeys() { char* errmsg; - int result = sqlite3_exec(dbConnection_, "PRAGMA foreign_keys = ON;", NULL, NULL, &errmsg); + int result = sqlite3_exec(dbConnection_, + "PRAGMA foreign_keys = ON;", + NULL, + NULL, + &errmsg); if (result != SQLITE_OK) { if (errmsg) @@ -86,7 +90,9 @@ void obelisk::KnowledgeBase::addEntities(std::vector& entities) catch (obelisk::DatabaseConstraintException& exception) { // ignore unique constraint error - if (std::strcmp(exception.what(), "UNIQUE constraint failed: entity.name") != 0) + if (std::strcmp(exception.what(), + "UNIQUE constraint failed: entity.name") + != 0) { throw; } @@ -105,7 +111,9 @@ void obelisk::KnowledgeBase::addVerbs(std::vector& verbs) catch (obelisk::DatabaseConstraintException& exception) { // ignore unique constraint error - if (std::strcmp(exception.what(), "UNIQUE constraint failed: verb.name") != 0) + if (std::strcmp(exception.what(), + "UNIQUE constraint failed: verb.name") + != 0) { throw; } @@ -124,7 +132,9 @@ void obelisk::KnowledgeBase::addActions(std::vector& actions) catch (obelisk::DatabaseConstraintException& exception) { // ignore unique constraint error - if (std::strcmp(exception.what(), "UNIQUE constraint failed: action.name") != 0) + if (std::strcmp(exception.what(), + "UNIQUE constraint failed: action.name") + != 0) { throw; } @@ -153,7 +163,8 @@ void obelisk::KnowledgeBase::addFacts(std::vector& facts) } } -void obelisk::KnowledgeBase::addSuggestActions(std::vector& suggestActions) +void obelisk::KnowledgeBase::addSuggestActions( + std::vector& suggestActions) { for (auto& suggestAction : suggestActions) { @@ -185,7 +196,9 @@ void obelisk::KnowledgeBase::addRules(std::vector& rules) catch (obelisk::DatabaseConstraintException& exception) { // ignore unique constraint error - if (std::strcmp(exception.what(), "UNIQUE constraint failed: rule.fact, rule.reason") != 0) + if (std::strcmp(exception.what(), + "UNIQUE constraint failed: rule.fact, rule.reason") + != 0) { throw; } @@ -213,7 +226,8 @@ void obelisk::KnowledgeBase::getFact(obelisk::Fact& fact) fact.selectById(dbConnection_); } -void obelisk::KnowledgeBase::getSuggestAction(obelisk::SuggestAction& suggestAction) +void obelisk::KnowledgeBase::getSuggestAction( + obelisk::SuggestAction& suggestAction) { suggestAction.selectById(dbConnection_); } @@ -245,7 +259,9 @@ void obelisk::KnowledgeBase::updateIsTrue(obelisk::Fact& fact) fact.updateIsTrue(dbConnection_); } -void obelisk::KnowledgeBase::getFloat(float& result1, float& result2, double var) +void obelisk::KnowledgeBase::getFloat(float& result1, + float& result2, + double var) { result1 = (float) var; result2 = (float) (var - (double) result1); diff --git a/src/lib/knowledge_base.h b/src/lib/knowledge_base.h index b9a3368..4fe6b6a 100644 --- a/src/lib/knowledge_base.h +++ b/src/lib/knowledge_base.h @@ -18,7 +18,8 @@ namespace obelisk { /** - * @brief The KnowledgeBase class represents a collection of facts, rules, actions, and related language connectors. + * @brief The KnowledgeBase class represents a collection of facts, rules, + * actions, and related language connectors. * */ class KnowledgeBase @@ -45,8 +46,8 @@ namespace obelisk /** * @brief Enable foreign key functionality in the open database. * - * This must always be done when the connection is opened or it will not enforce the foreign key - * constraints. + * This must always be done when the connection is opened or it will + * not enforce the foreign key constraints. */ void enableForeignKeys(); @@ -61,7 +62,8 @@ namespace obelisk /** * @brief Construct a new KnowledgeBase object. * - * @param[in] filename The name of the file to save the knowledge base as. + * @param[in] filename The name of the file to save the knowledge + * base as. * @param[in] flags The flags to open the KnowledgeBase with. */ KnowledgeBase(const char* filename, int flags); @@ -69,10 +71,12 @@ namespace obelisk /** * @brief Construct a new KnowledgeBase object. * - * @param[in] filename The name of the file to save the knowledge base as. + * @param[in] filename The name of the file to save the knowledge + * base as. */ KnowledgeBase(const char* filename) : - KnowledgeBase(filename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) + KnowledgeBase(filename, + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) { } @@ -86,96 +90,104 @@ namespace obelisk /** * @brief Add entities to the KnowledgeBase. * - * @param[in,out] entities The entities to add. If the insert is successful it will have a row ID, if not - * the ID will be 0. + * @param[in,out] entities The entities to add. If the insert is + * successful it will have a row ID, if not the ID will be 0. */ void addEntities(std::vector& entities); /** * @brief Add verbs to the KnowledgeBase. * - * @param[in,out] verbs The verbs to add. If the insert is successful it will have a row ID, if not the ID - * will be 0. + * @param[in,out] verbs The verbs to add. If the insert is + * successful it will have a row ID, if not the ID will be 0. */ void addVerbs(std::vector& verbs); /** * @brief Add actions to the KnowledgeBase. * - * @param[in,out] actions The actions to add. If the insert is successful it will have a row ID, if nto the - * ID will be 0. + * @param[in,out] actions The actions to add. If the insert is + * successful it will have a row ID, if nto the ID will be 0. */ void addActions(std::vector& actions); /** * @brief Add facts to the KnowledgeBase. * - * @param[in,out] facts The facts to add. If the insert is successful it will have a row ID, if not the ID - * will be 0. + * @param[in,out] facts The facts to add. If the insert is + * successful it will have a row ID, if not the ID will be 0. */ void addFacts(std::vector& facts); /** * @brief Add suggested actions to the KnowledgeBase. * - * @param[in,out] suggestActions The suggested actions to add. If the insert is successful it will have a - * row ID, if not the ID will be 0. + * @param[in,out] suggestActions The suggested actions to add. If + * the insert is successful it will have a row ID, if not the ID + * will be 0. */ - void addSuggestActions(std::vector& suggestActions); + void addSuggestActions( + std::vector& suggestActions); /** * @brief Add rules to the KnowledgeBase. * - * @param[in,out] rules The rules to add. If the insert is successful it will have a row ID, if not the ID - * will be 0. + * @param[in,out] rules The rules to add. If the insert is + * successful it will have a row ID, if not the ID will be 0. */ void addRules(std::vector& rules); /** * @brief Get an Entity object based on the ID it contains. * - * @param[in,out] entity The Entity object should contain just the ID and the rest will be filled in. + * @param[in,out] entity The Entity object should contain just the + * ID and the rest will be filled in. */ void getEntity(obelisk::Entity& entity); /** * @brief Get a Verb object based on the ID it contains. * - * @param[in,out] verb The Verb object should contain just the ID and the rest will be filled in. + * @param[in,out] verb The Verb object should contain just the ID + * and the rest will be filled in. */ void getVerb(obelisk::Verb& verb); /** * @brief Get an Action based on the ID it contains. * - * @param[in] action The Action object should contain just the ID and the rest will be filled in. + * @param[in] action The Action object should contain just the ID + * and the rest will be filled in. */ void getAction(obelisk::Action& action); /** * @brief Get a Fact object based on the ID it contains. * - * @param[in,out] fact The Fact object should contain just the ID and the rest will be filled in. + * @param[in,out] fact The Fact object should contain just the ID + * and the rest will be filled in. */ void getFact(obelisk::Fact& fact); /** * @brief Get a SuggestAction based on the ID it contains. * - * @param[in,out] suggestAction The SuggestAction object should contain just the ID and the rest will be - * filled in. + * @param[in,out] suggestAction The SuggestAction object should + * contain just the ID and the rest will be filled in. */ void getSuggestAction(obelisk::SuggestAction& suggestAction); /** * @brief Get a Rule based on the ID it contains. * - * @param[in,out] rule The Rule object should contain just the ID and the rest will be filled in. + * @param[in,out] rule The Rule object should contain just the ID + * and the rest will be filled in. */ void getRule(obelisk::Rule& rule); /** - * @brief Check if a rule looks for this Fact, if so update its truth. + * @brief Check if a rule looks for this Fact, if so update its + * truth. * * @param[in,out] fact The Fact to check for existing rules. */ @@ -191,8 +203,9 @@ namespace obelisk /** * @brief Take a float and divide it into 2 floats. * - * This is useful to store doubles in SQLite since SQLite doesn't have a double type. Instead just store the - * 2 floats in the database. Then after selecting them combine them. + * This is useful to store doubles in SQLite since SQLite doesn't + * have a double type. Instead just store the 2 floats in the + * database. Then after selecting them combine them. * * @param[out] result1 The first float generated from the double. * @param[out] result2 The second float generated from the double. @@ -203,7 +216,8 @@ namespace obelisk /** * @brief Combines 2 separated floats back into a double. * - * This will recombine the separated floats from the getFloat method. + * This will recombine the separated floats from the getFloat + * method. * * @param[out] result The double generated from the combined floats. * @param[in] var1 The first float to combine. diff --git a/src/lib/models/action.cpp b/src/lib/models/action.cpp index d6d3e10..8f8c880 100644 --- a/src/lib/models/action.cpp +++ b/src/lib/models/action.cpp @@ -21,7 +21,11 @@ void obelisk::Action::selectByName(sqlite3* dbConnection) sqlite3_stmt* ppStmt = nullptr; - auto result = sqlite3_prepare_v2(dbConnection, "SELECT id, name FROM action WHERE name=?", -1, &ppStmt, nullptr); + auto result = sqlite3_prepare_v2(dbConnection, + "SELECT id, name FROM action WHERE name=?", + -1, + &ppStmt, + nullptr); if (result != SQLITE_OK) { @@ -85,13 +89,18 @@ void obelisk::Action::insert(sqlite3* dbConnection) sqlite3_stmt* ppStmt = nullptr; - auto result = sqlite3_prepare_v2(dbConnection, "INSERT INTO action (name) VALUES (?)", -1, &ppStmt, nullptr); + auto result = sqlite3_prepare_v2(dbConnection, + "INSERT INTO action (name) VALUES (?)", + -1, + &ppStmt, + nullptr); if (result != SQLITE_OK) { throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); } - result = sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_TRANSIENT); + result + = sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_TRANSIENT); switch (result) { case SQLITE_OK : @@ -118,7 +127,8 @@ void obelisk::Action::insert(sqlite3* dbConnection) sqlite3_set_last_insert_rowid(dbConnection, 0); break; case SQLITE_CONSTRAINT : - throw obelisk::DatabaseConstraintException(sqlite3_errmsg(dbConnection)); + throw obelisk::DatabaseConstraintException( + sqlite3_errmsg(dbConnection)); case SQLITE_BUSY : throw obelisk::DatabaseBusyException(); break; diff --git a/src/lib/models/action.h b/src/lib/models/action.h index c45c536..2d06c03 100644 --- a/src/lib/models/action.h +++ b/src/lib/models/action.h @@ -8,7 +8,8 @@ namespace obelisk { /** - * @brief The Action model represents an action to take when a fact is true or false. + * @brief The Action model represents an action to take when a fact is true + * or false. * */ class Action @@ -107,14 +108,16 @@ namespace obelisk void setName(std::string name); /** - * @brief Select an Action from the datbase based on the object name. + * @brief Select an Action from the datbase based on the object + * name. * * @param[in] dbConnection The database connection to use. */ void selectByName(sqlite3* dbConnection); /** - * @brief Insert an Action into the KnowledgeBase based on the object's fields. + * @brief Insert an Action into the KnowledgeBase based on the + * object's fields. * * @param[in] dbConnection The database connection to use. */ diff --git a/src/lib/models/entity.cpp b/src/lib/models/entity.cpp index 8d9250f..d90da0a 100644 --- a/src/lib/models/entity.cpp +++ b/src/lib/models/entity.cpp @@ -21,7 +21,11 @@ void obelisk::Entity::selectByName(sqlite3* dbConnection) sqlite3_stmt* ppStmt = nullptr; - auto result = sqlite3_prepare_v2(dbConnection, "SELECT id, name FROM entity WHERE name=?", -1, &ppStmt, nullptr); + auto result = sqlite3_prepare_v2(dbConnection, + "SELECT id, name FROM entity WHERE name=?", + -1, + &ppStmt, + nullptr); if (result != SQLITE_OK) { @@ -85,13 +89,18 @@ void obelisk::Entity::insert(sqlite3* dbConnection) sqlite3_stmt* ppStmt = nullptr; - auto result = sqlite3_prepare_v2(dbConnection, "INSERT INTO entity (name) VALUES (?)", -1, &ppStmt, nullptr); + auto result = sqlite3_prepare_v2(dbConnection, + "INSERT INTO entity (name) VALUES (?)", + -1, + &ppStmt, + nullptr); if (result != SQLITE_OK) { throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); } - result = sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_TRANSIENT); + result + = sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_TRANSIENT); switch (result) { case SQLITE_OK : @@ -118,7 +127,8 @@ void obelisk::Entity::insert(sqlite3* dbConnection) sqlite3_set_last_insert_rowid(dbConnection, 0); break; case SQLITE_CONSTRAINT : - throw obelisk::DatabaseConstraintException(sqlite3_errmsg(dbConnection)); + throw obelisk::DatabaseConstraintException( + sqlite3_errmsg(dbConnection)); case SQLITE_BUSY : throw obelisk::DatabaseBusyException(); break; diff --git a/src/lib/models/entity.h b/src/lib/models/entity.h index 8f4ee1f..0241575 100644 --- a/src/lib/models/entity.h +++ b/src/lib/models/entity.h @@ -8,7 +8,8 @@ namespace obelisk { /** - * @brief The Entity model represents either a left or right side entity, typically used in facts and rules. + * @brief The Entity model represents either a left or right side entity, + * typically used in facts and rules. * */ class Entity @@ -107,14 +108,16 @@ namespace obelisk void setName(std::string name); /** - * @brief Select an Entity from the KnowledgeBase based on the object's name. + * @brief Select an Entity from the KnowledgeBase based on the + * object's name. * * @param[in] dbConnection The database connection to use. */ void selectByName(sqlite3* dbConnection); /** - * @brief Insert an Entity into the KnowledgeBase based on the object's fields. + * @brief Insert an Entity into the KnowledgeBase based on the + * object's fields. * * @param[in] dbConnection The database connection to use. */ diff --git a/src/lib/models/error.h b/src/lib/models/error.h index 7e88a13..ba28bd7 100644 --- a/src/lib/models/error.h +++ b/src/lib/models/error.h @@ -35,14 +35,16 @@ namespace obelisk * @param[in] errorCode The error code that came from sqlite. */ DatabaseException(const int errorCode) : - errorMessage_("database error " + std::to_string(errorCode) + " ocurred") + errorMessage_( + "database error " + std::to_string(errorCode) + " ocurred") { } /** * @brief Construct a new DatabaseException object. * - * @param[in] errorMessage The error message to describe the exception. + * @param[in] errorMessage The error message to describe the + * exception. */ DatabaseException(const std::string& errorMessage) : errorMessage_(errorMessage) @@ -71,7 +73,8 @@ namespace obelisk }; /** - * @brief Exception thrown if the string or blob size exceeds sqlite's limits. + * @brief Exception thrown if the string or blob size exceeds sqlite's + * limits. * */ class DatabaseSizeException : public obelisk::DatabaseException @@ -105,7 +108,8 @@ namespace obelisk }; /** - * @brief Exception thrown if there is not enough memory to perform the operation. + * @brief Exception thrown if there is not enough memory to perform the + * operation. * */ class DatabaseMemoryException : public obelisk::DatabaseException @@ -134,7 +138,8 @@ namespace obelisk */ DatabaseBusyException() { - setErrorMessage("database was busy and operation was not performed"); + setErrorMessage( + "database was busy and operation was not performed"); } }; @@ -175,7 +180,8 @@ namespace obelisk /** * @brief Construct a new DatabaseConstraintException object. * - * @param[in] errorMessage The error message to send when the constraint is violated. + * @param[in] errorMessage The error message to send when the + * constraint is violated. */ DatabaseConstraintException(const std::string& errorMessage) { diff --git a/src/lib/models/fact.cpp b/src/lib/models/fact.cpp index eafe955..ed6dad5 100644 --- a/src/lib/models/fact.cpp +++ b/src/lib/models/fact.cpp @@ -36,7 +36,8 @@ void obelisk::Fact::selectById(sqlite3* dbConnection) } else { - query = "SELECT id, left_entity, right_entity, verb, is_true FROM fact WHERE (id=?)"; + query + = "SELECT id, left_entity, right_entity, verb, is_true FROM fact WHERE (id=?)"; } auto result = sqlite3_prepare_v2(dbConnection, query, -1, &ppStmt, nullptr); if (result != SQLITE_OK) @@ -259,7 +260,8 @@ void obelisk::Fact::insert(sqlite3* dbConnection) sqlite3_set_last_insert_rowid(dbConnection, 0); break; case SQLITE_CONSTRAINT : - throw obelisk::DatabaseConstraintException(sqlite3_errmsg(dbConnection)); + throw obelisk::DatabaseConstraintException( + sqlite3_errmsg(dbConnection)); case SQLITE_BUSY : throw obelisk::DatabaseBusyException(); break; @@ -287,7 +289,11 @@ void obelisk::Fact::updateIsTrue(sqlite3* dbConnection) sqlite3_stmt* ppStmt = nullptr; - auto result = sqlite3_prepare_v2(dbConnection, "UPDATE fact SET is_true=? WHERE id=?", -1, &ppStmt, nullptr); + auto result = sqlite3_prepare_v2(dbConnection, + "UPDATE fact SET is_true=? WHERE id=?", + -1, + &ppStmt, + nullptr); if (result != SQLITE_OK) { throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); @@ -338,7 +344,8 @@ void obelisk::Fact::updateIsTrue(sqlite3* dbConnection) // Row updated break; case SQLITE_CONSTRAINT : - throw obelisk::DatabaseConstraintException(sqlite3_errmsg(dbConnection)); + throw obelisk::DatabaseConstraintException( + sqlite3_errmsg(dbConnection)); case SQLITE_BUSY : throw obelisk::DatabaseBusyException(); break; diff --git a/src/lib/models/fact.h b/src/lib/models/fact.h index 1232224..6f8c7a6 100644 --- a/src/lib/models/fact.h +++ b/src/lib/models/fact.h @@ -10,7 +10,8 @@ namespace obelisk { /** - * @brief The Fact model represents truth in the releationship between two entities separated by a verb. + * @brief The Fact model represents truth in the releationship between two + * entities separated by a verb. * */ class Fact @@ -35,7 +36,8 @@ namespace obelisk obelisk::Entity rightEntity_; /** - * @brief The Verb that represents the relationship in the expression. + * @brief The Verb that represents the relationship in the + * expression. * */ obelisk::Verb verb_; @@ -77,12 +79,17 @@ namespace obelisk /** * @brief Construct a new Fact object. * - * @param[in] leftEntity The Entity on the left side of the expression. - * @param[in] rightEntity The Entity on the right side of the expression. + * @param[in] leftEntity The Entity on the left side of the + * expression. + * @param[in] rightEntity The Entity on the right side of the + * expression. * @param[in] verb The Verb separating the entities. * @param[in] isTrue Whether or not the fact is true. */ - Fact(obelisk::Entity leftEntity, obelisk::Entity rightEntity, obelisk::Verb verb, bool isTrue = false) : + Fact(obelisk::Entity leftEntity, + obelisk::Entity rightEntity, + obelisk::Verb verb, + bool isTrue = false) : id_(0), leftEntity_(leftEntity), rightEntity_(rightEntity), @@ -95,8 +102,10 @@ namespace obelisk * @brief Construct a new Fact object. * * @param[in] id The ID of the Fact in the KnowledgeBase. - * @param[in] leftEntity The Entity on the left side of the expression. - * @param[in] rightEntity The Entity on the right side of the expression. + * @param[in] leftEntity The Entity on the left side of the + * expression. + * @param[in] rightEntity The Entity on the right side of the + * expression. * @param[in] verb The Verb separating the entities. * @param[in] isTrue Whether or not the fact is true. */ @@ -192,7 +201,8 @@ namespace obelisk void setIsTrue(bool isTrue); /** - * @brief Select the Fact from the KnowledgeBase by IDs of the sub-objects. + * @brief Select the Fact from the KnowledgeBase by IDs of the + * sub-objects. * * @param[in] dbConnection The database connection to use. */ @@ -206,7 +216,8 @@ namespace obelisk void insert(sqlite3* dbConnection); /** - * @brief Update whether or not the fact is true in the KnowledgeBase. + * @brief Update whether or not the fact is true in the + * KnowledgeBase. * * @param[in] dbConnection The database connection. */ diff --git a/src/lib/models/rule.cpp b/src/lib/models/rule.cpp index 9184b6c..fb2722c 100644 --- a/src/lib/models/rule.cpp +++ b/src/lib/models/rule.cpp @@ -111,8 +111,11 @@ void obelisk::Rule::insert(sqlite3* dbConnection) sqlite3_stmt* ppStmt = nullptr; - auto result - = sqlite3_prepare_v2(dbConnection, "INSERT INTO rule (fact, reason) VALUES (?, ?)", -1, &ppStmt, nullptr); + auto result = sqlite3_prepare_v2(dbConnection, + "INSERT INTO rule (fact, reason) VALUES (?, ?)", + -1, + &ppStmt, + nullptr); if (result != SQLITE_OK) { throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); @@ -164,7 +167,8 @@ void obelisk::Rule::insert(sqlite3* dbConnection) sqlite3_set_last_insert_rowid(dbConnection, 0); break; case SQLITE_CONSTRAINT : - throw obelisk::DatabaseConstraintException(sqlite3_errmsg(dbConnection)); + throw obelisk::DatabaseConstraintException( + sqlite3_errmsg(dbConnection)); case SQLITE_BUSY : throw obelisk::DatabaseBusyException(); break; @@ -183,7 +187,9 @@ void obelisk::Rule::insert(sqlite3* dbConnection) } } -void obelisk::Rule::selectByReason(sqlite3* dbConnection, int reasonId, std::vector& rules) +void obelisk::Rule::selectByReason(sqlite3* dbConnection, + int reasonId, + std::vector& rules) { if (dbConnection == nullptr) { @@ -192,8 +198,11 @@ void obelisk::Rule::selectByReason(sqlite3* dbConnection, int reasonId, std::vec sqlite3_stmt* ppStmt = nullptr; - auto result - = sqlite3_prepare_v2(dbConnection, "SELECT id, fact, reason FROM rule WHERE (reason=?)", -1, &ppStmt, nullptr); + auto result = sqlite3_prepare_v2(dbConnection, + "SELECT id, fact, reason FROM rule WHERE (reason=?)", + -1, + &ppStmt, + nullptr); if (result != SQLITE_OK) { throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); diff --git a/src/lib/models/rule.h b/src/lib/models/rule.h index 04d3efb..efbdced 100644 --- a/src/lib/models/rule.h +++ b/src/lib/models/rule.h @@ -134,7 +134,8 @@ namespace obelisk void setReason(obelisk::Fact reason); /** - * @brief Select the Rule from the KnowledgeBase by IDs of the sub-objects. + * @brief Select the Rule from the KnowledgeBase by IDs of the + * sub-objects. * * @param[in] dbConnection The database connection to use. */ @@ -146,7 +147,9 @@ namespace obelisk * @param[in] dbConnection The database connection to use. * @param[out] rules The rules to fill in from the database. */ - static void selectByReason(sqlite3* dbConnection, int reasonId, std::vector& rules); + static void selectByReason(sqlite3* dbConnection, + int reasonId, + std::vector& rules); /** * @brief Insert the Rule into the KnowledgeBase. diff --git a/src/lib/models/suggest_action.cpp b/src/lib/models/suggest_action.cpp index f92a1c7..4226079 100644 --- a/src/lib/models/suggest_action.cpp +++ b/src/lib/models/suggest_action.cpp @@ -208,7 +208,8 @@ void obelisk::SuggestAction::insert(sqlite3* dbConnection) sqlite3_set_last_insert_rowid(dbConnection, 0); break; case SQLITE_CONSTRAINT : - throw obelisk::DatabaseConstraintException(sqlite3_errmsg(dbConnection)); + throw obelisk::DatabaseConstraintException( + sqlite3_errmsg(dbConnection)); case SQLITE_BUSY : throw obelisk::DatabaseBusyException(); break; diff --git a/src/lib/models/suggest_action.h b/src/lib/models/suggest_action.h index f6854e4..16d123e 100644 --- a/src/lib/models/suggest_action.h +++ b/src/lib/models/suggest_action.h @@ -9,7 +9,8 @@ namespace obelisk { /** - * @brief The SuggestAction model representas the actions to take depending on if the Fact is true or false. + * @brief The SuggestAction model representas the actions to take depending + * on if the Fact is true or false. * */ class SuggestAction @@ -72,7 +73,9 @@ namespace obelisk * @param[in] trueAction The true Action. * @param[in] falseAction The false Action. */ - SuggestAction(obelisk::Fact fact, obelisk::Action trueAction, obelisk::Action falseAction) : + SuggestAction(obelisk::Fact fact, + obelisk::Action trueAction, + obelisk::Action falseAction) : id_(0), fact_(fact), trueAction_(trueAction), @@ -88,7 +91,10 @@ namespace obelisk * @param[in] trueAction The true Action. * @param[in] falseAction The false Action. */ - SuggestAction(int id, obelisk::Fact fact, obelisk::Action trueAction, obelisk::Action falseAction) : + SuggestAction(int id, + obelisk::Fact fact, + obelisk::Action trueAction, + obelisk::Action falseAction) : id_(id), fact_(fact), trueAction_(trueAction), @@ -160,7 +166,8 @@ namespace obelisk void setFalseAction(obelisk::Action falseAction); /** - * @brief Select the SuggestAction from the KnowledgeBase by IDs of the sub-objects. + * @brief Select the SuggestAction from the KnowledgeBase by IDs of + * the sub-objects. * * @param[in] dbConnection The database connection to use. */ diff --git a/src/lib/models/verb.cpp b/src/lib/models/verb.cpp index 61b46c1..cd8c40b 100644 --- a/src/lib/models/verb.cpp +++ b/src/lib/models/verb.cpp @@ -23,7 +23,11 @@ void obelisk::Verb::selectByName(sqlite3* dbConnection) sqlite3_stmt* ppStmt = nullptr; - auto result = sqlite3_prepare_v2(dbConnection, "SELECT id, name FROM verb WHERE name=?", -1, &ppStmt, nullptr); + auto result = sqlite3_prepare_v2(dbConnection, + "SELECT id, name FROM verb WHERE name=?", + -1, + &ppStmt, + nullptr); if (result != SQLITE_OK) { throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); @@ -85,13 +89,18 @@ void obelisk::Verb::insert(sqlite3* dbConnection) sqlite3_stmt* ppStmt = nullptr; - auto result = sqlite3_prepare_v2(dbConnection, "INSERT INTO verb (name) VALUES (?)", -1, &ppStmt, nullptr); + auto result = sqlite3_prepare_v2(dbConnection, + "INSERT INTO verb (name) VALUES (?)", + -1, + &ppStmt, + nullptr); if (result != SQLITE_OK) { throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); } - result = sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_TRANSIENT); + result + = sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_TRANSIENT); switch (result) { case SQLITE_OK : @@ -118,7 +127,8 @@ void obelisk::Verb::insert(sqlite3* dbConnection) sqlite3_set_last_insert_rowid(dbConnection, 0); break; case SQLITE_CONSTRAINT : - throw obelisk::DatabaseConstraintException(sqlite3_errmsg(dbConnection)); + throw obelisk::DatabaseConstraintException( + sqlite3_errmsg(dbConnection)); case SQLITE_BUSY : throw obelisk::DatabaseBusyException(); break; diff --git a/src/lib/models/verb.h b/src/lib/models/verb.h index 2edc546..150e86f 100644 --- a/src/lib/models/verb.h +++ b/src/lib/models/verb.h @@ -8,7 +8,8 @@ namespace obelisk { /** - * @brief The Verb model represents a verb which is used to connnect entities. + * @brief The Verb model represents a verb which is used to connnect + * entities. * */ class Verb diff --git a/src/main.cpp b/src/main.cpp index e7f5e02..3aa2422 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,13 +10,15 @@ #include #include -int obelisk::mainLoop(const std::vector& sourceFiles, const std::string& kbFile) +int obelisk::mainLoop(const std::vector& sourceFiles, + const std::string& kbFile) { std::unique_ptr kb; try { - kb = std::unique_ptr {new obelisk::KnowledgeBase(kbFile.c_str())}; + kb = std::unique_ptr { + new obelisk::KnowledgeBase(kbFile.c_str())}; } catch (obelisk::KnowledgeBaseException& exception) { @@ -28,7 +30,8 @@ int obelisk::mainLoop(const std::vector& sourceFiles, const std::st std::shared_ptr lexer; try { - lexer = std::shared_ptr {new obelisk::Lexer(sourceFiles[file++])}; + lexer = std::shared_ptr { + new obelisk::Lexer(sourceFiles[file++])}; } catch (obelisk::LexerException& exception) { @@ -53,14 +56,16 @@ int obelisk::mainLoop(const std::vector& sourceFiles, const std::st switch (parser->getCurrentToken()) { case obelisk::Lexer::kTokenEof : - // end of source file found, create a new lexer and pass it to the parser to use + // end of source file found, create a new lexer and pass it to + // the parser to use if (file >= sourceFiles.size()) { return EXIT_SUCCESS; } try { - lexer = std::shared_ptr {new obelisk::Lexer(sourceFiles[file++])}; + lexer = std::shared_ptr { + new obelisk::Lexer(sourceFiles[file++])}; parser->setLexer(lexer); // prime the first token in the parser parser->getNextToken(); @@ -138,7 +143,11 @@ int main(int argc, char** argv) while (true) { int option_index = 0; - switch (getopt_long(argc, argv, "k:hv", obelisk::long_options, &option_index)) + switch (getopt_long(argc, + argv, + "k:hv", + obelisk::long_options, + &option_index)) { case 'k' : kbFile = std::string(optarg); diff --git a/src/main.h b/src/main.h index 3d79355..38e7a29 100644 --- a/src/main.h +++ b/src/main.h @@ -1,7 +1,8 @@ #include /** - * @brief The obelisk namespace contains everything needed to compile obelisk code. + * @brief The obelisk namespace contains everything needed to compile obelisk + * code. * */ namespace obelisk @@ -42,5 +43,6 @@ Options: * * @return int Returns EXIT_SUCCESS or EXIT_FAILURE. */ - int mainLoop(const std::vector &sourceFiles, const std::string &kbFile); + int mainLoop(const std::vector &sourceFiles, + const std::string &kbFile); } // namespace obelisk diff --git a/src/parser.cpp b/src/parser.cpp index b3207c9..907fa6b 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -42,13 +42,15 @@ void obelisk::Parser::setCurrentToken(int currentToken) currentToken_ = currentToken; } -std::unique_ptr obelisk::Parser::logError(const char* str) +std::unique_ptr obelisk::Parser::logError( + const char* str) { fprintf(stderr, "Error: %s\n", str); return nullptr; } -std::unique_ptr obelisk::Parser::logErrorPrototype(const char* str) +std::unique_ptr obelisk::Parser::logErrorPrototype( + const char* str) { logError(str); return nullptr; @@ -82,12 +84,14 @@ std::unique_ptr obelisk::Parser::parsePrimary() std::unique_ptr obelisk::Parser::parseNumberExpression() { - auto result = std::make_unique(getLexer()->getNumberValue()); + auto result = std::make_unique( + getLexer()->getNumberValue()); getNextToken(); return result; } -std::unique_ptr obelisk::Parser::parseParenthesisExpression() +std::unique_ptr + obelisk::Parser::parseParenthesisExpression() { getNextToken(); auto v = parseExpression(); @@ -104,7 +108,8 @@ std::unique_ptr obelisk::Parser::parseParenthesisExpress return v; } -std::unique_ptr obelisk::Parser::parseIdentifierExpression() +std::unique_ptr + obelisk::Parser::parseIdentifierExpression() { std::string idName = getLexer()->getIdentifier(); getNextToken(); @@ -174,7 +179,8 @@ std::unique_ptr obelisk::Parser::parsePrototype() getNextToken(); - return std::make_unique(functionName, std::move(argNames)); + return std::make_unique(functionName, + std::move(argNames)); } std::unique_ptr obelisk::Parser::parseDefinition() @@ -188,7 +194,8 @@ std::unique_ptr obelisk::Parser::parseDefinition() if (auto expression = parseExpression()) { - return std::make_unique(std::move(prototype), std::move(expression)); + return std::make_unique(std::move(prototype), + std::move(expression)); } return nullptr; @@ -199,8 +206,10 @@ std::unique_ptr obelisk::Parser::parseTopLevelExpression() if (auto expression = parseExpression()) { // Make an anonymous prototype - auto prototype = std::make_unique("__anon_expr", std::vector()); - return std::make_unique(std::move(prototype), std::move(expression)); + auto prototype = std::make_unique("__anon_expr", + std::vector()); + return std::make_unique(std::move(prototype), + std::move(expression)); } return nullptr; } @@ -218,7 +227,8 @@ void obelisk::Parser::parseAction(obelisk::SuggestAction& suggestAction) getNextToken(); if (getCurrentToken() != '(') { - throw obelisk::ParserException("expected '(' but got '" + std::to_string(getCurrentToken()) + "'"); + throw obelisk::ParserException( + "expected '(' but got '" + std::to_string(getCurrentToken()) + "'"); } syntax.push('('); @@ -226,7 +236,8 @@ void obelisk::Parser::parseAction(obelisk::SuggestAction& suggestAction) getNextToken(); if (getLexer()->getIdentifier() != "if") { - throw obelisk::ParserException("expected 'if' but got '" + getLexer()->getIdentifier() + "'"); + throw obelisk::ParserException( + "expected 'if' but got '" + getLexer()->getIdentifier() + "'"); } bool getEntity {true}; @@ -302,7 +313,8 @@ void obelisk::Parser::parseAction(obelisk::SuggestAction& suggestAction) { if (!isalpha(letter)) { - throw new obelisk::ParserException("non alphabetic symbol in verb"); + throw new obelisk::ParserException( + "non alphabetic symbol in verb"); } } getEntity = true; @@ -357,7 +369,8 @@ void obelisk::Parser::parseAction(obelisk::SuggestAction& suggestAction) { if (getCurrentToken() == ')') { - // closing parenthesis found, make sure we have everything needed + // closing parenthesis found, make sure we have everything + // needed if (syntax.top() != '(') { throw obelisk::ParserException("unexpected ')'"); @@ -421,8 +434,9 @@ void obelisk::Parser::parseAction(obelisk::SuggestAction& suggestAction) } } - suggestAction.setFact( - obelisk::Fact(obelisk::Entity(leftEntity), obelisk::Entity(rightEntity), obelisk::Verb(verb))); + suggestAction.setFact(obelisk::Fact(obelisk::Entity(leftEntity), + obelisk::Entity(rightEntity), + obelisk::Verb(verb))); suggestAction.setTrueAction(obelisk::Action(trueAction)); suggestAction.setFalseAction(obelisk::Action(falseAction)); } @@ -434,7 +448,8 @@ void obelisk::Parser::parseRule(obelisk::Rule& rule) getNextToken(); if (getCurrentToken() != '(') { - throw obelisk::ParserException("expected '(' but got '" + std::to_string(getCurrentToken()) + "'"); + throw obelisk::ParserException( + "expected '(' but got '" + std::to_string(getCurrentToken()) + "'"); } syntax.push('('); @@ -509,7 +524,8 @@ void obelisk::Parser::parseRule(obelisk::Rule& rule) { if (getCurrentToken() == ')') { - // closing parenthesis found, make sure we have everything needed + // closing parenthesis found, make sure we have everything + // needed if (syntax.top() != '(') { throw obelisk::ParserException("unexpected ')'"); @@ -536,12 +552,14 @@ void obelisk::Parser::parseRule(obelisk::Rule& rule) if (leftReasonEntity == "") { - throw obelisk::ParserException("missing left reason entity"); + throw obelisk::ParserException( + "missing left reason entity"); } if (rightReasonEntity == "") { - throw obelisk::ParserException("missing right reason entity"); + throw obelisk::ParserException( + "missing right reason entity"); } if (reasonVerb == "") @@ -579,7 +597,8 @@ void obelisk::Parser::parseRule(obelisk::Rule& rule) { if (!isalpha(letter)) { - throw new obelisk::ParserException("non alphabetic symbol in verb"); + throw new obelisk::ParserException( + "non alphabetic symbol in verb"); } } getEntity = true; @@ -592,7 +611,8 @@ void obelisk::Parser::parseRule(obelisk::Rule& rule) { if (!isalpha(letter)) { - throw new obelisk::ParserException("non alphabetic symbol in verb"); + throw new obelisk::ParserException( + "non alphabetic symbol in verb"); } } getEntity = true; @@ -602,7 +622,9 @@ void obelisk::Parser::parseRule(obelisk::Rule& rule) } } - rule.setFact(obelisk::Fact(obelisk::Entity(leftEntity), obelisk::Entity(rightEntity), obelisk::Verb(verb))); + rule.setFact(obelisk::Fact(obelisk::Entity(leftEntity), + obelisk::Entity(rightEntity), + obelisk::Verb(verb))); rule.setReason(obelisk::Fact(obelisk::Entity(leftReasonEntity), obelisk::Entity(rightReasonEntity), obelisk::Verb(reasonVerb))); @@ -615,7 +637,8 @@ void obelisk::Parser::parseFact(std::vector& facts) getNextToken(); if (getCurrentToken() != '(') { - throw obelisk::ParserException("expected '(' but got '" + std::to_string(getCurrentToken()) + "'"); + throw obelisk::ParserException( + "expected '(' but got '" + std::to_string(getCurrentToken()) + "'"); } syntax.push('('); @@ -671,7 +694,8 @@ void obelisk::Parser::parseFact(std::vector& facts) { if (getCurrentToken() == ')') { - // closing parenthesis found, make sure we have everything needed + // closing parenthesis found, make sure we have everything + // needed if (syntax.top() != '(') { throw obelisk::ParserException("unexpected ')'"); @@ -688,12 +712,14 @@ void obelisk::Parser::parseFact(std::vector& facts) if (leftEntities.size() == 0) { - throw obelisk::ParserException("missing left side entities"); + throw obelisk::ParserException( + "missing left side entities"); } if (rightEntities.size() == 0) { - throw obelisk::ParserException("missing right side entities"); + throw obelisk::ParserException( + "missing right side entities"); } getNextToken(); @@ -723,7 +749,8 @@ void obelisk::Parser::parseFact(std::vector& facts) { if (!isalpha(letter)) { - throw new obelisk::ParserException("non alphabetic symbol in verb"); + throw new obelisk::ParserException( + "non alphabetic symbol in verb"); } } getEntity = true; @@ -736,8 +763,10 @@ void obelisk::Parser::parseFact(std::vector& facts) { for (auto& rightEntity : rightEntities) { - facts.push_back( - obelisk::Fact(obelisk::Entity(leftEntity), obelisk::Entity(rightEntity), obelisk::Verb(verb), true)); + facts.push_back(obelisk::Fact(obelisk::Entity(leftEntity), + obelisk::Entity(rightEntity), + obelisk::Verb(verb), + true)); } } } @@ -850,7 +879,8 @@ void obelisk::Parser::handleFact(std::unique_ptr& kb) } } -void obelisk::Parser::insertEntity(std::unique_ptr& kb, obelisk::Entity& entity) +void obelisk::Parser::insertEntity(std::unique_ptr& kb, + obelisk::Entity& entity) { std::vector entities {entity}; kb->addEntities(entities); @@ -862,12 +892,14 @@ void obelisk::Parser::insertEntity(std::unique_ptr& kb, kb->getEntity(entity); if (entity.getId() == 0) { - throw obelisk::ParserException("entity could not be inserted into the database"); + throw obelisk::ParserException( + "entity could not be inserted into the database"); } } } -void obelisk::Parser::insertVerb(std::unique_ptr& kb, obelisk::Verb& verb) +void obelisk::Parser::insertVerb(std::unique_ptr& kb, + obelisk::Verb& verb) { std::vector verbs {verb}; kb->addVerbs(verbs); @@ -879,12 +911,14 @@ void obelisk::Parser::insertVerb(std::unique_ptr& kb, ob kb->getVerb(verb); if (verb.getId() == 0) { - throw obelisk::ParserException("verb could not be inserted into the database"); + throw obelisk::ParserException( + "verb could not be inserted into the database"); } } } -void obelisk::Parser::insertAction(std::unique_ptr& kb, obelisk::Action& action) +void obelisk::Parser::insertAction(std::unique_ptr& kb, + obelisk::Action& action) { std::vector actions {action}; kb->addActions(actions); @@ -896,12 +930,15 @@ void obelisk::Parser::insertAction(std::unique_ptr& kb, kb->getAction(action); if (action.getId() == 0) { - throw obelisk::ParserException("action could not be inserted into the database"); + throw obelisk::ParserException( + "action could not be inserted into the database"); } } } -void obelisk::Parser::insertFact(std::unique_ptr& kb, obelisk::Fact& fact, bool updateIsTrue) +void obelisk::Parser::insertFact(std::unique_ptr& kb, + obelisk::Fact& fact, + bool updateIsTrue) { std::vector facts {fact}; kb->addFacts(facts); @@ -913,7 +950,8 @@ void obelisk::Parser::insertFact(std::unique_ptr& kb, ob kb->getFact(fact); if (fact.getId() == 0) { - throw obelisk::ParserException("fact could not be inserted into the database"); + throw obelisk::ParserException( + "fact could not be inserted into the database"); } else { @@ -926,7 +964,8 @@ void obelisk::Parser::insertFact(std::unique_ptr& kb, ob } } -void obelisk::Parser::insertSuggestAction(std::unique_ptr& kb, +void obelisk::Parser::insertSuggestAction( + std::unique_ptr& kb, obelisk::SuggestAction& suggestAction) { std::vector suggestActions {suggestAction}; @@ -939,12 +978,14 @@ void obelisk::Parser::insertSuggestAction(std::unique_ptrgetSuggestAction(suggestAction); if (suggestAction.getId() == 0) { - throw obelisk::ParserException("suggest_action could not be inserted into the database"); + throw obelisk::ParserException( + "suggest_action could not be inserted into the database"); } } } -void obelisk::Parser::insertRule(std::unique_ptr& kb, obelisk::Rule& rule) +void obelisk::Parser::insertRule(std::unique_ptr& kb, + obelisk::Rule& rule) { std::vector rules {rule}; kb->addRules(rules); @@ -956,7 +997,8 @@ void obelisk::Parser::insertRule(std::unique_ptr& kb, ob kb->getRule(rule); if (rule.getId() == 0) { - throw obelisk::ParserException("rule could not be inserted into the database"); + throw obelisk::ParserException( + "rule could not be inserted into the database"); } } } diff --git a/src/parser.h b/src/parser.h index 436fa13..34b4abf 100644 --- a/src/parser.h +++ b/src/parser.h @@ -18,14 +18,16 @@ namespace obelisk { /** - * @brief The Parser is responsible for analyzing the language's key words and taking action based on its analysis. + * @brief The Parser is responsible for analyzing the language's key words + * and taking action based on its analysis. * */ class Parser { private: /** - * @brief The Lexer object that the Parser is using to Parse a specific source file. + * @brief The Lexer object that the Parser is using to Parse a + * specific source file. * */ std::shared_ptr lexer_; @@ -47,7 +49,8 @@ namespace obelisk * @brief Log errors from the LLVM parsing. * * @param[in] str The error message. - * @return std::unique_ptr Returns the AST expression that caused the error. + * @return std::unique_ptr Returns the AST + * expression that caused the error. */ std::unique_ptr logError(const char* str); @@ -55,70 +58,82 @@ namespace obelisk * @brief Log errors from the LLVM parsing involving the prototypes. * * @param[in] str The error message. - * @return std::unique_ptr Returns the AST for the prototype. + * @return std::unique_ptr Returns the AST + * for the prototype. */ - std::unique_ptr logErrorPrototype(const char* str); + std::unique_ptr logErrorPrototype( + const char* str); /** * @brief The AST expression parser. * - * @return std::unique_ptr Returns the parsed AST expression. + * @return std::unique_ptr Returns the + * parsed AST expression. */ std::unique_ptr parseExpression(); /** * @brief The AST number expression parser. * - * @return std::unique_ptr Returns the parsed AST expression. + * @return std::unique_ptr Returns the + * parsed AST expression. */ std::unique_ptr parseNumberExpression(); /** * @brief The AST parenthesis expression parser. * - * @return std::unique_ptr Returns the parsed AST expression. + * @return std::unique_ptr Returns the + * parsed AST expression. */ - std::unique_ptr parseParenthesisExpression(); + std::unique_ptr + parseParenthesisExpression(); /** * @brief The AST identifier expression parser. * - * @return std::unique_ptr Returns the parsed AST expression. + * @return std::unique_ptr Returns the + * parsed AST expression. */ std::unique_ptr parseIdentifierExpression(); /** * @brief The AST primary expression parser. * - * @return std::unique_ptr Returns the parsed AST expression. + * @return std::unique_ptr Returns the + * parsed AST expression. */ std::unique_ptr parsePrimary(); /** * @brief The AST prototype parser. * - * @return std::unique_ptr Returns the parsed AST prototype expression. + * @return std::unique_ptr Returns the parsed + * AST prototype expression. */ std::unique_ptr parsePrototype(); /** * @brief The AST definition parser. * - * @return std::unique_ptr Returns the parsed AST definition expression. + * @return std::unique_ptr Returns the parsed + * AST definition expression. */ std::unique_ptr parseDefinition(); /** * @brief The AST top level expression parser. * - * @return std::unique_ptr Returns the parsed AST top level expression. + * @return std::unique_ptr Returns the parsed + * AST top level expression. */ std::unique_ptr parseTopLevelExpression(); /** * @brief The AST external definition parser. * - * @return std::unique_ptr Returns the parsed AST external definition. + * @return std::unique_ptr Returns the parsed + * AST external definition. */ std::unique_ptr parseExtern(); @@ -147,7 +162,8 @@ namespace obelisk /** * @brief Construct a new Parser object. * - * @param[in] lexer The lexer the parser uses to retrieve parts of the language. + * @param[in] lexer The lexer the parser uses to retrieve parts of + * the language. */ Parser(std::shared_ptr lexer) : lexer_(lexer) @@ -157,7 +173,8 @@ namespace obelisk /** * @brief Get the Lexer. * - * @return std::shared_ptr Returns the current Lexer in use by the Parser. + * @return std::shared_ptr Returns the current Lexer + * in use by the Parser. */ std::shared_ptr getLexer(); @@ -183,7 +200,8 @@ namespace obelisk int getNextToken(); /** - * @brief Parse the SuggestAction and then insert it into the KnowledgeBase. + * @brief Parse the SuggestAction and then insert it into the + * KnowledgeBase. * * @param[in] kb The KnowledgeBase to insert the SuggestAction into. */ @@ -207,55 +225,66 @@ namespace obelisk * @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. + * @param[in,out] entity The Entity to insert. It will contain the + * ID of the Entity after inserting it. */ - void insertEntity(std::unique_ptr& kb, obelisk::Entity& entity); + void insertEntity(std::unique_ptr& kb, + obelisk::Entity& entity); /** * @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. + * @param[in,out] verb The Verb to insert. It will contain the ID of + * the Verb after inserting it. */ - void insertVerb(std::unique_ptr& kb, obelisk::Verb& verb); + void insertVerb(std::unique_ptr& kb, + obelisk::Verb& verb); /** * @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. + * @param[in,out] action The Action to insert. It will contain the + * ID of the Action after inserting it. */ - void insertAction(std::unique_ptr& kb, obelisk::Action& action); + void insertAction(std::unique_ptr& kb, + obelisk::Action& action); /** * @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. + * @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. */ void insertFact(std::unique_ptr& kb, obelisk::Fact& fact, bool updateIsTrue = false); /** - * @brief Helper used to insert a SuggestAction into the KnowledgeBase. + * @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. + * @param[in,out] suggestAction The SuggestAction to insert. It will + * contain the ID of the SuggestAction after inserting it. */ - void insertSuggestAction(std::unique_ptr& kb, + void insertSuggestAction( + std::unique_ptr& kb, obelisk::SuggestAction& suggestAction); /** * @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. + * @param[in,out] rule The Rule to insert. It will contain the ID of + * the Rule after inserting it. */ - void insertRule(std::unique_ptr& kb, obelisk::Rule& rule); + void insertRule(std::unique_ptr& kb, + obelisk::Rule& rule); }; /**