diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/Impl/CommandsParser.cpp b/src/ZoneCodeGeneratorLib/Parsing/Commands/Impl/CommandsParser.cpp index fa4ed298..012a4000 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Commands/Impl/CommandsParser.cpp +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/Impl/CommandsParser.cpp @@ -17,7 +17,7 @@ #include "Parsing/Commands/Sequence/SequenceUse.h" CommandsParser::CommandsParser(CommandsLexer* lexer, IDataRepository* targetRepository) - : AbstractParser(lexer, std::make_unique()), + : AbstractParser(lexer, std::make_unique(targetRepository)), m_repository(targetRepository) { } diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/Impl/CommandsParserState.cpp b/src/ZoneCodeGeneratorLib/Parsing/Commands/Impl/CommandsParserState.cpp index e69de29b..004bd887 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Commands/Impl/CommandsParserState.cpp +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/Impl/CommandsParserState.cpp @@ -0,0 +1,42 @@ +#include "CommandsParserState.h" + +CommandsParserState::CommandsParserState(IDataRepository* repository) + : m_repository(repository), + m_in_use(nullptr) +{ +} + +const IDataRepository* CommandsParserState::GetRepository() const +{ + return m_repository; +} + +void CommandsParserState::AddBlock(std::unique_ptr block) const +{ + m_repository->Add(std::move(block)); +} + +void CommandsParserState::SetGame(std::string gameName) const +{ + m_repository->SetGame(std::move(gameName)); +} + +StructureInformation* CommandsParserState::GetInUse() const +{ + return m_in_use; +} + +void CommandsParserState::SetInUse(StructureInformation* structure) +{ + m_in_use = structure; +} + +bool CommandsParserState::GetMembersFromParts(const std::string& typeNameValue, StructureInformation* baseType, std::vector& members) +{ + return false; +} + +bool CommandsParserState::GetTypenameAndMembersFromParts(const std::string& typeNameValue, StructureInformation*& structure, std::vector& members) +{ + return false; +} diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/Impl/CommandsParserState.h b/src/ZoneCodeGeneratorLib/Parsing/Commands/Impl/CommandsParserState.h index 33856f96..a7803676 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Commands/Impl/CommandsParserState.h +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/Impl/CommandsParserState.h @@ -1,6 +1,27 @@ #pragma once +#include +#include + +#include "Utils/ClassUtils.h" +#include "Persistence/IDataRepository.h" + class CommandsParserState { + IDataRepository* m_repository; + StructureInformation* m_in_use; + public: -}; \ No newline at end of file + explicit CommandsParserState(IDataRepository* repository); + + _NODISCARD const IDataRepository* GetRepository() const; + + void AddBlock(std::unique_ptr block) const; + void SetGame(std::string gameName) const; + + _NODISCARD StructureInformation* GetInUse() const; + void SetInUse(StructureInformation* structure); + + bool GetMembersFromParts(const std::string& typeNameValue, StructureInformation* baseType, std::vector& members); + bool GetTypenameAndMembersFromParts(const std::string& typeNameValue, StructureInformation*& structure, std::vector& members); +}; diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/Matcher/CommandsCommonMatchers.cpp b/src/ZoneCodeGeneratorLib/Parsing/Commands/Matcher/CommandsCommonMatchers.cpp index 72f5d9e6..9a560f80 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Commands/Matcher/CommandsCommonMatchers.cpp +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/Matcher/CommandsCommonMatchers.cpp @@ -194,7 +194,12 @@ std::unique_ptr CommandsCommonMatchers::Evalu }).Tag(TAG_EVALUATION); } -std::unique_ptr CommandsCommonMatchers::ParseEvaluation(SequenceResult& result) +std::unique_ptr CommandsCommonMatchers::ParseEvaluation(CommandsParserState* state, SequenceResult& result) { + if (result.PeekAndRemoveIfTag(TAG_EVALUATION) != TAG_EVALUATION) + return nullptr; + + + return nullptr; } diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/Matcher/CommandsCommonMatchers.h b/src/ZoneCodeGeneratorLib/Parsing/Commands/Matcher/CommandsCommonMatchers.h index 5a9568bb..28ab35ee 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Commands/Matcher/CommandsCommonMatchers.h +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/Matcher/CommandsCommonMatchers.h @@ -4,6 +4,7 @@ #include #include "Domain/Evaluation/IEvaluation.h" +#include "Parsing/Commands/Impl/CommandsParserState.h" #include "Parsing/Commands/Impl/CommandsParserValue.h" #include "Parsing/Matcher/AbstractMatcher.h" #include "Parsing/Matcher/MatcherLabel.h" @@ -29,5 +30,5 @@ private: public: static std::unique_ptr Evaluation(const supplier_t* labelSupplier); - static std::unique_ptr ParseEvaluation(SequenceResult& result); + static std::unique_ptr ParseEvaluation(CommandsParserState* state, SequenceResult& result); }; diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceBlock.cpp b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceBlock.cpp index da163bf0..85f21dee 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceBlock.cpp +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceBlock.cpp @@ -7,15 +7,53 @@ SequenceBlock::SequenceBlock() { const CommandsMatcherFactory create(this); +#define DEFINE_FAST_FILE_BLOCK_TYPE(type) AddFastFileBlockToLookup(#type, FastFileBlockType::type) + DEFINE_FAST_FILE_BLOCK_TYPE(TEMP); + DEFINE_FAST_FILE_BLOCK_TYPE(RUNTIME); + DEFINE_FAST_FILE_BLOCK_TYPE(DELAY); + DEFINE_FAST_FILE_BLOCK_TYPE(NORMAL); +#undef DEFINE_FAST_FILE_BLOCK_TYPE + AddMatchers({ create.Keyword("block"), - create.Identifier().Capture(CAPTURE_BLOCK_ENUM_ENTRY), create.Identifier().Capture(CAPTURE_BLOCK_TYPE), + create.Identifier().Capture(CAPTURE_BLOCK_ENUM_ENTRY), create.Optional(create.Keyword("default").Tag(TAG_DEFAULT)), create.Char(';') }); } +void SequenceBlock::AddFastFileBlockToLookup(std::string name, const FastFileBlockType type) +{ + for (auto& c : name) + c = static_cast(tolower(c)); + + m_type_lookup[name] = type; +} + +bool SequenceBlock::GetFastFileBlockNameByType(const std::string& name, FastFileBlockType& type) const +{ + const auto foundEntry = m_type_lookup.find(name); + if (foundEntry == m_type_lookup.end()) + return false; + + type = foundEntry->second; + return true; +} + void SequenceBlock::ProcessMatch(CommandsParserState* state, SequenceResult& result) const { + const auto& enumEntryToken = result.NextCapture(CAPTURE_BLOCK_ENUM_ENTRY); + auto* enumMember = state->GetRepository()->GetEnumMemberByName(enumEntryToken.IdentifierValue()); + if (enumMember == nullptr) + throw ParsingException(enumEntryToken.GetPos(), "Unknown block enum entry"); + + const auto& typeToken = result.NextCapture(CAPTURE_BLOCK_TYPE); + FastFileBlockType type; + if (!GetFastFileBlockNameByType(typeToken.IdentifierValue(), type)) + throw ParsingException(typeToken.GetPos(), "Unknown fastfile block type"); + + auto isDefault = result.PeekAndRemoveIfTag(TAG_DEFAULT) == TAG_DEFAULT; + + state->AddBlock(std::make_unique(enumMember->m_name, static_cast(enumMember->m_value), type, isDefault)); } diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceBlock.h b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceBlock.h index 5c19eda4..296ac97d 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceBlock.h +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceBlock.h @@ -1,5 +1,8 @@ #pragma once +#include + +#include "Utils/ClassUtils.h" #include "Parsing/Commands/Impl/CommandsParser.h" class SequenceBlock final : public CommandsParser::sequence_t @@ -9,6 +12,11 @@ class SequenceBlock final : public CommandsParser::sequence_t static constexpr auto CAPTURE_BLOCK_ENUM_ENTRY = 1; static constexpr auto CAPTURE_BLOCK_TYPE = 2; + std::unordered_map m_type_lookup; + + void AddFastFileBlockToLookup(std::string name, FastFileBlockType type); + _NODISCARD bool GetFastFileBlockNameByType(const std::string& name, FastFileBlockType& type) const; + protected: void ProcessMatch(CommandsParserState* state, SequenceResult& result) const override; diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceGame.cpp b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceGame.cpp index a29f43c1..0b904b69 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceGame.cpp +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/Sequence/SequenceGame.cpp @@ -16,4 +16,5 @@ SequenceGame::SequenceGame() void SequenceGame::ProcessMatch(CommandsParserState* state, SequenceResult& result) const { + state->SetGame(result.NextCapture(CAPTURE_GAME).IdentifierValue()); } diff --git a/src/ZoneCodeGeneratorLib/Persistence/IDataRepository.h b/src/ZoneCodeGeneratorLib/Persistence/IDataRepository.h index 244402cb..cdea1534 100644 --- a/src/ZoneCodeGeneratorLib/Persistence/IDataRepository.h +++ b/src/ZoneCodeGeneratorLib/Persistence/IDataRepository.h @@ -27,6 +27,9 @@ public: virtual void Add(std::unique_ptr structureInformation) = 0; virtual void Add(std::unique_ptr fastFileBlock) = 0; + _NODISCARD virtual const std::string& GetGameName() const = 0; + virtual void SetGame(std::string gameName) = 0; + _NODISCARD virtual const std::vector& GetAllEnums() const = 0; _NODISCARD virtual const std::vector& GetAllStructs() const = 0; _NODISCARD virtual const std::vector& GetAllUnions() const = 0; @@ -36,4 +39,5 @@ public: _NODISCARD virtual DataDefinition* GetDataDefinitionByName(const std::string& name) const = 0; _NODISCARD virtual StructureInformation* GetInformationFor(DefinitionWithMembers* definitionWithMembers) const = 0; + _NODISCARD virtual EnumMember* GetEnumMemberByName(const std::string& name) const = 0; }; diff --git a/src/ZoneCodeGeneratorLib/Persistence/InMemory/InMemoryRepository.cpp b/src/ZoneCodeGeneratorLib/Persistence/InMemory/InMemoryRepository.cpp index b1e56be7..94fcfc22 100644 --- a/src/ZoneCodeGeneratorLib/Persistence/InMemory/InMemoryRepository.cpp +++ b/src/ZoneCodeGeneratorLib/Persistence/InMemory/InMemoryRepository.cpp @@ -1,10 +1,29 @@ #include "InMemoryRepository.h" +InMemoryRepository::~InMemoryRepository() +{ + for (auto* enumDefinition : m_enums) + delete enumDefinition; + for (auto* structDefinition : m_structs) + delete structDefinition; + for (auto* unionDefinition : m_unions) + delete unionDefinition; + for (auto* typedefDefinition : m_typedefs) + delete typedefDefinition; + for (auto* structureInformation : m_structures_information) + delete structureInformation; + for (auto* fastFileBlock : m_fast_file_blocks) + delete fastFileBlock; +} + void InMemoryRepository::Add(std::unique_ptr enumsDefinition) { auto* raw = enumsDefinition.release(); m_enums.push_back(raw); m_data_definitions_by_name[raw->m_name] = raw; + + for(const auto& enumMember : raw->m_members) + m_enum_members_by_name[enumMember->m_name] = enumMember.get(); } void InMemoryRepository::Add(std::unique_ptr structDefinition) @@ -40,6 +59,16 @@ void InMemoryRepository::Add(std::unique_ptr fastFileBlock) m_fast_file_blocks.push_back(fastFileBlock.release()); } +const std::string& InMemoryRepository::GetGameName() const +{ + return m_game_name; +} + +void InMemoryRepository::SetGame(std::string gameName) +{ + m_game_name = std::move(gameName); +} + const std::vector& InMemoryRepository::GetAllEnums() const { return m_enums; @@ -89,3 +118,13 @@ StructureInformation* InMemoryRepository::GetInformationFor(DefinitionWithMember return nullptr; } + +EnumMember* InMemoryRepository::GetEnumMemberByName(const std::string& name) const +{ + const auto foundEntry = m_enum_members_by_name.find(name); + + if (foundEntry != m_enum_members_by_name.end()) + return foundEntry->second; + + return nullptr; +} diff --git a/src/ZoneCodeGeneratorLib/Persistence/InMemory/InMemoryRepository.h b/src/ZoneCodeGeneratorLib/Persistence/InMemory/InMemoryRepository.h index 445ca0af..0fe965a7 100644 --- a/src/ZoneCodeGeneratorLib/Persistence/InMemory/InMemoryRepository.h +++ b/src/ZoneCodeGeneratorLib/Persistence/InMemory/InMemoryRepository.h @@ -13,9 +13,18 @@ class InMemoryRepository final : public IDataRepository std::vector m_structures_information; std::vector m_fast_file_blocks; std::map m_data_definitions_by_name; + std::map m_enum_members_by_name; std::map m_structure_information_by_definition; + std::string m_game_name; public: + InMemoryRepository() = default; + ~InMemoryRepository() override; + InMemoryRepository(const InMemoryRepository& other) = delete; + InMemoryRepository(InMemoryRepository&& other) noexcept = default; + InMemoryRepository& operator=(const InMemoryRepository& other) = delete; + InMemoryRepository& operator=(InMemoryRepository&& other) noexcept = default; + void Add(std::unique_ptr enumsDefinition) override; void Add(std::unique_ptr structDefinition) override; void Add(std::unique_ptr unionDefinition) override; @@ -23,6 +32,9 @@ public: void Add(std::unique_ptr structureInformation) override; void Add(std::unique_ptr fastFileBlock) override; + _NODISCARD const std::string& GetGameName() const override; + void SetGame(std::string gameName) override; + _NODISCARD const std::vector& GetAllEnums() const override; _NODISCARD const std::vector& GetAllStructs() const override; _NODISCARD const std::vector& GetAllUnions() const override; @@ -32,4 +44,5 @@ public: _NODISCARD DataDefinition* GetDataDefinitionByName(const std::string& name) const override; _NODISCARD StructureInformation* GetInformationFor(DefinitionWithMembers* definitionWithMembers) const override; + _NODISCARD EnumMember* GetEnumMemberByName(const std::string& name) const override; };