1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:47:34 +00:00

LibCpp: Parse nullptr literal

This commit is contained in:
Itamar 2021-03-28 10:40:20 +03:00 committed by Andreas Kling
parent cbb49f26d9
commit 9954a1837f
3 changed files with 27 additions and 4 deletions

View file

@ -437,4 +437,9 @@ void NamespaceDeclaration::dump(size_t indent) const
decl.dump(indent + 1); decl.dump(indent + 1);
} }
void NullPointerLiteral::dump(size_t indent) const
{
ASTNode::dump(indent);
}
} }

View file

@ -341,6 +341,20 @@ public:
StringView m_value; StringView m_value;
}; };
class NullPointerLiteral : public Expression {
public:
virtual ~NullPointerLiteral() override = default;
virtual const char* class_name() const override { return "NullPointerLiteral"; }
virtual void dump(size_t indent) const override;
NullPointerLiteral(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
: Expression(parent, start, end, filename)
{
}
};
class BooleanLiteral : public Expression { class BooleanLiteral : public Expression {
public: public:
virtual ~BooleanLiteral() override = default; virtual ~BooleanLiteral() override = default;

View file

@ -414,8 +414,10 @@ bool Parser::match_literal()
return true; return true;
case Token::Type::DoubleQuotedString: case Token::Type::DoubleQuotedString:
return true; return true;
case Token::Type::Float:
return true;
case Token::Type::Keyword: { case Token::Type::Keyword: {
return match_boolean_literal(); return match_boolean_literal() || peek().text() == "nullptr";
} }
default: default:
return false; return false;
@ -477,6 +479,10 @@ NonnullRefPtr<Expression> Parser::parse_literal(ASTNode& parent)
case Token::Type::Keyword: { case Token::Type::Keyword: {
if (match_boolean_literal()) if (match_boolean_literal())
return parse_boolean_literal(parent); return parse_boolean_literal(parent);
if (peek().text() == "nullptr") {
auto token = consume();
return create_ast_node<NullPointerLiteral>(parent, token.start(), token.end());
}
[[fallthrough]]; [[fallthrough]];
} }
default: { default: {
@ -771,10 +777,8 @@ void Parser::error(StringView message)
bool Parser::match_expression() bool Parser::match_expression()
{ {
auto token_type = peek().type(); auto token_type = peek().type();
return token_type == Token::Type::Integer return match_literal()
|| token_type == Token::Type::Float
|| token_type == Token::Type::Identifier || token_type == Token::Type::Identifier
|| token_type == Token::Type::DoubleQuotedString
|| match_unary_expression(); || match_unary_expression();
} }