Allow for empty return expressions

This commit is contained in:
Deukhoofd 2018-11-28 14:31:40 +01:00
parent cefe48cb4b
commit 062f0f84ad
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 20 additions and 3 deletions

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Text; using System.Text;
@ -198,6 +199,11 @@ namespace Upsilon.Parser
{ {
return new IdentifierToken(str, start); return new IdentifierToken(str, start);
} }
if (kind == SyntaxKind.ReturnKeyword)
{
return new ReturnSyntaxToken(start, Next == Environment.NewLine[0]);
}
return new SyntaxToken(kind, start, str, null); return new SyntaxToken(kind, start, str, null);
} }
@ -212,7 +218,7 @@ namespace Upsilon.Parser
if (Current != ' ') if (Current != ' ')
stringBuilder.Append(Current); stringBuilder.Append(Current);
} }
while (Next != '\n' && Next != '\0') while (Next != Environment.NewLine[0] && Next != '\0')
{ {
stringBuilder?.Append(Next); stringBuilder?.Append(Next);
_position++; _position++;

View File

@ -282,9 +282,9 @@ namespace Upsilon.Parser
private StatementSyntax ParseReturnStatement() private StatementSyntax ParseReturnStatement()
{ {
var returnToken = MatchToken(SyntaxKind.ReturnKeyword); var returnToken = (ReturnSyntaxToken)MatchToken(SyntaxKind.ReturnKeyword);
ExpressionSyntax expression = null; ExpressionSyntax expression = null;
if (Current.Kind != SyntaxKind.EndKeyword) if (Current.Kind != SyntaxKind.EndKeyword && !returnToken.FollowedByLineBreak)
expression = ParseExpression(); expression = ParseExpression();
return new ReturnStatementSyntax(returnToken, expression); return new ReturnStatementSyntax(returnToken, expression);
} }

View File

@ -28,4 +28,15 @@ namespace Upsilon.Parser
yield break; yield break;
} }
} }
public class ReturnSyntaxToken : SyntaxToken
{
public bool FollowedByLineBreak { get; }
public ReturnSyntaxToken(int position, bool followedByLineBreak)
: base(SyntaxKind.ReturnKeyword, position, "return", null)
{
FollowedByLineBreak = followedByLineBreak;
}
}
} }