diff options
Diffstat (limited to 'lib/Parser')
| -rw-r--r-- | lib/Parser/Parser.cc | 31 | ||||
| -rw-r--r-- | lib/Parser/ParserFunction.cc | 3 |
2 files changed, 33 insertions, 1 deletions
diff --git a/lib/Parser/Parser.cc b/lib/Parser/Parser.cc index 098ef82..aeb81fe 100644 --- a/lib/Parser/Parser.cc +++ b/lib/Parser/Parser.cc @@ -276,6 +276,7 @@ void Parser::syncToStatementStart() { case tok_openfile: case tok_readfile: case tok_writefile: case tok_closefile: case tok_type: case tok_define: case tok_endtype: case tok_class: case tok_endclass: + case tok_try: case tok_catch: case tok_endtry: case tok_throw: return; default: getNextToken(); @@ -399,6 +400,23 @@ std::unique_ptr<StmtAST> Parser::ParseDeclare() { } } +std::unique_ptr<StmtAST> Parser::ParseTryStmt() { + getNextToken(); + auto Body = ParseBlock({tok_catch, tok_endtry}); + if (CurTok != tok_catch) { + fprintf(stderr, "error: Expected CATCH in TRY at line %d\n", Lex.getLine()); + return nullptr; + } + getNextToken(); + auto CatchBody = ParseBlock({tok_endtry, tok_catch}); + if (CurTok != tok_endtry) { + fprintf(stderr, "error: Expected ENDTRY after CATCH at line %d\n", Lex.getLine()); + return nullptr; + } + getNextToken(); + return std::make_unique<TryStmtAST>(std::move(Body), std::move(CatchBody)); +} + std::unique_ptr<StmtAST> Parser::ParseIfStmt() { getNextToken(); auto Cond = ParseExpression(); @@ -569,6 +587,19 @@ std::unique_ptr<StmtAST> Parser::ParseStatementImpl() { else if (CurTok == tok_if) { return ParseIfStmt(); } + else if (CurTok == tok_try) { + return ParseTryStmt(); + } + else if (CurTok == tok_throw) { + getNextToken(); + return std::make_unique<ThrowStmtAST>(); + } + else if (CurTok == tok_catch || CurTok == tok_endtry) { + fprintf(stderr, "error: Unexpected %s without matching TRY at line %d\n", + CurTok == tok_catch ? "CATCH" : "ENDTRY", Lex.getLine()); + getNextToken(); + return nullptr; + } else if (CurTok == tok_while) { return ParseWhileStmt(); } diff --git a/lib/Parser/ParserFunction.cc b/lib/Parser/ParserFunction.cc index f4e39de..25375f2 100644 --- a/lib/Parser/ParserFunction.cc +++ b/lib/Parser/ParserFunction.cc @@ -224,7 +224,8 @@ std::unique_ptr<StmtAST> Parser::ParseReturnStmt() { // A bare RETURN is recognized right before any block terminator. if (CurTok != tok_endif && CurTok != tok_else && CurTok != tok_endfunction && CurTok != tok_endprocedure && CurTok != tok_endwhile && CurTok != tok_until && - CurTok != tok_next && CurTok != tok_eof) { + CurTok != tok_next && CurTok != tok_catch && CurTok != tok_endtry && + CurTok != tok_eof) { Expr = ParseExpression(); if (!Expr) return nullptr; } |
