1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:17:44 +00:00

LibCpp: Parse sizeof() expression

This commit is contained in:
Itamar 2021-04-02 10:49:12 +03:00 committed by Andreas Kling
parent 8bcf5daf3f
commit fc503b1368
4 changed files with 44 additions and 1 deletions

View file

@ -530,4 +530,11 @@ void CppCastExpression::dump(size_t indent) const
m_expression->dump(indent + 1);
}
void SizeofExpression::dump(size_t indent) const
{
ASTNode::dump(indent);
if(m_type)
m_type->dump(indent+1);
}
}

View file

@ -715,4 +715,18 @@ public:
RefPtr<Expression> m_expression;
};
class SizeofExpression : public Expression {
public:
SizeofExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
: Expression(parent, start, end, filename)
{
}
virtual ~SizeofExpression() override = default;
virtual const char* class_name() const override { return "SizeofExpression"; }
virtual void dump(size_t indent) const override;
RefPtr<Type> m_type;
};
}

View file

@ -447,6 +447,9 @@ NonnullRefPtr<Expression> Parser::parse_primary_expression(ASTNode& parent)
if (match_cpp_cast_expression())
return parse_cpp_cast_expression(parent);
if(match_sizeof_expression())
return parse_sizeof_expression(parent);
if (match_name()) {
if (match_function_call() != TemplatizedMatchResult::NoMatch)
return parse_function_call(parent);
@ -847,7 +850,8 @@ bool Parser::match_expression()
return match_literal()
|| token_type == Token::Type::Identifier
|| match_unary_expression()
|| match_cpp_cast_expression();
|| match_cpp_cast_expression()
|| match_sizeof_expression();
}
bool Parser::eof() const
@ -1399,4 +1403,20 @@ NonnullRefPtr<CppCastExpression> Parser::parse_cpp_cast_expression(ASTNode& pare
return cast_expression;
}
bool Parser::match_sizeof_expression()
{
return match_keyword("sizeof");
}
NonnullRefPtr<SizeofExpression> Parser::parse_sizeof_expression(ASTNode& parent)
{
auto exp = create_ast_node<SizeofExpression>(parent, position(), {});
consume(Token::Type::Keyword);
consume(Token::Type::LeftParen);
exp->m_type = parse_type(parent);
consume(Token::Type::RightParen);
exp->set_end(position());
return exp;
}
}

View file

@ -90,6 +90,7 @@ private:
bool match_template_arguments();
bool match_name();
bool match_cpp_cast_expression();
bool match_sizeof_expression();
enum class TemplatizedMatchResult {
NoMatch,
@ -133,6 +134,7 @@ private:
NonnullRefPtrVector<Type> parse_template_arguments(ASTNode& parent);
NonnullRefPtr<Name> parse_name(ASTNode& parent);
NonnullRefPtr<CppCastExpression> parse_cpp_cast_expression(ASTNode& parent);
NonnullRefPtr<SizeofExpression> parse_sizeof_expression(ASTNode& parent);
bool match(Token::Type);
Token consume(Token::Type);