public CompilerError(string message, SourceRange sourceRange) : base(message) { Range = sourceRange; }
public CompilerError(string message, SourceLocation location) : base(message) { Range = new SourceRange(location, null); }
/// <summary> /// Issue a fatal error message /// </summary> public void Fatal(string message, SourceRange range = null) { Consume(SeverityEnum.Fatal, range, message, null); }
private void Consume(SeverityEnum severity, SourceRange range, string message, string stackTrace) { var locStart = range?.Start; var locStartStr = locStart != null?IO.Normalize(locStart.ToString()) + " " : ""; // Log the message switch (severity) { case SeverityEnum.Warning: Logger.Warn($"{locStartStr}{message}"); break; case SeverityEnum.Error: Logger.Error($"{locStartStr}{message}"); break; case SeverityEnum.Fatal: Logger.Fatal($"{locStartStr}{message}"); break; } if (!string.IsNullOrEmpty(stackTrace)) { Logger.Debug(stackTrace); } // Print to console if (UseColors) { Console.ForegroundColor = ConsoleColor.White; Stream.Write($"{GetLocationOrExecutable(locStart)} "); switch (severity) { case SeverityEnum.Warning: Console.ForegroundColor = ConsoleColor.Magenta; break; case SeverityEnum.Error: Console.ForegroundColor = ConsoleColor.Red; break; case SeverityEnum.Fatal: Console.ForegroundColor = ConsoleColor.Red; break; } Stream.Write(GetNameOfSeverity(severity) + ": "); Console.ForegroundColor = ConsoleColor.White; Stream.WriteLine(message); Console.ResetColor(); } else { Stream.WriteLine($"{GetLocationOrExecutable(locStart)} {GetNameOfSeverity(severity)}: {message}"); } // Render the code location if (locStart != null && locStart.Row != -1 && locStart.Column != -1 && File.Exists(locStart.File)) { var lines = File.ReadAllLines(locStart.File); if (locStart.Row - 1 < lines.Count()) { var width = Console.WindowWidth - 1; var line = lines[locStart.Row - 1]; var size = line.Count(); int cursor; // Print line if (size > width) { var col = locStart.Column; // col // | // ~~~~~~~~~~v~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ size // ----------------------- width // if (col < (width - 4)) { // Chop right var left = line.Substring(0, col); var right = line.Substring(col, width - col - 4) + " ..."; Stream.WriteLine(left + right); cursor = col; } // col // | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~v~~~~~~~~~~ size // ----------------------- width // else if ((size - col) < (width - 4)) { // Chop left var left = "... " + line.Substring(size - width, col - 4); var right = line.Substring(col); Stream.WriteLine(left + right); cursor = size - width + col; } // col // | // ~~~~~~~~~~~~~~~~~~~~~~~~v~~~~~~~~~~~~~~~~~~ size // ----------------------- width // else { // Chop both ends var halfWidth = width / 2; var left = "... " + line.Substring(col - halfWidth + 4, halfWidth - 4); var right = line.Substring(col, halfWidth - 4) + " ..."; Stream.WriteLine(left + right); cursor = halfWidth; } } else { Stream.WriteLine(line); cursor = locStart.Column; } // Print cursor string arrow; if (range.End == null || range.End.Column == -1 || range.End.Column == range.Start.Column) { arrow = new string(' ', cursor - 1) + "^"; } else { if (range.End.Column > range.Start.Column) { var len = range.End.Column - range.Start.Column; arrow = new string(' ', cursor - 1) + "^" + new string('~', Math.Min(len - 1, width - cursor - 2)); } else { var len = range.Start.Column - range.End.Column; arrow = new string(' ', Math.Min(cursor - len - 1, 0)) + new string('~', Math.Min(len - 1, cursor)) + "^"; } } if (UseColors) { Console.ForegroundColor = ConsoleColor.Green; Stream.WriteLine(arrow); Console.ResetColor(); } else { Stream.WriteLine(arrow); } } } }
/// <summary> /// Issue an error message /// </summary> public void Error(string message, SourceRange range = null) { Consume(SeverityEnum.Error, range, message, null); }