mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
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:
parent
50f8f27ac6
commit
db75be1119
Notes:
sideshowbarker
2024-07-19 01:49:45 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/db75be1119b Pull-request: https://github.com/SerenityOS/serenity/pull/3809
2 changed files with 20 additions and 15 deletions
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue