-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Silverfly.Text; | ||
|
||
public class CompilerError(Message message) | ||
{ | ||
public Message Message { get; } = message; | ||
public List<string> SourceLines { get; set; } = []; | ||
public List<int> HighlightLines { get; set; } = []; | ||
public List<string> Notes { get; set; } = []; | ||
|
||
public static CompilerError FromMessage(Message message) | ||
{ | ||
var error = new CompilerError(message); | ||
error.HighlightLines.Add(message.Range.Start.Line); | ||
error.SourceLines.Add(message.Range.GetText()); | ||
|
||
return error; | ||
} | ||
} | ||
|
||
public static class MessageFormatter | ||
{ | ||
public static void PrintError(CompilerError error) | ||
{ | ||
Console.WriteLine("┌────────────────────────────────────────────────────────────────────────────"); | ||
Console.WriteLine($"{error.Message.Severity}: {error.Message.Text}"); | ||
Console.WriteLine($" ┌─ {error.Message.Document.Filename}:{error.Message.Range.Start.Line}:{error.Message.Range.Start.Column}"); | ||
|
||
for (var i = 0; i < error.SourceLines.Count; i++) | ||
{ | ||
var lineNumber = error.Message.Range.Start.Line + i; | ||
var lineContent = error.SourceLines[i]; | ||
|
||
if (error.HighlightLines.Contains(i)) | ||
{ | ||
Console.WriteLine($" {lineNumber} │ {lineContent}"); | ||
var highlightIndex = error.Message.Range.Start.Column; // Default to column if no caret | ||
|
||
var underline = new string(' ', highlightIndex) + "╭─" + new string('─', lineContent.Length - highlightIndex - 2) + "^"; | ||
Console.WriteLine($" │ {underline}"); | ||
} | ||
else | ||
{ | ||
Console.WriteLine($" {lineNumber} │ {lineContent}"); | ||
} | ||
|
||
// Notes | ||
foreach (var note in error.Notes) | ||
{ | ||
Console.WriteLine($" │ {note}"); | ||
} | ||
} | ||
|
||
Console.WriteLine(" ·"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters