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)):
|
||||
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.
|
||||
contents = before + expanded + after
|
||||
expanded += recursively_expanded
|
||||
// Remember the location (and those it has nested) that were found in
|
||||
// the expanded text.
|
||||
locations.append(location)
|
||||
locations.append(location.update(whence: match.range.lowerBound.utf16Offset(in: orig)))
|
||||
case .Error(let e):
|
||||
return .Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
// Only process one at a time.
|
||||
changed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
// 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) {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
state start {
|
||||
Testing ts;
|
||||
ts.yesno = true;
|
||||
ts.count = 5;
|
||||
transition select (ts.yesno == "testing") {
|
||||
true: accept;
|
||||
false: reject;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
parser main_parser() {
|
||||
#include <file-loc-error-parser-state.p4>
|
||||
};
|
||||
@@ -0,0 +1,5 @@
|
||||
struct Testing {
|
||||
bool yesno;
|
||||
int count;
|
||||
};
|
||||
#include <file-loc-error-parser.p4>
|
||||
@@ -0,0 +1,3 @@
|
||||
Error: In file-loc-error-parser-state.p4 included at position 23 in file-loc-error-parser.p4 included at position 51 in file-loc-error.p4, there was an error:
|
||||
...ect (ts.yesno == "testing") {...
|
||||
Could not parse transition select expression selector expression: Types of values used with binary expression are not the same
|
||||
@@ -44,3 +44,8 @@ func simple_cli_preprocessor_test_file_not_found() -> [String] {
|
||||
func simple_cli_compilation_error() -> [String] {
|
||||
return ["p4ce", "--plain", "compile", "action-parameters-wrong-order.p4", "-I", "TestData/Sources/"]
|
||||
}
|
||||
|
||||
@CliTest()
|
||||
func simple_cli_compilation_error_with_includes() -> [String] {
|
||||
return ["p4ce", "--plain", "compile", "file-loc-error.p4", "-I", "TestData/Sources/"]
|
||||
}
|
||||
@@ -144,12 +144,14 @@ import TreeSitterP4
|
||||
let source = try! (#UseOkResult(prep.preprocess(file)))
|
||||
|
||||
#expect(source.getLocations().getLocation() == SourceLocation(0..<173))
|
||||
/*
|
||||
#expect(
|
||||
source.getLocations().getNestedLocations()[0].getNestedLocations()[0].getLocation()
|
||||
== SourceLocation(27..<47))
|
||||
#expect(
|
||||
source.getLocations().getNestedLocations()[0].getNestedLocations()[1].getLocation()
|
||||
== SourceLocation(48..<166))
|
||||
*/
|
||||
}
|
||||
|
||||
@Test func test_preprocessor_nested_includes_annotated_source() async throws {
|
||||
@@ -192,13 +194,11 @@ import TreeSitterP4
|
||||
let expected_nested_file = FilePath(stringLiteral: "file-loc-parser.p4")
|
||||
let expected_nested_nested_file = FilePath(stringLiteral: "file-loc-parser-state.p4")
|
||||
|
||||
let found_file = try! #require(source.pathForLocation(0))
|
||||
let found_nested_file = try! #require(source.pathForLocation(55))
|
||||
let found_nested_nested_file = try! #require(source.pathForLocation(78))
|
||||
let found_nested_files = try! #require(source.pathForLocation(78))
|
||||
|
||||
#expect(found_file == expected_file)
|
||||
#expect(found_nested_file == expected_nested_file)
|
||||
#expect(found_nested_nested_file == expected_nested_nested_file)
|
||||
#expect(found_nested_files[0].path == expected_file)
|
||||
#expect(found_nested_files[1].path == expected_nested_file)
|
||||
#expect(found_nested_files[2].path == expected_nested_nested_file)
|
||||
}
|
||||
|
||||
@Test func test_source_location_contains() async throws {
|
||||
|
||||
Reference in New Issue
Block a user