Implements indexing with period identifier style (`foo.bar`)
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-06-17 15:45:33 +02:00
parent d06b04cae9
commit d91caa7f32
17 changed files with 207 additions and 32 deletions

View File

@@ -46,7 +46,9 @@ ParsedStatement* Parser::ParseStatement(const IToken* current){
return ParseVariableAssignment(current);
}
auto expression = this -> ParseExpression(current);
if (expression->GetKind() == ParsedExpressionKind::Indexer && this -> Peek()->GetKind() == TokenKind::AssignmentToken){
auto expKind = expression -> GetKind();
if ((expKind == ParsedExpressionKind::Indexer || expKind == ParsedExpressionKind::PeriodIndexer)
&& this -> Peek()->GetKind() == TokenKind::AssignmentToken){
return this -> ParseIndexAssignment(expression);
}
return new ParsedExpressionStatement(expression);
@@ -206,7 +208,7 @@ ParsedExpression* Parser::ParseExpression(const IToken* current){
} else if (peekKind == TokenKind::OpenSquareBracket){
expression = this->ParseIndexExpression(expression);
} else {
//TODO: index period expression
expression = this -> ParsePeriodIndexExpression(expression);
}
if (this -> _position >= this->_tokens.size())
break;
@@ -379,6 +381,18 @@ ParsedExpression* Parser::ParseIndexExpression(ParsedExpression* indexingExpress
return new IndexExpression(indexingExpression, indexExpression, start, closeBracket->GetEndPosition() - start);
}
ParsedExpression* Parser::ParsePeriodIndexExpression(ParsedExpression* indexingExpression){
this->Next(); // consume '.' token
auto identifier = this -> Next();
if (identifier->GetKind() != TokenKind::Identifier){
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(), identifier->GetLength());
return new BadExpression(indexingExpression->GetStartPosition(), identifier->GetEndPosition() - indexingExpression->GetStartPosition());
}
auto start = indexingExpression->GetStartPosition();
return new PeriodIndexExpression(indexingExpression, ((IdentifierToken*)identifier)->GetValue(), start, identifier->GetEndPosition() - start);
}
ParsedExpression* Parser::ParseTableExpression(const IToken* current){
if (this -> Peek() -> GetKind() == TokenKind::CloseCurlyBracket){
this -> Next();