LibJS: Add Parser save_state() and load_state() functions

These functions allow us to try to parse ambiguous expressions (such as
arrow function arguments in parentheses), and
rewind the state of the Parser if an expression candidate failed to
parse.
This commit is contained in:
Jack Karamanian 2020-03-30 08:24:43 -05:00 committed by Andreas Kling
commit f90da71d28
Notes: sideshowbarker 2024-07-19 08:03:05 +09:00
2 changed files with 59 additions and 35 deletions

View file

@ -68,7 +68,7 @@ public:
NonnullRefPtr<CallExpression> parse_call_expression(NonnullRefPtr<Expression>);
NonnullRefPtr<NewExpression> parse_new_expression();
bool has_errors() const { return m_has_errors; }
bool has_errors() const { return m_parser_state.m_has_errors; }
private:
int operator_precedence(TokenType) const;
@ -83,9 +83,18 @@ private:
void expected(const char* what);
Token consume();
Token consume(TokenType type);
void save_state();
void load_state();
Lexer m_lexer;
Token m_current_token;
bool m_has_errors = false;
struct ParserState {
Lexer m_lexer;
Token m_current_token;
bool m_has_errors = false;
explicit ParserState(Lexer);
};
ParserState m_parser_state;
Optional<ParserState> m_saved_state;
};
}