This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
SyntaxKit is a Swift package that provides a declarative DSL for generating Swift code using result builders. Built on SwiftSyntax, it allows programmatic creation of Swift code structures (structs, enums, classes, functions) in a type-safe manner.
# Build the package
swift build
# Run all tests
swift test
# Run specific test
swift test --filter TestName
# Run tests with coverage
swift test --enable-code-coverage# Run comprehensive linting (SwiftFormat, SwiftLint, Periphery)
./Scripts/lint.sh
# Format code only (skip other checks)
LINT_MODE=NONE ./Scripts/lint.sh# Generate DocC documentation
swift package generate-documentation- Result Builders: Declarative DSL using
@resultBuilderfor Swift code generation - Protocol-Oriented:
CodeBlockprotocol as foundation for all syntax elements - SwiftSyntax Integration: All components generate native SwiftSyntax AST nodes
CodeBlock- Core protocol for all syntax elementsPatternConvertible- For pattern matching constructsTypeRepresentable- For type system integration
Sources/SyntaxKit/
├── Core/ # Fundamental protocols and builders
├── Declarations/ # Type declarations (Class, Struct, Enum, etc.)
├── Expressions/ # Swift expressions and operators
├── Functions/ # Function definitions and method calls
├── Variables/ # Variable and property declarations
├── ControlFlow/ # Control flow constructs (Switch, If, For)
├── Collections/ # Array, dictionary helpers
├── Parameters/ # Function parameter handling
├── Patterns/ # Pattern matching constructs
├── Utilities/ # Helper functions and extensions
└── ErrorHandling/ # Error handling constructs
- Create source file in appropriate subdirectory
- Implement
CodeBlockprotocol - Add corresponding unit tests in
Tests/SyntaxKitTests/Unit/ - Run
./Scripts/lint.shto ensure code quality - Run
swift testto verify functionality
- SwiftSyntax (601.0.1+) - Apple's Swift syntax parser
- SwiftOperators - Operator handling
- SwiftParser - Swift code parsing
- SwiftDocC Plugin (1.4.0+) - Documentation generation
- SwiftFormat (602.0.0) - Code formatting
- SwiftLint (0.63.2) - Static analysis (90+ opt-in rules)
- Periphery (3.7.2) - Unused code detection
- SyntaxKit Library - Main DSL library
- skit Executable - Command-line tool for parsing Swift code to JSON
- Swift 6.0+ required
- Xcode 16.0+ for development
- Uses modern Swift Testing framework (
@Testsyntax) - Tests organized by component in
Tests/SyntaxKitTests/Unit/ - Integration tests in
Tests/SyntaxKitTests/Integration/ - Comprehensive CI/CD with GitHub Actions
Full Documentation: SwiftSyntax 601.0.1 Documentation
Local Reference: docs/SwiftSyntax-LLM.md - Complete SwiftSyntax API reference (590KB)
SwiftSyntax is Apple's source-accurate tree representation of Swift source code, enabling parsing, inspection, generation, and transformation of Swift code programmatically.
Syntax- Base protocol for all syntax nodesSyntaxProtocol- Protocol all syntax nodes conform toSyntaxCollection- Collection of syntax nodesSyntaxChildren- Collection of child syntax nodes
TokenSyntax- Single token representationTokenKind- Enumerates Swift language token typesTrivia- Whitespace, comments, and other non-semantic contentTriviaPiece- Individual trivia elementSourcePresence- Indicates if node was found in source
Declarations (DeclSyntax)
ClassDeclSyntax- Class declarationsStructDeclSyntax- Struct declarationsEnumDeclSyntax- Enum declarationsProtocolDeclSyntax- Protocol declarationsFunctionDeclSyntax- Function declarationsVariableDeclSyntax- Variable declarationsImportDeclSyntax- Import statementsExtensionDeclSyntax- Extension declarationsTypeAliasDeclSyntax- Type alias declarationsAssociatedTypeDeclSyntax- Associated type declarationsOperatorDeclSyntax- Operator declarationsPrecedenceGroupDeclSyntax- Precedence group declarationsMacroDeclSyntax- Macro declarationsMacroExpansionDeclSyntax- Macro expansion declarations
Expressions (ExprSyntax)
FunctionCallExprSyntax- Function callsMemberAccessExprSyntax- Member access (dot notation)SubscriptCallExprSyntax- Subscript callsBinaryOperatorExprSyntax- Binary operatorsPrefixOperatorExprSyntax- Prefix operatorsPostfixOperatorExprSyntax- Postfix operatorsTernaryExprSyntax- Ternary operator expressionsArrayExprSyntax- Array literalsDictionaryExprSyntax- Dictionary literalsStringLiteralExprSyntax- String literalsIntegerLiteralExprSyntax- Integer literalsFloatLiteralExprSyntax- Float literalsBooleanLiteralExprSyntax- Boolean literalsNilLiteralExprSyntax- Nil literalsClosureExprSyntax- Closure expressionsIfExprSyntax- If expressionsSwitchExprSyntax- Switch expressionsTryExprSyntax- Try expressionsAwaitExprSyntax- Await expressionsKeyPathExprSyntax- Key path expressionsRegexLiteralExprSyntax- Regex literals
Statements (StmtSyntax)
ExpressionStmtSyntax- Expression statementsIfStmtSyntax- If statementsGuardStmtSyntax- Guard statementsWhileStmtSyntax- While loopsForStmtSyntax- For loopsRepeatStmtSyntax- Repeat-while loopsDoStmtSyntax- Do statementsReturnStmtSyntax- Return statementsThrowStmtSyntax- Throw statementsBreakStmtSyntax- Break statementsContinueStmtSyntax- Continue statementsDeferStmtSyntax- Defer statementsDiscardStmtSyntax- Discard statementsYieldStmtSyntax- Yield statements
Types (TypeSyntax)
IdentifierTypeSyntax- Named typesArrayTypeSyntax- Array typesDictionaryTypeSyntax- Dictionary typesTupleTypeSyntax- Tuple typesFunctionTypeSyntax- Function typesAttributedTypeSyntax- Types with attributesOptionalTypeSyntax- Optional typesImplicitlyUnwrappedOptionalTypeSyntax- IUO typesCompositionTypeSyntax- Protocol composition typesPackExpansionTypeSyntax- Pack expansion typesPackElementTypeSyntax- Pack element typesMetatypeTypeSyntax- Metatype types
Patterns (PatternSyntax)
IdentifierPatternSyntax- Identifier patternsExpressionPatternSyntax- Expression patternsValueBindingPatternSyntax- Value binding patternsTuplePatternSyntax- Tuple patternsWildcardPatternSyntax- Wildcard patternsIsTypePatternSyntax- Type checking patterns
AttributeListSyntax- Attribute listsCodeBlockItemListSyntax- Code block itemsMemberBlockItemListSyntax- Member block itemsParameterListSyntax- Parameter listsGenericParameterListSyntax- Generic parameter listsGenericRequirementListSyntax- Generic requirement listsSwitchCaseListSyntax- Switch case listsCatchClauseListSyntax- Catch clause listsArrayElementListSyntax- Array element listsDictionaryElementListSyntax- Dictionary element lists
SyntaxVisitor- Base visitor class for traversing syntax treesSyntaxAnyVisitor- Generic visitor for any syntax nodeSyntaxRewriter- Transformer for modifying syntax treesSyntaxTreeViewMode- Controls how missing/unexpected nodes are handled
// Create a simple identifier
let identifier = TokenSyntax.identifier("myVariable")
// Create a string literal
let stringLiteral = StringLiteralExprSyntax(
openingQuote: .stringQuoteToken(),
segments: StringLiteralSegmentListSyntax([
StringSegmentSyntax(content: .stringSegment("Hello"))
]),
closingQuote: .stringQuoteToken()
)
// Create a function call
let functionCall = FunctionCallExprSyntax(
calledExpression: DeclReferenceExprSyntax(
baseName: .identifier("print")
),
leftParen: .leftParenToken(),
arguments: LabeledExprListSyntax([
LabeledExprSyntax(expression: stringLiteral)
]),
rightParen: .rightParenToken()
)class MyVisitor: SyntaxVisitor {
override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
print("Found function: \(node.name)")
return .visitChildren
}
override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
print("Found variable declaration")
return .visitChildren
}
}
// Usage
let visitor = MyVisitor()
visitor.walk(syntaxTree)class MyRewriter: SyntaxRewriter {
override func visit(_ node: StringLiteralExprSyntax) -> ExprSyntax {
// Transform string literals
let newContent = node.segments.map { segment in
// Modify content as needed
return segment
}
return node.with(\.segments, StringLiteralSegmentListSyntax(newContent))
}
}
// Usage
let rewriter = MyRewriter()
let modifiedTree = rewriter.visit(syntaxTree)When working with SyntaxKit, remember:
- All
CodeBlockimplementations should generate valid SwiftSyntax nodes - Use the appropriate SwiftSyntax types for each language construct
- Leverage SwiftSyntax's type safety for compile-time validation
- Consider using
SyntaxRewriterfor complex transformations - Use
SyntaxVisitorfor analysis and inspection tasks
Import Task Master's development workflow commands and guidelines, treat as if import is in the main CLAUDE.md file. @./.taskmaster/CLAUDE.md
- We suggest using the Swift OpenAPI Generator