Skip to content

Commit 9185ef5

Browse files
[Syntax] Add #selector and #keyPath tokens (#13615)
* [Syntax] Add #selector and #keyPath tokens * [Syntax][test] Add #selector and #keyPath to syntax parsing tests * [Syntax] Don't fatalError on an unknown token kind * Fix test build error
1 parent 0a41588 commit 9185ef5

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

test/SwiftSyntax/ParseFile.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ struct Foo {
1414
private(set) var y: [Bool]
1515
}
1616

17+
#if os(macOS)
18+
class Test: NSObject {
19+
@objc var bar: Int = 0
20+
func test() {
21+
print(#selector(function))
22+
print(#keyPath(bar))
23+
}
24+
@objc func function() {
25+
}
26+
}
27+
#endif
28+
1729
ParseFile.test("ParseSingleFile") {
1830
let currentFile = URL(fileURLWithPath: #file)
1931
expectDoesNotThrow({

tools/SwiftSyntax/TokenKind.swift.gyb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public enum TokenKind: Codable {
5252
case kind, text
5353
}
5454

55+
enum DecodeError: Error {
56+
case unknownTokenKind(String)
57+
}
58+
5559
public init(from decoder: Decoder) throws {
5660
let container = try decoder.container(keyedBy: CodingKeys.self)
5761
let kind = try container.decode(String.self, forKey: .kind)
@@ -67,10 +71,10 @@ public enum TokenKind: Codable {
6771
self = .${token.swift_kind()}(text)
6872
% end
6973
% end
70-
default: fatalError("unknown token kind \(kind)")
74+
default: throw DecodeError.unknownTokenKind(kind)
7175
}
7276
}
73-
77+
7478
public func encode(to encoder: Encoder) throws {
7579
var container = encoder.container(keyedBy: CodingKeys.self)
7680
try container.encode(kind, forKey: .kind)

utils/gyb_syntax_support/Token.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ def __init__(self, name, text):
108108
is_keyword=True),
109109
Token('PoundFunction', 'pound_function', text='#function',
110110
is_keyword=True),
111+
Token('PoundSelector', 'pound_selector', text='#selector',
112+
is_keyword=True),
113+
Token('PoundKeyPath', 'pound_keyPath', text='#keyPath',
114+
is_keyword=True),
111115
Token('Arrow', 'arrow', text='->'),
112116
Token('AtSign', 'at_sign', text='@'),
113117
Token('Colon', 'colon', text=':'),

0 commit comments

Comments
 (0)