compiler, cli: Support Nice Compilation Error Messages

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-22 02:37:33 -04:00
parent 5845cb75cc
commit e53c32f802
9 changed files with 115 additions and 4 deletions
+22
View File
@@ -39,7 +39,29 @@ public struct Error: Errorable, Equatable, CustomStringConvertible {
}
}
extension String {
public func trimmingPostfix(_ in_: String.Element) -> String {
return String(self.reversed().drop { $0 == in_ }.reversed())
}
}
public struct ErrorWithLocation: Errorable, Equatable, CustomStringConvertible {
public func format(_ formatter: any Formattable, _ sc: SourceCode) -> String {
guard let (fp, source, prior, after) = sc.getSourceSnippet(location: self.location, context: 5)
else {
return self.format(formatter)
}
let prior_snipped = prior.trimmingPrefix(["\n"])
let after_snipped = after.prefix { $0 != "\n" }
return formatter.formatWithStyle("Error: ", Style(StyleColor.Red))
+ "In \(fp), there was an error: \n..." + prior_snipped
+ formatter.formatWithStyle(source, Style(.none, [StyleFormat.Underline])) + after_snipped
+ "...\n"
+ self._msg
}
public func format(_ formatter: any Formattable) -> String {
let bold_red = Style(StyleColor.Red, [StyleFormat.Bold])
let formatted_location = formatter.formatWithStyle(self.location.description, bold_red)