diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-06-14 14:52:59 +0430 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-06-14 13:06:08 +0100 |
commit | d374295a265e960331fe83a0dc8c827683667857 (patch) | |
tree | 50c646d360bdb52e5c67ce90646a0a3a0972115a /Userland/Libraries/LibJS/Parser.cpp | |
parent | 2661a8810898548e26e98600a00e42e3ad1c1c84 (diff) | |
download | serenity-d374295a265e960331fe83a0dc8c827683667857.zip |
LibJS: Parse generator functions in object literals
Also add some parser tests
Diffstat (limited to 'Userland/Libraries/LibJS/Parser.cpp')
-rw-r--r-- | Userland/Libraries/LibJS/Parser.cpp | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/Userland/Libraries/LibJS/Parser.cpp b/Userland/Libraries/LibJS/Parser.cpp index 4f9d0de2b6..114e68b1ef 100644 --- a/Userland/Libraries/LibJS/Parser.cpp +++ b/Userland/Libraries/LibJS/Parser.cpp @@ -830,6 +830,7 @@ NonnullRefPtr<ObjectExpression> Parser::parse_object_expression() property_type = ObjectProperty::Type::KeyValue; RefPtr<Expression> property_name; RefPtr<Expression> property_value; + FunctionKind function_kind { FunctionKind::Regular }; if (match(TokenType::TripleDot)) { consume(); @@ -841,7 +842,12 @@ NonnullRefPtr<ObjectExpression> Parser::parse_object_expression() continue; } - if (match(TokenType::Identifier)) { + if (match(TokenType::Asterisk)) { + consume(); + property_type = ObjectProperty::Type::KeyValue; + property_name = parse_property_key(); + function_kind = FunctionKind ::Generator; + } else if (match(TokenType::Identifier)) { auto identifier = consume().value(); if (identifier == "get" && match_property_key()) { property_type = ObjectProperty::Type::Getter; @@ -872,6 +878,8 @@ NonnullRefPtr<ObjectExpression> Parser::parse_object_expression() parse_options |= FunctionNodeParseOptions::IsGetterFunction; if (property_type == ObjectProperty::Type::Setter) parse_options |= FunctionNodeParseOptions::IsSetterFunction; + if (function_kind == FunctionKind::Generator) + parse_options |= FunctionNodeParseOptions::IsGeneratorFunction; auto function = parse_function_node<FunctionExpression>(parse_options); properties.append(create_ast_node<ObjectProperty>({ m_parser_state.m_current_token.filename(), rule_start.position(), position() }, *property_name, function, property_type, true)); } else if (match(TokenType::Colon)) { @@ -1380,13 +1388,15 @@ NonnullRefPtr<FunctionNodeType> Parser::parse_function_node(u8 parse_options) ScopePusher scope(*this, ScopePusher::Var | ScopePusher::Function); - auto is_generator = false; + auto is_generator = (parse_options & FunctionNodeParseOptions::IsGeneratorFunction) != 0; String name; if (parse_options & FunctionNodeParseOptions::CheckForFunctionAndName) { consume(TokenType::Function); - is_generator = match(TokenType::Asterisk); - if (is_generator) - consume(TokenType::Asterisk); + if (!is_generator) { + is_generator = match(TokenType::Asterisk); + if (is_generator) + consume(TokenType::Asterisk); + } if (FunctionNodeType::must_have_name() || match(TokenType::Identifier)) name = consume(TokenType::Identifier).value(); |