Show full lines in error messages in Ycicle

This commit is contained in:
Deukhoofd 2018-11-17 14:49:26 +01:00
parent 16d50b3311
commit 7beeb713c9
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 30 additions and 5 deletions

View File

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

View File

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

View File

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

View File

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