diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-07-28 04:15:22 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-02 01:03:59 +0200 |
commit | 8fefbfd5ace73fb5a55565e9178a94c26b97cc06 (patch) | |
tree | c36fc42f4b0a3a1ec8b55a962afcc17db2c660c0 | |
parent | 67a19eaecbca321bbc7215765aa1593c1e1df3b2 (diff) | |
download | serenity-8fefbfd5ace73fb5a55565e9178a94c26b97cc06.zip |
LibCpp: Parse enum members with explicit values
-rw-r--r-- | Userland/Libraries/LibCpp/AST.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibCpp/AST.h | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibCpp/Parser.cpp | 8 |
3 files changed, 16 insertions, 4 deletions
diff --git a/Userland/Libraries/LibCpp/AST.cpp b/Userland/Libraries/LibCpp/AST.cpp index 237f550dfa..40f960b785 100644 --- a/Userland/Libraries/LibCpp/AST.cpp +++ b/Userland/Libraries/LibCpp/AST.cpp @@ -287,7 +287,9 @@ void EnumDeclaration::dump(FILE* output, size_t indent) const outln(output, "{}", m_name); for (auto& entry : m_entries) { print_indent(output, indent + 1); - outln(output, "{}", entry); + outln(output, "{}", entry.name); + if (entry.value) + entry.value->dump(output, indent + 2); } } diff --git a/Userland/Libraries/LibCpp/AST.h b/Userland/Libraries/LibCpp/AST.h index 2120599e8b..912312770a 100644 --- a/Userland/Libraries/LibCpp/AST.h +++ b/Userland/Libraries/LibCpp/AST.h @@ -616,11 +616,15 @@ public: }; void set_type(Type type) { m_type = type; } - void add_entry(StringView entry) { m_entries.append(move(entry)); } + void add_entry(StringView entry, RefPtr<Expression> value = nullptr) { m_entries.append({ entry, move(value) }); } private: Type m_type { Type::RegularEnum }; - Vector<StringView> m_entries; + struct EnumerationEntry { + StringView name; + RefPtr<Expression> value; + }; + Vector<EnumerationEntry> m_entries; }; class StructOrClassDeclaration : public Declaration { diff --git a/Userland/Libraries/LibCpp/Parser.cpp b/Userland/Libraries/LibCpp/Parser.cpp index 120793bc70..c18f1154d0 100644 --- a/Userland/Libraries/LibCpp/Parser.cpp +++ b/Userland/Libraries/LibCpp/Parser.cpp @@ -1076,7 +1076,13 @@ NonnullRefPtr<EnumDeclaration> Parser::parse_enum_declaration(ASTNode& parent) enum_decl->set_name(text_of_token(name_token)); consume(Token::Type::LeftCurly); while (!eof() && peek().type() != Token::Type::RightCurly) { - enum_decl->add_entry(text_of_token(consume(Token::Type::Identifier))); + auto name = text_of_token(consume(Token::Type::Identifier)); + RefPtr<Expression> value; + if (peek().type() == Token::Type::Equals) { + consume(); + value = parse_expression(enum_decl); + } + enum_decl->add_entry(name, move(value)); if (peek().type() != Token::Type::Comma) { break; } |