compiler, cli: Support Nice Compilation Error Messages
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user