diff --git a/Sources/Common/SourceCode.swift b/Sources/Common/SourceCode.swift index ee46294..55435d6 100644 --- a/Sources/Common/SourceCode.swift +++ b/Sources/Common/SourceCode.swift @@ -85,7 +85,7 @@ public struct SourceManager { let fm = FileManager() for path in self.paths { - let combined = path.pushing(file) + let combined = path.pushing(file).lexicallyNormalized() if fm.fileExists(atPath: combined.string) { return combined } @@ -247,14 +247,24 @@ public struct SourceCode { } func do_preprocess( - _ file: URL, _ manager: SourceManager, _ starting: Int = 0 + _ file: FilePath, _ manager: SourceManager, _ starting: Int = 0 ) -> Result<(FileSourceLocation, String)> { // First (1) match group has the name of the include file. let re = /\#include[\s]*<([^\s>]*)>/ do { var locations: [FileSourceLocation] = Array() - var contents = try String.init(contentsOf: file, encoding: String.defaultCStringEncoding) + + // First, try to find the file. + guard let included_path = manager.firstExisting(file) else { + return .Error( + Error( + withMessage: + "Could not open \(file) for preprocessing")) + } + + var contents = try String.init( + contentsOf: URL(filePath: included_path.string), encoding: String.defaultCStringEncoding) var changed = true while changed { @@ -264,20 +274,9 @@ func do_preprocess( let before = contents[.. Result { - // First, decide whether the given file exists at the path the user gave. - let fm = FileManager() - let file_to_open = - if !fm.fileExists(atPath: file.string) { - self.manager.firstExisting(file) - } else { - file - } - - guard let file_to_open else { - return .Error(Error(withMessage: "Could not open \(file) for preprocessing")) - } - - let url = URL.init(filePath: file_to_open.string) - - switch do_preprocess(url, self.manager) { + switch do_preprocess(file, self.manager) { case .Ok((let location, let contents)): return .Ok(SourceCode(contents, self.manager, location)) case .Error(let e): return .Error(e) diff --git a/TestData/simple_cli_compilation_error.golden b/TestData/simple_cli_compilation_error.golden index c847013..8751785 100644 --- a/TestData/simple_cli_compilation_error.golden +++ b/TestData/simple_cli_compilation_error.golden @@ -1,3 +1,3 @@ -Error: In /Users/hawkinsw/code/p4ce/TestData/Sources/action-parameters-wrong-order.p4, there was an error: +Error: In action-parameters-wrong-order.p4, there was an error: ...ion a(inout bool aa, int ax, inout bool ay) {... All parameters with direction must precede directionless parameters diff --git a/Tests/p4rseTests/SupportTests/SourceCode.swift b/Tests/p4rseTests/SupportTests/SourceCode.swift index f8e66a8..95cf602 100644 --- a/Tests/p4rseTests/SupportTests/SourceCode.swift +++ b/Tests/p4rseTests/SupportTests/SourceCode.swift @@ -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 = """