compiler: Track/Report Relative Paths Names
Continuous Integration / Grammar Tests (push) Successful in 37s
Continuous Integration / Library Format Tests (push) Successful in 1m22s
Continuous Integration / Library Tests (push) Successful in 4m23s

When the user gives relative path names for p4 files, report
those in error messages (etc.). The SourceManager can/does
resolve those to absolute path names.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-22 04:27:37 -04:00
parent 0cb50e2e2f
commit 041009a22e
3 changed files with 33 additions and 59 deletions
+16 -26
View File
@@ -31,57 +31,49 @@ import TreeSitterP4
@Test func test_preprocessor() async throws {
let sm = SourceManager(["./TestData/Sources/"])
let prep = SourceCodePreprocessor(sm)
let file = FilePath.init(stringLiteral: "./TestData/Sources/simple.p4")
let expected_file = FilePath.init(FileManager().currentDirectoryPath + "/" + file.string)
.lexicallyNormalized()
let file = FilePath.init(stringLiteral: "simple.p4")
let source = try! (#UseOkResult(prep.preprocess(file)))
let program = try! #UseOkResult(Program.Compile(source.getSource()))
#expect(#RequireOkResult((program.find_parser(withName: Identifier(name: "main_parser")))))
#expect(source.getLocations().getPath() == expected_file)
#expect(source.getLocations().getPath() == file)
}
@Test func test_preprocessor_search_for_file() async throws {
let sm = SourceManager(["./TestData/Sources/"])
let prep = SourceCodePreprocessor(sm)
let file = FilePath.init(stringLiteral: "simple.p4")
let expected_file = FilePath.init(
FileManager().currentDirectoryPath + "/TestData/Sources/simple.p4")
let source = try! (#UseOkResult(prep.preprocess(file)))
let program = try! #UseOkResult(Program.Compile(source.getSource()))
#expect(#RequireOkResult((program.find_parser(withName: Identifier(name: "main_parser")))))
#expect(source.getLocations().getPath() == expected_file)
#expect(source.getLocations().getPath() == file)
}
@Test func test_preprocessor_nested_includes() async throws {
let sm = SourceManager(["./TestData/Sources/"])
let prep = SourceCodePreprocessor(sm)
let file = FilePath.init(stringLiteral: "./TestData/Sources/simple-split.p4")
let expected_file = FilePath.init(FileManager().currentDirectoryPath + "/" + file.string)
.lexicallyNormalized()
let file = FilePath.init(stringLiteral: "simple-split.p4")
#expect(#RequireOkResult(prep.preprocess(file)))
let source = try! (#UseOkResult(prep.preprocess(file)))
let program = try! #UseOkResult(Program.Compile(source.getSource()))
#expect(#RequireOkResult((program.find_parser(withName: Identifier(name: "main_parser")))))
#expect(source.getLocations().getPath() == expected_file)
#expect(source.getLocations().getPath() == file)
}
@Test func test_preprocessor_oneline_includes() async throws {
let sm = SourceManager(["./TestData/Sources/"])
let prep = SourceCodePreprocessor(sm)
let file = FilePath.init(stringLiteral: "./TestData/Sources/simple-split-oneline.p4")
let expected_file = FilePath.init(FileManager().currentDirectoryPath + "/" + file.string)
.lexicallyNormalized()
let file = FilePath.init(stringLiteral: "simple-split-oneline.p4")
#expect(#RequireOkResult(prep.preprocess(file)))
let source = try! (#UseOkResult(prep.preprocess(file)))
let program = try! #UseOkResult(Program.Compile(source.getSource()))
#expect(#RequireOkResult((program.find_parser(withName: Identifier(name: "main_parser")))))
#expect(source.getLocations().getPath() == expected_file)
#expect(source.getLocations().getPath() == file)
}
@Test func test_preprocessor_missing_file() async throws {
@@ -99,11 +91,11 @@ import TreeSitterP4
@Test func test_preprocessor_missing_included_file() async throws {
let sm = SourceManager(["./TestData/Sources/"])
let prep = SourceCodePreprocessor(sm)
let file = FilePath.init(stringLiteral: "./TestData/Sources/simple-unfound.p4")
let file = FilePath.init(stringLiteral: "simple-unfound.p4")
#expect(
#RequireErrorResult(
Error(withMessage: "Could not open unfound.p4 for inclusion"), (prep.preprocess(file))))
Error(withMessage: "Could not open unfound.p4 for preprocessing"), (prep.preprocess(file))))
}
@@ -131,7 +123,7 @@ import TreeSitterP4
@Test func test_preprocessor_oneline_includes_locations() async throws {
let sm = SourceManager(["./TestData/Sources/"])
let prep = SourceCodePreprocessor(sm)
let file = FilePath.init(stringLiteral: "./TestData/Sources/simple-split-oneline.p4")
let file = FilePath.init(stringLiteral: "simple-split-oneline.p4")
#expect(#RequireOkResult(prep.preprocess(file)))
@@ -145,7 +137,7 @@ import TreeSitterP4
@Test func test_preprocessor_nested_includes_locations() async throws {
let sm = SourceManager(["./TestData/Sources/"])
let prep = SourceCodePreprocessor(sm)
let file = FilePath.init(stringLiteral: "./TestData/Sources/nested-split.p4")
let file = FilePath.init(stringLiteral: "nested-split.p4")
#expect(#RequireOkResult(prep.preprocess(file)))
@@ -163,7 +155,7 @@ import TreeSitterP4
@Test func test_preprocessor_nested_includes_annotated_source() async throws {
let sm = SourceManager(["./TestData/Sources/"])
let prep = SourceCodePreprocessor(sm)
let file = FilePath.init(stringLiteral: "./TestData/Sources/annotate.p4")
let file = FilePath.init(stringLiteral: "annotate.p4")
let expected = """
<struct Testing {
@@ -192,15 +184,13 @@ import TreeSitterP4
@Test func test_preprocessor_nested_includes_get_file_location() async throws {
let sm = SourceManager(["./TestData/Sources/"], FileManager()) // Add a FileManager to get absolute paths.
let prep = SourceCodePreprocessor(sm)
let file = FilePath.init(stringLiteral: "./TestData/Sources/file-loc.p4")
let file = FilePath.init(stringLiteral: "file-loc.p4")
let source = try! (#UseOkResult(prep.preprocess(file)))
let expected_file = FilePath.init(FileManager().currentDirectoryPath + "/" + file.string)
.lexicallyNormalized()
let expected_nested_file = sm.firstExisting("file-loc-parser.p4")!.lexicallyNormalized()
let expected_nested_nested_file = sm.firstExisting("file-loc-parser-state.p4")!
.lexicallyNormalized()
let expected_file = file
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))