From 062f0f84ade3b614e3d51b8f79a2a64f5fa03e85 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Wed, 28 Nov 2018 14:31:40 +0100 Subject: [PATCH] Allow for empty return expressions --- Upsilon/Parser/Lexer.cs | 8 +++++++- Upsilon/Parser/Parser.cs | 4 ++-- Upsilon/Parser/SyntaxToken.cs | 11 +++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Upsilon/Parser/Lexer.cs b/Upsilon/Parser/Lexer.cs index 6f2f5d3..819daac 100644 --- a/Upsilon/Parser/Lexer.cs +++ b/Upsilon/Parser/Lexer.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Text; @@ -198,6 +199,11 @@ namespace Upsilon.Parser { return new IdentifierToken(str, start); } + + if (kind == SyntaxKind.ReturnKeyword) + { + return new ReturnSyntaxToken(start, Next == Environment.NewLine[0]); + } return new SyntaxToken(kind, start, str, null); } @@ -212,7 +218,7 @@ namespace Upsilon.Parser if (Current != ' ') stringBuilder.Append(Current); } - while (Next != '\n' && Next != '\0') + while (Next != Environment.NewLine[0] && Next != '\0') { stringBuilder?.Append(Next); _position++; diff --git a/Upsilon/Parser/Parser.cs b/Upsilon/Parser/Parser.cs index 53018e6..b3bfcda 100644 --- a/Upsilon/Parser/Parser.cs +++ b/Upsilon/Parser/Parser.cs @@ -282,9 +282,9 @@ namespace Upsilon.Parser private StatementSyntax ParseReturnStatement() { - var returnToken = MatchToken(SyntaxKind.ReturnKeyword); + var returnToken = (ReturnSyntaxToken)MatchToken(SyntaxKind.ReturnKeyword); ExpressionSyntax expression = null; - if (Current.Kind != SyntaxKind.EndKeyword) + if (Current.Kind != SyntaxKind.EndKeyword && !returnToken.FollowedByLineBreak) expression = ParseExpression(); return new ReturnStatementSyntax(returnToken, expression); } diff --git a/Upsilon/Parser/SyntaxToken.cs b/Upsilon/Parser/SyntaxToken.cs index d34817d..0136799 100644 --- a/Upsilon/Parser/SyntaxToken.cs +++ b/Upsilon/Parser/SyntaxToken.cs @@ -28,4 +28,15 @@ namespace Upsilon.Parser yield break; } } + + public class ReturnSyntaxToken : SyntaxToken + { + public bool FollowedByLineBreak { get; } + + public ReturnSyntaxToken(int position, bool followedByLineBreak) + : base(SyntaxKind.ReturnKeyword, position, "return", null) + { + FollowedByLineBreak = followedByLineBreak; + } + } } \ No newline at end of file