Skip to content

Commit 59b5efe

Browse files
committed
Generalize generation of raw choices enum
1 parent df9e8ad commit 59b5efe

File tree

2 files changed

+18
-279
lines changed

2 files changed

+18
-279
lines changed

Sources/SwiftSyntax/Raw/RawSyntaxNodes.swift.gyb

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,42 @@ public protocol Raw${node.name}NodeProtocol: Raw${node.base_type}NodeProtocol {}
2929

3030
@_spi(RawSyntax)
3131
public struct Raw${node.name}: Raw${node.name if node.is_base() else node.base_type}NodeProtocol, RawSyntaxToSyntax {
32+
% enums = []
3233
% for child in node.children:
3334
% if child.node_choices:
34-
public enum ${child.name}: RawSyntaxNodeProtocol {
35-
% for choice in child.node_choices:
36-
case `${choice.swift_name}`(Raw${choice.type_name})
37-
% end
35+
% enums.append((child.name, [(choice.swift_name, choice.type_name) for choice in child.node_choices]))
36+
% end
37+
% end
38+
% for (name, choices) in enums:
39+
public enum ${name}: RawSyntaxNodeProtocol {
40+
% for (swift_name, type_name) in choices:
41+
case `${swift_name}`(Raw${type_name})
42+
% end
3843

3944
public static func isKindOf(_ raw: RawSyntax) -> Bool {
40-
return ${" || ".join([f"Raw{choice.type_name}.isKindOf(raw)" for choice in child.node_choices])}
45+
return ${" || ".join([f"Raw{type_name}.isKindOf(raw)" for (swift_name, type_name) in choices])}
4146
}
4247

4348
public var raw: RawSyntax {
4449
switch self {
45-
% for choice in child.node_choices:
46-
case .${choice.swift_name}(let node): return node.raw
47-
% end
50+
% for (swift_name, type_name) in choices:
51+
case .${swift_name}(let node): return node.raw
52+
% end
4853
}
4954
}
5055

5156
public init?<T>(_ other: T) where T : RawSyntaxNodeProtocol {
52-
% for choice in child.node_choices:
53-
if let node = Raw${choice.type_name}(other) {
54-
self = .${choice.swift_name}(node)
57+
% for (swift_name, type_name) in choices:
58+
if let node = Raw${type_name}(other) {
59+
self = .${swift_name}(node)
5560
return
5661
}
57-
% end
62+
% end
5863
return nil
5964
}
6065
}
61-
% end
62-
% end
6366

67+
% end
6468
public typealias SyntaxType = ${node.name}
6569

6670
@_spi(RawSyntax)

0 commit comments

Comments
 (0)