summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Parser.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-03-24 14:03:55 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-24 16:14:10 +0100
commitc33d4aefc3eb1d91d90c55b4d18e6b972fd8d6eb (patch)
treef13f55382d84fe0d23f9be4fd17090e12b672f8b /Libraries/LibJS/Parser.cpp
parent404de10a151650fd0f69d4c9347a509757941d5f (diff)
downloadserenity-c33d4aefc3eb1d91d90c55b4d18e6b972fd8d6eb.zip
LibJS: Parse "try", "catch" and "finally"
This is the first step towards support exceptions. :^)
Diffstat (limited to 'Libraries/LibJS/Parser.cpp')
-rw-r--r--Libraries/LibJS/Parser.cpp37
1 files changed, 36 insertions, 1 deletions
diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp
index 082f83f457..77e892e553 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::Try:
+ return parse_try_statement();
default:
if (match_expression())
return adopt(*new ExpressionStatement(parse_expression(0)));
@@ -518,6 +520,40 @@ NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration()
return create_ast_node<VariableDeclaration>(create_ast_node<Identifier>(name), move(initializer), declaration_type);
}
+NonnullRefPtr<TryStatement> Parser::parse_try_statement()
+{
+ consume(TokenType::Try);
+
+ auto block = parse_block_statement();
+
+ RefPtr<CatchClause> handler;
+ if (match(TokenType::Catch))
+ handler = parse_catch_clause();
+
+ RefPtr<BlockStatement> finalizer;
+ if (match(TokenType::Finally)) {
+ consume();
+ finalizer = parse_block_statement();
+ }
+
+ return create_ast_node<TryStatement>(move(block), move(handler), move(finalizer));
+}
+
+NonnullRefPtr<CatchClause> Parser::parse_catch_clause()
+{
+ consume(TokenType::Catch);
+
+ String parameter;
+ if (match(TokenType::ParenOpen)) {
+ consume();
+ parameter = consume(TokenType::Identifier).value();
+ consume(TokenType::ParenClose);
+ }
+
+ auto body = parse_block_statement();
+ return create_ast_node<CatchClause>(parameter, move(body));
+}
+
NonnullRefPtr<IfStatement> Parser::parse_if_statement()
{
consume(TokenType::If);
@@ -660,7 +696,6 @@ bool Parser::match_statement() const
|| type == TokenType::Function
|| type == TokenType::Return
|| type == TokenType::Let
- || type == TokenType::Catch
|| type == TokenType::Class
|| type == TokenType::Delete
|| type == TokenType::Do