Add blocks and built in types to zcg cpp

This commit is contained in:
Jan 2021-02-14 00:53:23 +01:00
parent 216125739c
commit e40f1ec0b7
18 changed files with 384 additions and 25 deletions

View File

@ -0,0 +1,31 @@
#include "HeaderBlockEnum.h"
#include "Parsing/Header/Sequence/SequenceCloseBlock.h"
#include "Parsing/Header/Sequence/SequenceEnumMember.h"
HeaderBlockType HeaderBlockEnum::GetType()
{
return HeaderBlockType::UNION;
}
const std::vector<IHeaderBlock::sequence_t*>& HeaderBlockEnum::GetTestsForBlock()
{
static std::vector<sequence_t*> tests({
new SequenceCloseBlock(),
new SequenceEnumMember()
});
return tests;
}
void HeaderBlockEnum::OnOpen()
{
}
void HeaderBlockEnum::OnClose()
{
}
void HeaderBlockEnum::OnChildBlockClose(IHeaderBlock* block)
{
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "IHeaderBlock.h"
class HeaderBlockEnum final : public IHeaderBlock
{
public:
HeaderBlockType GetType() override;
const std::vector<sequence_t*>& GetTestsForBlock() override;
void OnOpen() override;
void OnClose() override;
void OnChildBlockClose(IHeaderBlock* block) override;
};

View File

@ -0,0 +1,41 @@
#include "HeaderBlockNamespace.h"
#include "Parsing/Header/Sequence/SequenceCloseBlock.h"
#include "Parsing/Header/Sequence/SequenceEnum.h"
#include "Parsing/Header/Sequence/SequenceForwardDecl.h"
#include "Parsing/Header/Sequence/SequenceNamespace.h"
#include "Parsing/Header/Sequence/SequenceStruct.h"
#include "Parsing/Header/Sequence/SequenceTypedef.h"
#include "Parsing/Header/Sequence/SequenceUnion.h"
HeaderBlockType HeaderBlockNamespace::GetType()
{
return HeaderBlockType::NAMESPACE;
}
const std::vector<IHeaderBlock::sequence_t*>& HeaderBlockNamespace::GetTestsForBlock()
{
static std::vector<sequence_t*> tests({
new SequenceCloseBlock(),
new SequenceEnum(),
new SequenceForwardDecl(),
new SequenceNamespace(),
new SequenceStruct(),
new SequenceTypedef(),
new SequenceUnion()
});
return tests;
}
void HeaderBlockNamespace::OnOpen()
{
}
void HeaderBlockNamespace::OnClose()
{
}
void HeaderBlockNamespace::OnChildBlockClose(IHeaderBlock* block)
{
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "IHeaderBlock.h"
class HeaderBlockNamespace final : public IHeaderBlock
{
public:
HeaderBlockType GetType() override;
const std::vector<sequence_t*>& GetTestsForBlock() override;
void OnOpen() override;
void OnClose() override;
void OnChildBlockClose(IHeaderBlock* block) override;
};

View File

@ -0,0 +1,39 @@
#include "HeaderBlockNone.h"
#include "Parsing/Header/Sequence/SequenceEnum.h"
#include "Parsing/Header/Sequence/SequenceForwardDecl.h"
#include "Parsing/Header/Sequence/SequenceNamespace.h"
#include "Parsing/Header/Sequence/SequenceStruct.h"
#include "Parsing/Header/Sequence/SequenceTypedef.h"
#include "Parsing/Header/Sequence/SequenceUnion.h"
HeaderBlockType HeaderBlockNone::GetType()
{
return HeaderBlockType::NONE;
}
const std::vector<IHeaderBlock::sequence_t*>& HeaderBlockNone::GetTestsForBlock()
{
static std::vector<sequence_t*> tests({
new SequenceEnum(),
new SequenceForwardDecl(),
new SequenceNamespace(),
new SequenceStruct(),
new SequenceTypedef(),
new SequenceUnion()
});
return tests;
}
void HeaderBlockNone::OnOpen()
{
}
void HeaderBlockNone::OnClose()
{
}
void HeaderBlockNone::OnChildBlockClose(IHeaderBlock* block)
{
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "IHeaderBlock.h"
class HeaderBlockNone final : public IHeaderBlock
{
public:
HeaderBlockType GetType() override;
const std::vector<sequence_t*>& GetTestsForBlock() override;
void OnOpen() override;
void OnClose() override;
void OnChildBlockClose(IHeaderBlock* block) override;
};

View File

@ -0,0 +1,37 @@
#include "HeaderBlockStruct.h"
#include "Parsing/Header/Sequence/SequenceCloseBlock.h"
#include "Parsing/Header/Sequence/SequenceEnum.h"
#include "Parsing/Header/Sequence/SequenceStruct.h"
#include "Parsing/Header/Sequence/SequenceUnion.h"
#include "Parsing/Header/Sequence/SequenceVariable.h"
HeaderBlockType HeaderBlockStruct::GetType()
{
return HeaderBlockType::STRUCT;
}
const std::vector<IHeaderBlock::sequence_t*>& HeaderBlockStruct::GetTestsForBlock()
{
static std::vector<sequence_t*> tests({
new SequenceCloseBlock(),
new SequenceEnum(),
new SequenceStruct(),
new SequenceUnion(),
new SequenceVariable()
});
return tests;
}
void HeaderBlockStruct::OnOpen()
{
}
void HeaderBlockStruct::OnClose()
{
}
void HeaderBlockStruct::OnChildBlockClose(IHeaderBlock* block)
{
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "IHeaderBlock.h"
class HeaderBlockStruct final : public IHeaderBlock
{
public:
HeaderBlockType GetType() override;
const std::vector<sequence_t*>& GetTestsForBlock() override;
void OnOpen() override;
void OnClose() override;
void OnChildBlockClose(IHeaderBlock* block) override;
};

View File

@ -0,0 +1,37 @@
#include "HeaderBlockUnion.h"
#include "Parsing/Header/Sequence/SequenceCloseBlock.h"
#include "Parsing/Header/Sequence/SequenceEnum.h"
#include "Parsing/Header/Sequence/SequenceStruct.h"
#include "Parsing/Header/Sequence/SequenceUnion.h"
#include "Parsing/Header/Sequence/SequenceVariable.h"
HeaderBlockType HeaderBlockUnion::GetType()
{
return HeaderBlockType::UNION;
}
const std::vector<IHeaderBlock::sequence_t*>& HeaderBlockUnion::GetTestsForBlock()
{
static std::vector<sequence_t*> tests({
new SequenceCloseBlock(),
new SequenceEnum(),
new SequenceStruct(),
new SequenceUnion(),
new SequenceVariable()
});
return tests;
}
void HeaderBlockUnion::OnOpen()
{
}
void HeaderBlockUnion::OnClose()
{
}
void HeaderBlockUnion::OnChildBlockClose(IHeaderBlock* block)
{
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "IHeaderBlock.h"
class HeaderBlockUnion final : public IHeaderBlock
{
public:
HeaderBlockType GetType() override;
const std::vector<sequence_t*>& GetTestsForBlock() override;
void OnOpen() override;
void OnClose() override;
void OnChildBlockClose(IHeaderBlock* block) override;
};

View File

@ -0,0 +1,38 @@
#pragma once
#include <vector>
#include "Parsing/Sequence/AbstractSequence.h"
#include "Parsing/Header/Impl/HeaderParserState.h"
enum class HeaderBlockType
{
NONE,
NAMESPACE,
ENUM,
STRUCT,
UNION
};
class HeaderParserValue;
class HeaderParserState;
class IHeaderBlock
{
public:
typedef AbstractSequence<HeaderParserValue, HeaderParserState> sequence_t;
IHeaderBlock() = default;
virtual ~IHeaderBlock() = default;
IHeaderBlock(const IHeaderBlock& other) = default;
IHeaderBlock(IHeaderBlock&& other) noexcept = default;
IHeaderBlock& operator=(const IHeaderBlock& other) = default;
IHeaderBlock& operator=(IHeaderBlock&& other) noexcept = default;
virtual HeaderBlockType GetType() = 0;
virtual const std::vector<sequence_t*>& GetTestsForBlock() = 0;
virtual void OnOpen() = 0;
virtual void OnClose() = 0;
virtual void OnChildBlockClose(IHeaderBlock* block) = 0;
};

View File

@ -11,12 +11,17 @@ void HeaderLexer::PrepareKeywords()
m_keywords["__declspec"] = HeaderParserValueType::DECLSPEC; m_keywords["__declspec"] = HeaderParserValueType::DECLSPEC;
m_keywords["align"] = HeaderParserValueType::ALIGN; m_keywords["align"] = HeaderParserValueType::ALIGN;
m_keywords["alignas"] = HeaderParserValueType::ALIGNAS; m_keywords["alignas"] = HeaderParserValueType::ALIGNAS;
m_keywords["char"] = HeaderParserValueType::CHAR;
m_keywords["const"] = HeaderParserValueType::CONST; m_keywords["const"] = HeaderParserValueType::CONST;
m_keywords["enum"] = HeaderParserValueType::ENUM; m_keywords["enum"] = HeaderParserValueType::ENUM;
m_keywords["int"] = HeaderParserValueType::INT;
m_keywords["long"] = HeaderParserValueType::LONG;
m_keywords["namespace"] = HeaderParserValueType::NAMESPACE; m_keywords["namespace"] = HeaderParserValueType::NAMESPACE;
m_keywords["short"] = HeaderParserValueType::SHORT;
m_keywords["struct"] = HeaderParserValueType::STRUCT; m_keywords["struct"] = HeaderParserValueType::STRUCT;
m_keywords["typedef"] = HeaderParserValueType::TYPEDEF; m_keywords["typedef"] = HeaderParserValueType::TYPEDEF;
m_keywords["union"] = HeaderParserValueType::UNION; m_keywords["union"] = HeaderParserValueType::UNION;
m_keywords["unsigned"] = HeaderParserValueType::UNSIGNED;
} }
HeaderParserValue HeaderLexer::GetNextToken() HeaderParserValue HeaderLexer::GetNextToken()

View File

@ -7,11 +7,9 @@ HeaderParser::HeaderParser(HeaderLexer* lexer, IDataRepository* targetRepository
m_repository(targetRepository) m_repository(targetRepository)
{ {
auto sequenceNamespace = std::make_unique<SequenceNamespace>(); auto sequenceNamespace = std::make_unique<SequenceNamespace>();
m_normal_tests.push_back(sequenceNamespace.get());
m_tests.emplace_back(std::move(sequenceNamespace));
} }
const std::vector<HeaderParser::sequence_t*>& HeaderParser::GetTestsForState() const std::vector<HeaderParser::sequence_t*>& HeaderParser::GetTestsForState()
{ {
return m_normal_tests; return m_state->GetBlock()->GetTestsForBlock();
} }

View File

@ -9,9 +9,6 @@ class HeaderParser final : public AbstractParser<HeaderParserValue, HeaderParser
{ {
IDataRepository* m_repository; IDataRepository* m_repository;
std::vector<std::unique_ptr<sequence_t>> m_tests;
std::vector<sequence_t*> m_normal_tests;
protected: protected:
const std::vector<sequence_t*>& GetTestsForState() override; const std::vector<sequence_t*>& GetTestsForState() override;

View File

@ -0,0 +1,13 @@
#include "HeaderParserState.h"
#include "Parsing/Header/Block/HeaderBlockNone.h"
HeaderParserState::HeaderParserState()
{
m_blocks.push(std::make_unique<HeaderBlockNone>());
}
IHeaderBlock* HeaderParserState::GetBlock() const
{
return m_blocks.top().get();
}

View File

@ -1,7 +1,18 @@
#pragma once #pragma once
#include <memory>
#include <stack>
#include "Utils/ClassUtils.h"
#include "Parsing/Header/Block/IHeaderBlock.h"
class IHeaderBlock;
class HeaderParserState class HeaderParserState
{ {
public: std::stack<std::unique_ptr<IHeaderBlock>> m_blocks;
public:
HeaderParserState();
_NODISCARD IHeaderBlock* GetBlock() const;
}; };

View File

@ -2,7 +2,6 @@
#include <string> #include <string>
#include "Parsing/IParserValue.h" #include "Parsing/IParserValue.h"
#include "Utils/ClassUtils.h" #include "Utils/ClassUtils.h"
#include "Parsing/TokenPos.h" #include "Parsing/TokenPos.h"
@ -32,6 +31,15 @@ enum class HeaderParserValueType
STRING, STRING,
IDENTIFIER, IDENTIFIER,
// Built-in types
BUILT_IN_FIRST,
UNSIGNED = BUILT_IN_FIRST,
CHAR,
SHORT,
INT,
LONG,
BUILT_IN_LAST = LONG,
// Keywords // Keywords
DECLSPEC, DECLSPEC,
ALIGN, ALIGN,

View File

@ -35,7 +35,7 @@ std::unique_ptr<HeaderCommonMatchers::matcher_t> HeaderCommonMatchers::ArrayDef(
create.Char(']') create.Char(']')
}).Transform([](HeaderMatcherFactory::token_list_t& values) }).Transform([](HeaderMatcherFactory::token_list_t& values)
{ {
if(values[1].get().m_type == HeaderParserValueType::INTEGER) if (values[1].get().m_type == HeaderParserValueType::INTEGER)
return HeaderParserValue::Integer(values[1].get().GetPos(), values[1].get().IntegerValue()); return HeaderParserValue::Integer(values[1].get().GetPos(), values[1].get().IntegerValue());
return HeaderParserValue::Identifier(values[1].get().GetPos(), new std::string(values[1].get().IdentifierValue())); return HeaderParserValue::Identifier(values[1].get().GetPos(), new std::string(values[1].get().IdentifierValue()));
@ -44,9 +44,47 @@ std::unique_ptr<HeaderCommonMatchers::matcher_t> HeaderCommonMatchers::ArrayDef(
std::unique_ptr<HeaderCommonMatchers::matcher_t> HeaderCommonMatchers::Typename(const supplier_t* labelSupplier) std::unique_ptr<HeaderCommonMatchers::matcher_t> HeaderCommonMatchers::Typename(const supplier_t* labelSupplier)
{ {
static constexpr const char* BUILT_IN_TYPE_NAMES[]
{
"unsigned",
"char",
"short",
"int",
"long"
};
static_assert(_countof(BUILT_IN_TYPE_NAMES) == static_cast<int>(HeaderParserValueType::BUILT_IN_LAST) - static_cast<int>(HeaderParserValueType::BUILT_IN_FIRST) + 1);
const HeaderMatcherFactory create(labelSupplier); const HeaderMatcherFactory create(labelSupplier);
return create.And({ return create.Or({
create.And({
create.Optional(create.Type(HeaderParserValueType::UNSIGNED)),
create.Or({
create.Type(HeaderParserValueType::CHAR),
create.Type(HeaderParserValueType::SHORT),
create.Type(HeaderParserValueType::INT),
create.And({
create.Type(HeaderParserValueType::LONG),
create.Optional(create.Type(HeaderParserValueType::LONG))
})
})
}).Transform([](HeaderMatcherFactory::token_list_t& values)
{
std::ostringstream str;
auto first = false;
for(const auto& token : values)
{
if (first)
first = false;
else
str << " ";
str << BUILT_IN_TYPE_NAMES[static_cast<int>(token.get().m_type) - static_cast<int>(HeaderParserValueType::BUILT_IN_FIRST)];
}
return HeaderParserValue::TypeName(values[0].get().GetPos(), new std::string(str.str()));
}),
create.And({
create.Identifier(), create.Identifier(),
create.OptionalLoop(create.And({ create.OptionalLoop(create.And({
create.Char(':'), create.Char(':'),
@ -62,5 +100,6 @@ std::unique_ptr<HeaderCommonMatchers::matcher_t> HeaderCommonMatchers::Typename(
str << "::" << values[i].get().IdentifierValue(); str << "::" << values[i].get().IdentifierValue();
return HeaderParserValue::TypeName(values[0].get().GetPos(), new std::string(str.str())); return HeaderParserValue::TypeName(values[0].get().GetPos(), new std::string(str.str()));
}).Build(); })
});
} }