From a2263535d91f5a52c12e65d4b3f409e5bf717e71 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 8 Jun 2019 14:40:21 +0200 Subject: [PATCH] Fixed issue where function parser keeps consuming tokens and causes out of range exceptions --- .../ParsedStatements/ParsedStatement.hpp | 5 +++++ src/Parser/Parser.cpp | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Parser/ParsedStatements/ParsedStatement.hpp b/src/Parser/ParsedStatements/ParsedStatement.hpp index ff9918a..d75e3df 100644 --- a/src/Parser/ParsedStatements/ParsedStatement.hpp +++ b/src/Parser/ParsedStatements/ParsedStatement.hpp @@ -59,6 +59,11 @@ public: : ParsedStatement(statements.front()->GetStartPosition(), statements.back()->GetEndPosition() - statements.front()->GetStartPosition()){ _statements = std::move(statements); } + ParsedBlockStatement(std::vector statements, unsigned int start) : ParsedStatement(start, 0){ + _statements = std::move(statements); + } + + ~ParsedBlockStatement() override { for (auto s: _statements){ delete s; diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index e7e8a07..14adae4 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -9,9 +9,9 @@ ParsedScriptStatement* Parser::Parse() { vector statements; - while (true){ + while (this->_position < this->_tokens.size()){ auto next = this -> Next(); - if (next == nullptr || next->GetKind() == TokenKind::EndOfFile){ + if (next->GetKind() == TokenKind::EndOfFile){ break; } statements.push_back(this -> ParseStatement(next)); @@ -74,7 +74,8 @@ ParsedStatement *Parser::ParseAssignment(IToken *current) { ParsedStatement *Parser::ParseBlock(const vector& endTokens) { vector statements; - while (true){ + auto start = this->_position; + while (this->_position < this->_tokens.size()){ auto next = this -> Next(); auto nextKind = next->GetKind(); if (std::find(endTokens.begin(), endTokens.end(), nextKind) != endTokens.end()){ @@ -86,6 +87,9 @@ ParsedStatement *Parser::ParseBlock(const vector& endTokens) { } statements.push_back(this -> ParseStatement(next)); } + if (statements.size() == 0){ + return new ParsedBlockStatement(statements,start); + } return new ParsedBlockStatement(statements); } @@ -103,7 +107,7 @@ ParsedStatement *Parser::ParseFunctionDeclaration(IToken *current) { hasErrors = true; } - while (true){ + while (this -> _position < this->_tokens.size()){ auto type = this->Next(); auto identifier = this->Next(); auto next = this->Next(); @@ -117,6 +121,12 @@ ParsedStatement *Parser::ParseFunctionDeclaration(IToken *current) { hasErrors = true; continue; } + + if (type->GetKind() != TokenKind::Identifier || identifier->GetKind() != TokenKind::Identifier){ + this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, type->GetStartPosition(), type->GetLength()); + hasErrors = true; + continue; + } auto typeToken = (IdentifierToken*)type; auto identifierToken = (IdentifierToken*)identifier; parameters.push_back(new TypedVariableIdentifier(HashedString(typeToken->Value), HashedString(identifierToken->Value)));