Skip to content

Commit 7f08587

Browse files
committed
Rough support for parsing @_package argument list
1 parent 5f9140d commit 7f08587

File tree

16 files changed

+1335
-11
lines changed

16 files changed

+1335
-11
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/AttributeNodes.swift

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ public let ATTRIBUTE_NODES: [Node] = [
7878
Child(name: "EffectsArguments",
7979
kind: "EffectsArguments"),
8080
Child(name: "DocumentationArguments",
81-
kind: "DocumentationAttributeArguments")
81+
kind: "DocumentationAttributeArguments"),
82+
Child(name: "PackageAttributeArguments",
83+
kind: "PackageAttributeArguments")
8284
]),
8385
Child(name: "RightParen",
8486
kind: "RightParenToken",
@@ -236,6 +238,93 @@ public let ATTRIBUTE_NODES: [Node] = [
236238
isOptional: true)
237239
]),
238240

241+
Node(name: "PackageAttributeArguments",
242+
nameForDiagnostics: "@_package arguemnts",
243+
description: "The arguments for the `@_package` attribute imitating `PackageDescription`",
244+
kind: "Syntax",
245+
children: [
246+
Child(name: "LocationLabel",
247+
kind: "IdentifierToken",
248+
description: "The location label.",
249+
tokenChoices: [
250+
"Identifier"
251+
],
252+
textChoices: [
253+
"url",
254+
"path",
255+
"id"
256+
]),
257+
Child(name: "LocationColon",
258+
kind: "ColonToken",
259+
tokenChoices: [
260+
"Colon"
261+
]),
262+
Child(name: "Location",
263+
kind: "StringLiteralToken",
264+
description: "The location/identifier of package.",
265+
tokenChoices: [
266+
"StringLiteral"
267+
]),
268+
Child(name: "LocReqComma",
269+
kind: "CommaToken",
270+
description: "The comma separating the location and requirement",
271+
tokenChoices: [
272+
"Comma"
273+
]),
274+
Child(name: "RequirementLabel",
275+
kind: "IdentifierToken",
276+
description: "The requirement label.",
277+
isOptional: true,
278+
tokenChoices: [
279+
"Identifier"
280+
],
281+
textChoices: [
282+
"from",
283+
"branch",
284+
"revision"
285+
]),
286+
Child(name: "RequirementColon",
287+
kind: "ColonToken",
288+
isOptional: true,
289+
tokenChoices: [
290+
"Colon"
291+
]),
292+
Child(name: "Requirement",
293+
kind: "Expr",
294+
description: "The version requirement of package.",
295+
isOptional: true),
296+
Child(name: "ReqProdComma",
297+
kind: "CommaToken",
298+
description: "The comma separating the requirement and product name",
299+
isOptional: true,
300+
tokenChoices: [
301+
"Comma"
302+
]),
303+
Child(name: "ProductLabel",
304+
kind: "IdentifierToken",
305+
description: "The product label.",
306+
isOptional: true,
307+
tokenChoices: [
308+
"Identifier"
309+
],
310+
textChoices: [
311+
"product"
312+
]),
313+
Child(name: "ProductColon",
314+
kind: "ColonToken",
315+
isOptional: true,
316+
tokenChoices: [
317+
"Colon"
318+
]),
319+
Child(name: "ProductName",
320+
kind: "StringLiteralToken",
321+
description: "The exact product name from package",
322+
isOptional: true,
323+
tokenChoices: [
324+
"StringLiteral"
325+
])
326+
]),
327+
239328
Node(name: "ObjCSelectorPiece",
240329
nameForDiagnostics: "Objective-C selector piece",
241330
description: "A piece of an Objective-C selector. Either consisting of just anidentifier for a nullary selector, an identifier and a colon for alabeled argument or just a colon for an unlabeled argument",

Sources/SwiftParser/Attributes.swift

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,7 @@ extension Parser {
243243
}
244244
case ._package:
245245
return parseAttribute(argumentMode: .required) { parser in
246-
// TODO: @_package(...) argument parsing
247-
if !parser.at(.rightParen) {
248-
return .token(parser.consumeAnyToken())
249-
} else {
250-
return .token(parser.missingToken(.identifier))
251-
}
246+
return .packageAttributeArguments(parser.parsePackageAttributeArguments())
252247
}
253248
case ._private:
254249
return parseAttribute(argumentMode: .required) { parser in
@@ -881,6 +876,93 @@ extension Parser {
881876
}
882877
}
883878

879+
extension Parser {
880+
mutating func parsePackageAttribute() -> RawAttributeSyntax {
881+
let (unexpectedBeforeAtSign, atSign) = self.expect(.atSign)
882+
let (unexpectedBeforePackageToken, packageToken) = self.expect(.keyword(._package))
883+
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
884+
let arguments = self.parsePackageAttributeArguments()
885+
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
886+
return RawAttributeSyntax(
887+
unexpectedBeforeAtSign,
888+
atSignToken: atSign,
889+
unexpectedBeforePackageToken,
890+
attributeName: RawTypeSyntax(RawSimpleTypeIdentifierSyntax(name: packageToken, genericArgumentClause: nil, arena: self.arena)),
891+
unexpectedBeforeLeftParen,
892+
leftParen: leftParen,
893+
argument: .packageAttributeArguments(arguments),
894+
unexpectedBeforeRightParen,
895+
rightParen: rightParen,
896+
arena: self.arena
897+
)
898+
}
899+
900+
mutating func parsePackageAttributeArguments() -> RawPackageAttributeArgumentsSyntax {
901+
let (unexpectedBeforeLocationLabel, locationLabel) = self.parseArgumentLabel()
902+
let (unexpectedBeforeLocationColon, locationColon) = self.expect(.colon)
903+
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?
911+
guard self.at(.comma) else {
912+
return RawPackageAttributeArgumentsSyntax(
913+
unexpectedBeforeLocationLabel,
914+
locationLabel: locationLabel,
915+
unexpectedBeforeLocationColon,
916+
locationColon: locationColon,
917+
unexpectedBeforeLocation,
918+
location: location,
919+
unexpectedBeforeLocReqComma,
920+
locReqComma: locReqComma,
921+
unexpectedBeforeRequirementLabel,
922+
requirementLabel: requirementLabel,
923+
unexpectedBeforeRequirementColon,
924+
requirementColon: requirementColon,
925+
requirement: requirement,
926+
reqProdComma: nil,
927+
productLabel: nil,
928+
productColon: nil,
929+
productName: nil,
930+
self.remainingTokensIfMaximumNestingLevelReached(),
931+
arena: self.arena
932+
)
933+
}
934+
let (unexpectedBeforeReqProdComma, reqProdComma) = self.expect(.comma)
935+
let (unexpectedBeforeProductLabel, productLabel) = self.parseArgumentLabel()
936+
let (unexpectedBeforeProductColon, productColon) = self.expect(.colon)
937+
let (unexpectedBeforeProductName, productName) = self.expect(.stringLiteral)
938+
return RawPackageAttributeArgumentsSyntax(
939+
unexpectedBeforeLocationLabel,
940+
locationLabel: locationLabel,
941+
unexpectedBeforeLocationColon,
942+
locationColon: locationColon,
943+
unexpectedBeforeLocation,
944+
location: location,
945+
unexpectedBeforeLocReqComma,
946+
locReqComma: locReqComma,
947+
unexpectedBeforeRequirementLabel,
948+
requirementLabel: requirementLabel,
949+
unexpectedBeforeRequirementColon,
950+
requirementColon: requirementColon,
951+
requirement: requirement,
952+
unexpectedBeforeReqProdComma,
953+
reqProdComma: reqProdComma,
954+
unexpectedBeforeProductLabel,
955+
productLabel: productLabel,
956+
unexpectedBeforeProductColon,
957+
productColon: productColon,
958+
unexpectedBeforeProductName,
959+
productName: productName,
960+
self.remainingTokensIfMaximumNestingLevelReached(),
961+
arena: self.arena
962+
)
963+
}
964+
}
965+
884966
extension Parser {
885967
mutating func parseConventionArguments() -> RawAttributeSyntax.Argument {
886968
if let witnessMethod = self.consume(if: .keyword(.witness_method)) {

Sources/SwiftSyntax/Documentation.docc/gyb_generated/SwiftSyntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
347347
- <doc:SwiftSyntax/TargetFunctionEntrySyntax>
348348
- <doc:SwiftSyntax/DeclNameSyntax>
349349
- <doc:SwiftSyntax/ImplementsAttributeArgumentsSyntax>
350+
- <doc:SwiftSyntax/PackageAttributeArgumentsSyntax>
350351
- <doc:SwiftSyntax/ObjCSelectorPieceSyntax>
351352
- <doc:SwiftSyntax/ObjCSelectorSyntax>
352353
- <doc:SwiftSyntax/DifferentiableAttributeArgumentsSyntax>

0 commit comments

Comments
 (0)