Skip to content

Commit 2d31a79

Browse files
committed
Rough support for parsing @_package argument list
1 parent 7e523fd commit 2d31a79

File tree

16 files changed

+1005
-10
lines changed

16 files changed

+1005
-10
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/AttributeNodes.swift

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ public let ATTRIBUTE_NODES: [Node] = [
7171
Child(name: "EffectsArguments",
7272
kind: .node(kind: "EffectsArguments")),
7373
Child(name: "DocumentationArguments",
74-
kind: .node(kind: "DocumentationAttributeArguments"))
74+
kind: .node(kind: "DocumentationAttributeArguments")),
75+
Child(name: "PackageAttributeArguments",
76+
kind: .node(kind: "PackageAttributeArguments"))
7577
]),
7678
description: "The arguments of the attribute. In case the attribute takes multiple arguments, they are gather in the appropriate takes first.",
7779
isOptional: true),
@@ -193,6 +195,50 @@ public let ATTRIBUTE_NODES: [Node] = [
193195
isOptional: true)
194196
]),
195197

198+
Node(name: "PackageAttributeArguments",
199+
nameForDiagnostics: "@_package arguemnts",
200+
description: "The arguments for the `@_package` attribute imitating `PackageDescription`",
201+
kind: "Syntax",
202+
children: [
203+
Child(name: "LocationLabel",
204+
kind: .token(choices: [.keyword(text: "id"), .keyword(text: "path"), .keyword(text: "url")]),
205+
description: "The location label."),
206+
Child(name: "LocationColon",
207+
kind: .token(choices: [.token(tokenKind: "ColonToken")])),
208+
Child(name: "Location",
209+
kind: .node(kind: "StringLiteralExpr"),
210+
description: "The location/identifier of package."),
211+
Child(name: "LocReqComma",
212+
kind: .token(choices: [.token(tokenKind: "CommaToken")]),
213+
description: "The comma separating the location and requirement"),
214+
Child(name: "RequirementLabel",
215+
kind: .token(choices: [.keyword(text: "branch"), .keyword(text: "from"), .keyword(text: "revision")]),
216+
description: "The requirement label.",
217+
isOptional: true),
218+
Child(name: "RequirementColon",
219+
kind: .token(choices: [.token(tokenKind: "ColonToken")]),
220+
isOptional: true),
221+
Child(name: "Requirement",
222+
kind: .node(kind: "Expr"),
223+
description: "The version requirement of package.",
224+
isOptional: true),
225+
Child(name: "ReqProdComma",
226+
kind: .token(choices: [.token(tokenKind: "CommaToken")]),
227+
description: "The comma separating the requirement and product name",
228+
isOptional: true),
229+
Child(name: "ProductLabel",
230+
kind: .token(choices: [.keyword(text: "product")]),
231+
description: "The product label.",
232+
isOptional: true),
233+
Child(name: "ProductColon",
234+
kind: .token(choices: [.token(tokenKind: "ColonToken")]),
235+
isOptional: true),
236+
Child(name: "ProductName",
237+
kind: .node(kind: "StringLiteralExpr"),
238+
description: "The exact product name from package",
239+
isOptional: true)
240+
]),
241+
196242
Node(name: "ObjCSelectorPiece",
197243
nameForDiagnostics: "Objective-C selector piece",
198244
description: "A piece of an Objective-C selector. Either consisting of just an identifier for a nullary selector, an identifier and a colon for a labeled argument or just a colon for an unlabeled argument",

Sources/SwiftParser/Attributes.swift

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,7 @@ extension Parser {
248248
}
249249
case ._package:
250250
return parseAttribute(argumentMode: .required) { parser in
251-
// TODO: @_package(...) argument parsing
252-
if !parser.at(.rightParen) {
253-
return .token(parser.consumeAnyToken())
254-
} else {
255-
return .token(parser.missingToken(.identifier))
256-
}
251+
return .packageAttributeArguments(parser.parsePackageAttributeArguments())
257252
}
258253
case ._private:
259254
return parseAttribute(argumentMode: .required) { parser in
@@ -877,6 +872,90 @@ extension Parser {
877872
}
878873
}
879874

875+
extension Parser {
876+
mutating func parsePackageAttribute() -> RawAttributeSyntax {
877+
let (unexpectedBeforeAtSign, atSign) = self.expect(.atSign)
878+
let (unexpectedBeforePackageToken, packageToken) = self.expect(.keyword(._package))
879+
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
880+
let arguments = self.parsePackageAttributeArguments()
881+
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
882+
return RawAttributeSyntax(
883+
unexpectedBeforeAtSign,
884+
atSignToken: atSign,
885+
unexpectedBeforePackageToken,
886+
attributeName: RawTypeSyntax(RawSimpleTypeIdentifierSyntax(name: packageToken, genericArgumentClause: nil, arena: self.arena)),
887+
unexpectedBeforeLeftParen,
888+
leftParen: leftParen,
889+
argument: .packageAttributeArguments(arguments),
890+
unexpectedBeforeRightParen,
891+
rightParen: rightParen,
892+
arena: self.arena
893+
)
894+
}
895+
896+
mutating func parsePackageAttributeArguments() -> RawPackageAttributeArgumentsSyntax {
897+
let (unexpectedBeforeLocationLabel, locationLabel) = self.parseArgumentLabel()
898+
let (unexpectedBeforeLocationColon, locationColon) = self.expect(.colon)
899+
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?
907+
guard self.at(.comma) else {
908+
return RawPackageAttributeArgumentsSyntax(
909+
unexpectedBeforeLocationLabel,
910+
locationLabel: locationLabel,
911+
unexpectedBeforeLocationColon,
912+
locationColon: locationColon,
913+
location: location,
914+
unexpectedBeforeLocReqComma,
915+
locReqComma: locReqComma,
916+
unexpectedBeforeRequirementLabel,
917+
requirementLabel: requirementLabel,
918+
unexpectedBeforeRequirementColon,
919+
requirementColon: requirementColon,
920+
requirement: requirement,
921+
reqProdComma: nil,
922+
productLabel: nil,
923+
productColon: nil,
924+
productName: nil,
925+
self.remainingTokensIfMaximumNestingLevelReached(),
926+
arena: self.arena
927+
)
928+
}
929+
let (unexpectedBeforeReqProdComma, reqProdComma) = self.expect(.comma)
930+
let (unexpectedBeforeProductLabel, productLabel) = self.parseArgumentLabel()
931+
let (unexpectedBeforeProductColon, productColon) = self.expect(.colon)
932+
let productName = self.parseStringLiteral()
933+
return RawPackageAttributeArgumentsSyntax(
934+
unexpectedBeforeLocationLabel,
935+
locationLabel: locationLabel,
936+
unexpectedBeforeLocationColon,
937+
locationColon: locationColon,
938+
location: location,
939+
unexpectedBeforeLocReqComma,
940+
locReqComma: locReqComma,
941+
unexpectedBeforeRequirementLabel,
942+
requirementLabel: requirementLabel,
943+
unexpectedBeforeRequirementColon,
944+
requirementColon: requirementColon,
945+
requirement: requirement,
946+
unexpectedBeforeReqProdComma,
947+
reqProdComma: reqProdComma,
948+
unexpectedBeforeProductLabel,
949+
productLabel: productLabel,
950+
unexpectedBeforeProductColon,
951+
productColon: productColon,
952+
productName: productName,
953+
self.remainingTokensIfMaximumNestingLevelReached(),
954+
arena: self.arena
955+
)
956+
}
957+
}
958+
880959
extension Parser {
881960
mutating func parseConventionArguments() -> RawAttributeSyntax.Argument {
882961
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
@@ -352,6 +352,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
352352
- <doc:SwiftSyntax/TargetFunctionEntrySyntax>
353353
- <doc:SwiftSyntax/DeclNameSyntax>
354354
- <doc:SwiftSyntax/ImplementsAttributeArgumentsSyntax>
355+
- <doc:SwiftSyntax/PackageAttributeArgumentsSyntax>
355356
- <doc:SwiftSyntax/ObjCSelectorPieceSyntax>
356357
- <doc:SwiftSyntax/ObjCSelectorSyntax>
357358
- <doc:SwiftSyntax/DifferentiableAttributeArgumentsSyntax>

Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxNodes.swift

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10976,9 +10976,10 @@ public struct RawAttributeSyntax: RawSyntaxNodeProtocol {
1097610976
case `unavailableFromAsyncArguments`(RawUnavailableFromAsyncArgumentsSyntax)
1097710977
case `effectsArguments`(RawEffectsArgumentsSyntax)
1097810978
case `documentationArguments`(RawDocumentationAttributeArgumentsSyntax)
10979+
case `packageAttributeArguments`(RawPackageAttributeArgumentsSyntax)
1097910980

1098010981
public static func isKindOf(_ raw: RawSyntax) -> Bool {
10981-
return RawTupleExprElementListSyntax.isKindOf(raw) || RawTokenSyntax.isKindOf(raw) || RawStringLiteralExprSyntax.isKindOf(raw) || RawAvailabilitySpecListSyntax.isKindOf(raw) || RawSpecializeAttributeSpecListSyntax.isKindOf(raw) || RawObjCSelectorSyntax.isKindOf(raw) || RawImplementsAttributeArgumentsSyntax.isKindOf(raw) || RawDifferentiableAttributeArgumentsSyntax.isKindOf(raw) || RawDerivativeRegistrationAttributeArgumentsSyntax.isKindOf(raw) || RawBackDeployedAttributeSpecListSyntax.isKindOf(raw) || RawConventionAttributeArgumentsSyntax.isKindOf(raw) || RawConventionWitnessMethodAttributeArgumentsSyntax.isKindOf(raw) || RawOpaqueReturnTypeOfAttributeArgumentsSyntax.isKindOf(raw) || RawExposeAttributeArgumentsSyntax.isKindOf(raw) || RawOriginallyDefinedInArgumentsSyntax.isKindOf(raw) || RawUnderscorePrivateAttributeArgumentsSyntax.isKindOf(raw) || RawDynamicReplacementArgumentsSyntax.isKindOf(raw) || RawUnavailableFromAsyncArgumentsSyntax.isKindOf(raw) || RawEffectsArgumentsSyntax.isKindOf(raw) || RawDocumentationAttributeArgumentsSyntax.isKindOf(raw)
10982+
return RawTupleExprElementListSyntax.isKindOf(raw) || RawTokenSyntax.isKindOf(raw) || RawStringLiteralExprSyntax.isKindOf(raw) || RawAvailabilitySpecListSyntax.isKindOf(raw) || RawSpecializeAttributeSpecListSyntax.isKindOf(raw) || RawObjCSelectorSyntax.isKindOf(raw) || RawImplementsAttributeArgumentsSyntax.isKindOf(raw) || RawDifferentiableAttributeArgumentsSyntax.isKindOf(raw) || RawDerivativeRegistrationAttributeArgumentsSyntax.isKindOf(raw) || RawBackDeployedAttributeSpecListSyntax.isKindOf(raw) || RawConventionAttributeArgumentsSyntax.isKindOf(raw) || RawConventionWitnessMethodAttributeArgumentsSyntax.isKindOf(raw) || RawOpaqueReturnTypeOfAttributeArgumentsSyntax.isKindOf(raw) || RawExposeAttributeArgumentsSyntax.isKindOf(raw) || RawOriginallyDefinedInArgumentsSyntax.isKindOf(raw) || RawUnderscorePrivateAttributeArgumentsSyntax.isKindOf(raw) || RawDynamicReplacementArgumentsSyntax.isKindOf(raw) || RawUnavailableFromAsyncArgumentsSyntax.isKindOf(raw) || RawEffectsArgumentsSyntax.isKindOf(raw) || RawDocumentationAttributeArgumentsSyntax.isKindOf(raw) || RawPackageAttributeArgumentsSyntax.isKindOf(raw)
1098210983
}
1098310984

1098410985
public var raw: RawSyntax {
@@ -11003,6 +11004,7 @@ public struct RawAttributeSyntax: RawSyntaxNodeProtocol {
1100311004
case .unavailableFromAsyncArguments(let node): return node.raw
1100411005
case .effectsArguments(let node): return node.raw
1100511006
case .documentationArguments(let node): return node.raw
11007+
case .packageAttributeArguments(let node): return node.raw
1100611008
}
1100711009
}
1100811010

@@ -11087,6 +11089,10 @@ public struct RawAttributeSyntax: RawSyntaxNodeProtocol {
1108711089
self = .documentationArguments(node)
1108811090
return
1108911091
}
11092+
if let node = RawPackageAttributeArgumentsSyntax(other) {
11093+
self = .packageAttributeArguments(node)
11094+
return
11095+
}
1109011096
return nil
1109111097
}
1109211098
}
@@ -11709,6 +11715,156 @@ public struct RawImplementsAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
1170911715
}
1171011716
}
1171111717

11718+
@_spi(RawSyntax)
11719+
public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
11720+
11721+
@_spi(RawSyntax)
11722+
public var layoutView: RawSyntaxLayoutView {
11723+
return raw.layoutView!
11724+
}
11725+
11726+
public static func isKindOf(_ raw: RawSyntax) -> Bool {
11727+
return raw.kind == .packageAttributeArguments
11728+
}
11729+
11730+
public var raw: RawSyntax
11731+
init(raw: RawSyntax) {
11732+
assert(Self.isKindOf(raw))
11733+
self.raw = raw
11734+
}
11735+
11736+
public init?<Node: RawSyntaxNodeProtocol>(_ other: Node) {
11737+
guard Self.isKindOf(other.raw) else { return nil }
11738+
self.init(raw: other.raw)
11739+
}
11740+
11741+
public init(
11742+
_ unexpectedBeforeLocationLabel: RawUnexpectedNodesSyntax? = nil,
11743+
locationLabel: RawTokenSyntax,
11744+
_ unexpectedBetweenLocationLabelAndLocationColon: RawUnexpectedNodesSyntax? = nil,
11745+
locationColon: RawTokenSyntax,
11746+
_ unexpectedBetweenLocationColonAndLocation: RawUnexpectedNodesSyntax? = nil,
11747+
location: RawStringLiteralExprSyntax,
11748+
_ unexpectedBetweenLocationAndLocReqComma: RawUnexpectedNodesSyntax? = nil,
11749+
locReqComma: RawTokenSyntax,
11750+
_ unexpectedBetweenLocReqCommaAndRequirementLabel: RawUnexpectedNodesSyntax? = nil,
11751+
requirementLabel: RawTokenSyntax?,
11752+
_ unexpectedBetweenRequirementLabelAndRequirementColon: RawUnexpectedNodesSyntax? = nil,
11753+
requirementColon: RawTokenSyntax?,
11754+
_ unexpectedBetweenRequirementColonAndRequirement: RawUnexpectedNodesSyntax? = nil,
11755+
requirement: RawExprSyntax?,
11756+
_ unexpectedBetweenRequirementAndReqProdComma: RawUnexpectedNodesSyntax? = nil,
11757+
reqProdComma: RawTokenSyntax?,
11758+
_ unexpectedBetweenReqProdCommaAndProductLabel: RawUnexpectedNodesSyntax? = nil,
11759+
productLabel: RawTokenSyntax?,
11760+
_ unexpectedBetweenProductLabelAndProductColon: RawUnexpectedNodesSyntax? = nil,
11761+
productColon: RawTokenSyntax?,
11762+
_ unexpectedBetweenProductColonAndProductName: RawUnexpectedNodesSyntax? = nil,
11763+
productName: RawStringLiteralExprSyntax?,
11764+
_ unexpectedAfterProductName: RawUnexpectedNodesSyntax? = nil,
11765+
arena: __shared SyntaxArena
11766+
) {
11767+
let raw = RawSyntax.makeLayout(
11768+
kind: .packageAttributeArguments, uninitializedCount: 23, arena: arena) { layout in
11769+
layout.initialize(repeating: nil)
11770+
layout[0] = unexpectedBeforeLocationLabel?.raw
11771+
layout[1] = locationLabel.raw
11772+
layout[2] = unexpectedBetweenLocationLabelAndLocationColon?.raw
11773+
layout[3] = locationColon.raw
11774+
layout[4] = unexpectedBetweenLocationColonAndLocation?.raw
11775+
layout[5] = location.raw
11776+
layout[6] = unexpectedBetweenLocationAndLocReqComma?.raw
11777+
layout[7] = locReqComma.raw
11778+
layout[8] = unexpectedBetweenLocReqCommaAndRequirementLabel?.raw
11779+
layout[9] = requirementLabel?.raw
11780+
layout[10] = unexpectedBetweenRequirementLabelAndRequirementColon?.raw
11781+
layout[11] = requirementColon?.raw
11782+
layout[12] = unexpectedBetweenRequirementColonAndRequirement?.raw
11783+
layout[13] = requirement?.raw
11784+
layout[14] = unexpectedBetweenRequirementAndReqProdComma?.raw
11785+
layout[15] = reqProdComma?.raw
11786+
layout[16] = unexpectedBetweenReqProdCommaAndProductLabel?.raw
11787+
layout[17] = productLabel?.raw
11788+
layout[18] = unexpectedBetweenProductLabelAndProductColon?.raw
11789+
layout[19] = productColon?.raw
11790+
layout[20] = unexpectedBetweenProductColonAndProductName?.raw
11791+
layout[21] = productName?.raw
11792+
layout[22] = unexpectedAfterProductName?.raw
11793+
}
11794+
self.init(raw: raw)
11795+
}
11796+
11797+
public var unexpectedBeforeLocationLabel: RawUnexpectedNodesSyntax? {
11798+
layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:))
11799+
}
11800+
public var locationLabel: RawTokenSyntax {
11801+
layoutView.children[1].map(RawTokenSyntax.init(raw:))!
11802+
}
11803+
public var unexpectedBetweenLocationLabelAndLocationColon: RawUnexpectedNodesSyntax? {
11804+
layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:))
11805+
}
11806+
public var locationColon: RawTokenSyntax {
11807+
layoutView.children[3].map(RawTokenSyntax.init(raw:))!
11808+
}
11809+
public var unexpectedBetweenLocationColonAndLocation: RawUnexpectedNodesSyntax? {
11810+
layoutView.children[4].map(RawUnexpectedNodesSyntax.init(raw:))
11811+
}
11812+
public var location: RawStringLiteralExprSyntax {
11813+
layoutView.children[5].map(RawStringLiteralExprSyntax.init(raw:))!
11814+
}
11815+
public var unexpectedBetweenLocationAndLocReqComma: RawUnexpectedNodesSyntax? {
11816+
layoutView.children[6].map(RawUnexpectedNodesSyntax.init(raw:))
11817+
}
11818+
public var locReqComma: RawTokenSyntax {
11819+
layoutView.children[7].map(RawTokenSyntax.init(raw:))!
11820+
}
11821+
public var unexpectedBetweenLocReqCommaAndRequirementLabel: RawUnexpectedNodesSyntax? {
11822+
layoutView.children[8].map(RawUnexpectedNodesSyntax.init(raw:))
11823+
}
11824+
public var requirementLabel: RawTokenSyntax? {
11825+
layoutView.children[9].map(RawTokenSyntax.init(raw:))
11826+
}
11827+
public var unexpectedBetweenRequirementLabelAndRequirementColon: RawUnexpectedNodesSyntax? {
11828+
layoutView.children[10].map(RawUnexpectedNodesSyntax.init(raw:))
11829+
}
11830+
public var requirementColon: RawTokenSyntax? {
11831+
layoutView.children[11].map(RawTokenSyntax.init(raw:))
11832+
}
11833+
public var unexpectedBetweenRequirementColonAndRequirement: RawUnexpectedNodesSyntax? {
11834+
layoutView.children[12].map(RawUnexpectedNodesSyntax.init(raw:))
11835+
}
11836+
public var requirement: RawExprSyntax? {
11837+
layoutView.children[13].map(RawExprSyntax.init(raw:))
11838+
}
11839+
public var unexpectedBetweenRequirementAndReqProdComma: RawUnexpectedNodesSyntax? {
11840+
layoutView.children[14].map(RawUnexpectedNodesSyntax.init(raw:))
11841+
}
11842+
public var reqProdComma: RawTokenSyntax? {
11843+
layoutView.children[15].map(RawTokenSyntax.init(raw:))
11844+
}
11845+
public var unexpectedBetweenReqProdCommaAndProductLabel: RawUnexpectedNodesSyntax? {
11846+
layoutView.children[16].map(RawUnexpectedNodesSyntax.init(raw:))
11847+
}
11848+
public var productLabel: RawTokenSyntax? {
11849+
layoutView.children[17].map(RawTokenSyntax.init(raw:))
11850+
}
11851+
public var unexpectedBetweenProductLabelAndProductColon: RawUnexpectedNodesSyntax? {
11852+
layoutView.children[18].map(RawUnexpectedNodesSyntax.init(raw:))
11853+
}
11854+
public var productColon: RawTokenSyntax? {
11855+
layoutView.children[19].map(RawTokenSyntax.init(raw:))
11856+
}
11857+
public var unexpectedBetweenProductColonAndProductName: RawUnexpectedNodesSyntax? {
11858+
layoutView.children[20].map(RawUnexpectedNodesSyntax.init(raw:))
11859+
}
11860+
public var productName: RawStringLiteralExprSyntax? {
11861+
layoutView.children[21].map(RawStringLiteralExprSyntax.init(raw:))
11862+
}
11863+
public var unexpectedAfterProductName: RawUnexpectedNodesSyntax? {
11864+
layoutView.children[22].map(RawUnexpectedNodesSyntax.init(raw:))
11865+
}
11866+
}
11867+
1171211868
@_spi(RawSyntax)
1171311869
public struct RawObjCSelectorPieceSyntax: RawSyntaxNodeProtocol {
1171411870

0 commit comments

Comments
 (0)