mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-15 09:17:57 -05:00
parse struct and union sequences
This commit is contained in:
@ -5,6 +5,16 @@
|
||||
#include "Parsing/Header/Sequence/SequenceStruct.h"
|
||||
#include "Parsing/Header/Sequence/SequenceUnion.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()
|
||||
{
|
||||
@ -26,17 +36,77 @@ const std::vector<IHeaderBlock::sequence_t*>& HeaderBlockStruct::GetTestsForBloc
|
||||
|
||||
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)
|
||||
{
|
||||
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::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)
|
||||
{
|
||||
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
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "IHeaderBlock.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;
|
||||
|
||||
public:
|
||||
HeaderBlockStruct(std::string name, bool isTypedef);
|
||||
|
||||
HeaderBlockType GetType() override;
|
||||
const std::vector<sequence_t*>& GetTestsForBlock() override;
|
||||
void OnOpen(HeaderParserState* state) override;
|
||||
void OnClose(HeaderParserState* state) 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;
|
||||
bool IsDefiningVariable() override;
|
||||
DataDefinition* GetVariableType() override;
|
||||
std::string GetVariableName() override;
|
||||
};
|
||||
|
@ -5,6 +5,16 @@
|
||||
#include "Parsing/Header/Sequence/SequenceStruct.h"
|
||||
#include "Parsing/Header/Sequence/SequenceUnion.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()
|
||||
{
|
||||
@ -26,17 +36,77 @@ const std::vector<IHeaderBlock::sequence_t*>& HeaderBlockUnion::GetTestsForBlock
|
||||
|
||||
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)
|
||||
{
|
||||
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::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)
|
||||
{
|
||||
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 "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;
|
||||
|
||||
public:
|
||||
HeaderBlockUnion(std::string name, bool isTypedef);
|
||||
|
||||
HeaderBlockType GetType() override;
|
||||
const std::vector<sequence_t*>& GetTestsForBlock() override;
|
||||
void OnOpen(HeaderParserState* state) override;
|
||||
void OnClose(HeaderParserState* state) 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;
|
||||
bool IsDefiningVariable() override;
|
||||
DataDefinition* GetVariableType() override;
|
||||
std::string GetVariableName() override;
|
||||
};
|
||||
|
@ -61,7 +61,7 @@ bool HeaderFileReader::ReadHeaderFile(IDataRepository* repository)
|
||||
SetupStreamProxies();
|
||||
|
||||
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 result = parser->Parse();
|
||||
|
@ -16,7 +16,7 @@ class HeaderFileReader
|
||||
std::string m_filename;
|
||||
|
||||
std::vector<std::unique_ptr<IParserLineStream>> m_open_streams;
|
||||
IPackValueSupplier* m_pack_value_supplier;
|
||||
const IPackValueSupplier* m_pack_value_supplier;
|
||||
IParserLineStream* m_stream;
|
||||
|
||||
bool OpenBaseStream();
|
||||
|
@ -2,8 +2,8 @@
|
||||
|
||||
#include "Parsing/Header/Sequence/SequenceNamespace.h"
|
||||
|
||||
HeaderParser::HeaderParser(HeaderLexer* lexer)
|
||||
: AbstractParser(lexer, std::make_unique<HeaderParserState>())
|
||||
HeaderParser::HeaderParser(HeaderLexer* lexer, const IPackValueSupplier* packValueSupplier)
|
||||
: AbstractParser(lexer, std::make_unique<HeaderParserState>(packValueSupplier))
|
||||
{
|
||||
auto sequenceNamespace = std::make_unique<SequenceNamespace>();
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include "HeaderLexer.h"
|
||||
#include "HeaderParserState.h"
|
||||
#include "Parsing/IPackValueSupplier.h"
|
||||
#include "Parsing/Impl/AbstractParser.h"
|
||||
#include "Persistence/IDataRepository.h"
|
||||
|
||||
@ -11,7 +12,7 @@ protected:
|
||||
const std::vector<sequence_t*>& GetTestsForState() override;
|
||||
|
||||
public:
|
||||
explicit HeaderParser(HeaderLexer* lexer);
|
||||
HeaderParser(HeaderLexer* lexer, const IPackValueSupplier* packValueSupplier);
|
||||
|
||||
void SaveToRepository(IDataRepository* repository) const;
|
||||
};
|
||||
|
@ -3,7 +3,9 @@
|
||||
#include "Domain/Definition/EnumDefinition.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>());
|
||||
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include "Domain/Definition/DataDefinition.h"
|
||||
#include "Domain/Definition/EnumMember.h"
|
||||
#include "Domain/Definition/ForwardDeclaration.h"
|
||||
#include "Parsing/IPackValueSupplier.h"
|
||||
#include "Utils/NamespaceBuilder.h"
|
||||
#include "Parsing/Header/Block/IHeaderBlock.h"
|
||||
#include "Persistence/IDataRepository.h"
|
||||
@ -27,9 +28,10 @@ class HeaderParserState
|
||||
void AddBaseDataType(const BaseTypeDefinition* baseType);
|
||||
|
||||
public:
|
||||
const IPackValueSupplier* const m_pack_value_supplier;
|
||||
NamespaceBuilder m_namespace;
|
||||
|
||||
HeaderParserState();
|
||||
explicit HeaderParserState(const IPackValueSupplier* packValueSupplier);
|
||||
|
||||
_NODISCARD IHeaderBlock* GetBlock() const;
|
||||
void PushBlock(std::unique_ptr<IHeaderBlock> block);
|
||||
|
@ -26,5 +26,36 @@ SequenceStruct::SequenceStruct()
|
||||
|
||||
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
|
||||
{
|
||||
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=(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();
|
||||
}
|
||||
|
||||
int PackDefinitionStreamProxy::GetCurrentPack()
|
||||
int PackDefinitionStreamProxy::GetCurrentPack() const
|
||||
{
|
||||
return m_current_pack.empty() ? DEFAULT_PACK : m_current_pack.top();
|
||||
}
|
||||
|
@ -31,5 +31,5 @@ public:
|
||||
_NODISCARD bool IsOpen() const override;
|
||||
_NODISCARD bool Eof() const override;
|
||||
|
||||
int GetCurrentPack() override;
|
||||
int GetCurrentPack() const override;
|
||||
};
|
||||
|
Reference in New Issue
Block a user