From db75be1119b5b888255840d6131f47af66f40151 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Tue, 20 Oct 2020 17:56:49 +0100 Subject: [PATCH] LibJS: Refactor parse_function_node() bool parameters into bit flags I'm about to add even more options and a bunch of unnamed true/false arguments is really not helpful. Let's make this a single parse options parameter using bit flags. --- Libraries/LibJS/Parser.cpp | 25 +++++++++++-------------- Libraries/LibJS/Parser.h | 10 +++++++++- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index 5c77edadac..21bda41742 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -514,7 +514,10 @@ NonnullRefPtr Parser::parse_class_expression(bool expect_class_ } if (match(TokenType::ParenOpen)) { - auto function = parse_function_node(false, true, !super_class.is_null()); + u8 parse_options = FunctionNodeParseOptions::AllowSuperPropertyLookup; + if (!super_class.is_null()) + parse_options |= FunctionNodeParseOptions::AllowSuperConstructorCall; + auto function = parse_function_node(parse_options); auto arg_count = function->parameters().size(); if (method_kind == ClassMethod::Kind::Getter && arg_count != 0) { @@ -761,7 +764,7 @@ NonnullRefPtr Parser::parse_object_expression() if (match(TokenType::ParenOpen)) { ASSERT(property_name); - auto function = parse_function_node(false, true); + auto function = parse_function_node(FunctionNodeParseOptions::AllowSuperPropertyLookup); auto arg_count = function->parameters().size(); if (property_type == ObjectProperty::Type::Getter && arg_count != 0) { @@ -1251,24 +1254,18 @@ NonnullRefPtr Parser::parse_block_statement(bool& is_strict) } template -NonnullRefPtr Parser::parse_function_node(bool check_for_function_and_name, bool allow_super_property_lookup, bool allow_super_constructor_call) +NonnullRefPtr Parser::parse_function_node(u8 parse_options) { - TemporaryChange super_property_access_rollback(m_parser_state.m_allow_super_property_lookup, allow_super_property_lookup); - TemporaryChange super_constructor_call_rollback(m_parser_state.m_allow_super_constructor_call, allow_super_constructor_call); + TemporaryChange super_property_access_rollback(m_parser_state.m_allow_super_property_lookup, !!(parse_options & FunctionNodeParseOptions::AllowSuperPropertyLookup)); + TemporaryChange super_constructor_call_rollback(m_parser_state.m_allow_super_constructor_call, !!(parse_options & FunctionNodeParseOptions::AllowSuperConstructorCall)); ScopePusher scope(*this, ScopePusher::Var | ScopePusher::Function); - if (check_for_function_and_name) - consume(TokenType::Function); - String name; - if (check_for_function_and_name) { - if (FunctionNodeType::must_have_name()) { + if (parse_options & FunctionNodeParseOptions::CheckForFunctionAndName) { + consume(TokenType::Function); + if (FunctionNodeType::must_have_name() || match(TokenType::Identifier)) name = consume(TokenType::Identifier).value(); - } else { - if (match(TokenType::Identifier)) - name = consume(TokenType::Identifier).value(); - } } consume(TokenType::ParenOpen); i32 function_length = -1; diff --git a/Libraries/LibJS/Parser.h b/Libraries/LibJS/Parser.h index ff41b8e240..caeecd34da 100644 --- a/Libraries/LibJS/Parser.h +++ b/Libraries/LibJS/Parser.h @@ -40,6 +40,14 @@ enum class Associativity { Right }; +struct FunctionNodeParseOptions { + enum { + CheckForFunctionAndName = 1 << 0, + AllowSuperPropertyLookup = 1 << 1, + AllowSuperConstructorCall = 1 << 2, + }; +}; + class Parser { public: explicit Parser(Lexer lexer); @@ -47,7 +55,7 @@ public: NonnullRefPtr parse_program(); template - NonnullRefPtr parse_function_node(bool check_for_function_and_name = true, bool allow_super_property_lookup = false, bool allow_super_constructor_call = false); + NonnullRefPtr parse_function_node(u8 parse_options = FunctionNodeParseOptions::CheckForFunctionAndName); Vector parse_function_parameters(int& function_length); NonnullRefPtr parse_statement();