Skip to content

Commit b80213d

Browse files
committed
Move isLexerClassifiedKeyword from SwiftSyntax to SwiftParser
This allows us to make the properties internal and remove them from the public API surface.
1 parent cc2ea59 commit b80213d

File tree

8 files changed

+243
-267
lines changed

8 files changed

+243
-267
lines changed

CodeGeneration/Sources/generate-swiftsyntax/GenerateSwiftSyntax.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
8787

8888
// SwiftParser
8989
GeneratedFileSpec(swiftParserGeneratedDir + ["DeclarationModifier.swift"], declarationModifierFile),
90+
GeneratedFileSpec(swiftParserGeneratedDir + ["IsLexerClassified.swift"], isLexerClassifiedFile),
9091
GeneratedFileSpec(swiftParserGeneratedDir + ["Parser+Entry.swift"], parserEntryFile),
9192
GeneratedFileSpec(swiftParserGeneratedDir + ["TokenSpecStaticMembers.swift"], tokenSpecStaticMembersFile),
9293
GeneratedFileSpec(swiftParserGeneratedDir + ["TypeAttribute.swift"], typeAttributeFile),
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
import SwiftSyntaxBuilder
15+
import SyntaxSupport
16+
import Utils
17+
18+
let isLexerClassifiedFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
19+
DeclSyntax("import SwiftSyntax")
20+
21+
try! ExtensionDeclSyntax(
22+
"""
23+
extension Keyword
24+
"""
25+
) {
26+
try! VariableDeclSyntax(
27+
"""
28+
/// Whether the token kind is switched from being an identifier to being a keyword in the lexer.
29+
/// This is true for keywords that used to be considered non-contextual.
30+
var isLexerClassified: Bool
31+
"""
32+
) {
33+
try! SwitchExprSyntax("switch self") {
34+
for keyword in KEYWORDS {
35+
if keyword.isLexerClassified {
36+
SwitchCaseSyntax("case .\(raw: keyword.escapedName): return true")
37+
}
38+
}
39+
SwitchCaseSyntax("default: return false")
40+
}
41+
}
42+
}
43+
44+
try! ExtensionDeclSyntax(
45+
"""
46+
extension TokenKind
47+
"""
48+
) {
49+
try! VariableDeclSyntax(
50+
"""
51+
/// Returns `true` if the token is a Swift keyword.
52+
///
53+
/// Keywords are reserved unconditionally for use by Swift and may not
54+
/// appear as identifiers in any position without being escaped. For example,
55+
/// `class`, `func`, or `import`.
56+
public var isLexerClassifiedKeyword: Bool
57+
"""
58+
) {
59+
try! SwitchExprSyntax("switch self") {
60+
SwitchCaseSyntax("case .eof:") {
61+
StmtSyntax("return false")
62+
}
63+
64+
for token in SYNTAX_TOKENS where token.isKeyword {
65+
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
66+
StmtSyntax("return true")
67+
}
68+
}
69+
70+
SwitchCaseSyntax("case .keyword(let keyword):") {
71+
StmtSyntax("return keyword.isLexerClassified")
72+
}
73+
SwitchCaseSyntax("default: return false")
74+
}
75+
}
76+
}
77+
}

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/KeywordFile.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,6 @@ let keywordFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
5050
}
5151
}
5252

53-
try! VariableDeclSyntax(
54-
"""
55-
/// Whether the token kind is switched from being an identifier to being a keyword in the lexer.
56-
/// This is true for keywords that used to be considered non-contextual.
57-
public var isLexerClassified: Bool
58-
"""
59-
) {
60-
try! SwitchExprSyntax("switch self") {
61-
for keyword in KEYWORDS {
62-
if keyword.isLexerClassified {
63-
SwitchCaseSyntax("case .\(raw: keyword.escapedName): return true")
64-
}
65-
}
66-
SwitchCaseSyntax("default: return false")
67-
}
68-
}
69-
7053
DeclSyntax(
7154
"""
7255
/// This is really unfortunate. Really, we should have a `switch` in

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/TokenKindFile.swift

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -96,33 +96,6 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
9696
}
9797
}
9898

99-
try VariableDeclSyntax(
100-
"""
101-
/// Returns `true` if the token is a Swift keyword.
102-
///
103-
/// Keywords are reserved unconditionally for use by Swift and may not
104-
/// appear as identifiers in any position without being escaped. For example,
105-
/// `class`, `func`, or `import`.
106-
public var isLexerClassifiedKeyword: Bool
107-
"""
108-
) {
109-
try SwitchExprSyntax("switch self") {
110-
SwitchCaseSyntax("case .eof:") {
111-
StmtSyntax("return false")
112-
}
113-
114-
for token in SYNTAX_TOKENS where token.swiftKind != "keyword" {
115-
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
116-
StmtSyntax("return \(raw: token.isKeyword)")
117-
}
118-
}
119-
120-
SwitchCaseSyntax("case .keyword(let keyword):") {
121-
StmtSyntax("return keyword.isLexerClassified")
122-
}
123-
}
124-
}
125-
12699
try VariableDeclSyntax(
127100
"""
128101
/// Returns `true` if the token is a Swift punctuator.

Sources/SwiftParser/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ add_swift_host_library(SwiftParser
3535
Types.swift
3636

3737
generated/DeclarationModifier.swift
38+
generated/IsLexerClassified.swift
3839
generated/Parser+Entry.swift
3940
generated/TokenSpecStaticMembers.swift
4041
generated/TypeAttribute.swift
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
//// Automatically generated by generate-swiftsyntax
2+
//// Do not edit directly!
3+
//===----------------------------------------------------------------------===//
4+
//
5+
// This source file is part of the Swift.org open source project
6+
//
7+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
8+
// Licensed under Apache License v2.0 with Runtime Library Exception
9+
//
10+
// See https://swift.org/LICENSE.txt for license information
11+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import SwiftSyntax
16+
17+
extension Keyword {
18+
/// Whether the token kind is switched from being an identifier to being a keyword in the lexer.
19+
/// This is true for keywords that used to be considered non-contextual.
20+
var isLexerClassified: Bool {
21+
switch self {
22+
case .`Any`:
23+
return true
24+
case .`as`:
25+
return true
26+
case .`associatedtype`:
27+
return true
28+
case .`break`:
29+
return true
30+
case .`case`:
31+
return true
32+
case .`catch`:
33+
return true
34+
case .`class`:
35+
return true
36+
case .`continue`:
37+
return true
38+
case .`default`:
39+
return true
40+
case .`defer`:
41+
return true
42+
case .`deinit`:
43+
return true
44+
case .`do`:
45+
return true
46+
case .`else`:
47+
return true
48+
case .`enum`:
49+
return true
50+
case .`extension`:
51+
return true
52+
case .`fallthrough`:
53+
return true
54+
case .`false`:
55+
return true
56+
case .`fileprivate`:
57+
return true
58+
case .`for`:
59+
return true
60+
case .`func`:
61+
return true
62+
case .`guard`:
63+
return true
64+
case .`if`:
65+
return true
66+
case .`import`:
67+
return true
68+
case .`in`:
69+
return true
70+
case .`init`:
71+
return true
72+
case .`inout`:
73+
return true
74+
case .`internal`:
75+
return true
76+
case .`is`:
77+
return true
78+
case .`let`:
79+
return true
80+
case .`nil`:
81+
return true
82+
case .`operator`:
83+
return true
84+
case .`precedencegroup`:
85+
return true
86+
case .`private`:
87+
return true
88+
case .`protocol`:
89+
return true
90+
case .`public`:
91+
return true
92+
case .`repeat`:
93+
return true
94+
case .`rethrows`:
95+
return true
96+
case .`return`:
97+
return true
98+
case .`self`:
99+
return true
100+
case .`Self`:
101+
return true
102+
case .`static`:
103+
return true
104+
case .`struct`:
105+
return true
106+
case .`subscript`:
107+
return true
108+
case .`super`:
109+
return true
110+
case .`switch`:
111+
return true
112+
case .`throw`:
113+
return true
114+
case .`throws`:
115+
return true
116+
case .`true`:
117+
return true
118+
case .`try`:
119+
return true
120+
case .`typealias`:
121+
return true
122+
case .`var`:
123+
return true
124+
case .`where`:
125+
return true
126+
case .`while`:
127+
return true
128+
default:
129+
return false
130+
}
131+
}
132+
}
133+
134+
extension TokenKind {
135+
/// Returns `true` if the token is a Swift keyword.
136+
///
137+
/// Keywords are reserved unconditionally for use by Swift and may not
138+
/// appear as identifiers in any position without being escaped. For example,
139+
/// `class`, `func`, or `import`.
140+
public var isLexerClassifiedKeyword: Bool {
141+
switch self {
142+
case .eof:
143+
return false
144+
case .poundAvailableKeyword:
145+
return true
146+
case .poundElseKeyword:
147+
return true
148+
case .poundElseifKeyword:
149+
return true
150+
case .poundEndifKeyword:
151+
return true
152+
case .poundIfKeyword:
153+
return true
154+
case .poundSourceLocationKeyword:
155+
return true
156+
case .poundUnavailableKeyword:
157+
return true
158+
case .keyword(let keyword):
159+
return keyword.isLexerClassified
160+
default:
161+
return false
162+
}
163+
}
164+
}

0 commit comments

Comments
 (0)