summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Parser.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-03-28 16:33:52 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-28 16:33:52 +0100
commit0593ce406b588e5bf76440bdf56eeb04bbe12ec9 (patch)
treeb1c5bdaa3332acc6c6d390cf06b7b9334d2d3ef0 /Libraries/LibJS/Parser.cpp
parentfecbef4ffe907ff160c0570b134fb87102f1194f (diff)
downloadserenity-0593ce406b588e5bf76440bdf56eeb04bbe12ec9.zip
LibJS: Implement basic support for the "new" keyword
NewExpression mostly piggybacks on the existing CallExpression. The big difference is that "new" creates a new Object and passes it as |this| to the callee.
Diffstat (limited to 'Libraries/LibJS/Parser.cpp')
-rw-r--r--Libraries/LibJS/Parser.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp
index c4f2a3966f..4552e22cdb 100644
--- a/Libraries/LibJS/Parser.cpp
+++ b/Libraries/LibJS/Parser.cpp
@@ -247,6 +247,8 @@ NonnullRefPtr<Expression> Parser::parse_primary_expression()
return parse_function_node<FunctionExpression>();
case TokenType::BracketOpen:
return parse_array_expression();
+ case TokenType::New:
+ return parse_new_expression();
default:
m_has_errors = true;
expected("primary expression (missing switch case)");
@@ -443,6 +445,29 @@ NonnullRefPtr<CallExpression> Parser::parse_call_expression(NonnullRefPtr<Expres
return create_ast_node<CallExpression>(move(lhs), move(arguments));
}
+NonnullRefPtr<NewExpression> Parser::parse_new_expression()
+{
+ consume(TokenType::New);
+
+ // FIXME: Support full expressions as the callee as well.
+ auto callee = create_ast_node<Identifier>(consume(TokenType::Identifier).value());
+
+ NonnullRefPtrVector<Expression> arguments;
+
+ if (match(TokenType::ParenOpen)) {
+ consume(TokenType::ParenOpen);
+ while (match_expression()) {
+ arguments.append(parse_expression(0));
+ if (!match(TokenType::Comma))
+ break;
+ consume();
+ }
+ consume(TokenType::ParenClose);
+ }
+
+ return create_ast_node<NewExpression>(move(callee), move(arguments));
+}
+
NonnullRefPtr<ReturnStatement> Parser::parse_return_statement()
{
consume(TokenType::Return);