1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:17:45 +00:00

LibJS: Do not save state for peeking at the next token from the lexer

This saves having to save and load the parser state.
This could give an incorrect token in some cases where the parser
communicates to the lexer. However this is not applicable in any of the
current usages and this would require one to parse the current token
as normal which is exactly what you don't want to do in that scenario.
This commit is contained in:
davidot 2021-10-08 00:38:24 +02:00 committed by Linus Groh
parent c3cb44ca8a
commit 9394cbcd24
2 changed files with 12 additions and 17 deletions

View file

@ -615,8 +615,7 @@ RefPtr<FunctionExpression> Parser::try_parse_arrow_function_expression(bool expe
// The logic is duplicated below in the "real" !expect_parens branch. // The logic is duplicated below in the "real" !expect_parens branch.
if (!match_identifier() && !match(TokenType::Yield) && !match(TokenType::Await)) if (!match_identifier() && !match(TokenType::Yield) && !match(TokenType::Await))
return nullptr; return nullptr;
auto forked_lexer = m_state.lexer; auto token = next_token();
auto token = forked_lexer.next();
if (token.trivia_contains_line_terminator()) if (token.trivia_contains_line_terminator())
return nullptr; return nullptr;
if (token.type() != TokenType::Arrow) if (token.type() != TokenType::Arrow)
@ -725,9 +724,7 @@ RefPtr<Statement> Parser::try_parse_labelled_statement(AllowLabelledFunction all
{ {
{ {
// NOTE: This is a fast path where we try to fail early to avoid the expensive save_state+load_state. // NOTE: This is a fast path where we try to fail early to avoid the expensive save_state+load_state.
auto forked_lexer = m_state.lexer; if (next_token().type() != TokenType::Colon)
auto token = forked_lexer.next();
if (token.type() != TokenType::Colon)
return {}; return {};
} }
@ -3138,7 +3135,7 @@ bool Parser::match_export_or_import() const
|| type == TokenType::Import; || type == TokenType::Import;
} }
bool Parser::match_declaration() bool Parser::match_declaration() const
{ {
auto type = m_state.current_token.type(); auto type = m_state.current_token.type();
@ -3152,18 +3149,16 @@ bool Parser::match_declaration()
|| type == TokenType::Let; || type == TokenType::Let;
} }
Token Parser::next_token() Token Parser::next_token() const
{ {
save_state(); Lexer lookahead_lexer = m_state.lexer;
consume(); auto token_after = lookahead_lexer.next();
auto token_after = m_state.current_token;
load_state();
return token_after; return token_after;
} }
bool Parser::try_match_let_declaration() bool Parser::try_match_let_declaration() const
{ {
VERIFY(m_state.current_token.type() == TokenType::Let); VERIFY(m_state.current_token.type() == TokenType::Let);
auto token_after = next_token(); auto token_after = next_token();
@ -3177,7 +3172,7 @@ bool Parser::try_match_let_declaration()
return false; return false;
} }
bool Parser::match_variable_declaration() bool Parser::match_variable_declaration() const
{ {
auto type = m_state.current_token.type(); auto type = m_state.current_token.type();

View file

@ -172,9 +172,9 @@ private:
bool match_secondary_expression(const Vector<TokenType>& forbidden = {}) const; bool match_secondary_expression(const Vector<TokenType>& forbidden = {}) const;
bool match_statement() const; bool match_statement() const;
bool match_export_or_import() const; bool match_export_or_import() const;
bool match_declaration(); bool match_declaration() const;
bool try_match_let_declaration(); bool try_match_let_declaration() const;
bool match_variable_declaration(); bool match_variable_declaration() const;
bool match_identifier() const; bool match_identifier() const;
bool match_identifier_name() const; bool match_identifier_name() const;
bool match_property_key() const; bool match_property_key() const;
@ -195,7 +195,7 @@ private:
RefPtr<BindingPattern> synthesize_binding_pattern(Expression const& expression); RefPtr<BindingPattern> synthesize_binding_pattern(Expression const& expression);
Token next_token(); Token next_token() const;
void check_identifier_name_for_assignment_validity(StringView, bool force_strict = false); void check_identifier_name_for_assignment_validity(StringView, bool force_strict = false);