Skip to content

Commit 5ee31ce

Browse files
committed
Rough semantic parsing of @_package arguments
1 parent 2d31a79 commit 5ee31ce

File tree

9 files changed

+132
-30
lines changed

9 files changed

+132
-30
lines changed

CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public let KEYWORDS: [KeywordSpec] = [
101101
KeywordSpec("backDeployed"),
102102
KeywordSpec("before"),
103103
KeywordSpec("block"),
104+
KeywordSpec("branch"),
104105
KeywordSpec("break", isLexerClassified: true, requiresTrailingSpace: true),
105106
KeywordSpec("case", isLexerClassified: true, requiresTrailingSpace: true),
106107
KeywordSpec("catch", isLexerClassified: true, requiresLeadingSpace: true),
@@ -133,10 +134,12 @@ public let KEYWORDS: [KeywordSpec] = [
133134
KeywordSpec("final"),
134135
KeywordSpec("for", isLexerClassified: true, requiresTrailingSpace: true),
135136
KeywordSpec("forward"),
137+
KeywordSpec("from"),
136138
KeywordSpec("func", isLexerClassified: true, requiresTrailingSpace: true),
137139
KeywordSpec("get"),
138140
KeywordSpec("guard", isLexerClassified: true, requiresTrailingSpace: true),
139141
KeywordSpec("higherThan"),
142+
KeywordSpec("id"),
140143
KeywordSpec("if", isLexerClassified: true, requiresTrailingSpace: true),
141144
KeywordSpec("import", isLexerClassified: true, requiresTrailingSpace: true),
142145
KeywordSpec("in", isLexerClassified: true, requiresLeadingSpace: true, requiresTrailingSpace: true),
@@ -178,10 +181,12 @@ public let KEYWORDS: [KeywordSpec] = [
178181
KeywordSpec("optional"),
179182
KeywordSpec("override"),
180183
KeywordSpec("package"),
184+
KeywordSpec("path"),
181185
KeywordSpec("postfix"),
182186
KeywordSpec("precedencegroup", isLexerClassified: true, requiresTrailingSpace: true),
183187
KeywordSpec("prefix"),
184188
KeywordSpec("private", isLexerClassified: true, requiresTrailingSpace: true),
189+
KeywordSpec("product"),
185190
KeywordSpec("Protocol"),
186191
KeywordSpec("protocol", isLexerClassified: true, requiresTrailingSpace: true),
187192
KeywordSpec("public", isLexerClassified: true, requiresTrailingSpace: true),
@@ -192,6 +197,7 @@ public let KEYWORDS: [KeywordSpec] = [
192197
KeywordSpec("rethrows", isLexerClassified: true, requiresTrailingSpace: true),
193198
KeywordSpec("return", isLexerClassified: true, requiresTrailingSpace: true),
194199
KeywordSpec("reverse"),
200+
KeywordSpec("revision"),
195201
KeywordSpec("right"),
196202
KeywordSpec("safe"),
197203
KeywordSpec("self", isLexerClassified: true),
@@ -222,6 +228,7 @@ public let KEYWORDS: [KeywordSpec] = [
222228
KeywordSpec("unsafe"),
223229
KeywordSpec("unsafeAddress"),
224230
KeywordSpec("unsafeMutableAddress"),
231+
KeywordSpec("url"),
225232
KeywordSpec("var", isLexerClassified: true, requiresTrailingSpace: true),
226233
KeywordSpec("visibility"),
227234
KeywordSpec("weak"),

CodeGeneration/Sources/SyntaxSupport/gyb_generated/AttributeNodes.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ public let ATTRIBUTE_NODES: [Node] = [
210210
description: "The location/identifier of package."),
211211
Child(name: "LocReqComma",
212212
kind: .token(choices: [.token(tokenKind: "CommaToken")]),
213-
description: "The comma separating the location and requirement"),
213+
description: "The comma separating the location and requirement",
214+
isOptional: true),
214215
Child(name: "RequirementLabel",
215216
kind: .token(choices: [.keyword(text: "branch"), .keyword(text: "from"), .keyword(text: "revision")]),
216217
description: "The requirement label.",

Sources/SwiftParser/Attributes.swift

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -893,20 +893,80 @@ extension Parser {
893893
)
894894
}
895895

896+
enum PackageLocationLabel: RawTokenKindSubset {
897+
case id
898+
case path
899+
case url
900+
901+
init?(lexeme: Lexer.Lexeme) {
902+
switch lexeme {
903+
case RawTokenKindMatch(.id): self = .id
904+
case RawTokenKindMatch(.path): self = .path
905+
case RawTokenKindMatch(.url): self = .url
906+
default: return nil
907+
}
908+
}
909+
910+
var rawTokenKind: RawTokenKind {
911+
switch self {
912+
case .id: return .keyword(.id)
913+
case .path: return .keyword(.path)
914+
case .url: return .keyword(.url)
915+
}
916+
}
917+
}
918+
919+
enum PackageRequirementLabel: RawTokenKindSubset {
920+
case branch
921+
case from
922+
case revision
923+
924+
init?(lexeme: Lexer.Lexeme) {
925+
switch lexeme {
926+
case RawTokenKindMatch(.branch): self = .branch
927+
case RawTokenKindMatch(.from): self = .from
928+
case RawTokenKindMatch(.revision): self = .revision
929+
default: return nil
930+
}
931+
}
932+
933+
var rawTokenKind: RawTokenKind {
934+
switch self {
935+
case .branch: return .keyword(.branch)
936+
case .from: return .keyword(.from)
937+
case .revision: return .keyword(.revision)
938+
}
939+
}
940+
}
941+
896942
mutating func parsePackageAttributeArguments() -> RawPackageAttributeArgumentsSyntax {
897-
let (unexpectedBeforeLocationLabel, locationLabel) = self.parseArgumentLabel()
943+
// Parsing package location.
944+
let locationLabel = self.consume(ifAnyIn: PackageLocationLabel.self) ?? missingToken(.identifier)
898945
let (unexpectedBeforeLocationColon, locationColon) = self.expect(.colon)
899946
let location = self.parseStringLiteral()
900-
let (unexpectedBeforeLocReqComma, locReqComma) = self.expect(.comma)
901-
// FIXME: Requirement label/colon is optional.
902-
let (unexpectedBeforeRequirementLabel, requirementLabel) = self.parseArgumentLabel()
903-
let (unexpectedBeforeRequirementColon, requirementColon) = self.expect(.colon)
904-
// TODO: Semantically parsing according to `locationLabel`.
905-
let requirement = self.parseExpression()
906-
// FIXME: What about unexpected token before comma?
947+
// Parsing package requirement.
948+
let (unexpectedBeforeLocReqComma, locReqComma): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
949+
let (unexpectedBeforeRequirementLabel, requirementLabel): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
950+
let (unexpectedBeforeRequirementColon, requirementColon): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
951+
let requirement: RawExprSyntax?
952+
if locationLabel.tokenKind != .keyword(.path) {
953+
(unexpectedBeforeLocReqComma, locReqComma) = self.expect(.comma)
954+
if let label = self.consume(ifAnyIn: PackageRequirementLabel.self) {
955+
(unexpectedBeforeRequirementLabel, requirementLabel) = (nil, label)
956+
(unexpectedBeforeRequirementColon, requirementColon) = self.expect(.colon)
957+
} else {
958+
(unexpectedBeforeRequirementLabel, requirementLabel) = (nil, nil)
959+
(unexpectedBeforeRequirementColon, requirementColon) = (nil, nil)
960+
}
961+
requirement = self.parseExpression()
962+
} else {
963+
(unexpectedBeforeLocReqComma, locReqComma) = (nil, nil)
964+
(unexpectedBeforeRequirementLabel, requirementLabel) = (nil, nil)
965+
(unexpectedBeforeRequirementColon, requirementColon) = (nil, nil)
966+
requirement = nil
967+
}
907968
guard self.at(.comma) else {
908969
return RawPackageAttributeArgumentsSyntax(
909-
unexpectedBeforeLocationLabel,
910970
locationLabel: locationLabel,
911971
unexpectedBeforeLocationColon,
912972
locationColon: locationColon,
@@ -927,11 +987,10 @@ extension Parser {
927987
)
928988
}
929989
let (unexpectedBeforeReqProdComma, reqProdComma) = self.expect(.comma)
930-
let (unexpectedBeforeProductLabel, productLabel) = self.parseArgumentLabel()
990+
let (unexpectedBeforeProductLabel, productLabel) = self.expect(.keyword(.product))
931991
let (unexpectedBeforeProductColon, productColon) = self.expect(.colon)
932992
let productName = self.parseStringLiteral()
933993
return RawPackageAttributeArgumentsSyntax(
934-
unexpectedBeforeLocationLabel,
935994
locationLabel: locationLabel,
936995
unexpectedBeforeLocationColon,
937996
locationColon: locationColon,

Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxNodes.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11746,7 +11746,7 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
1174611746
_ unexpectedBetweenLocationColonAndLocation: RawUnexpectedNodesSyntax? = nil,
1174711747
location: RawStringLiteralExprSyntax,
1174811748
_ unexpectedBetweenLocationAndLocReqComma: RawUnexpectedNodesSyntax? = nil,
11749-
locReqComma: RawTokenSyntax,
11749+
locReqComma: RawTokenSyntax?,
1175011750
_ unexpectedBetweenLocReqCommaAndRequirementLabel: RawUnexpectedNodesSyntax? = nil,
1175111751
requirementLabel: RawTokenSyntax?,
1175211752
_ unexpectedBetweenRequirementLabelAndRequirementColon: RawUnexpectedNodesSyntax? = nil,
@@ -11774,7 +11774,7 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
1177411774
layout[4] = unexpectedBetweenLocationColonAndLocation?.raw
1177511775
layout[5] = location.raw
1177611776
layout[6] = unexpectedBetweenLocationAndLocReqComma?.raw
11777-
layout[7] = locReqComma.raw
11777+
layout[7] = locReqComma?.raw
1177811778
layout[8] = unexpectedBetweenLocReqCommaAndRequirementLabel?.raw
1177911779
layout[9] = requirementLabel?.raw
1178011780
layout[10] = unexpectedBetweenRequirementLabelAndRequirementColon?.raw
@@ -11815,8 +11815,8 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
1181511815
public var unexpectedBetweenLocationAndLocReqComma: RawUnexpectedNodesSyntax? {
1181611816
layoutView.children[6].map(RawUnexpectedNodesSyntax.init(raw:))
1181711817
}
11818-
public var locReqComma: RawTokenSyntax {
11819-
layoutView.children[7].map(RawTokenSyntax.init(raw:))!
11818+
public var locReqComma: RawTokenSyntax? {
11819+
layoutView.children[7].map(RawTokenSyntax.init(raw:))
1182011820
}
1182111821
public var unexpectedBetweenLocReqCommaAndRequirementLabel: RawUnexpectedNodesSyntax? {
1182211822
layoutView.children[8].map(RawUnexpectedNodesSyntax.init(raw:))

Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxValidation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
17331733
assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self))
17341734
assertNoError(kind, 5, verify(layout[5], as: RawStringLiteralExprSyntax.self))
17351735
assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self))
1736-
assertNoError(kind, 7, verify(layout[7], as: RawTokenSyntax.self))
1736+
assertNoError(kind, 7, verify(layout[7], as: RawTokenSyntax?.self))
17371737
assertNoError(kind, 8, verify(layout[8], as: RawUnexpectedNodesSyntax?.self))
17381738
assertNoError(kind, 9, verify(layout[9], as: RawTokenSyntax?.self))
17391739
assertNoError(kind, 10, verify(layout[10], as: RawUnexpectedNodesSyntax?.self))

Sources/SwiftSyntax/generated/Keyword.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ public enum Keyword: UInt8, Hashable {
153153

154154
case block
155155

156+
case branch
157+
156158
case `break`
157159

158160
case `case`
@@ -217,6 +219,8 @@ public enum Keyword: UInt8, Hashable {
217219

218220
case forward
219221

222+
case from
223+
220224
case `func`
221225

222226
case get
@@ -225,6 +229,8 @@ public enum Keyword: UInt8, Hashable {
225229

226230
case higherThan
227231

232+
case id
233+
228234
case `if`
229235

230236
case `import`
@@ -307,6 +313,8 @@ public enum Keyword: UInt8, Hashable {
307313

308314
case package
309315

316+
case path
317+
310318
case postfix
311319

312320
case `precedencegroup`
@@ -315,6 +323,8 @@ public enum Keyword: UInt8, Hashable {
315323

316324
case `private`
317325

326+
case product
327+
318328
case `Protocol`
319329

320330
case `protocol`
@@ -335,6 +345,8 @@ public enum Keyword: UInt8, Hashable {
335345

336346
case reverse
337347

348+
case revision
349+
338350
case right
339351

340352
case safe
@@ -395,6 +407,8 @@ public enum Keyword: UInt8, Hashable {
395407

396408
case unsafeMutableAddress
397409

410+
case url
411+
398412
case `var`
399413

400414
case visibility
@@ -421,6 +435,8 @@ public enum Keyword: UInt8, Hashable {
421435
self = .`as`
422436
case "do":
423437
self = .`do`
438+
case "id":
439+
self = .id
424440
case "if":
425441
self = .`if`
426442
case "in":
@@ -452,6 +468,8 @@ public enum Keyword: UInt8, Hashable {
452468
self = .spi
453469
case "try":
454470
self = .`try`
471+
case "url":
472+
self = .url
455473
case "var":
456474
self = .`var`
457475
case "wrt":
@@ -473,6 +491,8 @@ public enum Keyword: UInt8, Hashable {
473491
self = .`enum`
474492
case "file":
475493
self = .file
494+
case "from":
495+
self = .from
476496
case "func":
477497
self = .`func`
478498
case "init":
@@ -491,6 +511,8 @@ public enum Keyword: UInt8, Hashable {
491511
self = .objc
492512
case "open":
493513
self = .open
514+
case "path":
515+
self = .path
494516
case "safe":
495517
self = .safe
496518
case "self":
@@ -573,6 +595,8 @@ public enum Keyword: UInt8, Hashable {
573595
self = ._local
574596
case "before":
575597
self = .before
598+
case "branch":
599+
self = .branch
576600
case "deinit":
577601
self = .`deinit`
578602
case "didSet":
@@ -636,6 +660,8 @@ public enum Keyword: UInt8, Hashable {
636660
self = .postfix
637661
case "private":
638662
self = .`private`
663+
case "product":
664+
self = .product
639665
case "reasync":
640666
self = .reasync
641667
case "renamed":
@@ -695,6 +721,8 @@ public enum Keyword: UInt8, Hashable {
695721
self = .required
696722
case "rethrows":
697723
self = .`rethrows`
724+
case "revision":
725+
self = .revision
698726
case "Sendable":
699727
self = .Sendable
700728
default:
@@ -1108,6 +1136,7 @@ public enum Keyword: UInt8, Hashable {
11081136
"backDeployed",
11091137
"before",
11101138
"block",
1139+
"branch",
11111140
"break",
11121141
"case",
11131142
"catch",
@@ -1140,10 +1169,12 @@ public enum Keyword: UInt8, Hashable {
11401169
"final",
11411170
"for",
11421171
"forward",
1172+
"from",
11431173
"func",
11441174
"get",
11451175
"guard",
11461176
"higherThan",
1177+
"id",
11471178
"if",
11481179
"import",
11491180
"in",
@@ -1185,10 +1216,12 @@ public enum Keyword: UInt8, Hashable {
11851216
"optional",
11861217
"override",
11871218
"package",
1219+
"path",
11881220
"postfix",
11891221
"precedencegroup",
11901222
"prefix",
11911223
"private",
1224+
"product",
11921225
"Protocol",
11931226
"protocol",
11941227
"public",
@@ -1199,6 +1232,7 @@ public enum Keyword: UInt8, Hashable {
11991232
"rethrows",
12001233
"return",
12011234
"reverse",
1235+
"revision",
12021236
"right",
12031237
"safe",
12041238
"self",
@@ -1229,6 +1263,7 @@ public enum Keyword: UInt8, Hashable {
12291263
"unsafe",
12301264
"unsafeAddress",
12311265
"unsafeMutableAddress",
1266+
"url",
12321267
"var",
12331268
"visibility",
12341269
"weak",

0 commit comments

Comments
 (0)