From 956d6212431880b1af23b704b634e945668caacd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Leuth=C3=A4user?= <1417198+max-leuthaeuser@users.noreply.github.com> Date: Fri, 10 Apr 2026 14:34:02 +0200 Subject: [PATCH] Fix parse failures for files using custom operators Use a non-throwing error handler for operator folding so that unknown operators degrade gracefully (unfolded SequenceExprSyntax) instead of failing the entire file. Fixes #21 --- Sources/SwiftAstGenLib/SyntaxParser.swift | 2 +- Tests/SwiftAstGenTests/SwiftAstGenTests.swift | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftAstGenLib/SyntaxParser.swift b/Sources/SwiftAstGenLib/SyntaxParser.swift index 738fadf..a2ca135 100644 --- a/Sources/SwiftAstGenLib/SyntaxParser.swift +++ b/Sources/SwiftAstGenLib/SyntaxParser.swift @@ -73,7 +73,7 @@ struct SyntaxParser { let loc = countLines(in: code) let opPrecedence = OperatorTable.standardOperators let ast = Parser.parse(source: code) - let folded = try opPrecedence.foldAll(ast) + let folded = opPrecedence.foldAll(ast) { _ in } let locationConverter = SourceLocationConverter(fileName: fileUrl.path, tree: folded) let treeNode = folded.toJson(converter: locationConverter) diff --git a/Tests/SwiftAstGenTests/SwiftAstGenTests.swift b/Tests/SwiftAstGenTests/SwiftAstGenTests.swift index f527b56..f168bc0 100644 --- a/Tests/SwiftAstGenTests/SwiftAstGenTests.swift +++ b/Tests/SwiftAstGenTests/SwiftAstGenTests.swift @@ -11,6 +11,7 @@ final class SwiftAstGenTests: XCTestCase, TestUtils { ("testIgnoresTestTargetPathsFromPackageSwift", testIgnoresTestTargetPathsFromPackageSwift), ("testIgnoresMultipleTestTargetPaths", testIgnoresMultipleTestTargetPaths), ("testIgnoresCustomTestTargetPath", testIgnoresCustomTestTargetPath), + ("testCustomOperatorDoesNotFailParsing", testCustomOperatorDoesNotFailParsing), ] func testJsonSourceFileSyntax() throws { @@ -191,6 +192,30 @@ final class SwiftAstGenTests: XCTestCase, TestUtils { ) } + func testCustomOperatorDoesNotFailParsing() throws { + try withCode( + code: """ + infix operator <<<: AdditionPrecedence + func <<< (lhs: Int, rhs: Int) -> Int { lhs + rhs } + let result = 1 <<< 2 + """ + ) { srcDir, outputDir, jsonFile in + + try SwiftAstGenerator( + srcDir: srcDir, + outputDir: outputDir, + prettyPrint: false + ).generate() + + XCTAssertTrue(FileManager.default.fileExists(atPath: jsonFile.path)) + if let treeNode = loadJson(file: jsonFile) { + XCTAssertEqual(treeNode.nodeType, "SourceFileSyntax") + } else { + XCTFail("Could not create the JSON containing the Swift AST.") + } + } + } + func testIgnoresCustomTestTargetPath() throws { let tempDir = createTemporaryDirectory() defer { cleanup(directory: tempDir) }