|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftSyntax |
| 14 | + |
| 15 | +// MARK: - Protocols |
| 16 | + |
| 17 | +public protocol DeclListBuildable: SyntaxListBuildable { |
| 18 | + func buildDeclList(format: Format, leadingTrivia: Trivia) -> [DeclSyntax] |
| 19 | +} |
| 20 | + |
| 21 | +public protocol DeclBuildable: SyntaxBuildable, DeclListBuildable { |
| 22 | + func buildDecl(format: Format, leadingTrivia: Trivia) -> DeclSyntax |
| 23 | +} |
| 24 | + |
| 25 | +extension DeclBuildable { |
| 26 | + public func buildSyntax(format: Format, leadingTrivia: Trivia) -> Syntax { |
| 27 | + buildDecl(format: format, leadingTrivia: leadingTrivia) |
| 28 | + } |
| 29 | + |
| 30 | + public func buildDeclList(format: Format, leadingTrivia: Trivia) -> [DeclSyntax] { |
| 31 | + [buildDecl(format: format, leadingTrivia: leadingTrivia)] |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// MARK: - Function Builder |
| 36 | + |
| 37 | +@_functionBuilder |
| 38 | +public struct DeclListBuilder { |
| 39 | + public static func buildBlock(_ builders: DeclListBuildable...) -> DeclListBuildable { |
| 40 | + DeclList(builders: builders) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// MARK: - List |
| 45 | + |
| 46 | +public struct DeclList: DeclListBuildable { |
| 47 | + let builders: [DeclListBuildable] |
| 48 | + |
| 49 | + public func buildDeclList(format: Format, leadingTrivia: Trivia) -> [DeclSyntax] { |
| 50 | + builders.flatMap { $0.buildDeclList(format: format, leadingTrivia: leadingTrivia) } |
| 51 | + } |
| 52 | + |
| 53 | + public func buildSyntaxList(format: Format, leadingTrivia: Trivia) -> [Syntax] { |
| 54 | + buildDeclList(format: format, leadingTrivia: leadingTrivia) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +extension DeclList { |
| 59 | + public static let empty: DeclList = DeclList(builders: []) |
| 60 | +} |
| 61 | + |
| 62 | +// MARK: - Buildables |
| 63 | + |
| 64 | +// MARK: Import |
| 65 | + |
| 66 | +public struct Import: DeclBuildable { |
| 67 | + let moduleName: String |
| 68 | + |
| 69 | + public init(_ moduleName: String) { |
| 70 | + self.moduleName = moduleName |
| 71 | + } |
| 72 | + |
| 73 | + public func buildDecl(format: Format, leadingTrivia: Trivia) -> DeclSyntax { |
| 74 | + let importToken = Tokens.import.withLeadingTrivia(leadingTrivia) |
| 75 | + let moduleNameToken = SyntaxFactory.makeIdentifier(moduleName) |
| 76 | + |
| 77 | + return ImportDeclSyntax { |
| 78 | + $0.useImportTok(importToken) |
| 79 | + $0.addPathComponent(AccessPathComponentSyntax { |
| 80 | + $0.useName(moduleNameToken) |
| 81 | + }) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// MARK: Variables |
| 87 | + |
| 88 | +public protocol VariableMutability { |
| 89 | + static var token: TokenSyntax { get } |
| 90 | +} |
| 91 | + |
| 92 | +public enum VariableLetMutability: VariableMutability { |
| 93 | + public static let token = Tokens.let |
| 94 | +} |
| 95 | + |
| 96 | +public enum VariableVarMutability: VariableMutability { |
| 97 | + public static let token = Tokens.var |
| 98 | +} |
| 99 | + |
| 100 | +public typealias Let = Variable<VariableLetMutability> |
| 101 | +public typealias Var = Variable<VariableVarMutability> |
| 102 | + |
| 103 | +public struct Variable<Mutability: VariableMutability>: DeclBuildable { |
| 104 | + let name: String |
| 105 | + let type: String |
| 106 | + let initializer: ExprBuildable? |
| 107 | + |
| 108 | + public init(_ name: String, of type: String, value: ExprBuildable? = nil) { |
| 109 | + self.name = name |
| 110 | + self.type = type |
| 111 | + self.initializer = value |
| 112 | + } |
| 113 | + |
| 114 | + public func buildDecl(format: Format, leadingTrivia: Trivia) -> DeclSyntax { |
| 115 | + let mutabilityKeyword = Mutability.token.withLeadingTrivia(leadingTrivia) |
| 116 | + |
| 117 | + let nameIdentifier = SyntaxFactory.makeIdentifier(name) |
| 118 | + let namePattern = SyntaxFactory.makeIdentifierPattern(identifier: nameIdentifier) |
| 119 | + |
| 120 | + let typeIdentifier = SyntaxFactory.makeTypeIdentifier(type) |
| 121 | + let typeAnnotation = SyntaxFactory.makeTypeAnnotation( |
| 122 | + colon: Tokens.colon, |
| 123 | + type: typeIdentifier |
| 124 | + ) |
| 125 | + |
| 126 | + let initClause = initializer.flatMap { builder -> InitializerClauseSyntax in |
| 127 | + let expr = builder.buildExpr(format: format, leadingTrivia: .zero) |
| 128 | + return SyntaxFactory.makeInitializerClause(equal: Tokens.equal, value: expr) |
| 129 | + } |
| 130 | + |
| 131 | + return VariableDeclSyntax { |
| 132 | + $0.useLetOrVarKeyword(mutabilityKeyword) |
| 133 | + $0.addBinding(PatternBindingSyntax { |
| 134 | + $0.usePattern(namePattern) |
| 135 | + $0.useTypeAnnotation(typeAnnotation) |
| 136 | + |
| 137 | + if let initClause = initClause { |
| 138 | + $0.useInitializer(initClause) |
| 139 | + } |
| 140 | + }) |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// MARK: Struct |
| 146 | + |
| 147 | +public struct Struct: DeclBuildable { |
| 148 | + let name: String |
| 149 | + let memberList: DeclListBuildable |
| 150 | + |
| 151 | + public init( |
| 152 | + _ name: String, |
| 153 | + @DeclListBuilder buildMemberList: () -> DeclListBuildable = { DeclList.empty } |
| 154 | + ) { |
| 155 | + self.name = name |
| 156 | + self.memberList = buildMemberList() |
| 157 | + } |
| 158 | + |
| 159 | + public func buildDecl(format: Format, leadingTrivia: Trivia) -> DeclSyntax { |
| 160 | + let structKeyword = Tokens.struct.withLeadingTrivia(leadingTrivia) |
| 161 | + |
| 162 | + let declList = memberList.buildDeclList( |
| 163 | + format: format._indented(), |
| 164 | + leadingTrivia: .zero |
| 165 | + ) |
| 166 | + |
| 167 | + return StructDeclSyntax { |
| 168 | + $0.useStructKeyword(structKeyword) |
| 169 | + $0.useIdentifier(SyntaxFactory.makeIdentifier(name)) |
| 170 | + $0.useMembers(MemberDeclBlockSyntax { |
| 171 | + $0.useLeftBrace(Tokens.leftBrace.withLeadingTrivia(.spaces(1))) |
| 172 | + $0.useRightBrace(Tokens.rightBrace.withLeadingTrivia(.newlines(1) + format._makeIndent())) |
| 173 | + |
| 174 | + let format = format._indented() |
| 175 | + for decl in declList { |
| 176 | + let member = SyntaxFactory |
| 177 | + .makeMemberDeclListItem(decl: decl, semicolon: nil) |
| 178 | + .withLeadingTrivia(.newlines(1) + format._makeIndent()) |
| 179 | + $0.addMember(member) |
| 180 | + } |
| 181 | + }) |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments