From 4c4077646be5da6d70e153b67da5897914a2665f Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Sat, 15 Apr 2023 15:23:41 +0200 Subject: [PATCH] Separate out Types::ParseModel --- btparser/types.h | 2 ++ btparser/typesparser.cpp | 27 ++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/btparser/types.h b/btparser/types.h index f0ba339..988cf9b 100644 --- a/btparser/types.h +++ b/btparser/types.h @@ -209,5 +209,7 @@ namespace Types bool addType(const Type & t); bool visitMember(const Member & root, Visitor & visitor) const; }; + + bool ParseModel(const std::string& code, const std::string& owner, std::vector& errors, Model& model); }; diff --git a/btparser/typesparser.cpp b/btparser/typesparser.cpp index 28f1cd3..087bab3 100644 --- a/btparser/typesparser.cpp +++ b/btparser/typesparser.cpp @@ -1013,7 +1013,7 @@ struct Parser return false; } - void LoadModel(TypeManager typeManager, const std::string& owner, Model& model) + bool LoadModel(TypeManager& typeManager) { //Add all base struct/union types first to avoid errors later for (auto& su : model.structUnions) @@ -1024,6 +1024,7 @@ struct Parser //TODO properly handle errors dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add %s %s;\n"), su.isunion ? "union" : "struct", su.name.c_str()); su.name.clear(); //signal error + return false; } } @@ -1038,6 +1039,7 @@ struct Parser { //TODO properly handle errors dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add typedef %s %s;\n"), type.type.pretty().c_str(), type.name.c_str()); + return false; } } @@ -1051,6 +1053,7 @@ struct Parser { dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add enum %s;\n"), e.name.c_str()); e.name.clear(); // signal error + return false; } else { @@ -1059,6 +1062,7 @@ struct Parser if (!typeManager.AddEnumerator(e.name, v.name, v.value)) { dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add enum member %s.%s = %llu;\n"), e.name.c_str(), v.name.c_str(), v.value); + return false; } } } @@ -1073,6 +1077,7 @@ struct Parser //TODO properly handle errors dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add function %s %s()\n"), function.rettype.pretty().c_str(), function.name.c_str()); function.name.clear(); //signal error + return false; } } @@ -1088,6 +1093,7 @@ struct Parser { //TODO properly handle errors dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add member %s %s.%s;\n"), member.type.pretty().c_str(), su.name.c_str(), member.name.c_str()); + return false; } } } @@ -1107,12 +1113,15 @@ struct Parser { //TODO properly handle errors dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to add argument %s[%zu]: %s %s;\n"), function.name.c_str(), i, arg.type.pretty().c_str(), arg.name.c_str()); + return false; } } } + + return true; } - bool operator()(TypeManager& typeManager) + bool Parse() { std::string error; if (!lexer.DoLexing(tokens, error)) @@ -1139,13 +1148,21 @@ struct Parser model.functions.push_back(fn); } } - - LoadModel(typeManager, owner, model); return true; } }; bool TypeManager::ParseTypes(const std::string& code, const std::string& owner, std::vector& errors) { - return Parser(code, owner, errors)(*this); + Parser p(code, owner, errors); + return p.Parse() && p.LoadModel(*this); +} + +bool Types::ParseModel(const std::string& code, const std::string& owner, std::vector& errors, Model& model) +{ + Parser p(code, owner, errors); + if (!p.Parse()) + return false; + model = std::move(p.model); + return true; } \ No newline at end of file