Fixed issue where function parser keeps consuming tokens and causes out of range exceptions

This commit is contained in:
Deukhoofd 2019-06-08 14:40:21 +02:00
parent ed6fbdbef0
commit a2263535d9
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
2 changed files with 19 additions and 4 deletions

View File

@ -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;

View File

@ -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)));