mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:17:34 +00:00
LibCpp: Parse enum members with explicit values
This commit is contained in:
parent
67a19eaecb
commit
8fefbfd5ac
3 changed files with 16 additions and 4 deletions
|
@ -287,7 +287,9 @@ void EnumDeclaration::dump(FILE* output, size_t indent) const
|
||||||
outln(output, "{}", m_name);
|
outln(output, "{}", m_name);
|
||||||
for (auto& entry : m_entries) {
|
for (auto& entry : m_entries) {
|
||||||
print_indent(output, indent + 1);
|
print_indent(output, indent + 1);
|
||||||
outln(output, "{}", entry);
|
outln(output, "{}", entry.name);
|
||||||
|
if (entry.value)
|
||||||
|
entry.value->dump(output, indent + 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -616,11 +616,15 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
void set_type(Type type) { m_type = type; }
|
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:
|
private:
|
||||||
Type m_type { Type::RegularEnum };
|
Type m_type { Type::RegularEnum };
|
||||||
Vector<StringView> m_entries;
|
struct EnumerationEntry {
|
||||||
|
StringView name;
|
||||||
|
RefPtr<Expression> value;
|
||||||
|
};
|
||||||
|
Vector<EnumerationEntry> m_entries;
|
||||||
};
|
};
|
||||||
|
|
||||||
class StructOrClassDeclaration : public Declaration {
|
class StructOrClassDeclaration : public Declaration {
|
||||||
|
|
|
@ -1076,7 +1076,13 @@ NonnullRefPtr<EnumDeclaration> Parser::parse_enum_declaration(ASTNode& parent)
|
||||||
enum_decl->set_name(text_of_token(name_token));
|
enum_decl->set_name(text_of_token(name_token));
|
||||||
consume(Token::Type::LeftCurly);
|
consume(Token::Type::LeftCurly);
|
||||||
while (!eof() && peek().type() != Token::Type::RightCurly) {
|
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) {
|
if (peek().type() != Token::Type::Comma) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue