...
This commit is contained in:
@@ -1,88 +1,96 @@
|
||||
namespace Compiler;
|
||||
|
||||
public sealed class Diagnostic(DiagnosticSeverity severity, string message, string? help, FileInfo? file)
|
||||
public class Diagnostic
|
||||
{
|
||||
public static DiagnosticBuilder Info(string message) => new DiagnosticBuilder(DiagnosticSeverity.Info, message);
|
||||
public static DiagnosticBuilder Warning(string message) => new DiagnosticBuilder(DiagnosticSeverity.Warning, message);
|
||||
public static DiagnosticBuilder Error(string message) => new DiagnosticBuilder(DiagnosticSeverity.Error, message);
|
||||
public static Builder Info(string message) => new Builder(DiagnosticSeverity.Info, message);
|
||||
public static Builder Warning(string message) => new Builder(DiagnosticSeverity.Warning, message);
|
||||
public static Builder Error(string message) => new Builder(DiagnosticSeverity.Error, message);
|
||||
|
||||
public DiagnosticSeverity Severity { get; } = severity;
|
||||
public string Message { get; } = message;
|
||||
public string? Help { get; } = help;
|
||||
public FileInfo? File { get; } = file;
|
||||
}
|
||||
|
||||
public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string message)
|
||||
{
|
||||
private FileInfo? file;
|
||||
private string? help;
|
||||
|
||||
public DiagnosticBuilder At(string fileName, int line, int column, int length)
|
||||
private Diagnostic(DiagnosticSeverity severity, string message, string? help, FileInfo? file)
|
||||
{
|
||||
file = new FileInfo(fileName, line, column, length);
|
||||
return this;
|
||||
Severity = severity;
|
||||
Message = message;
|
||||
Help = help;
|
||||
File = file;
|
||||
}
|
||||
|
||||
public DiagnosticBuilder At(string fileName, Token? token)
|
||||
public class FileInfo(string file, int line, int column, int length)
|
||||
{
|
||||
if (token != null)
|
||||
public string File { get; } = file;
|
||||
public int Line { get; } = line;
|
||||
public int Column { get; } = column;
|
||||
public int Length { get; } = length;
|
||||
}
|
||||
|
||||
public DiagnosticSeverity Severity { get; }
|
||||
public string Message { get; }
|
||||
public string? Help { get; }
|
||||
public FileInfo? File { get; }
|
||||
|
||||
public enum DiagnosticSeverity
|
||||
{
|
||||
Info,
|
||||
Warning,
|
||||
Error,
|
||||
}
|
||||
|
||||
public class Builder(DiagnosticSeverity severity, string message)
|
||||
{
|
||||
private FileInfo? file;
|
||||
private string? help;
|
||||
|
||||
public Builder At(string fileName, int line, int column, int length)
|
||||
{
|
||||
At(fileName, token.Line, token.Column, token.Length);
|
||||
file = new FileInfo(fileName, line, column, length);
|
||||
return this;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public DiagnosticBuilder At(string fileName, Node? node)
|
||||
{
|
||||
if (node != null && node.Tokens.Count != 0)
|
||||
public Builder At(string fileName, Token? token)
|
||||
{
|
||||
// todo(nub31): Calculate length based on last token
|
||||
At(fileName, node.Tokens[0]);
|
||||
if (token != null)
|
||||
{
|
||||
At(fileName, token.Line, token.Column, token.Length);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public DiagnosticBuilder At(string fileName, TypedNode? node)
|
||||
{
|
||||
if (node != null && node.Tokens.Count != 0)
|
||||
public Builder At(string fileName, Node? node)
|
||||
{
|
||||
// todo(nub31): Calculate length based on last token
|
||||
At(fileName, node.Tokens[0]);
|
||||
if (node != null && node.Tokens.Count != 0)
|
||||
{
|
||||
// todo(nub31): Calculate length based on last token
|
||||
At(fileName, node.Tokens[0]);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
public Builder At(string fileName, TypedNode? node)
|
||||
{
|
||||
if (node != null && node.Tokens.Count != 0)
|
||||
{
|
||||
// todo(nub31): Calculate length based on last token
|
||||
At(fileName, node.Tokens[0]);
|
||||
}
|
||||
|
||||
public DiagnosticBuilder WithHelp(string helpMessage)
|
||||
{
|
||||
help = helpMessage;
|
||||
return this;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Diagnostic Build()
|
||||
{
|
||||
return new Diagnostic(severity, message, help, file);
|
||||
public Builder WithHelp(string helpMessage)
|
||||
{
|
||||
help = helpMessage;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Diagnostic Build()
|
||||
{
|
||||
return new Diagnostic(severity, message, help, file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class FileInfo(string file, int line, int column, int length)
|
||||
{
|
||||
public string File { get; } = file;
|
||||
public int Line { get; } = line;
|
||||
public int Column { get; } = column;
|
||||
public int Length { get; } = length;
|
||||
}
|
||||
|
||||
public enum DiagnosticSeverity
|
||||
{
|
||||
Info,
|
||||
Warning,
|
||||
Error,
|
||||
}
|
||||
|
||||
public sealed class CompileException(Diagnostic diagnostic) : Exception
|
||||
public class CompileException(Diagnostic diagnostic) : Exception
|
||||
{
|
||||
public Diagnostic Diagnostic { get; } = diagnostic;
|
||||
}
|
||||
@@ -93,9 +101,9 @@ public static class DiagnosticFormatter
|
||||
{
|
||||
var (label, color) = diagnostic.Severity switch
|
||||
{
|
||||
DiagnosticSeverity.Info => ("info", Ansi.Cyan),
|
||||
DiagnosticSeverity.Warning => ("warning", Ansi.Yellow),
|
||||
DiagnosticSeverity.Error => ("error", Ansi.Red),
|
||||
Diagnostic.DiagnosticSeverity.Info => ("info", Ansi.Cyan),
|
||||
Diagnostic.DiagnosticSeverity.Warning => ("warning", Ansi.Yellow),
|
||||
Diagnostic.DiagnosticSeverity.Error => ("error", Ansi.Red),
|
||||
_ => ("unknown", Ansi.Reset),
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user