Skip to content

Commit 9e6ac89

Browse files
committed
cleaning up old API
1 parent 0ddb368 commit 9e6ac89

File tree

10 files changed

+82
-177
lines changed

10 files changed

+82
-177
lines changed

Sources/SyntaxParser/SourceRange.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ import Foundation
3939
/// - Refactoring tools modifying specific code regions
4040
///
4141
/// Coordinates use 1-based indexing to match common editor conventions.
42-
internal struct SourceRange: Codable, Equatable {
42+
package struct SourceRange: Codable, Equatable {
4343
/// The line number where this syntax element begins (1-based).
44-
internal let startRow: Int
44+
package let startRow: Int
4545

4646
/// The column number where this syntax element begins (1-based).
47-
internal let startColumn: Int
47+
package let startColumn: Int
4848

4949
/// The line number where this syntax element ends (1-based).
50-
internal let endRow: Int
50+
package let endRow: Int
5151

5252
/// The column number where this syntax element ends (1-based).
53-
internal let endColumn: Int
53+
package let endColumn: Int
5454

5555
/// Creates a new SourceRange with the specified coordinates.
5656
///
@@ -59,7 +59,7 @@ internal struct SourceRange: Codable, Equatable {
5959
/// - startColumn: Starting column number (1-based)
6060
/// - endRow: Ending line number (1-based)
6161
/// - endColumn: Ending column number (1-based)
62-
internal init(startRow: Int, startColumn: Int, endRow: Int, endColumn: Int) {
62+
package init(startRow: Int, startColumn: Int, endRow: Int, endColumn: Int) {
6363
self.startRow = startRow
6464
self.startColumn = startColumn
6565
self.endRow = endRow

Sources/SyntaxParser/String.swift

Lines changed: 0 additions & 122 deletions
This file was deleted.

Sources/SyntaxParser/StructureProperty.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,28 @@ import Foundation
4040
/// - Function declaration "name" property → StructureValue with identifier text
4141
/// - Function declaration "body" property → Reference to "CodeBlockSyntax"
4242
/// - Missing optional property → nil value with just the property name
43-
internal struct StructureProperty: Codable, Equatable {
43+
package struct StructureProperty: Codable, Equatable {
4444
/// The name of this structural property.
4545
/// Corresponds to SwiftSyntax property names like "name", "parameters", "body", etc.
46-
internal let name: String
46+
package let name: String
4747

4848
/// The value of this property, if it contains terminal data.
4949
/// Present for tokens, literals, and other concrete values.
5050
/// Nil for missing optional properties.
51-
internal let value: StructureValue?
51+
package let value: StructureValue?
5252

5353
/// Reference to another syntax node type, if this property contains a nested structure.
5454
/// Used when this property points to another syntax node rather than containing terminal data.
5555
/// Example: "body" property might reference "CodeBlockSyntax"
56-
internal let ref: String?
56+
package let ref: String?
5757

5858
/// Creates a new StructureProperty with the specified components.
5959
///
6060
/// - Parameters:
6161
/// - name: The property name
6262
/// - value: Terminal value data, if any
6363
/// - ref: Reference to another syntax type, if any
64-
internal init(name: String, value: StructureValue? = nil, ref: String? = nil) {
64+
package init(name: String, value: StructureValue? = nil, ref: String? = nil) {
6565
self.name = name
6666
self.value = value
6767
self.ref = ref

Sources/SyntaxParser/StructureValue.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ import Foundation
4040
/// - Token value: text="let", kind="keyword(SwiftSyntax.Keyword.let)"
4141
/// - Type reference: text="VariableDeclSyntax", kind=nil
4242
/// - Literal value: text="42", kind="integerLiteral(42)"
43-
internal struct StructureValue: Codable, Equatable {
43+
package struct StructureValue: Codable, Equatable {
4444
/// The string representation of this value.
4545
/// Contains the actual text content or type name.
46-
internal let text: String
46+
package let text: String
4747

4848
/// Optional kind information that provides additional context about the value type.
4949
/// Present for tokens to indicate their specific token kind (e.g., "keyword", "identifier").
5050
/// Nil for simple text values and type references.
51-
internal let kind: String?
51+
package let kind: String?
5252

5353
/// Creates a new StructureValue with the specified content.
5454
///
5555
/// - Parameters:
5656
/// - text: The string representation of the value
5757
/// - kind: Optional kind information for additional context
58-
internal init(text: String, kind: String? = nil) {
58+
package init(text: String, kind: String? = nil) {
5959
self.text = text
6060
self.kind = kind
6161
}

Sources/SyntaxParser/SyntaxParser.swift

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
135135
package 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)

Sources/SyntaxParser/SyntaxResponse.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ import Foundation
3939
/// The JSON contains a flat array of TreeNode objects representing the
4040
/// complete Swift syntax tree in a format suitable for external analysis
4141
/// tools, IDEs, and other applications that need to understand Swift code structure.
42+
@available(*, deprecated, message: "Use parse(code:) which returns [TreeNode] directly instead of JSON")
4243
package struct SyntaxResponse: Codable {
4344
/// JSON string representation of the parsed syntax tree.
4445
/// Contains a serialized array of TreeNode objects with their relationships,
4546
/// source locations, and structural information.
47+
@available(*, deprecated, message: "Use parse(code:) which returns [TreeNode] directly instead of JSON")
4648
package let syntaxJSON: String
4749

4850
/// Creates a new SyntaxResponse with the provided JSON data.
4951
///
5052
/// - Parameter syntaxJSON: The JSON string representation of the syntax tree
53+
@available(*, deprecated, message: "Use parse(code:) which returns [TreeNode] directly instead of JSON")
5154
package init(syntaxJSON: String) {
5255
self.syntaxJSON = syntaxJSON
5356
}

Sources/SyntaxParser/SyntaxType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import Foundation
3939
///
4040
/// The classification is based on the SwiftSyntax type hierarchy and groups
4141
/// related syntax elements into meaningful categories.
42-
internal enum SyntaxType: String, Codable, Equatable {
42+
package enum SyntaxType: String, Codable, Equatable {
4343
/// Declaration syntax elements.
4444
/// Includes: struct, class, enum, func, var, let, import, protocol, extension, etc.
4545
case decl

Sources/SyntaxParser/Token.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,26 @@ import Foundation
4040
/// newlines, comments, and other formatting elements that don't directly
4141
/// participate in the Swift language grammar but are important for
4242
/// code reconstruction and analysis.
43-
internal struct Token: Codable, Equatable {
43+
package struct Token: Codable, Equatable {
4444
/// The classification of this token (e.g., "keyword", "identifier", "integerLiteral").
4545
/// Corresponds to SwiftSyntax TokenKind descriptions.
46-
internal let kind: String
46+
package let kind: String
4747

4848
/// Trivia that appears before this token.
4949
/// Includes whitespace, comments, and other non-semantic text preceding the token.
50-
internal var leadingTrivia: String
50+
package var leadingTrivia: String
5151

5252
/// Trivia that appears after this token.
5353
/// Includes whitespace, comments, and other non-semantic text following the token.
54-
internal var trailingTrivia: String
54+
package var trailingTrivia: String
5555

5656
/// Creates a new Token with the specified properties.
5757
///
5858
/// - Parameters:
5959
/// - kind: The token classification
6060
/// - leadingTrivia: Text appearing before the token
6161
/// - trailingTrivia: Text appearing after the token
62-
internal init(kind: String, leadingTrivia: String, trailingTrivia: String) {
62+
package init(kind: String, leadingTrivia: String, trailingTrivia: String) {
6363
self.kind = kind
6464
self.leadingTrivia = leadingTrivia
6565
self.trailingTrivia = trailingTrivia

0 commit comments

Comments
 (0)