Skip to content

A Swift code generator based on SwiftSyntax, leveraging Function Builders.

Notifications You must be signed in to change notification settings

RoBo-Inc/SyntaxBuilder

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SyntaxBuilder

A toy Swift code generator based on SwiftSyntax, leveraging Function Builders.

Requirements

Xcode 11 beta-bundled Swift 5.1

Example

import SyntaxBuilder

struct UserSourceFile: SourceFile {
    let idType: Type

    @SyntaxListBuilder
    var body: Body {
        Import("Foundation")

        Struct("User") {
            Typealias("ID", of: idType)

            Let("id", of: "ID")
                .prependingComment("The user's ID.", .docLine)

            Let("name", of: "String")
                .prependingComment("The user's name.", .docLine)

            Var("age", of: "Int")
                .prependingComment("The user's age.", .docLine)

            ForEach(0 ..< 3) { i in
                Let("value\(i)", of: "String")
                    .prependingNewline()
            }
        }
        .prependingComment("""
            User is an user.
            <https://github.com/akkyie/SyntaxBuilder/>
        """, .docBlock)
    }
}

let user = UserSourceFile(idType: "String")

var str: String = ""
user.write(to: &str)

print(str)

Output

import Foundation

/**
    User is an user.
    <https://github.com/akkyie/SyntaxBuilder/>
 */
struct User {
    typealias ID = String
    
    /// The user's ID.
    let id: ID
    
    /// The user's name.
    let name: String
    
    /// The user's age.
    var age: Int
    
    let value0: String
    
    let value1: String
    
    let value2: String
}

About

A Swift code generator based on SwiftSyntax, leveraging Function Builders.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 100.0%