|
| 1 | +%{ |
| 2 | + from gyb_syntax_support import * |
| 3 | + from gyb_syntax_support.kinds import lowercase_first_word |
| 4 | + from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS |
| 5 | + # -*- mode: Swift -*- |
| 6 | + # Ignore the following admonition it applies to the resulting .swift file only |
| 7 | + |
| 8 | + def syntax_buildable_child_type(type_name, syntax_kind, is_token, is_optional=False): |
| 9 | + if syntax_kind in SYNTAX_BASE_KINDS: |
| 10 | + buildable_type = syntax_kind + 'Buildable' |
| 11 | + elif not is_token: |
| 12 | + buildable_type = syntax_kind |
| 13 | + else: |
| 14 | + buildable_type = type_name |
| 15 | + |
| 16 | + if is_optional: |
| 17 | + buildable_type += '?' |
| 18 | + |
| 19 | + return buildable_type |
| 20 | +}% |
| 21 | +//// Automatically Generated From DeclBuildables.swift.gyb. |
| 22 | +//===----------------------------------------------------------------------===// |
| 23 | +// |
| 24 | +// This source file is part of the Swift.org open source project |
| 25 | +// |
| 26 | +// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
| 27 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 28 | +// |
| 29 | +// See https://swift.org/LICENSE.txt for license information |
| 30 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 31 | +// |
| 32 | +//===----------------------------------------------------------------------===// |
| 33 | + |
| 34 | +import SwiftSyntax |
| 35 | + |
| 36 | +// MARK: Protocols |
| 37 | + |
| 38 | +% for kind in SYNTAX_BASE_KINDS: |
| 39 | +% if kind != 'SyntaxCollection': |
| 40 | +% build_kind = 'Syntax' if kind == 'Syntax' else kind + 'Syntax' |
| 41 | +% if kind == 'Syntax': |
| 42 | +public protocol ${kind}ListBuildable { |
| 43 | +% else: |
| 44 | +public protocol ${kind}ListBuildable: SyntaxListBuildable { |
| 45 | +% end |
| 46 | + func build${kind}List(format: Format, leadingTrivia: Trivia?) -> [${build_kind}] |
| 47 | +} |
| 48 | + |
| 49 | +% if kind == 'Syntax': |
| 50 | +public protocol ${kind}Buildable: ${kind}ListBuildable { |
| 51 | +% else: |
| 52 | +public protocol ${kind}Buildable: SyntaxBuildable, ${kind}ListBuildable { |
| 53 | +% end |
| 54 | + func build${kind}(format: Format, leadingTrivia: Trivia?) -> ${build_kind} |
| 55 | +} |
| 56 | + |
| 57 | +extension ${kind}Buildable { |
| 58 | +% if kind != 'Syntax': |
| 59 | + func build${kind}(format: Format) -> ${build_kind} { |
| 60 | + build${kind}(format: format, leadingTrivia: nil) |
| 61 | + } |
| 62 | +% end |
| 63 | + public func buildSyntax(format: Format) -> Syntax { |
| 64 | + buildSyntax(format: format, leadingTrivia: nil) |
| 65 | + } |
| 66 | + |
| 67 | + public func buildSyntax(format: Format, leadingTrivia: Trivia?) -> Syntax { |
| 68 | + Syntax(build${kind}(format: format, leadingTrivia: leadingTrivia)) |
| 69 | + } |
| 70 | + |
| 71 | + public func build${kind}List(format: Format) -> [${build_kind}] { |
| 72 | + build${kind}List(format: format, leadingTrivia: nil) |
| 73 | + } |
| 74 | + |
| 75 | + public func build${kind}List(format: Format, leadingTrivia: Trivia?) -> [${build_kind}] { |
| 76 | + [build${kind}(format: format, leadingTrivia: leadingTrivia)] |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +% end |
| 81 | +% end |
| 82 | + |
| 83 | +// MARK: - Buildables |
| 84 | + |
| 85 | +% for node in SYNTAX_NODES: |
| 86 | +% if node.is_buildable(): |
| 87 | +public struct ${node.syntax_kind}: ${node.base_kind}Buildable { |
| 88 | +% child_params = [] |
| 89 | +% for child in node.children: |
| 90 | +% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional) |
| 91 | +% child_params.append("%s: %s" % (child.swift_name, param_type)) |
| 92 | +% end |
| 93 | +% for child_param in child_params: |
| 94 | + let ${child_param} |
| 95 | +% end |
| 96 | + |
| 97 | + public init( |
| 98 | + ${',\n '.join(child_params)} |
| 99 | + ) { |
| 100 | +% for child in node.children: |
| 101 | + self.${child.swift_name} = ${child.swift_name} |
| 102 | +% end |
| 103 | + } |
| 104 | + |
| 105 | + func build${node.syntax_kind}(format: Format, leadingTrivia: Trivia? = nil) -> ${node.syntax_kind}Syntax { |
| 106 | +% child_params = [] |
| 107 | +% for child in node.children: |
| 108 | +% param_expr = child.swift_name |
| 109 | +% if child.is_token() and child.requires_leading_newline: |
| 110 | +% param_expr = param_expr + '.withLeadingTrivia(.newlines(1) + format._makeIndent())' |
| 111 | +% elif child.syntax_kind in SYNTAX_BASE_KINDS or not child.is_token(): |
| 112 | +% if child.is_optional: |
| 113 | +% param_expr = param_expr + "?" |
| 114 | +% end |
| 115 | +% format = 'format' |
| 116 | +% if child.is_indented: |
| 117 | +% format += '._indented()' |
| 118 | +% end |
| 119 | +% param_expr = param_expr + ".build" + child.syntax_kind + "(format: " + format + ")" |
| 120 | +% end |
| 121 | +% child_params.append("%s: %s" % (child.swift_name, param_expr)) |
| 122 | +% end |
| 123 | + let ${node.swift_syntax_kind} = SyntaxFactory.make${node.syntax_kind}( |
| 124 | + ${',\n '.join(child_params)} |
| 125 | + ) |
| 126 | + |
| 127 | + if let leadingTrivia = leadingTrivia { |
| 128 | + return ${node.swift_syntax_kind} |
| 129 | + .withLeadingTrivia(leadingTrivia) |
| 130 | + } |
| 131 | + |
| 132 | + return ${node.swift_syntax_kind} |
| 133 | + } |
| 134 | + |
| 135 | +% return_type = 'Syntax' if node.base_kind == 'Syntax' else node.base_kind + 'Syntax' |
| 136 | + public func build${node.base_kind}(format: Format, leadingTrivia: Trivia? = nil) -> ${return_type} { |
| 137 | + let ${node.swift_syntax_kind} = build${node.syntax_kind}(format: format, leadingTrivia: leadingTrivia) |
| 138 | + return ${return_type}(${node.swift_syntax_kind}) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +% elif node.is_syntax_collection(): |
| 143 | +// MARK: - Syntax collection |
| 144 | +public struct ${node.syntax_kind}: SyntaxBuildable { |
| 145 | +// ${node.collection_element} |
| 146 | +% element_type = syntax_buildable_child_type(node.collection_element_type, node.collection_element, node.is_token()) |
| 147 | + let elements: [${element_type}] |
| 148 | + |
| 149 | + public init(_ elements: [${element_type}]) { |
| 150 | + self.elements = elements |
| 151 | + } |
| 152 | + |
| 153 | + public func build${node.syntax_kind}(format: Format, leadingTrivia: Trivia? = nil) -> ${node.syntax_kind}Syntax { |
| 154 | +% if not node.is_token(): |
| 155 | +% leading_trivia = 'nil' |
| 156 | +% if node.elements_separated_by_newline: |
| 157 | +% leading_trivia = '.newlines(1) + format._makeIndent()' |
| 158 | +% end |
| 159 | + return SyntaxFactory.make${node.syntax_kind}(elements.map { |
| 160 | + $0.build${node.collection_element}(format: format, leadingTrivia: ${leading_trivia}) |
| 161 | + }) |
| 162 | +% else: |
| 163 | + return SyntaxFactory.make${node.syntax_kind}(elements) |
| 164 | +% end |
| 165 | + } |
| 166 | + |
| 167 | + public func buildSyntax(format: Format, leadingTrivia: Trivia? = nil) -> Syntax { |
| 168 | + let ${node.swift_syntax_kind} = build${node.syntax_kind}(format: format, leadingTrivia: leadingTrivia) |
| 169 | + |
| 170 | + if let leadingTrivia = leadingTrivia { |
| 171 | + return Syntax(${node.swift_syntax_kind}).withLeadingTrivia(leadingTrivia) |
| 172 | + } |
| 173 | + |
| 174 | + return Syntax(${node.swift_syntax_kind}) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +% end |
| 179 | +% end |
0 commit comments