Implements try catch statement.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
54ccac6d96
commit
28ece69be6
|
@ -984,6 +984,7 @@ namespace MalachScript::Parser {
|
|||
if (current->GetKind() != LexTokenKind::DoKeyword) {
|
||||
return false;
|
||||
}
|
||||
PROGRESS_TOKEN(current);
|
||||
ScopedPtr<const ParsedStatement> body = nullptr;
|
||||
if (!ParseStatement(body, current, log)) {
|
||||
logError(DiagnosticType::UnexpectedToken, current->GetSpan());
|
||||
|
@ -1003,4 +1004,29 @@ namespace MalachScript::Parser {
|
|||
currentToken = current;
|
||||
return true;
|
||||
}
|
||||
bool Parser::ParseTryStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken,
|
||||
const Parser::log_func& log) {
|
||||
const auto* current = currentToken;
|
||||
if (current->GetKind() != LexTokenKind::TryKeyword) {
|
||||
return false;
|
||||
}
|
||||
PROGRESS_TOKEN(current);
|
||||
|
||||
ScopedPtr<const ParsedStatement> tryStatement = nullptr;
|
||||
if (!ParseStatBlock(tryStatement, current, log)) {
|
||||
logError(DiagnosticType::UnexpectedToken, current->GetSpan());
|
||||
return false;
|
||||
}
|
||||
EXPECT_TOKEN(current, CatchKeyword);
|
||||
ScopedPtr<const ParsedStatement> catchStatement = nullptr;
|
||||
if (!ParseStatBlock(catchStatement, current, log)) {
|
||||
logError(DiagnosticType::UnexpectedToken, current->GetSpan());
|
||||
return false;
|
||||
}
|
||||
|
||||
out = new ParsedTryStatement(TextSpan(currentToken->GetSpan().GetStart(), current->GetSpan().GetEnd()),
|
||||
tryStatement.TakeOwnership(), catchStatement.TakeOwnership());
|
||||
currentToken = current;
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -158,7 +158,8 @@ namespace MalachScript::Parser {
|
|||
const log_func&);
|
||||
static bool ParseDoWhileStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken,
|
||||
const log_func&);
|
||||
// Try
|
||||
static bool ParseTryStatement(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken,
|
||||
const log_func&);
|
||||
// Case
|
||||
// Switch
|
||||
|
||||
|
|
|
@ -369,7 +369,7 @@ namespace MalachScript::Parser {
|
|||
ParsedContinueStatement(const TextSpan& span) : ParsedStatementImpl(span) {}
|
||||
};
|
||||
|
||||
class ParsedBreakStatement : public ParsedStatementImpl<ParsedStatementKind::Continue> {
|
||||
class ParsedBreakStatement : public ParsedStatementImpl<ParsedStatementKind::Break> {
|
||||
public:
|
||||
ParsedBreakStatement(const TextSpan& span) : ParsedStatementImpl(span) {}
|
||||
};
|
||||
|
@ -407,6 +407,7 @@ namespace MalachScript::Parser {
|
|||
std::unique_ptr<const ParsedStatement> _condition;
|
||||
std::unique_ptr<const ParsedStatement> _body;
|
||||
};
|
||||
|
||||
class ParsedDoWhileStatement : public ParsedStatementImpl<ParsedStatementKind::DoWhile> {
|
||||
public:
|
||||
ParsedDoWhileStatement(const TextSpan& span, const ParsedStatement* condition, const ParsedStatement* body)
|
||||
|
@ -417,6 +418,17 @@ namespace MalachScript::Parser {
|
|||
std::unique_ptr<const ParsedStatement> _body;
|
||||
};
|
||||
|
||||
class ParsedTryStatement : public ParsedStatementImpl<ParsedStatementKind::Try> {
|
||||
public:
|
||||
ParsedTryStatement(const TextSpan& span, const ParsedStatement* tryStatement,
|
||||
const ParsedStatement* catchStatement)
|
||||
: ParsedStatementImpl(span), _tryStatement(tryStatement), _catchStatement(catchStatement) {}
|
||||
|
||||
private:
|
||||
std::unique_ptr<const ParsedStatement> _tryStatement;
|
||||
std::unique_ptr<const ParsedStatement> _catchStatement;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // MALACHSCRIPT_PARSEDSTATEMENT_HPP
|
||||
|
|
|
@ -26,7 +26,8 @@ namespace MalachScript::Parser {
|
|||
Break,
|
||||
For,
|
||||
While,
|
||||
DoWhile
|
||||
DoWhile,
|
||||
Try
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue