@@ -64,7 +64,7 @@ import SwiftSyntax
6464 - `Token`: Token-specific metadata (kind, trivia)
6565 - `SourceRange`: Line/column location coordinates
6666 - `SyntaxType`: Semantic classification of syntax elements
67- - `SyntaxResponse`: Container for final JSON output
67+ - `SyntaxResponse`: Container for final JSON output (deprecated)
6868
6969 ## Processing Pipeline
7070
@@ -115,39 +115,71 @@ import SwiftSyntax
115115 }
116116 """
117117
118- let response = try SyntaxParser.parse(code: code)
119- // response.syntaxJSON contains the flattened tree structure
118+ let treeNodes = SyntaxParser.parse(code: code)
119+ // treeNodes contains the array of TreeNode objects directly
120120 ```
121121
122122 This module is primarily consumed by the `skit` command-line tool for
123123 converting Swift source code to JSON for external analysis and tooling.
124124 */
125125
126- /// Main entry point for parsing Swift source code into JSON representation.
126+ /// Main entry point for parsing Swift source code into TreeNode representation.
127127///
128- /// SyntaxParser converts Swift source code into a structured JSON format that represents
129- /// the Abstract Syntax Tree (AST). This is primarily used by the `skit` command-line tool
130- /// to provide Swift code analysis for external tools and applications .
128+ /// SyntaxParser converts Swift source code into an array of TreeNode objects that represents
129+ /// the Abstract Syntax Tree (AST). This provides direct access to the parsed syntax structure
130+ /// for programmatic analysis and manipulation .
131131///
132132/// The parser leverages Apple's SwiftSyntax framework to perform the actual parsing,
133- /// then transforms the complex SwiftSyntax AST into a simplified, serializable format
134- /// suitable for JSON output and console consumption .
133+ /// then transforms the complex SwiftSyntax AST into a simplified, flat structure
134+ /// suitable for direct consumption by Swift code .
135135package enum SyntaxParser {
136136 // MARK: - Configuration Constants
137137
138138 /// Option key to enable operator precedence folding during parsing.
139139 /// When enabled, expressions are reorganized according to Swift's operator precedence rules.
140+ @available ( * , deprecated, message: " Operator precedence folding is not supported in the new parse(code:) method " )
140141 private static let fold = " fold "
141142
142143 /// Option key to include missing/implicit tokens in the output.
143144 /// Useful for debugging or when you need to see all syntax elements including placeholders.
145+ @available ( * , deprecated, message: " Missing token display is not supported in the new parse(code:) method " )
144146 private static let showMissing = " showmissing "
145147
146148 /// Default filename used for source location tracking when no specific file is provided.
147149 private static let defaultFileName = " "
148150
149151 // MARK: - Public Interface
150152
153+ /// Parses Swift source code and returns an array of TreeNode objects.
154+ ///
155+ /// This method performs the complete parsing pipeline:
156+ /// 1. Parses Swift source code using SwiftSyntax
157+ /// 2. Traverses the AST to extract structure and token information
158+ /// 3. Returns the tree nodes directly without JSON serialization
159+ ///
160+ /// - Parameter code: Swift source code to parse
161+ /// - Returns: Array of TreeNode objects representing the syntax tree
162+ package static func parse( code: String ) -> [ TreeNode ] {
163+ // Parse the Swift source code into a SwiftSyntax AST
164+ let sourceFile = Parser . parse ( source: code)
165+
166+ // Use raw syntax tree without precedence folding for simplicity
167+ let syntax = Syntax ( sourceFile)
168+
169+ // Create visitor to traverse AST and extract structured information
170+ let visitor = TokenVisitor (
171+ locationConverter: SourceLocationConverter (
172+ fileName: defaultFileName, tree: sourceFile) ,
173+ showMissingTokens: false
174+ )
175+
176+ // Traverse the syntax tree and build our simplified representation
177+ _ = visitor. rewrite ( syntax)
178+
179+ // Return the tree nodes directly
180+ return visitor. tree
181+ }
182+
151183 /// Parses Swift source code and returns a JSON representation of its syntax tree.
152184 ///
153185 /// This method performs the complete parsing pipeline:
@@ -163,6 +195,7 @@ package enum SyntaxParser {
163195 /// - "showmissing": Include missing/implicit tokens in output
164196 /// - Returns: SyntaxResponse containing the JSON representation
165197 /// - Throws: JSONEncoder errors if serialization fails
198+ @available ( * , deprecated, message: " Use parse(code:) instead for direct TreeNode access " )
166199 package static func parse( code: String , options: [ String ] = [ ] ) throws -> SyntaxResponse {
167200 // Parse the Swift source code into a SwiftSyntax AST
168201 let sourceFile = Parser . parse ( source: code)
0 commit comments