%{ # -*- mode: Swift -*- from gyb_syntax_support import * NODE_MAP = create_node_map() # Ignore the following admonition; it applies to the resulting .swift file only }% //// Automatically Generated From SyntaxBuilders.swift.gyb. //// Do Not Edit Directly! //===------------ SyntaxBuilders.swift - Syntax Builder definitions -------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// %{ """ A builder is a struct with a mutable layout inside. Clients can specify as many or as few of the children as they want, and the structure is guaranted to be structurally (if not syntactically) valid. """ }% % for node in SYNTAX_NODES: % if node.is_buildable(): % Builder = node.name + "Builder" public struct ${Builder} { private var layout = Array(repeating: nil, count: ${len(node.children)}) internal init() {} % for child in node.children: % child_node = NODE_MAP.get(child.syntax_kind) % if child_node and child_node.is_syntax_collection(): % child_elt = child_node.collection_element_name % child_elt_type = child_node.collection_element_type public mutating func add${child_elt}(_ elt: ${child_elt_type}) { let idx = ${node.name}.Cursor.${child.swift_name}.rawValue if let list = layout[idx] { layout[idx] = list.appending(elt.raw) } else { layout[idx] = RawSyntax.node( .${child.swift_syntax_kind}, [elt.raw], .present) } } % else: public mutating func use${child.name}(_ node: ${child.type_name}) { let idx = ${node.name}.Cursor.${child.swift_name}.rawValue layout[idx] = node.raw } % end % end internal mutating func buildData() -> SyntaxData { % for (idx, child) in enumerate(node.children): % if not child.is_optional: if (layout[${idx}] == nil) { layout[${idx}] = ${make_missing_swift_child(child)} } % end % end return SyntaxData(raw: .node(.${node.swift_syntax_kind}, layout, .present)) } } extension ${node.name} { /// Creates a `${node.name}` using the provided build function. /// - Parameter: /// - build: A closure that wil be invoked in order to initialize /// the fields of the syntax node. /// This closure is passed a `${Builder}` which you can use to /// incrementally build the structure of the node. /// - Returns: A `${node.name}` with all the fields populated in the builder /// closure. public init(_ build: (inout ${Builder}) -> Void) { var builder = ${Builder}() build(&builder) let data = builder.buildData() self.init(root: data, data: data) } } % end % end