mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 08:05:45 +00:00
parse struct and union sequences
This commit is contained in:
parent
57547854c4
commit
fb55cdb468
@ -13,6 +13,10 @@ class DeclarationModifier
|
|||||||
public:
|
public:
|
||||||
DeclarationModifier() = default;
|
DeclarationModifier() = default;
|
||||||
virtual ~DeclarationModifier() = default;
|
virtual ~DeclarationModifier() = default;
|
||||||
|
DeclarationModifier(const DeclarationModifier& other) = default;
|
||||||
|
DeclarationModifier(DeclarationModifier&& other) noexcept = default;
|
||||||
|
DeclarationModifier& operator=(const DeclarationModifier& other) = default;
|
||||||
|
DeclarationModifier& operator=(DeclarationModifier&& other) noexcept = default;
|
||||||
|
|
||||||
_NODISCARD virtual DeclarationModifierType GetType() const = 0;
|
_NODISCARD virtual DeclarationModifierType GetType() const = 0;
|
||||||
};
|
};
|
@ -26,7 +26,7 @@ public:
|
|||||||
const unsigned m_pack;
|
const unsigned m_pack;
|
||||||
unsigned m_alignment_override;
|
unsigned m_alignment_override;
|
||||||
|
|
||||||
std::vector<std::unique_ptr<Variable>> m_members;
|
std::vector<std::shared_ptr<Variable>> m_members;
|
||||||
|
|
||||||
DefinitionWithMembers(std::string _namespace, std::string name, unsigned pack);
|
DefinitionWithMembers(std::string _namespace, std::string name, unsigned pack);
|
||||||
|
|
||||||
|
@ -20,11 +20,11 @@ void TypeDeclaration::CalculateSize()
|
|||||||
{
|
{
|
||||||
auto currentSize = m_type->GetSize();
|
auto currentSize = m_type->GetSize();
|
||||||
|
|
||||||
for(auto i = m_declaration_modifiers.size(); i > 0; i--)
|
for (auto i = m_declaration_modifiers.size(); i > 0; i--)
|
||||||
{
|
{
|
||||||
const auto& declarationModifier = m_declaration_modifiers[i - 1];
|
const auto& declarationModifier = m_declaration_modifiers[i - 1];
|
||||||
|
|
||||||
switch(declarationModifier->GetType())
|
switch (declarationModifier->GetType())
|
||||||
{
|
{
|
||||||
case DeclarationModifierType::POINTER:
|
case DeclarationModifierType::POINTER:
|
||||||
currentSize = POINTER_SIZE;
|
currentSize = POINTER_SIZE;
|
||||||
|
@ -1,5 +1,13 @@
|
|||||||
#include "Variable.h"
|
#include "Variable.h"
|
||||||
|
|
||||||
|
Variable::Variable(std::string name, std::unique_ptr<TypeDeclaration> typeDeclaration)
|
||||||
|
: m_name(std::move(name)),
|
||||||
|
m_has_alignment_override(false),
|
||||||
|
m_alignment_override(0),
|
||||||
|
m_type_declaration(std::move(typeDeclaration))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
unsigned Variable::GetAlignment()
|
unsigned Variable::GetAlignment()
|
||||||
{
|
{
|
||||||
if (m_has_alignment_override)
|
if (m_has_alignment_override)
|
||||||
|
@ -13,6 +13,8 @@ public:
|
|||||||
unsigned m_alignment_override;
|
unsigned m_alignment_override;
|
||||||
std::unique_ptr<TypeDeclaration> m_type_declaration;
|
std::unique_ptr<TypeDeclaration> m_type_declaration;
|
||||||
|
|
||||||
|
Variable(std::string name, std::unique_ptr<TypeDeclaration> typeDeclaration);
|
||||||
|
|
||||||
unsigned GetAlignment();
|
unsigned GetAlignment();
|
||||||
bool GetForceAlignment();
|
bool GetForceAlignment();
|
||||||
};
|
};
|
||||||
|
@ -5,6 +5,16 @@
|
|||||||
#include "Parsing/Header/Sequence/SequenceStruct.h"
|
#include "Parsing/Header/Sequence/SequenceStruct.h"
|
||||||
#include "Parsing/Header/Sequence/SequenceUnion.h"
|
#include "Parsing/Header/Sequence/SequenceUnion.h"
|
||||||
#include "Parsing/Header/Sequence/SequenceVariable.h"
|
#include "Parsing/Header/Sequence/SequenceVariable.h"
|
||||||
|
#include "Utils/NameUtils.h"
|
||||||
|
|
||||||
|
HeaderBlockStruct::HeaderBlockStruct(std::string name, const bool isTypedef)
|
||||||
|
: m_type_name(std::move(name)),
|
||||||
|
m_struct_definition(nullptr),
|
||||||
|
m_custom_alignment(0),
|
||||||
|
m_is_typedef(isTypedef),
|
||||||
|
m_has_custom_align(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
HeaderBlockType HeaderBlockStruct::GetType()
|
HeaderBlockType HeaderBlockStruct::GetType()
|
||||||
{
|
{
|
||||||
@ -26,17 +36,77 @@ const std::vector<IHeaderBlock::sequence_t*>& HeaderBlockStruct::GetTestsForBloc
|
|||||||
|
|
||||||
void HeaderBlockStruct::OnOpen(HeaderParserState* state)
|
void HeaderBlockStruct::OnOpen(HeaderParserState* state)
|
||||||
{
|
{
|
||||||
|
m_namespace = state->m_namespace.ToString();
|
||||||
|
|
||||||
|
if (!m_type_name.empty())
|
||||||
|
state->m_namespace.Push(m_type_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HeaderBlockStruct::OnClose(HeaderParserState* state)
|
void HeaderBlockStruct::OnClose(HeaderParserState* state)
|
||||||
{
|
{
|
||||||
|
if (!m_type_name.empty())
|
||||||
|
state->m_namespace.Pop();
|
||||||
|
|
||||||
|
auto isAnonymous = false;
|
||||||
|
auto typeName = m_type_name;
|
||||||
|
if(typeName.empty())
|
||||||
|
{
|
||||||
|
isAnonymous = true;
|
||||||
|
typeName = NameUtils::GenerateRandomName();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto structDefinition = std::make_unique<StructDefinition>(m_namespace, std::move(typeName), state->m_pack_value_supplier->GetCurrentPack());
|
||||||
|
m_struct_definition = structDefinition.get();
|
||||||
|
|
||||||
|
if (isAnonymous)
|
||||||
|
structDefinition->m_anonymous = true;
|
||||||
|
|
||||||
|
for (auto& member : m_members)
|
||||||
|
structDefinition->m_members.emplace_back(std::move(member));
|
||||||
|
|
||||||
|
state->AddDataType(std::move(structDefinition));
|
||||||
|
|
||||||
|
if (m_is_typedef)
|
||||||
|
state->AddDataType(std::make_unique<TypedefDefinition>(m_namespace, m_variable_name, std::make_unique<TypeDeclaration>(m_struct_definition)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void HeaderBlockStruct::OnChildBlockClose(HeaderParserState* state, IHeaderBlock* block)
|
void HeaderBlockStruct::OnChildBlockClose(HeaderParserState* state, IHeaderBlock* block)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HeaderBlockStruct::AddVariable(std::shared_ptr<Variable> variable)
|
||||||
|
{
|
||||||
|
m_members.emplace_back(std::move(variable));
|
||||||
|
}
|
||||||
|
|
||||||
|
void HeaderBlockStruct::SetCustomAlignment(const int alignment)
|
||||||
|
{
|
||||||
|
m_has_custom_align = true;
|
||||||
|
m_custom_alignment = alignment;
|
||||||
|
}
|
||||||
|
|
||||||
|
void HeaderBlockStruct::Inherit(const StructDefinition* parentStruct)
|
||||||
|
{
|
||||||
|
for(const auto& parentMember : parentStruct->m_members)
|
||||||
|
AddVariable(parentMember);
|
||||||
|
}
|
||||||
|
|
||||||
void HeaderBlockStruct::SetBlockName(const TokenPos& nameTokenPos, std::string name)
|
void HeaderBlockStruct::SetBlockName(const TokenPos& nameTokenPos, std::string name)
|
||||||
{
|
{
|
||||||
m_variable_name = std::move(name);
|
m_variable_name = std::move(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool HeaderBlockStruct::IsDefiningVariable()
|
||||||
|
{
|
||||||
|
return !m_is_typedef && !m_variable_name.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
DataDefinition* HeaderBlockStruct::GetVariableType()
|
||||||
|
{
|
||||||
|
return m_struct_definition;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string HeaderBlockStruct::GetVariableName()
|
||||||
|
{
|
||||||
|
return m_variable_name;
|
||||||
|
}
|
||||||
|
@ -1,18 +1,42 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "IHeaderBlock.h"
|
#include "IHeaderBlock.h"
|
||||||
#include "IHeaderBlockNameHolder.h"
|
#include "IHeaderBlockNameHolder.h"
|
||||||
|
#include "IHeaderBlockVariableDefining.h"
|
||||||
|
#include "Domain/Definition/StructDefinition.h"
|
||||||
|
#include "Domain/Definition/Variable.h"
|
||||||
|
|
||||||
class HeaderBlockStruct final : public IHeaderBlock, public IHeaderBlockNameHolder
|
class HeaderBlockStruct final : public IHeaderBlock, public IHeaderBlockNameHolder, public IHeaderBlockVariableDefining
|
||||||
{
|
{
|
||||||
|
std::string m_namespace;
|
||||||
|
std::string m_type_name;
|
||||||
|
std::vector<std::shared_ptr<Variable>> m_members;
|
||||||
|
StructDefinition* m_struct_definition;
|
||||||
|
|
||||||
|
int m_custom_alignment;
|
||||||
|
|
||||||
|
bool m_is_typedef;
|
||||||
|
bool m_has_custom_align;
|
||||||
|
|
||||||
std::string m_variable_name;
|
std::string m_variable_name;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
HeaderBlockStruct(std::string name, bool isTypedef);
|
||||||
|
|
||||||
HeaderBlockType GetType() override;
|
HeaderBlockType GetType() override;
|
||||||
const std::vector<sequence_t*>& GetTestsForBlock() override;
|
const std::vector<sequence_t*>& GetTestsForBlock() override;
|
||||||
void OnOpen(HeaderParserState* state) override;
|
void OnOpen(HeaderParserState* state) override;
|
||||||
void OnClose(HeaderParserState* state) override;
|
void OnClose(HeaderParserState* state) override;
|
||||||
void OnChildBlockClose(HeaderParserState* state, IHeaderBlock* block) override;
|
void OnChildBlockClose(HeaderParserState* state, IHeaderBlock* block) override;
|
||||||
|
|
||||||
|
void AddVariable(std::shared_ptr<Variable> variable);
|
||||||
|
void SetCustomAlignment(int alignment);
|
||||||
|
void Inherit(const StructDefinition* parentStruct);
|
||||||
|
|
||||||
void SetBlockName(const TokenPos& nameTokenPos, std::string name) override;
|
void SetBlockName(const TokenPos& nameTokenPos, std::string name) override;
|
||||||
|
bool IsDefiningVariable() override;
|
||||||
|
DataDefinition* GetVariableType() override;
|
||||||
|
std::string GetVariableName() override;
|
||||||
};
|
};
|
||||||
|
@ -5,6 +5,16 @@
|
|||||||
#include "Parsing/Header/Sequence/SequenceStruct.h"
|
#include "Parsing/Header/Sequence/SequenceStruct.h"
|
||||||
#include "Parsing/Header/Sequence/SequenceUnion.h"
|
#include "Parsing/Header/Sequence/SequenceUnion.h"
|
||||||
#include "Parsing/Header/Sequence/SequenceVariable.h"
|
#include "Parsing/Header/Sequence/SequenceVariable.h"
|
||||||
|
#include "Utils/NameUtils.h"
|
||||||
|
|
||||||
|
HeaderBlockUnion::HeaderBlockUnion(std::string name, const bool isTypedef)
|
||||||
|
: m_type_name(std::move(name)),
|
||||||
|
m_union_definition(nullptr),
|
||||||
|
m_custom_alignment(0),
|
||||||
|
m_is_typedef(isTypedef),
|
||||||
|
m_has_custom_align(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
HeaderBlockType HeaderBlockUnion::GetType()
|
HeaderBlockType HeaderBlockUnion::GetType()
|
||||||
{
|
{
|
||||||
@ -26,17 +36,77 @@ const std::vector<IHeaderBlock::sequence_t*>& HeaderBlockUnion::GetTestsForBlock
|
|||||||
|
|
||||||
void HeaderBlockUnion::OnOpen(HeaderParserState* state)
|
void HeaderBlockUnion::OnOpen(HeaderParserState* state)
|
||||||
{
|
{
|
||||||
|
m_namespace = state->m_namespace.ToString();
|
||||||
|
|
||||||
|
if (!m_type_name.empty())
|
||||||
|
state->m_namespace.Push(m_type_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HeaderBlockUnion::OnClose(HeaderParserState* state)
|
void HeaderBlockUnion::OnClose(HeaderParserState* state)
|
||||||
{
|
{
|
||||||
|
if (!m_type_name.empty())
|
||||||
|
state->m_namespace.Pop();
|
||||||
|
|
||||||
|
auto isAnonymous = false;
|
||||||
|
auto typeName = m_type_name;
|
||||||
|
if (typeName.empty())
|
||||||
|
{
|
||||||
|
isAnonymous = true;
|
||||||
|
typeName = NameUtils::GenerateRandomName();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto unionDefinition = std::make_unique<UnionDefinition>(m_namespace, std::move(typeName), state->m_pack_value_supplier->GetCurrentPack());
|
||||||
|
m_union_definition = unionDefinition.get();
|
||||||
|
|
||||||
|
if (isAnonymous)
|
||||||
|
unionDefinition->m_anonymous = true;
|
||||||
|
|
||||||
|
for (auto& member : m_members)
|
||||||
|
unionDefinition->m_members.emplace_back(std::move(member));
|
||||||
|
|
||||||
|
state->AddDataType(std::move(unionDefinition));
|
||||||
|
|
||||||
|
if (m_is_typedef)
|
||||||
|
state->AddDataType(std::make_unique<TypedefDefinition>(m_namespace, m_variable_name, std::make_unique<TypeDeclaration>(m_union_definition)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void HeaderBlockUnion::OnChildBlockClose(HeaderParserState* state, IHeaderBlock* block)
|
void HeaderBlockUnion::OnChildBlockClose(HeaderParserState* state, IHeaderBlock* block)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HeaderBlockUnion::AddVariable(std::shared_ptr<Variable> variable)
|
||||||
|
{
|
||||||
|
m_members.emplace_back(std::move(variable));
|
||||||
|
}
|
||||||
|
|
||||||
|
void HeaderBlockUnion::SetCustomAlignment(const int alignment)
|
||||||
|
{
|
||||||
|
m_has_custom_align = true;
|
||||||
|
m_custom_alignment = alignment;
|
||||||
|
}
|
||||||
|
|
||||||
|
void HeaderBlockUnion::Inherit(const StructDefinition* parentStruct)
|
||||||
|
{
|
||||||
|
for (const auto& parentMember : parentStruct->m_members)
|
||||||
|
AddVariable(parentMember);
|
||||||
|
}
|
||||||
|
|
||||||
void HeaderBlockUnion::SetBlockName(const TokenPos& nameTokenPos, std::string name)
|
void HeaderBlockUnion::SetBlockName(const TokenPos& nameTokenPos, std::string name)
|
||||||
{
|
{
|
||||||
m_variable_name = std::move(name);
|
m_variable_name = std::move(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool HeaderBlockUnion::IsDefiningVariable()
|
||||||
|
{
|
||||||
|
return !m_is_typedef && !m_variable_name.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
DataDefinition* HeaderBlockUnion::GetVariableType()
|
||||||
|
{
|
||||||
|
return m_union_definition;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string HeaderBlockUnion::GetVariableName()
|
||||||
|
{
|
||||||
|
return m_variable_name;
|
||||||
|
}
|
||||||
|
@ -2,17 +2,37 @@
|
|||||||
|
|
||||||
#include "IHeaderBlock.h"
|
#include "IHeaderBlock.h"
|
||||||
#include "IHeaderBlockNameHolder.h"
|
#include "IHeaderBlockNameHolder.h"
|
||||||
|
#include "IHeaderBlockVariableDefining.h"
|
||||||
|
|
||||||
class HeaderBlockUnion final : public IHeaderBlock, public IHeaderBlockNameHolder
|
class HeaderBlockUnion final : public IHeaderBlock, public IHeaderBlockNameHolder, public IHeaderBlockVariableDefining
|
||||||
{
|
{
|
||||||
|
std::string m_namespace;
|
||||||
|
std::string m_type_name;
|
||||||
|
std::vector<std::shared_ptr<Variable>> m_members;
|
||||||
|
UnionDefinition* m_union_definition;
|
||||||
|
|
||||||
|
int m_custom_alignment;
|
||||||
|
|
||||||
|
bool m_is_typedef;
|
||||||
|
bool m_has_custom_align;
|
||||||
|
|
||||||
std::string m_variable_name;
|
std::string m_variable_name;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
HeaderBlockUnion(std::string name, bool isTypedef);
|
||||||
|
|
||||||
HeaderBlockType GetType() override;
|
HeaderBlockType GetType() override;
|
||||||
const std::vector<sequence_t*>& GetTestsForBlock() override;
|
const std::vector<sequence_t*>& GetTestsForBlock() override;
|
||||||
void OnOpen(HeaderParserState* state) override;
|
void OnOpen(HeaderParserState* state) override;
|
||||||
void OnClose(HeaderParserState* state) override;
|
void OnClose(HeaderParserState* state) override;
|
||||||
void OnChildBlockClose(HeaderParserState* state, IHeaderBlock* block) override;
|
void OnChildBlockClose(HeaderParserState* state, IHeaderBlock* block) override;
|
||||||
|
|
||||||
|
void AddVariable(std::shared_ptr<Variable> variable);
|
||||||
|
void SetCustomAlignment(int alignment);
|
||||||
|
void Inherit(const StructDefinition* parentStruct);
|
||||||
|
|
||||||
void SetBlockName(const TokenPos& nameTokenPos, std::string name) override;
|
void SetBlockName(const TokenPos& nameTokenPos, std::string name) override;
|
||||||
|
bool IsDefiningVariable() override;
|
||||||
|
DataDefinition* GetVariableType() override;
|
||||||
|
std::string GetVariableName() override;
|
||||||
};
|
};
|
||||||
|
@ -61,7 +61,7 @@ bool HeaderFileReader::ReadHeaderFile(IDataRepository* repository)
|
|||||||
SetupStreamProxies();
|
SetupStreamProxies();
|
||||||
|
|
||||||
const auto lexer = std::make_unique<HeaderLexer>(m_stream);
|
const auto lexer = std::make_unique<HeaderLexer>(m_stream);
|
||||||
const auto parser = std::make_unique<HeaderParser>(lexer.get());
|
const auto parser = std::make_unique<HeaderParser>(lexer.get(), m_pack_value_supplier);
|
||||||
|
|
||||||
const auto start = std::chrono::steady_clock::now();
|
const auto start = std::chrono::steady_clock::now();
|
||||||
const auto result = parser->Parse();
|
const auto result = parser->Parse();
|
||||||
|
@ -16,7 +16,7 @@ class HeaderFileReader
|
|||||||
std::string m_filename;
|
std::string m_filename;
|
||||||
|
|
||||||
std::vector<std::unique_ptr<IParserLineStream>> m_open_streams;
|
std::vector<std::unique_ptr<IParserLineStream>> m_open_streams;
|
||||||
IPackValueSupplier* m_pack_value_supplier;
|
const IPackValueSupplier* m_pack_value_supplier;
|
||||||
IParserLineStream* m_stream;
|
IParserLineStream* m_stream;
|
||||||
|
|
||||||
bool OpenBaseStream();
|
bool OpenBaseStream();
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
#include "Parsing/Header/Sequence/SequenceNamespace.h"
|
#include "Parsing/Header/Sequence/SequenceNamespace.h"
|
||||||
|
|
||||||
HeaderParser::HeaderParser(HeaderLexer* lexer)
|
HeaderParser::HeaderParser(HeaderLexer* lexer, const IPackValueSupplier* packValueSupplier)
|
||||||
: AbstractParser(lexer, std::make_unique<HeaderParserState>())
|
: AbstractParser(lexer, std::make_unique<HeaderParserState>(packValueSupplier))
|
||||||
{
|
{
|
||||||
auto sequenceNamespace = std::make_unique<SequenceNamespace>();
|
auto sequenceNamespace = std::make_unique<SequenceNamespace>();
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "HeaderLexer.h"
|
#include "HeaderLexer.h"
|
||||||
#include "HeaderParserState.h"
|
#include "HeaderParserState.h"
|
||||||
|
#include "Parsing/IPackValueSupplier.h"
|
||||||
#include "Parsing/Impl/AbstractParser.h"
|
#include "Parsing/Impl/AbstractParser.h"
|
||||||
#include "Persistence/IDataRepository.h"
|
#include "Persistence/IDataRepository.h"
|
||||||
|
|
||||||
@ -11,7 +12,7 @@ protected:
|
|||||||
const std::vector<sequence_t*>& GetTestsForState() override;
|
const std::vector<sequence_t*>& GetTestsForState() override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit HeaderParser(HeaderLexer* lexer);
|
HeaderParser(HeaderLexer* lexer, const IPackValueSupplier* packValueSupplier);
|
||||||
|
|
||||||
void SaveToRepository(IDataRepository* repository) const;
|
void SaveToRepository(IDataRepository* repository) const;
|
||||||
};
|
};
|
||||||
|
@ -3,7 +3,9 @@
|
|||||||
#include "Domain/Definition/EnumDefinition.h"
|
#include "Domain/Definition/EnumDefinition.h"
|
||||||
#include "Parsing/Header/Block/HeaderBlockNone.h"
|
#include "Parsing/Header/Block/HeaderBlockNone.h"
|
||||||
|
|
||||||
HeaderParserState::HeaderParserState()
|
HeaderParserState::HeaderParserState(const IPackValueSupplier* packValueSupplier)
|
||||||
|
: m_pack_value_supplier(packValueSupplier)
|
||||||
|
|
||||||
{
|
{
|
||||||
m_blocks.push(std::make_unique<HeaderBlockNone>());
|
m_blocks.push(std::make_unique<HeaderBlockNone>());
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#include "Domain/Definition/DataDefinition.h"
|
#include "Domain/Definition/DataDefinition.h"
|
||||||
#include "Domain/Definition/EnumMember.h"
|
#include "Domain/Definition/EnumMember.h"
|
||||||
#include "Domain/Definition/ForwardDeclaration.h"
|
#include "Domain/Definition/ForwardDeclaration.h"
|
||||||
|
#include "Parsing/IPackValueSupplier.h"
|
||||||
#include "Utils/NamespaceBuilder.h"
|
#include "Utils/NamespaceBuilder.h"
|
||||||
#include "Parsing/Header/Block/IHeaderBlock.h"
|
#include "Parsing/Header/Block/IHeaderBlock.h"
|
||||||
#include "Persistence/IDataRepository.h"
|
#include "Persistence/IDataRepository.h"
|
||||||
@ -27,9 +28,10 @@ class HeaderParserState
|
|||||||
void AddBaseDataType(const BaseTypeDefinition* baseType);
|
void AddBaseDataType(const BaseTypeDefinition* baseType);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
const IPackValueSupplier* const m_pack_value_supplier;
|
||||||
NamespaceBuilder m_namespace;
|
NamespaceBuilder m_namespace;
|
||||||
|
|
||||||
HeaderParserState();
|
explicit HeaderParserState(const IPackValueSupplier* packValueSupplier);
|
||||||
|
|
||||||
_NODISCARD IHeaderBlock* GetBlock() const;
|
_NODISCARD IHeaderBlock* GetBlock() const;
|
||||||
void PushBlock(std::unique_ptr<IHeaderBlock> block);
|
void PushBlock(std::unique_ptr<IHeaderBlock> block);
|
||||||
|
@ -26,5 +26,36 @@ SequenceStruct::SequenceStruct()
|
|||||||
|
|
||||||
void SequenceStruct::ProcessMatch(HeaderParserState* state, SequenceResult<HeaderParserValue>& result) const
|
void SequenceStruct::ProcessMatch(HeaderParserState* state, SequenceResult<HeaderParserValue>& result) const
|
||||||
{
|
{
|
||||||
state->PushBlock(std::make_unique<HeaderBlockStruct>());
|
auto isTypedef = result.PeekAndRemoveIfTag(TAG_TYPEDEF) == TAG_TYPEDEF;
|
||||||
|
std::string name;
|
||||||
|
|
||||||
|
if (result.HasNextCapture(CAPTURE_NAME))
|
||||||
|
name = result.NextCapture(CAPTURE_NAME).IdentifierValue();
|
||||||
|
|
||||||
|
auto structBlock = std::make_unique<HeaderBlockStruct>(std::move(name), isTypedef);
|
||||||
|
|
||||||
|
if (result.HasNextCapture(CAPTURE_ALIGN))
|
||||||
|
structBlock->SetCustomAlignment(result.NextCapture(CAPTURE_ALIGN).IntegerValue());
|
||||||
|
|
||||||
|
if(result.HasNextCapture(CAPTURE_PARENT_TYPE))
|
||||||
|
{
|
||||||
|
const auto& parentTypeToken = result.NextCapture(CAPTURE_PARENT_TYPE);
|
||||||
|
const auto* parentDefinition = state->FindType(parentTypeToken.TypeNameValue());
|
||||||
|
|
||||||
|
if(parentDefinition == nullptr && !state->m_namespace.IsEmpty())
|
||||||
|
{
|
||||||
|
const auto fullTypeName = NamespaceBuilder::Combine(state->m_namespace.ToString(), parentTypeToken.TypeNameValue());
|
||||||
|
parentDefinition = state->FindType(fullTypeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parentDefinition == nullptr)
|
||||||
|
throw ParsingException(parentTypeToken.GetPos(), "Cannot find specified parent type");
|
||||||
|
|
||||||
|
if(parentDefinition->GetType() != DataDefinitionType::STRUCT)
|
||||||
|
throw ParsingException(parentTypeToken.GetPos(), "Parent type must be a struct");
|
||||||
|
|
||||||
|
structBlock->Inherit(dynamic_cast<const StructDefinition*>(parentDefinition));
|
||||||
|
}
|
||||||
|
|
||||||
|
state->PushBlock(std::move(structBlock));
|
||||||
}
|
}
|
||||||
|
@ -26,5 +26,36 @@ SequenceUnion::SequenceUnion()
|
|||||||
|
|
||||||
void SequenceUnion::ProcessMatch(HeaderParserState* state, SequenceResult<HeaderParserValue>& result) const
|
void SequenceUnion::ProcessMatch(HeaderParserState* state, SequenceResult<HeaderParserValue>& result) const
|
||||||
{
|
{
|
||||||
state->PushBlock(std::make_unique<HeaderBlockUnion>());
|
auto isTypedef = result.PeekAndRemoveIfTag(TAG_TYPEDEF) == TAG_TYPEDEF;
|
||||||
|
std::string name;
|
||||||
|
|
||||||
|
if (result.HasNextCapture(CAPTURE_NAME))
|
||||||
|
name = result.NextCapture(CAPTURE_NAME).IdentifierValue();
|
||||||
|
|
||||||
|
auto unionBlock = std::make_unique<HeaderBlockUnion>(std::move(name), isTypedef);
|
||||||
|
|
||||||
|
if (result.HasNextCapture(CAPTURE_ALIGN))
|
||||||
|
unionBlock->SetCustomAlignment(result.NextCapture(CAPTURE_ALIGN).IntegerValue());
|
||||||
|
|
||||||
|
if (result.HasNextCapture(CAPTURE_PARENT_TYPE))
|
||||||
|
{
|
||||||
|
const auto& parentTypeToken = result.NextCapture(CAPTURE_PARENT_TYPE);
|
||||||
|
const auto* parentDefinition = state->FindType(parentTypeToken.TypeNameValue());
|
||||||
|
|
||||||
|
if (parentDefinition == nullptr && !state->m_namespace.IsEmpty())
|
||||||
|
{
|
||||||
|
const auto fullTypeName = NamespaceBuilder::Combine(state->m_namespace.ToString(), parentTypeToken.TypeNameValue());
|
||||||
|
parentDefinition = state->FindType(fullTypeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parentDefinition == nullptr)
|
||||||
|
throw ParsingException(parentTypeToken.GetPos(), "Cannot find specified parent type");
|
||||||
|
|
||||||
|
if (parentDefinition->GetType() != DataDefinitionType::STRUCT)
|
||||||
|
throw ParsingException(parentTypeToken.GetPos(), "Parent type must be a struct");
|
||||||
|
|
||||||
|
unionBlock->Inherit(dynamic_cast<const StructDefinition*>(parentDefinition));
|
||||||
|
}
|
||||||
|
|
||||||
|
state->PushBlock(std::move(unionBlock));
|
||||||
}
|
}
|
||||||
|
@ -10,5 +10,5 @@ public:
|
|||||||
IPackValueSupplier& operator=(const IPackValueSupplier& other) = default;
|
IPackValueSupplier& operator=(const IPackValueSupplier& other) = default;
|
||||||
IPackValueSupplier& operator=(IPackValueSupplier&& other) noexcept = default;
|
IPackValueSupplier& operator=(IPackValueSupplier&& other) noexcept = default;
|
||||||
|
|
||||||
virtual int GetCurrentPack() = 0;
|
virtual int GetCurrentPack() const = 0;
|
||||||
};
|
};
|
@ -86,7 +86,7 @@ bool PackDefinitionStreamProxy::Eof() const
|
|||||||
return m_stream->Eof();
|
return m_stream->Eof();
|
||||||
}
|
}
|
||||||
|
|
||||||
int PackDefinitionStreamProxy::GetCurrentPack()
|
int PackDefinitionStreamProxy::GetCurrentPack() const
|
||||||
{
|
{
|
||||||
return m_current_pack.empty() ? DEFAULT_PACK : m_current_pack.top();
|
return m_current_pack.empty() ? DEFAULT_PACK : m_current_pack.top();
|
||||||
}
|
}
|
||||||
|
@ -31,5 +31,5 @@ public:
|
|||||||
_NODISCARD bool IsOpen() const override;
|
_NODISCARD bool IsOpen() const override;
|
||||||
_NODISCARD bool Eof() const override;
|
_NODISCARD bool Eof() const override;
|
||||||
|
|
||||||
int GetCurrentPack() override;
|
int GetCurrentPack() const override;
|
||||||
};
|
};
|
||||||
|
17
src/ZoneCodeGeneratorLib/Utils/NameUtils.cpp
Normal file
17
src/ZoneCodeGeneratorLib/Utils/NameUtils.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include "NameUtils.h"
|
||||||
|
|
||||||
|
#include <random>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
std::string NameUtils::GenerateRandomName()
|
||||||
|
{
|
||||||
|
static constexpr auto ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
std::ostringstream str;
|
||||||
|
|
||||||
|
std::random_device random;
|
||||||
|
|
||||||
|
for (auto i = 0u; i < 32u; i++)
|
||||||
|
str << ALPHABET[random() % std::char_traits<char>::length(ALPHABET)];
|
||||||
|
|
||||||
|
return str.str();
|
||||||
|
}
|
9
src/ZoneCodeGeneratorLib/Utils/NameUtils.h
Normal file
9
src/ZoneCodeGeneratorLib/Utils/NameUtils.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class NameUtils
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static std::string GenerateRandomName();
|
||||||
|
};
|
@ -13,6 +13,11 @@ std::string NamespaceBuilder::Combine(const std::string& _namespace, const std::
|
|||||||
return str.str();
|
return str.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool NamespaceBuilder::IsEmpty() const
|
||||||
|
{
|
||||||
|
return m_elements.empty();
|
||||||
|
}
|
||||||
|
|
||||||
void NamespaceBuilder::Push(std::string element)
|
void NamespaceBuilder::Push(std::string element)
|
||||||
{
|
{
|
||||||
m_elements.emplace_back(std::move(element));
|
m_elements.emplace_back(std::move(element));
|
||||||
|
@ -10,6 +10,7 @@ class NamespaceBuilder
|
|||||||
public:
|
public:
|
||||||
static std::string Combine(const std::string& _namespace, const std::string& name);
|
static std::string Combine(const std::string& _namespace, const std::string& name);
|
||||||
|
|
||||||
|
_NODISCARD bool IsEmpty() const;
|
||||||
void Push(std::string element);
|
void Push(std::string element);
|
||||||
void Pop();
|
void Pop();
|
||||||
std::string ToString();
|
std::string ToString();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user