|
| 1 | +%{ |
| 2 | + # -*- mode: Swift -*- |
| 3 | + from gyb_syntax_support import * |
| 4 | + from gyb_syntax_support.Traits import TRAITS |
| 5 | + NODE_MAP = create_node_map() |
| 6 | + # Ignore the following admonition it applies to the resulting .swift file only |
| 7 | +}% |
| 8 | +//// Automatically Generated From SyntaxNodes.swift.gyb. |
| 9 | +//// Do Not Edit Directly! |
| 10 | +//===------------ SyntaxNodes.swift - Syntax Node definitions -------------===// |
| 11 | +// |
| 12 | +// This source file is part of the Swift.org open source project |
| 13 | +// |
| 14 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 15 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 16 | +// |
| 17 | +// See https://swift.org/LICENSE.txt for license information |
| 18 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 19 | +// |
| 20 | +//===----------------------------------------------------------------------===// |
| 21 | + |
| 22 | +% for node in SYNTAX_NODES: |
| 23 | +% base_type = node.base_type |
| 24 | +% if node.is_base(): |
| 25 | +// MARK: - ${node.name} |
| 26 | + |
| 27 | +/// Protocol to which all `${node.name}` nodes conform. Extension point to add |
| 28 | +/// common methods to all `${node.name}` nodes. |
| 29 | +/// DO NOT CONFORM TO THIS PROTOCOL YOURSELF! |
| 30 | +public protocol ${node.name}Protocol: ${base_type}Protocol {} |
| 31 | + |
| 32 | +% for line in dedented_lines(node.description): |
| 33 | +/// ${line} |
| 34 | +% end |
| 35 | +public struct ${node.name}: ${node.name}Protocol { |
| 36 | + public let _syntaxNode: Syntax |
| 37 | + |
| 38 | + public init<S: ${node.name}Protocol>(_ syntax: S) { |
| 39 | + // We know this cast is going to succeed. Go through init(_: SyntaxData) |
| 40 | + // to do a sanity check and verify the kind matches in debug builds and get |
| 41 | + // maximum performance in release builds. |
| 42 | + self.init(syntax._syntaxNode.data) |
| 43 | + } |
| 44 | + |
| 45 | + /// Converts the given `Syntax` node to a `${node.name}` if possible. Returns |
| 46 | + /// `nil` if the conversion is not possible. |
| 47 | + public init?(_ syntax: Syntax) { |
| 48 | + switch syntax.raw.kind { |
| 49 | +% castable_kinds = ['.' + child_node.swift_syntax_kind for child_node \ |
| 50 | +% in SYNTAX_NODES \ |
| 51 | +% if child_node.base_kind == node.syntax_kind] |
| 52 | + case ${', '.join(castable_kinds)}: |
| 53 | + self._syntaxNode = syntax |
| 54 | + default: |
| 55 | + return nil |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// Creates a `${node.name}` node from the given `SyntaxData`. This assumes |
| 60 | + /// that the `SyntaxData` is of the correct kind. If it is not, the behaviour |
| 61 | + /// is undefined. |
| 62 | + internal init(_ data: SyntaxData) { |
| 63 | + // Assert that the kind of the given data matches in debug builds. |
| 64 | +#if DEBUG |
| 65 | + switch data.raw.kind { |
| 66 | +% castable_kinds = ['.' + child_node.swift_syntax_kind for child_node \ |
| 67 | +% in SYNTAX_NODES \ |
| 68 | +% if child_node.base_kind == node.syntax_kind] |
| 69 | + case ${', '.join(castable_kinds)}: |
| 70 | + break |
| 71 | + default: |
| 72 | + fatalError("Unable to create ${node.name} from \(data.raw.kind)") |
| 73 | + } |
| 74 | +#endif |
| 75 | + |
| 76 | + self._syntaxNode = Syntax(data) |
| 77 | + } |
| 78 | + |
| 79 | + public func `is`<S: ${node.name}Protocol>(_ syntaxType: S.Type) -> Bool { |
| 80 | + return self.as(syntaxType) != nil |
| 81 | + } |
| 82 | + |
| 83 | + public func `as`<S: ${node.name}Protocol>(_ syntaxType: S.Type) -> S? { |
| 84 | + return S.init(_syntaxNode) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +extension ${node.name}: CustomReflectable { |
| 89 | + /// Reconstructs the real syntax type for this type from the node's kind and |
| 90 | + /// provides a mirror that reflects this type. |
| 91 | + public var customMirror: Mirror { |
| 92 | + return Mirror(reflecting: Syntax(self)._asConcreteType) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +% end |
| 97 | +% end |
0 commit comments