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.
This commit is contained in:
Linus Groh 2020-10-20 17:56:49 +01:00 committed by Andreas Kling
parent 50f8f27ac6
commit db75be1119
Notes: sideshowbarker 2024-07-19 01:49:45 +09:00
2 changed files with 20 additions and 15 deletions

View file

@ -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;

View file

@ -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();