summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2021-04-02 11:02:14 +0300
committerAndreas Kling <kling@serenityos.org>2021-04-06 21:51:58 +0200
commitd482b3fd60af5bc3bd1a2da5fb72d6f3dcfdb47d (patch)
tree6a8ce0a4c59c9d05dff3e4e40932421ea520c52c /Userland
parentfc503b13688dd7d6078caa7692640302e861df61 (diff)
downloadserenity-d482b3fd60af5bc3bd1a2da5fb72d6f3dcfdb47d.zip
LibCpp: Parse empty for loops
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibCpp/AST.cpp7
-rw-r--r--Userland/Libraries/LibCpp/Parser.cpp13
2 files changed, 15 insertions, 5 deletions
diff --git a/Userland/Libraries/LibCpp/AST.cpp b/Userland/Libraries/LibCpp/AST.cpp
index 5ae64b30a4..f81e65ef88 100644
--- a/Userland/Libraries/LibCpp/AST.cpp
+++ b/Userland/Libraries/LibCpp/AST.cpp
@@ -394,8 +394,11 @@ NonnullRefPtrVector<Declaration> Statement::declarations() const
NonnullRefPtrVector<Declaration> ForStatement::declarations() const
{
- auto declarations = m_init->declarations();
- declarations.append(m_body->declarations());
+ NonnullRefPtrVector<Declaration> declarations;
+ if (m_init)
+ declarations.append(m_init->declarations());
+ if (m_body)
+ declarations.append(m_body->declarations());
return declarations;
}
diff --git a/Userland/Libraries/LibCpp/Parser.cpp b/Userland/Libraries/LibCpp/Parser.cpp
index 6c77bcf956..c4afb9ccf0 100644
--- a/Userland/Libraries/LibCpp/Parser.cpp
+++ b/Userland/Libraries/LibCpp/Parser.cpp
@@ -1204,13 +1204,20 @@ NonnullRefPtr<ForStatement> Parser::parse_for_statement(ASTNode& parent)
auto for_statement = create_ast_node<ForStatement>(parent, position(), {});
consume(Token::Type::Keyword);
consume(Token::Type::LeftParen);
- for_statement->m_init = parse_variable_declaration(*for_statement);
+ if (peek().type() != Token::Type::Semicolon)
+ for_statement->m_init = parse_variable_declaration(*for_statement, false);
consume(Token::Type::Semicolon);
- for_statement->m_test = parse_expression(*for_statement);
+
+ if (peek().type() != Token::Type::Semicolon)
+ for_statement->m_test = parse_expression(*for_statement);
consume(Token::Type::Semicolon);
- for_statement->m_update = parse_expression(*for_statement);
+
+ if (peek().type() != Token::Type::RightParen)
+ for_statement->m_update = parse_expression(*for_statement);
consume(Token::Type::RightParen);
+
for_statement->m_body = parse_statement(*for_statement);
+
for_statement->set_end(for_statement->m_body->end());
return for_statement;
}