Skip to content

Commit

Permalink
feat: Add MessageFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft authored Jul 9, 2024
1 parent 056e5c7 commit e2d6a17
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Source/Samples/Sample.FuncLanguage/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public static void Main(string[] args)
//Console.WriteLine("New: ");
//Console.WriteLine(rewritten.Accept(new PrintVisitor()));
var evaluated = rewritten.Accept(new EvaluationVisitor(), Scope.Root);

parsed.PrintMessages();

//Console.WriteLine("> " + evaluated);
}
}
Expand Down
58 changes: 58 additions & 0 deletions Source/Silverfly/Text/MessageFormatter.cs
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(" ·");
}
}
6 changes: 3 additions & 3 deletions Source/Silverfly/Text/SourceRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public string GetText()
var endLine = End.Line - 1;

if (startLine < 0 || endLine >= CountLines(source))
throw new ArgumentOutOfRangeException("Range is out of document lines.");
throw new IndexOutOfRangeException("Range is out of document lines.");

var startIdx = GetLineStartIndex(source, startLine) + Start.Column;
var startIdx = GetLineStartIndex(source, startLine) + Start.Column - 1;
var endIdx = GetLineStartIndex(source, endLine) + End.Column;

return new string(source[startIdx..endIdx]);
Expand Down Expand Up @@ -104,7 +104,7 @@ private int GetLineStartIndex(ReadOnlySpan<char> source, int line)
currentLine++;
}

throw new ArgumentOutOfRangeException("Line number out of range.");
throw new ArgumentOutOfRangeException(nameof(line), " line out of range.");
}

/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions Source/Silverfly/TranslationUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ public class TranslationUnit(AstNode tree, SourceDocument document)
/// Gets the source document associated with the translation unit.
/// </summary>
public SourceDocument Document { get; } = document;

public void PrintMessages()
{
foreach (var message in Document.Messages)
{
MessageFormatter.PrintError(CompilerError.FromMessage(message));
}
}
}

0 comments on commit e2d6a17

Please sign in to comment.