Skip to content

Commit e2cf485

Browse files
committed
Rough semantic parsing of @_package arguments
1 parent 7f08587 commit e2cf485

File tree

10 files changed

+126
-30
lines changed

10 files changed

+126
-30
lines changed

CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public let KEYWORDS: [KeywordSpec] = [
9999
KeywordSpec("available"),
100100
KeywordSpec("await"),
101101
KeywordSpec("before"),
102+
KeywordSpec("branch"),
102103
KeywordSpec("break", isLexerClassified: true, requiresTrailingSpace: true),
103104
KeywordSpec("case", isLexerClassified: true, requiresTrailingSpace: true),
104105
KeywordSpec("catch", isLexerClassified: true, requiresLeadingSpace: true),
@@ -128,10 +129,12 @@ public let KEYWORDS: [KeywordSpec] = [
128129
KeywordSpec("fileprivate", isLexerClassified: true, requiresTrailingSpace: true),
129130
KeywordSpec("final"),
130131
KeywordSpec("for", isLexerClassified: true, requiresTrailingSpace: true),
132+
KeywordSpec("from"),
131133
KeywordSpec("func", isLexerClassified: true, requiresTrailingSpace: true),
132134
KeywordSpec("get"),
133135
KeywordSpec("guard", isLexerClassified: true, requiresTrailingSpace: true),
134136
KeywordSpec("higherThan"),
137+
KeywordSpec("id"),
135138
KeywordSpec("if", isLexerClassified: true, requiresTrailingSpace: true),
136139
KeywordSpec("import", isLexerClassified: true, requiresTrailingSpace: true),
137140
KeywordSpec("in", isLexerClassified: true, requiresLeadingSpace: true, requiresTrailingSpace: true),
@@ -169,10 +172,12 @@ public let KEYWORDS: [KeywordSpec] = [
169172
KeywordSpec("optional"),
170173
KeywordSpec("override"),
171174
KeywordSpec("package"),
175+
KeywordSpec("path"),
172176
KeywordSpec("postfix"),
173177
KeywordSpec("precedencegroup", isLexerClassified: true, requiresTrailingSpace: true),
174178
KeywordSpec("prefix"),
175179
KeywordSpec("private", isLexerClassified: true, requiresTrailingSpace: true),
180+
KeywordSpec("product"),
176181
KeywordSpec("Protocol"),
177182
KeywordSpec("protocol", isLexerClassified: true, requiresTrailingSpace: true),
178183
KeywordSpec("public", isLexerClassified: true, requiresTrailingSpace: true),
@@ -183,6 +188,7 @@ public let KEYWORDS: [KeywordSpec] = [
183188
KeywordSpec("rethrows", isLexerClassified: true, requiresTrailingSpace: true),
184189
KeywordSpec("return", isLexerClassified: true, requiresTrailingSpace: true),
185190
KeywordSpec("reverse"),
191+
KeywordSpec("revision"),
186192
KeywordSpec("safe"),
187193
KeywordSpec("self", isLexerClassified: true),
188194
KeywordSpec("Self", isLexerClassified: true),
@@ -212,6 +218,7 @@ public let KEYWORDS: [KeywordSpec] = [
212218
KeywordSpec("unsafe"),
213219
KeywordSpec("unsafeAddress"),
214220
KeywordSpec("unsafeMutableAddress"),
221+
KeywordSpec("url"),
215222
KeywordSpec("var", isLexerClassified: true, requiresTrailingSpace: true),
216223
KeywordSpec("visibility"),
217224
KeywordSpec("weak"),

CodeGeneration/Sources/SyntaxSupport/gyb_generated/AttributeNodes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ public let ATTRIBUTE_NODES: [Node] = [
268268
Child(name: "LocReqComma",
269269
kind: "CommaToken",
270270
description: "The comma separating the location and requirement",
271+
isOptional: true,
271272
tokenChoices: [
272273
"Comma"
273274
]),

Sources/SwiftParser/Attributes.swift

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -897,20 +897,80 @@ extension Parser {
897897
)
898898
}
899899

900+
enum PackageLocationLabel: RawTokenKindSubset {
901+
case id
902+
case path
903+
case url
904+
905+
init?(lexeme: Lexer.Lexeme) {
906+
switch lexeme {
907+
case RawTokenKindMatch(.id): self = .id
908+
case RawTokenKindMatch(.path): self = .path
909+
case RawTokenKindMatch(.url): self = .url
910+
default: return nil
911+
}
912+
}
913+
914+
var rawTokenKind: RawTokenKind {
915+
switch self {
916+
case .id: return .keyword(.id)
917+
case .path: return .keyword(.path)
918+
case .url: return .keyword(.url)
919+
}
920+
}
921+
}
922+
923+
enum PackageRequirementLabel: RawTokenKindSubset {
924+
case branch
925+
case from
926+
case revision
927+
928+
init?(lexeme: Lexer.Lexeme) {
929+
switch lexeme {
930+
case RawTokenKindMatch(.branch): self = .branch
931+
case RawTokenKindMatch(.from): self = .from
932+
case RawTokenKindMatch(.revision): self = .revision
933+
default: return nil
934+
}
935+
}
936+
937+
var rawTokenKind: RawTokenKind {
938+
switch self {
939+
case .branch: return .keyword(.branch)
940+
case .from: return .keyword(.from)
941+
case .revision: return .keyword(.revision)
942+
}
943+
}
944+
}
945+
900946
mutating func parsePackageAttributeArguments() -> RawPackageAttributeArgumentsSyntax {
901-
let (unexpectedBeforeLocationLabel, locationLabel) = self.parseArgumentLabel()
947+
// Parsing package location.
948+
let locationLabel = self.consume(ifAnyIn: PackageLocationLabel.self) ?? missingToken(.identifier)
902949
let (unexpectedBeforeLocationColon, locationColon) = self.expect(.colon)
903950
let (unexpectedBeforeLocation, location) = self.expect(.stringLiteral)
904-
let (unexpectedBeforeLocReqComma, locReqComma) = self.expect(.comma)
905-
// FIXME: Requirement label/colon is optional.
906-
let (unexpectedBeforeRequirementLabel, requirementLabel) = self.parseArgumentLabel()
907-
let (unexpectedBeforeRequirementColon, requirementColon) = self.expect(.colon)
908-
// TODO: Semantically parsing according to `locationLabel`.
909-
let requirement = self.parseExpression()
910-
// FIXME: What about unexpected token before comma?
951+
// Parsing package requirement.
952+
let (unexpectedBeforeLocReqComma, locReqComma): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
953+
let (unexpectedBeforeRequirementLabel, requirementLabel): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
954+
let (unexpectedBeforeRequirementColon, requirementColon): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
955+
let requirement: RawExprSyntax?
956+
if locationLabel.tokenKind != .keyword(.path) {
957+
(unexpectedBeforeLocReqComma, locReqComma) = self.expect(.comma)
958+
if let label = self.consume(ifAnyIn: PackageRequirementLabel.self) {
959+
(unexpectedBeforeRequirementLabel, requirementLabel) = (nil, label)
960+
(unexpectedBeforeRequirementColon, requirementColon) = self.expect(.colon)
961+
} else {
962+
(unexpectedBeforeRequirementLabel, requirementLabel) = (nil, nil)
963+
(unexpectedBeforeRequirementColon, requirementColon) = (nil, nil)
964+
}
965+
requirement = self.parseExpression()
966+
} else {
967+
(unexpectedBeforeLocReqComma, locReqComma) = (nil, nil)
968+
(unexpectedBeforeRequirementLabel, requirementLabel) = (nil, nil)
969+
(unexpectedBeforeRequirementColon, requirementColon) = (nil, nil)
970+
requirement = nil
971+
}
911972
guard self.at(.comma) else {
912973
return RawPackageAttributeArgumentsSyntax(
913-
unexpectedBeforeLocationLabel,
914974
locationLabel: locationLabel,
915975
unexpectedBeforeLocationColon,
916976
locationColon: locationColon,
@@ -932,11 +992,10 @@ extension Parser {
932992
)
933993
}
934994
let (unexpectedBeforeReqProdComma, reqProdComma) = self.expect(.comma)
935-
let (unexpectedBeforeProductLabel, productLabel) = self.parseArgumentLabel()
995+
let (unexpectedBeforeProductLabel, productLabel) = self.expect(.keyword(.product))
936996
let (unexpectedBeforeProductColon, productColon) = self.expect(.colon)
937997
let (unexpectedBeforeProductName, productName) = self.expect(.stringLiteral)
938998
return RawPackageAttributeArgumentsSyntax(
939-
unexpectedBeforeLocationLabel,
940999
locationLabel: locationLabel,
9411000
unexpectedBeforeLocationColon,
9421001
locationColon: locationColon,

Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxNodes.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11203,7 +11203,7 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
1120311203
_ unexpectedBetweenLocationColonAndLocation: RawUnexpectedNodesSyntax? = nil,
1120411204
location: RawTokenSyntax,
1120511205
_ unexpectedBetweenLocationAndLocReqComma: RawUnexpectedNodesSyntax? = nil,
11206-
locReqComma: RawTokenSyntax,
11206+
locReqComma: RawTokenSyntax?,
1120711207
_ unexpectedBetweenLocReqCommaAndRequirementLabel: RawUnexpectedNodesSyntax? = nil,
1120811208
requirementLabel: RawTokenSyntax?,
1120911209
_ unexpectedBetweenRequirementLabelAndRequirementColon: RawUnexpectedNodesSyntax? = nil,
@@ -11231,7 +11231,7 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
1123111231
layout[4] = unexpectedBetweenLocationColonAndLocation?.raw
1123211232
layout[5] = location.raw
1123311233
layout[6] = unexpectedBetweenLocationAndLocReqComma?.raw
11234-
layout[7] = locReqComma.raw
11234+
layout[7] = locReqComma?.raw
1123511235
layout[8] = unexpectedBetweenLocReqCommaAndRequirementLabel?.raw
1123611236
layout[9] = requirementLabel?.raw
1123711237
layout[10] = unexpectedBetweenRequirementLabelAndRequirementColon?.raw
@@ -11272,8 +11272,8 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
1127211272
public var unexpectedBetweenLocationAndLocReqComma: RawUnexpectedNodesSyntax? {
1127311273
layoutView.children[6].map(RawUnexpectedNodesSyntax.init(raw:))
1127411274
}
11275-
public var locReqComma: RawTokenSyntax {
11276-
layoutView.children[7].map(RawTokenSyntax.init(raw:))!
11275+
public var locReqComma: RawTokenSyntax? {
11276+
layoutView.children[7].map(RawTokenSyntax.init(raw:))
1127711277
}
1127811278
public var unexpectedBetweenLocReqCommaAndRequirementLabel: RawUnexpectedNodesSyntax? {
1127911279
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
@@ -1663,7 +1663,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
16631663
assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self))
16641664
assertNoError(kind, 5, verify(layout[5], as: RawTokenSyntax.self))
16651665
assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self))
1666-
assertNoError(kind, 7, verify(layout[7], as: RawTokenSyntax.self))
1666+
assertNoError(kind, 7, verify(layout[7], as: RawTokenSyntax?.self))
16671667
assertNoError(kind, 8, verify(layout[8], as: RawUnexpectedNodesSyntax?.self))
16681668
assertNoError(kind, 9, verify(layout[9], as: RawTokenSyntax?.self))
16691669
assertNoError(kind, 10, verify(layout[10], as: RawUnexpectedNodesSyntax?.self))

Sources/SwiftSyntax/generated/Keyword.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ public enum Keyword: StaticString {
149149

150150
case before
151151

152+
case branch
153+
152154
case `break`
153155

154156
case `case`
@@ -207,6 +209,8 @@ public enum Keyword: StaticString {
207209

208210
case `for`
209211

212+
case from
213+
210214
case `func`
211215

212216
case get
@@ -215,6 +219,8 @@ public enum Keyword: StaticString {
215219

216220
case higherThan
217221

222+
case id
223+
218224
case `if`
219225

220226
case `import`
@@ -289,6 +295,8 @@ public enum Keyword: StaticString {
289295

290296
case package
291297

298+
case path
299+
292300
case postfix
293301

294302
case `precedencegroup`
@@ -297,6 +305,8 @@ public enum Keyword: StaticString {
297305

298306
case `private`
299307

308+
case product
309+
300310
case `Protocol`
301311

302312
case `protocol`
@@ -317,6 +327,8 @@ public enum Keyword: StaticString {
317327

318328
case reverse
319329

330+
case revision
331+
320332
case safe
321333

322334
case `self`
@@ -375,6 +387,8 @@ public enum Keyword: StaticString {
375387

376388
case unsafeMutableAddress
377389

390+
case url
391+
378392
case `var`
379393

380394
case visibility
@@ -401,6 +415,8 @@ public enum Keyword: StaticString {
401415
self = .`as`
402416
case "do":
403417
self = .`do`
418+
case "id":
419+
self = .id
404420
case "if":
405421
self = .`if`
406422
case "in":
@@ -432,6 +448,8 @@ public enum Keyword: StaticString {
432448
self = .spi
433449
case "try":
434450
self = .`try`
451+
case "url":
452+
self = .url
435453
case "var":
436454
self = .`var`
437455
case "wrt":
@@ -451,6 +469,8 @@ public enum Keyword: StaticString {
451469
self = .`else`
452470
case "enum":
453471
self = .`enum`
472+
case "from":
473+
self = .from
454474
case "func":
455475
self = .`func`
456476
case "init":
@@ -463,6 +483,8 @@ public enum Keyword: StaticString {
463483
self = .objc
464484
case "open":
465485
self = .open
486+
case "path":
487+
self = .path
466488
case "safe":
467489
self = .safe
468490
case "self":
@@ -539,6 +561,8 @@ public enum Keyword: StaticString {
539561
self = ._local
540562
case "before":
541563
self = .before
564+
case "branch":
565+
self = .branch
542566
case "deinit":
543567
self = .`deinit`
544568
case "didSet":
@@ -598,6 +622,8 @@ public enum Keyword: StaticString {
598622
self = .postfix
599623
case "private":
600624
self = .`private`
625+
case "product":
626+
self = .product
601627
case "reasync":
602628
self = .reasync
603629
case "renamed":
@@ -657,6 +683,8 @@ public enum Keyword: StaticString {
657683
self = .required
658684
case "rethrows":
659685
self = .`rethrows`
686+
case "revision":
687+
self = .revision
660688
case "Sendable":
661689
self = .Sendable
662690
default:

Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5181,7 +5181,7 @@ public enum SyntaxFactory {
51815181
}
51825182
}
51835183
@available(*, deprecated, message: "Use initializer on PackageAttributeArgumentsSyntax")
5184-
public static func makePackageAttributeArguments(_ unexpectedBeforeLocationLabel: UnexpectedNodesSyntax? = nil, locationLabel: TokenSyntax, _ unexpectedBetweenLocationLabelAndLocationColon: UnexpectedNodesSyntax? = nil, locationColon: TokenSyntax, _ unexpectedBetweenLocationColonAndLocation: UnexpectedNodesSyntax? = nil, location: TokenSyntax, _ unexpectedBetweenLocationAndLocReqComma: UnexpectedNodesSyntax? = nil, locReqComma: TokenSyntax, _ unexpectedBetweenLocReqCommaAndRequirementLabel: UnexpectedNodesSyntax? = nil, requirementLabel: TokenSyntax?, _ unexpectedBetweenRequirementLabelAndRequirementColon: UnexpectedNodesSyntax? = nil, requirementColon: TokenSyntax?, _ unexpectedBetweenRequirementColonAndRequirement: UnexpectedNodesSyntax? = nil, requirement: ExprSyntax?, _ unexpectedBetweenRequirementAndReqProdComma: UnexpectedNodesSyntax? = nil, reqProdComma: TokenSyntax?, _ unexpectedBetweenReqProdCommaAndProductLabel: UnexpectedNodesSyntax? = nil, productLabel: TokenSyntax?, _ unexpectedBetweenProductLabelAndProductColon: UnexpectedNodesSyntax? = nil, productColon: TokenSyntax?, _ unexpectedBetweenProductColonAndProductName: UnexpectedNodesSyntax? = nil, productName: TokenSyntax?, _ unexpectedAfterProductName: UnexpectedNodesSyntax? = nil) -> PackageAttributeArgumentsSyntax {
5184+
public static func makePackageAttributeArguments(_ unexpectedBeforeLocationLabel: UnexpectedNodesSyntax? = nil, locationLabel: TokenSyntax, _ unexpectedBetweenLocationLabelAndLocationColon: UnexpectedNodesSyntax? = nil, locationColon: TokenSyntax, _ unexpectedBetweenLocationColonAndLocation: UnexpectedNodesSyntax? = nil, location: TokenSyntax, _ unexpectedBetweenLocationAndLocReqComma: UnexpectedNodesSyntax? = nil, locReqComma: TokenSyntax?, _ unexpectedBetweenLocReqCommaAndRequirementLabel: UnexpectedNodesSyntax? = nil, requirementLabel: TokenSyntax?, _ unexpectedBetweenRequirementLabelAndRequirementColon: UnexpectedNodesSyntax? = nil, requirementColon: TokenSyntax?, _ unexpectedBetweenRequirementColonAndRequirement: UnexpectedNodesSyntax? = nil, requirement: ExprSyntax?, _ unexpectedBetweenRequirementAndReqProdComma: UnexpectedNodesSyntax? = nil, reqProdComma: TokenSyntax?, _ unexpectedBetweenReqProdCommaAndProductLabel: UnexpectedNodesSyntax? = nil, productLabel: TokenSyntax?, _ unexpectedBetweenProductLabelAndProductColon: UnexpectedNodesSyntax? = nil, productColon: TokenSyntax?, _ unexpectedBetweenProductColonAndProductName: UnexpectedNodesSyntax? = nil, productName: TokenSyntax?, _ unexpectedAfterProductName: UnexpectedNodesSyntax? = nil) -> PackageAttributeArgumentsSyntax {
51855185
let layout: [RawSyntax?] = [
51865186
unexpectedBeforeLocationLabel?.raw,
51875187
locationLabel.raw,
@@ -5190,7 +5190,7 @@ public enum SyntaxFactory {
51905190
unexpectedBetweenLocationColonAndLocation?.raw,
51915191
location.raw,
51925192
unexpectedBetweenLocationAndLocReqComma?.raw,
5193-
locReqComma.raw,
5193+
locReqComma?.raw,
51945194
unexpectedBetweenLocReqCommaAndRequirementLabel?.raw,
51955195
requirementLabel?.raw,
51965196
unexpectedBetweenRequirementLabelAndRequirementColon?.raw,
@@ -5227,7 +5227,7 @@ public enum SyntaxFactory {
52275227
nil,
52285228
RawSyntax.makeMissingToken(kind: TokenKind.stringLiteral(""), arena: arena),
52295229
nil,
5230-
RawSyntax.makeMissingToken(kind: TokenKind.comma, arena: arena),
5230+
nil,
52315231
nil,
52325232
nil,
52335233
nil,

0 commit comments

Comments
 (0)