diff options
author | Linus Groh <mail@linusgroh.de> | 2020-10-20 17:56:49 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-20 20:27:58 +0200 |
commit | db75be1119b5b888255840d6131f47af66f40151 (patch) | |
tree | 0a8fab7ef683dbcdd3177c8a7c8042d101556437 /Libraries | |
parent | 50f8f27ac6867c2d69db8dbb4e1ca84c255bb729 (diff) | |
download | serenity-db75be1119b5b888255840d6131f47af66f40151.zip |
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.
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Parser.cpp | 25 | ||||
-rw-r--r-- | 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<ClassExpression> Parser::parse_class_expression(bool expect_class_ } if (match(TokenType::ParenOpen)) { - auto function = parse_function_node<FunctionExpression>(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<FunctionExpression>(parse_options); auto arg_count = function->parameters().size(); if (method_kind == ClassMethod::Kind::Getter && arg_count != 0) { @@ -761,7 +764,7 @@ NonnullRefPtr<ObjectExpression> Parser::parse_object_expression() if (match(TokenType::ParenOpen)) { ASSERT(property_name); - auto function = parse_function_node<FunctionExpression>(false, true); + auto function = parse_function_node<FunctionExpression>(FunctionNodeParseOptions::AllowSuperPropertyLookup); auto arg_count = function->parameters().size(); if (property_type == ObjectProperty::Type::Getter && arg_count != 0) { @@ -1251,24 +1254,18 @@ NonnullRefPtr<BlockStatement> Parser::parse_block_statement(bool& is_strict) } template<typename FunctionNodeType> -NonnullRefPtr<FunctionNodeType> Parser::parse_function_node(bool check_for_function_and_name, bool allow_super_property_lookup, bool allow_super_constructor_call) +NonnullRefPtr<FunctionNodeType> 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<Program> parse_program(); template<typename FunctionNodeType> - NonnullRefPtr<FunctionNodeType> parse_function_node(bool check_for_function_and_name = true, bool allow_super_property_lookup = false, bool allow_super_constructor_call = false); + NonnullRefPtr<FunctionNodeType> parse_function_node(u8 parse_options = FunctionNodeParseOptions::CheckForFunctionAndName); Vector<FunctionNode::Parameter> parse_function_parameters(int& function_length); NonnullRefPtr<Statement> parse_statement(); |