From 7beeb713c9d2fc155876885d0e58928899efbd6a Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 17 Nov 2018 14:49:26 +0100 Subject: [PATCH] Show full lines in error messages in Ycicle --- Upsilon/Diagnostics.cs | 15 +++++++++++++++ Upsilon/Parser/Parser.cs | 6 +++--- Upsilon/Text/SourceText.cs | 10 ++++++++++ Ycicle/Program.cs | 4 ++-- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Upsilon/Diagnostics.cs b/Upsilon/Diagnostics.cs index 86b785a..12f2cdb 100644 --- a/Upsilon/Diagnostics.cs +++ b/Upsilon/Diagnostics.cs @@ -93,6 +93,13 @@ namespace Upsilon return $"({linePos.Line},{linePos.Pos})"; } + public string LineBeforeError() + { + var linePos = Diagnostics.ScriptString.GetLinePosition(Span.Start); + var lineIndex = Diagnostics.ScriptString.GetLineStartPos(linePos.Line); + return Diagnostics.ScriptString.GetSpan(lineIndex, linePos.Pos); + } + public string BeforeError(int i = 5) { return Diagnostics.ScriptString.GetSpan(Span.Start - i, i); @@ -103,6 +110,14 @@ namespace Upsilon return Diagnostics.ScriptString.GetSpan(Span); } + public string LineAfterError() + { + var linePos = Diagnostics.ScriptString.GetLinePosition(Span.Start); + var lineInfo = Diagnostics.ScriptString.GetLineInfo(linePos.Line); + + return Diagnostics.ScriptString.GetSpan(Span.End, lineInfo.End - Span.End); + } + public string AfterError(int i = 5) { return Diagnostics.ScriptString.GetSpan(Span.Start + Span.Length, i); diff --git a/Upsilon/Parser/Parser.cs b/Upsilon/Parser/Parser.cs index 2d34f68..b0fbb70 100644 --- a/Upsilon/Parser/Parser.cs +++ b/Upsilon/Parser/Parser.cs @@ -28,9 +28,9 @@ namespace Upsilon.Parser private SyntaxToken Get(int offset) { - if (_position + offset >= _tokens.Length) - return new SyntaxToken(SyntaxKind.EndOfFile, _position + offset, "\0", null); - return _tokens[_position + offset]; + return _position + offset >= _tokens.Length + ? new SyntaxToken(SyntaxKind.EndOfFile, _position + offset, "\0", null) + : _tokens[_position + offset]; } private SyntaxToken NextToken() diff --git a/Upsilon/Text/SourceText.cs b/Upsilon/Text/SourceText.cs index 9b15de0..6b5b098 100644 --- a/Upsilon/Text/SourceText.cs +++ b/Upsilon/Text/SourceText.cs @@ -60,6 +60,16 @@ namespace Upsilon.Text return (min, newPos); } + public int GetLineStartPos(int lineIndex) + { + return _lines[lineIndex].Start; + } + + public SourceTextLine GetLineInfo(int lineIndex) + { + return _lines[lineIndex]; + } + public string GetSpan(TextSpan span) { return GetSpan(span.Start, span.Length); diff --git a/Ycicle/Program.cs b/Ycicle/Program.cs index 1c65f66..9fd54a7 100644 --- a/Ycicle/Program.cs +++ b/Ycicle/Program.cs @@ -66,13 +66,13 @@ namespace Ycicle Console.ForegroundColor = ConsoleColor.Gray; Console.Write(message.GetDiagnosticPosition() + " "); - Console.Write(message.BeforeError()); + Console.Write(message.LineBeforeError()); Console.ForegroundColor = ConsoleColor.Red; Console.Write(message.AtError()); Console.ForegroundColor = ConsoleColor.Gray; - Console.Write(message.AfterError()); + Console.Write(message.LineAfterError()); Console.WriteLine(); } }