compiler: Reworked Preprocessor To Support Better Error Messages
Can now support showing the trail of includes in an error message. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -55,8 +55,16 @@ public struct ErrorWithLocation: Errorable, Equatable, CustomStringConvertible {
|
||||
|
||||
let prior_snipped = prior.trimmingPrefix(["\n"])
|
||||
let after_snipped = after.prefix { $0 != "\n" }
|
||||
let include_list = fp.reversed().map {
|
||||
let at = if let whence = $0.whence {
|
||||
" included at position \(whence) in"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
return $0.path.string + at
|
||||
}.joined(separator: " ")
|
||||
return formatter.formatWithStyle("Error: ", Style(StyleColor.Red))
|
||||
+ "In \(fp), there was an error: \n..." + prior_snipped
|
||||
+ "In \(include_list), there was an error: \n..." + prior_snipped
|
||||
+ formatter.formatWithStyle(source, Style(.none, [StyleFormat.Underline])) + after_snipped
|
||||
+ "...\n"
|
||||
+ self._msg
|
||||
|
||||
@@ -101,14 +101,21 @@ public struct SourceManager {
|
||||
public struct FileSourceLocation: Equatable, CustomStringConvertible {
|
||||
let location: SourceLocation
|
||||
let path: FilePath
|
||||
let whence: Int?
|
||||
let nested: [FileSourceLocation]
|
||||
|
||||
public init(
|
||||
_ location: SourceLocation, _ path: FilePath, _ nested: [FileSourceLocation] = Array()
|
||||
_ location: SourceLocation, _ path: FilePath, _ whence: Int? = .none,
|
||||
_ nested: [FileSourceLocation] = Array()
|
||||
) {
|
||||
self.location = location
|
||||
self.path = path
|
||||
self.nested = nested
|
||||
self.whence = whence
|
||||
}
|
||||
|
||||
public func update(whence: Int) -> FileSourceLocation {
|
||||
return FileSourceLocation(self.location, self.path, whence, self.nested)
|
||||
}
|
||||
|
||||
public func getLocation() -> SourceLocation {
|
||||
@@ -130,7 +137,7 @@ public struct FileSourceLocation: Equatable, CustomStringConvertible {
|
||||
}).joined(separator: ",") + ")"
|
||||
}
|
||||
|
||||
public func pathForLocation(_ location: Int) -> FilePath? {
|
||||
public func pathForLocation(_ location: Int) -> [FileSourceLocation]? {
|
||||
|
||||
let queried_location = SourceLocation(location, 1)
|
||||
if !self.location.contains(queried_location) {
|
||||
@@ -139,11 +146,11 @@ public struct FileSourceLocation: Equatable, CustomStringConvertible {
|
||||
|
||||
for nested in self.nested {
|
||||
if nested.location.contains(queried_location) {
|
||||
return nested.pathForLocation(location)
|
||||
return [self] + nested.pathForLocation(location)!
|
||||
}
|
||||
}
|
||||
|
||||
return self.getPath()
|
||||
return [self]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,8 +219,8 @@ public struct SourceCode {
|
||||
|
||||
public func getSourceSnippet(
|
||||
location: SourceLocation, context: Int = 0
|
||||
) -> (FilePath, String, String, String)? {
|
||||
guard let path = self.pathForLocation(location.range.lowerBound) else {
|
||||
) -> ([FileSourceLocation], String, String, String)? {
|
||||
guard let paths = self.pathForLocation(location.range.lowerBound) else {
|
||||
return .none
|
||||
}
|
||||
let lower = String.UTF8View.Index(utf16Offset: location.range.lowerBound, in: self.code)
|
||||
@@ -234,14 +241,14 @@ public struct SourceCode {
|
||||
let result = String(self.code.utf16[lower..<upper])!
|
||||
let prior = String(self.code.utf16[prior_start..<lower])!
|
||||
let after = String(self.code.utf16[upper...after_end])!
|
||||
return (path, result, prior, after)
|
||||
return (paths, result, prior, after)
|
||||
}
|
||||
|
||||
public func getLocations() -> FileSourceLocation {
|
||||
return self.locations
|
||||
}
|
||||
|
||||
public func pathForLocation(_ location: Int) -> FilePath? {
|
||||
public func pathForLocation(_ location: Int) -> [FileSourceLocation]? {
|
||||
return self.locations.pathForLocation(location)
|
||||
}
|
||||
}
|
||||
@@ -263,42 +270,40 @@ func do_preprocess(
|
||||
"Could not open \(file) for preprocessing"))
|
||||
}
|
||||
|
||||
var contents = try String.init(
|
||||
let orig = try String.init(
|
||||
contentsOf: URL(filePath: included_path.string), encoding: String.defaultCStringEncoding)
|
||||
var changed = true
|
||||
var expanded = ""
|
||||
var oloc = orig.startIndex
|
||||
|
||||
while changed {
|
||||
changed = false
|
||||
for match in contents.matches(of: re) {
|
||||
for match in orig.matches(of: re) {
|
||||
|
||||
let before = contents[..<match.range.lowerBound]
|
||||
let after = contents[match.range.upperBound...]
|
||||
if oloc != match.range.lowerBound {
|
||||
expanded += String(orig[oloc..<match.range.lowerBound])
|
||||
}
|
||||
oloc = match.range.upperBound
|
||||
|
||||
// By calling ourselves recursively, the include being processed will
|
||||
// be _completely_ expanded (including any nested includes).
|
||||
switch do_preprocess(FilePath("\(match.1)"), manager, starting + before.count) {
|
||||
case .Ok((let location, let expanded)):
|
||||
// Recombine what was before and after the include being processed
|
||||
// with the expanded text.
|
||||
contents = before + expanded + after
|
||||
// Remember the location (and those it has nested) that were found in
|
||||
// the expanded text.
|
||||
locations.append(location)
|
||||
case .Error(let e):
|
||||
return .Error(e)
|
||||
}
|
||||
|
||||
// Only process one at a time.
|
||||
changed = true
|
||||
break
|
||||
// By calling ourselves recursively, the include being processed will
|
||||
// be _completely_ expanded (including any nested includes).
|
||||
switch do_preprocess(FilePath("\(match.1)"), manager, starting + expanded.count) {
|
||||
case .Ok((let location, let recursively_expanded)):
|
||||
// Recombine what was before and after the include being processed
|
||||
// with the expanded text.
|
||||
expanded += recursively_expanded
|
||||
// Remember the location (and those it has nested) that were found in
|
||||
// the expanded text.
|
||||
locations.append(location.update(whence: match.range.lowerBound.utf16Offset(in: orig)))
|
||||
case .Error(let e):
|
||||
return .Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure that we got it all!
|
||||
expanded += orig[oloc...]
|
||||
|
||||
return .Ok(
|
||||
(
|
||||
FileSourceLocation(
|
||||
SourceLocation(starting..<(starting + contents.count)), file, locations),
|
||||
contents
|
||||
SourceLocation(starting..<(starting + expanded.count)), file, .none, locations), expanded
|
||||
))
|
||||
|
||||
} catch (let e) {
|
||||
|
||||
Reference in New Issue
Block a user