Swift Contract Extraction
Overview
The Swift extractor scans a package directory for .swift files and uses tree-sitter-swift to parse them. It identifies classes, structs, enums, protocols, actors, and free functions, extracting only public and open declarations.
API
from goga.contract.swift import swift_contract
contracts = swift_contract("path/to/package")
# Returns list[EntityContract | RoutineContract]
swift_contract accepts a path to a Swift package directory. Returns an empty list if no .swift files are found.
What Gets Extracted
| Source Construct | Contract Type | Notes |
|---|---|---|
public class X { ... } |
EntityContract |
Init params form signature |
public struct X { ... } |
EntityContract |
Same handling as class |
public actor X { ... } |
EntityContract |
Same handling as class |
public enum X { ... } |
EntityContract |
Enum cases become properties |
public protocol X { ... } |
EntityContract |
Protocol method requirements collected |
public func x() -> T |
RoutineContract |
Top-level public functions |
Visibility Rules
- Only
publicandopendeclarations are included internal(default) andprivatedeclarations are excluded- Extensions are skipped entirely
- Non-public initializers are ignored (entity signature falls back to
())
Key Node Types
The parser walks these tree-sitter node types:
class_declaration-- class, struct, actor, and enum declarations (distinguished bydeclaration_kind)protocol_declaration-- protocol declarationsfunction_declaration-- free functions and type member functionsproperty_declaration-- stored and computed propertiesinit_declaration-- designated and failable initializersenum_entry-- enum cases
Example
Given a Swift file Server.swift:
public class Server {
public init(host: String, port: Int) {}
public func start() -> Bool { return true }
public var name: String = ""
}
public func greet(name: String) -> String {
return name
}
The extracted contracts are:
| Name | Type | Signature |
|---|---|---|
Server |
EntityContract | (host: String, port: Int) |
greet |
RoutineContract | (name: String) -> String |
Server has one property (name: String) and one method (start() -> Bool).
External Parameter Names
Swift's external parameter names are preserved in signatures:
public func configure(with host: String, port: Int) {}
Extracted signature: (with host: String, port: Int)