Parse structured data def reserved enum count

This commit is contained in:
Jan 2022-01-20 22:37:33 +01:00
parent 040f945009
commit b894a524e8
4 changed files with 35 additions and 5 deletions

View File

@ -12,9 +12,18 @@ CommonStructuredDataDefEnumEntry::CommonStructuredDataDefEnumEntry(std::string n
}
CommonStructuredDataDefEnum::CommonStructuredDataDefEnum()
= default;
CommonStructuredDataDefEnum::CommonStructuredDataDefEnum(std::string name)
: m_name(std::move(name))
: m_reserved_entry_count(-1)
{
}
CommonStructuredDataDefEnum::CommonStructuredDataDefEnum(std::string name)
: m_name(std::move(name)),
m_reserved_entry_count(-1)
{
}
CommonStructuredDataDefEnum::CommonStructuredDataDefEnum(std::string name, const int reservedEntryCount)
: m_name(std::move(name)),
m_reserved_entry_count(reservedEntryCount)
{
}

View File

@ -14,8 +14,10 @@ struct CommonStructuredDataDefEnumEntry
struct CommonStructuredDataDefEnum
{
std::string m_name;
int m_reserved_entry_count;
std::vector<CommonStructuredDataDefEnumEntry> m_entries;
CommonStructuredDataDefEnum();
explicit CommonStructuredDataDefEnum(std::string name);
CommonStructuredDataDefEnum(std::string name, int reservedEntryCount);
};

View File

@ -7,6 +7,7 @@ namespace sdd::def_scope_sequences
class SequenceEnum final : public StructuredDataDefParser::sequence_t
{
static constexpr auto CAPTURE_NAME = 1;
static constexpr auto CAPTURE_RESERVED_COUNT = 2;
public:
SequenceEnum()
@ -15,6 +16,11 @@ namespace sdd::def_scope_sequences
AddMatchers({
create.Keyword("enum"),
create.Optional(create.And({
create.Char('('),
create.Integer().Capture(CAPTURE_RESERVED_COUNT),
create.Char(')')
})),
create.Identifier().Capture(CAPTURE_NAME),
create.Char('{')
});
@ -26,6 +32,15 @@ namespace sdd::def_scope_sequences
assert(state->m_current_def);
auto newEnum = std::make_unique<CommonStructuredDataDefEnum>(result.NextCapture(CAPTURE_NAME).IdentifierValue());
if (result.HasNextCapture(CAPTURE_RESERVED_COUNT))
{
const auto& reservedCountToken = result.NextCapture(CAPTURE_RESERVED_COUNT);
newEnum->m_reserved_entry_count = reservedCountToken.IntegerValue();
if (newEnum->m_reserved_entry_count <= 0)
throw ParsingException(reservedCountToken.GetPos(), "Reserved enum entry count must be greater than zero");
}
state->m_current_enum = newEnum.get();
state->m_def_types_by_name.emplace(newEnum->m_name, CommonStructuredDataDefType(CommonStructuredDataDefTypeCategory::ENUM, state->m_current_def->m_enums.size()));
state->m_current_def->m_enums.emplace_back(std::move(newEnum));

View File

@ -29,7 +29,11 @@ namespace sdd::enum_scope_sequences
{
assert(state->m_current_enum);
state->m_current_enum->m_entries.emplace_back(result.NextCapture(CAPTURE_ENTRY_VALUE).StringValue(), state->m_current_enum->m_entries.size());
const auto& entryValueToken = result.NextCapture(CAPTURE_ENTRY_VALUE);
if (state->m_current_enum->m_reserved_entry_count > 0 && static_cast<size_t>(state->m_current_enum->m_reserved_entry_count) <= state->m_current_enum->m_entries.size())
throw ParsingException(entryValueToken.GetPos(), "Enum entry count exceeds reserved count");
state->m_current_enum->m_entries.emplace_back(entryValueToken.StringValue(), state->m_current_enum->m_entries.size());
}
};