Fixed issue where function parser keeps consuming tokens and causes out of range exceptions
This commit is contained in:
		| @@ -59,6 +59,11 @@ public: | |||||||
|         : ParsedStatement(statements.front()->GetStartPosition(), statements.back()->GetEndPosition() - statements.front()->GetStartPosition()){ |         : ParsedStatement(statements.front()->GetStartPosition(), statements.back()->GetEndPosition() - statements.front()->GetStartPosition()){ | ||||||
|         _statements = std::move(statements); |         _statements = std::move(statements); | ||||||
|     } |     } | ||||||
|  |     ParsedBlockStatement(std::vector<ParsedStatement*> statements, unsigned int start) : ParsedStatement(start, 0){ | ||||||
|  |         _statements = std::move(statements); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|     ~ParsedBlockStatement() override { |     ~ParsedBlockStatement() override { | ||||||
|         for (auto s: _statements){ |         for (auto s: _statements){ | ||||||
|             delete s; |             delete s; | ||||||
|   | |||||||
| @@ -9,9 +9,9 @@ | |||||||
|  |  | ||||||
| ParsedScriptStatement* Parser::Parse() { | ParsedScriptStatement* Parser::Parse() { | ||||||
|     vector<ParsedStatement*> statements; |     vector<ParsedStatement*> statements; | ||||||
|     while (true){ |     while (this->_position < this->_tokens.size()){ | ||||||
|         auto next = this -> Next(); |         auto next = this -> Next(); | ||||||
|         if (next == nullptr || next->GetKind() == TokenKind::EndOfFile){ |         if (next->GetKind() == TokenKind::EndOfFile){ | ||||||
|             break; |             break; | ||||||
|         } |         } | ||||||
|         statements.push_back(this -> ParseStatement(next)); |         statements.push_back(this -> ParseStatement(next)); | ||||||
| @@ -74,7 +74,8 @@ ParsedStatement *Parser::ParseAssignment(IToken *current) { | |||||||
|  |  | ||||||
| ParsedStatement *Parser::ParseBlock(const vector<TokenKind>& endTokens) { | ParsedStatement *Parser::ParseBlock(const vector<TokenKind>& endTokens) { | ||||||
|     vector<ParsedStatement*> statements; |     vector<ParsedStatement*> statements; | ||||||
|     while (true){ |     auto start = this->_position; | ||||||
|  |     while (this->_position < this->_tokens.size()){ | ||||||
|         auto next = this -> Next(); |         auto next = this -> Next(); | ||||||
|         auto nextKind = next->GetKind(); |         auto nextKind = next->GetKind(); | ||||||
|         if (std::find(endTokens.begin(), endTokens.end(), nextKind) != endTokens.end()){ |         if (std::find(endTokens.begin(), endTokens.end(), nextKind) != endTokens.end()){ | ||||||
| @@ -86,6 +87,9 @@ ParsedStatement *Parser::ParseBlock(const vector<TokenKind>& endTokens) { | |||||||
|         } |         } | ||||||
|         statements.push_back(this -> ParseStatement(next)); |         statements.push_back(this -> ParseStatement(next)); | ||||||
|     } |     } | ||||||
|  |     if (statements.size() == 0){ | ||||||
|  |         return new ParsedBlockStatement(statements,start); | ||||||
|  |     } | ||||||
|     return new ParsedBlockStatement(statements); |     return new ParsedBlockStatement(statements); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -103,7 +107,7 @@ ParsedStatement *Parser::ParseFunctionDeclaration(IToken *current) { | |||||||
|         hasErrors = true; |         hasErrors = true; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     while (true){ |     while (this -> _position < this->_tokens.size()){ | ||||||
|         auto type = this->Next(); |         auto type = this->Next(); | ||||||
|         auto identifier = this->Next(); |         auto identifier = this->Next(); | ||||||
|         auto next = this->Next(); |         auto next = this->Next(); | ||||||
| @@ -117,6 +121,12 @@ ParsedStatement *Parser::ParseFunctionDeclaration(IToken *current) { | |||||||
|             hasErrors = true; |             hasErrors = true; | ||||||
|             continue; |             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 typeToken = (IdentifierToken*)type; | ||||||
|         auto identifierToken = (IdentifierToken*)identifier; |         auto identifierToken = (IdentifierToken*)identifier; | ||||||
|         parameters.push_back(new TypedVariableIdentifier(HashedString(typeToken->Value), HashedString(identifierToken->Value))); |         parameters.push_back(new TypedVariableIdentifier(HashedString(typeToken->Value), HashedString(identifierToken->Value))); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user