From c1ee0c1685dccdc648aecbae0ca2c8892ee84de5 Mon Sep 17 00:00:00 2001 From: Itamar Date: Sun, 20 Jun 2021 21:51:53 +0300 Subject: [PATCH] LibCpp: Support parsing enum classes --- Userland/Libraries/LibCpp/AST.h | 6 ++++++ Userland/Libraries/LibCpp/Parser.cpp | 26 +++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibCpp/AST.h b/Userland/Libraries/LibCpp/AST.h index d0713808a9d..c882cda55b7 100644 --- a/Userland/Libraries/LibCpp/AST.h +++ b/Userland/Libraries/LibCpp/AST.h @@ -505,6 +505,12 @@ public: { } + enum class Type { + RegularEnum, + EnumClass + }; + + Type type { Type::RegularEnum }; Vector m_entries; }; diff --git a/Userland/Libraries/LibCpp/Parser.cpp b/Userland/Libraries/LibCpp/Parser.cpp index 4906216e090..044885c680e 100644 --- a/Userland/Libraries/LibCpp/Parser.cpp +++ b/Userland/Libraries/LibCpp/Parser.cpp @@ -654,7 +654,23 @@ Optional 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 Parser::parse_enum_declaration(ASTNode& parent) ScopeLogger logger; auto enum_decl = create_ast_node(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);