LibCpp: Support parsing enum classes

This commit is contained in:
Itamar 2021-06-20 21:51:53 +03:00 committed by Ali Mohammad Pur
parent 5ee753ffaa
commit c1ee0c1685
Notes: sideshowbarker 2024-07-18 11:23:38 +09:00
2 changed files with 31 additions and 1 deletions

View file

@ -505,6 +505,12 @@ public:
{
}
enum class Type {
RegularEnum,
EnumClass
};
Type type { Type::RegularEnum };
Vector<StringView> m_entries;
};

View file

@ -654,7 +654,23 @@ Optional<Parser::DeclarationType> Parser::match_class_member(const StringView& c
bool Parser::match_enum_declaration()
{
return match_keyword("enum");
save_state();
ScopeGuard state_guard = [this] { load_state(); };
if (!match_keyword("enum"))
return false;
consume(Token::Type::Keyword);
if (match_keyword("class"))
consume(Token::Type::Keyword);
if (!match(Token::Type::Identifier))
return false;
consume(Token::Type::Identifier);
return match(Token::Type::LeftCurly);
}
bool Parser::match_class_declaration()
@ -1026,6 +1042,14 @@ NonnullRefPtr<EnumDeclaration> Parser::parse_enum_declaration(ASTNode& parent)
ScopeLogger<CPP_DEBUG> logger;
auto enum_decl = create_ast_node<EnumDeclaration>(parent, position(), {});
consume_keyword("enum");
if (match_keyword("class")) {
consume(Token::Type::Keyword);
enum_decl->type = EnumDeclaration::Type::EnumClass;
} else {
enum_decl->type = EnumDeclaration::Type::RegularEnum;
}
auto name_token = consume(Token::Type::Identifier);
enum_decl->m_name = text_of_token(name_token);
consume(Token::Type::LeftCurly);