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