Skip to content

Commit bffcd8c

Browse files
committed
Move base syntax nodes to their own file
1 parent 9436722 commit bffcd8c

File tree

5 files changed

+421
-1468
lines changed

5 files changed

+421
-1468
lines changed

Sources/SwiftSyntax/SyntaxNodes.swift.gyb

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -40,68 +40,9 @@ Each node will have:
4040

4141
% for node in SYNTAX_NODES:
4242
% base_type = node.base_type
43-
/// Protocol to which all `${node.name}` nodes conform. Extension point to add
44-
/// common methods to all `${node.name}` nodes.
45-
/// DO NOT CONFORM TO THIS PROTOCOL YOURSELF!
46-
public protocol ${node.name}Protocol: ${base_type}Protocol {}
47-
4843
% if node.is_base():
49-
% for line in dedented_lines(node.description):
50-
/// ${line}
51-
% end
52-
public struct ${node.name}: ${node.name}Protocol {
53-
public let _syntaxNode: Syntax
54-
55-
public init<S: ${node.name}Protocol>(_ syntax: S) {
56-
// We know this cast is going to succeed. Go through init(_: SyntaxData)
57-
// to do a sanity check and verify the kind matches in debug builds and get
58-
// maximum performance in release builds.
59-
self.init(syntax._syntaxNode.data)
60-
}
61-
62-
/// Converts the given `Syntax` node to a `${node.name}` if possible. Returns
63-
/// `nil` if the conversion is not possible.
64-
public init?(_ syntax: Syntax) {
65-
switch syntax.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-
self._syntaxNode = syntax
71-
default:
72-
return nil
73-
}
74-
}
75-
76-
/// Creates a `${node.name}` node from the given `SyntaxData`. This assumes
77-
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
78-
/// is undefined.
79-
internal init(_ data: SyntaxData) {
80-
// Assert that the kind of the given data matches in debug builds.
81-
#if DEBUG
82-
switch data.raw.kind {
83-
% castable_kinds = ['.' + child_node.swift_syntax_kind for child_node \
84-
% in SYNTAX_NODES \
85-
% if child_node.base_kind == node.syntax_kind]
86-
case ${', '.join(castable_kinds)}:
87-
break
88-
default:
89-
fatalError("Unable to create ${node.name} from \(data.raw.kind)")
90-
}
91-
#endif
92-
93-
self._syntaxNode = Syntax(data)
94-
}
95-
96-
public func `is`<S: ${node.name}Protocol>(_ syntaxType: S.Type) -> Bool {
97-
return self.as(syntaxType) != nil
98-
}
99-
100-
public func `as`<S: ${node.name}Protocol>(_ syntaxType: S.Type) -> S? {
101-
return S.init(_syntaxNode)
102-
}
103-
}
104-
44+
% # Handled in SyntaxNodesBase.swift.gyb
45+
% pass
10546
% elif node.collection_element:
10647
% pass
10748
% else:
@@ -251,13 +192,8 @@ public struct ${node.name}: ${base_type}Protocol {
251192

252193
% for node in SYNTAX_NODES:
253194
% if node.is_base():
254-
extension ${node.name}: CustomReflectable {
255-
/// Reconstructs the real syntax type for this type from the node's kind and
256-
/// provides a mirror that reflects this type.
257-
public var customMirror: Mirror {
258-
return Mirror(reflecting: Syntax(self)._asConcreteType)
259-
}
260-
}
195+
% # Handled in SyntaxNodesBase.swift.gyb
196+
% pass
261197
% elif node.is_syntax_collection():
262198
% pass
263199
% else:
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)