diff options
author | Andreas Kling <kling@serenityos.org> | 2020-03-24 22:03:50 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-24 22:21:58 +0100 |
commit | faddf3a1db6d9c8bb4106eb2259f91fe8e58b817 (patch) | |
tree | ad93acf4f78d132e27259fc4ef5109e3266f9efe /Libraries/LibJS/Parser.cpp | |
parent | db024a9cb147731cfb14ef60a862a3e392eedc9b (diff) | |
download | serenity-faddf3a1db6d9c8bb4106eb2259f91fe8e58b817.zip |
LibJS: Implement "throw"
You can now throw an expression to the nearest catcher! :^)
To support throwing arbitrary values, I added an Exception class that
sits as a wrapper around whatever is thrown. In the future it will be
a logical place to store a call stack.
Diffstat (limited to 'Libraries/LibJS/Parser.cpp')
-rw-r--r-- | Libraries/LibJS/Parser.cpp | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index 77e892e553..7b1769ac4a 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -197,6 +197,8 @@ NonnullRefPtr<Statement> Parser::parse_statement() return parse_for_statement(); case TokenType::If: return parse_if_statement(); + case TokenType::Throw: + return parse_throw_statement(); case TokenType::Try: return parse_try_statement(); default: @@ -520,6 +522,12 @@ NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration() return create_ast_node<VariableDeclaration>(create_ast_node<Identifier>(name), move(initializer), declaration_type); } +NonnullRefPtr<ThrowStatement> Parser::parse_throw_statement() +{ + consume(TokenType::Throw); + return create_ast_node<ThrowStatement>(parse_expression(0)); +} + NonnullRefPtr<TryStatement> Parser::parse_try_statement() { consume(TokenType::Try); @@ -700,6 +708,7 @@ bool Parser::match_statement() const || type == TokenType::Delete || type == TokenType::Do || type == TokenType::If + || type == TokenType::Throw || type == TokenType::Try || type == TokenType::While || type == TokenType::For |