Introducing SyntaxKit 🎯 Declarative Swift Code Generation
SwiftSyntax made simple: Declarative DSL transforms complex AST manipulation into readable, maintainable Swift code generation for macros.
Issue #115
Hello everyone!
Today I'm thrilled to share something that's been months in the making - a tool that transforms one of Swift development's most frustrating experiences into something elegant and intuitive. If you've ever wrestled with SwiftSyntax for macro development, you'll understand why this matters.

Introducing SyntaxKit: Declarative Swift Code Generation
After years of struggling with SwiftSyntax's verbose and procedural approach, I've created SyntaxKit - a Swift package that brings SwiftUI-like declarative syntax to Swift code generation. This DSL abstraction transforms SwiftSyntax's complex AST manipulation into something that feels natural and Swift-like, making macro development actually enjoyable.
The SwiftSyntax Problem:
- Verbose and Procedural: Simple structs require 80+ lines of complex AST manipulation
- Hard to Read: Generated code logic is buried in SwiftSyntax boilerplate
- Error-Prone: Manual AST construction leads to runtime failures
- Steep Learning Curve: Requires deep understanding of Abstract Syntax Trees
- Not Swift-like: Feels like a completely different language
What SyntaxKit Provides:
- Declarative Syntax: Write Swift code generation like you write SwiftUI
- Type Safety: Compile-time checking prevents common SwiftSyntax errors
- Result Builder Magic: Natural, composable API using modern Swift features
- Comprehensive Coverage: Structs, enums, protocols, variables, and more
- Perfect for Macros: Designed specifically for Swift macro development
Before and After Comparison:
// SwiftSyntax - verbose and complex (80+ lines)
let structKeyword = TokenSyntax.keyword(.struct, trailingTrivia: .space)
let identifier = TokenSyntax.identifier("User", trailingTrivia: .space)
let leftBrace = TokenSyntax.leftBrace(leadingTrivia: .space, trailingTrivia: .newline)
let members = MemberDeclListSyntax([
MemberDeclListSyntax.Element(
decl: VariableDeclSyntax(
bindingKeyword: .keyword(.let),
bindings: PatternBindingListSyntax([...])
)
)
])
// ... 70+ more lines of boilerplate
// SyntaxKit - clean and declarative
let userModel = Struct("User") {
Variable(.let, name: "id", type: "UUID")
Variable(.let, name: "name", type: "String")
Variable(.let, name: "email", type: "String")
}
.inherits("Equatable")
Real-World Impact: SyntaxKit transforms macro development from a painful exercise in AST manipulation into something that feels natural and Swift-like. What used to take hours of debugging SwiftSyntax structures now takes minutes of writing clear, declarative code.
The package includes comprehensive documentation, getting started guides, and real-world examples. Perfect for developers building Swift macros, code generators, or any tool that needs to create Swift code programmatically.
Check out the complete development story and GitHub repository to see how AI-assisted development made this possible.
How I Built SyntaxKit: AI-Assisted Development Story
While SyntaxKit itself is a traditional Swift DSL built on SwiftSyntax, the most fascinating aspect of its development was how AI tools like Cursor and Claude Code fundamentally changed my development process and made building this complex abstraction layer possible.
The Failed LLM Approach:
My first instinct was to create a custom LLM trained specifically for Swift macro generation using the Swift AST Explorer as a training data source. This approach failed spectacularly - training data scarcity, over-engineering, and Swift-specific complexity made it impractical.
Success with Interactive AI:
- Cursor for Implementation: Perfect for building individual SyntaxKit components with contextual code suggestions
- Claude Code for Architecture: Excellent for project-wide decisions and maintaining consistency across components
- Iterative Development: Breaking down complex problems into AI-digestible pieces
- Unit Test Generation: AI excels at creating comprehensive test coverage for all scenarios
Key Lessons Learned:
- Plan and Break Down: AI works best with specific, focused tasks rather than building entire systems
- Guide the Process: Think of AI as a coding partner that needs clear direction and feedback
- Avoid Over-Engineering: LLMs can add unnecessary complexity - review and simplify generated code
- Human Review is Essential: AI-generated code still needs architectural oversight and code review
The combination of thoughtful DSL design and AI-assisted implementation opened up exciting possibilities for developer tooling that I couldn't have achieved alone in a reasonable timeframe. SyntaxKit proves that AI tools can accelerate the creation of traditional software solutions when used strategically.
Ready to Transform Your Macro Development?
SyntaxKit is available now as a Swift package and can be integrated into your macro projects today. The package includes comprehensive documentation, step-by-step tutorials, and real-world examples.
Quick Start Example:
// Add to your Package.swift
dependencies: [
.package(url: "https://github.com/brightdigit/SyntaxKit.git", from: "0.0.3")
]
// Use in your macro
import SyntaxKit
let blackjackCard = Struct("BlackjackCard") {
Enum("Suit") {
EnumCase("spades").equals("♠")
EnumCase("hearts").equals("♡")
EnumCase("diamonds").equals("♢")
EnumCase("clubs").equals("♣")
}
.inherits("Character")
}
Resources:
- Repository: github.com/brightdigit/SyntaxKit
- Documentation: Complete API Reference
- Tutorial: Development Journey with AI
- Getting Started: 10-minute Quick Start Guide
Whether you're building your first Swift macro or looking to simplify existing SwiftSyntax code, SyntaxKit provides a more approachable path to programmatic Swift code generation. The declarative approach makes code generation logic clear, maintainable, and actually enjoyable to work with.
I'm excited to see how SyntaxKit transforms your macro development experience. If you give it a try, I'd love to hear about your projects and any feedback you have.
Happy coding!
Leo