Skip to content

Commit 310c9b8

Browse files
committed
Simplify @_package parsing logic
1 parent 0f4cfba commit 310c9b8

File tree

7 files changed

+105
-180
lines changed

7 files changed

+105
-180
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/AttributeNodes.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ public let ATTRIBUTE_NODES: [Node] = [
208208
Child(name: "Location",
209209
kind: .node(kind: "StringLiteralExpr"),
210210
description: "The location/identifier of package."),
211-
Child(name: "LocReqComma",
211+
Child(name: "RequirementComma",
212212
kind: .token(choices: [.token(tokenKind: "CommaToken")]),
213-
description: "The comma separating the location and requirement",
213+
description: "The comma before the package requirement, if it exists.",
214214
isOptional: true),
215215
Child(name: "RequirementLabel",
216216
kind: .token(choices: [.keyword(text: "branch"), .keyword(text: "from"), .keyword(text: "revision")]),
@@ -223,9 +223,9 @@ public let ATTRIBUTE_NODES: [Node] = [
223223
kind: .node(kind: "Expr"),
224224
description: "The version requirement of package.",
225225
isOptional: true),
226-
Child(name: "ReqProdComma",
226+
Child(name: "ProductComma",
227227
kind: .token(choices: [.token(tokenKind: "CommaToken")]),
228-
description: "The comma separating the requirement and product name",
228+
description: "The comma before the package product, if it exists.",
229229
isOptional: true),
230230
Child(name: "ProductLabel",
231231
kind: .token(choices: [.keyword(text: "product")]),

Sources/SwiftParser/Attributes.swift

Lines changed: 24 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -873,143 +873,66 @@ extension Parser {
873873
}
874874

875875
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-
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-
942876
mutating func parsePackageAttributeArguments() -> RawPackageAttributeArgumentsSyntax {
943877
// Parsing package location.
944-
let locationLabel = self.consume(ifAnyIn: PackageLocationLabel.self) ?? missingToken(.identifier)
878+
let (unexpectedBeforeLocationLabel, locationLabel) = self.expectAny([.keyword(.id), .keyword(.path), .keyword(.url)], default: .keyword(.id))
945879
let (unexpectedBeforeLocationColon, locationColon) = self.expect(.colon)
946880
let location = self.parseStringLiteral()
947881
// Parsing package requirement.
948-
let (unexpectedBeforeLocReqComma, locReqComma): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
882+
let (unexpectedBeforeRequirementComma, requirementComma): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
949883
let (unexpectedBeforeRequirementLabel, requirementLabel): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
950884
let (unexpectedBeforeRequirementColon, requirementColon): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
951885
let requirement: RawExprSyntax?
952886
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)
887+
(unexpectedBeforeRequirementComma, requirementComma) = self.expect(.comma)
888+
if self.at(any: [.keyword(.from), .keyword(.branch), .keyword(.revision)]) {
889+
(unexpectedBeforeRequirementLabel, requirementLabel) = self.expectAny([.keyword(.from), .keyword(.branch), .keyword(.revision)], default: .keyword(.from))
956890
(unexpectedBeforeRequirementColon, requirementColon) = self.expect(.colon)
957891
} else {
958892
(unexpectedBeforeRequirementLabel, requirementLabel) = (nil, nil)
959893
(unexpectedBeforeRequirementColon, requirementColon) = (nil, nil)
960894
}
961895
requirement = self.parseExpression()
962896
} else {
963-
(unexpectedBeforeLocReqComma, locReqComma) = (nil, nil)
897+
(unexpectedBeforeRequirementComma, requirementComma) = (nil, nil)
964898
(unexpectedBeforeRequirementLabel, requirementLabel) = (nil, nil)
965899
(unexpectedBeforeRequirementColon, requirementColon) = (nil, nil)
966900
requirement = nil
967901
}
968-
guard self.at(.comma) else {
969-
return RawPackageAttributeArgumentsSyntax(
970-
locationLabel: locationLabel,
971-
unexpectedBeforeLocationColon,
972-
locationColon: locationColon,
973-
location: location,
974-
unexpectedBeforeLocReqComma,
975-
locReqComma: locReqComma,
976-
unexpectedBeforeRequirementLabel,
977-
requirementLabel: requirementLabel,
978-
unexpectedBeforeRequirementColon,
979-
requirementColon: requirementColon,
980-
requirement: requirement,
981-
reqProdComma: nil,
982-
productLabel: nil,
983-
productColon: nil,
984-
productName: nil,
985-
self.remainingTokensIfMaximumNestingLevelReached(),
986-
arena: self.arena
987-
)
902+
// Parsing package requirement.
903+
let productComma = self.consume(if: .comma)
904+
let (unexpectedBeforeProductLabel, productLabel): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
905+
let (unexpectedBeforeProductColon, productColon): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
906+
let productName: RawStringLiteralExprSyntax?
907+
if productComma != nil {
908+
(unexpectedBeforeProductLabel, productLabel) = self.expect(.keyword(.product))
909+
(unexpectedBeforeProductColon, productColon) = self.expect(.colon)
910+
productName = self.parseStringLiteral()
911+
} else {
912+
(unexpectedBeforeProductLabel, productLabel) = (nil, nil)
913+
(unexpectedBeforeProductColon, productColon) = (nil, nil)
914+
productName = nil
988915
}
989-
let (unexpectedBeforeReqProdComma, reqProdComma) = self.expect(.comma)
990-
let (unexpectedBeforeProductLabel, productLabel) = self.expect(.keyword(.product))
991-
let (unexpectedBeforeProductColon, productColon) = self.expect(.colon)
992-
let productName = self.parseStringLiteral()
916+
// Returning @_package argument list
993917
return RawPackageAttributeArgumentsSyntax(
918+
unexpectedBeforeLocationLabel,
994919
locationLabel: locationLabel,
995920
unexpectedBeforeLocationColon,
996921
locationColon: locationColon,
997922
location: location,
998-
unexpectedBeforeLocReqComma,
999-
locReqComma: locReqComma,
923+
unexpectedBeforeRequirementComma,
924+
requirementComma: requirementComma,
1000925
unexpectedBeforeRequirementLabel,
1001926
requirementLabel: requirementLabel,
1002927
unexpectedBeforeRequirementColon,
1003928
requirementColon: requirementColon,
1004929
requirement: requirement,
1005-
unexpectedBeforeReqProdComma,
1006-
reqProdComma: reqProdComma,
930+
productComma: productComma,
1007931
unexpectedBeforeProductLabel,
1008932
productLabel: productLabel,
1009933
unexpectedBeforeProductColon,
1010934
productColon: productColon,
1011935
productName: productName,
1012-
self.remainingTokensIfMaximumNestingLevelReached(),
1013936
arena: self.arena
1014937
)
1015938
}

Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxNodes.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11745,17 +11745,17 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
1174511745
locationColon: RawTokenSyntax,
1174611746
_ unexpectedBetweenLocationColonAndLocation: RawUnexpectedNodesSyntax? = nil,
1174711747
location: RawStringLiteralExprSyntax,
11748-
_ unexpectedBetweenLocationAndLocReqComma: RawUnexpectedNodesSyntax? = nil,
11749-
locReqComma: RawTokenSyntax?,
11750-
_ unexpectedBetweenLocReqCommaAndRequirementLabel: RawUnexpectedNodesSyntax? = nil,
11748+
_ unexpectedBetweenLocationAndRequirementComma: RawUnexpectedNodesSyntax? = nil,
11749+
requirementComma: RawTokenSyntax?,
11750+
_ unexpectedBetweenRequirementCommaAndRequirementLabel: RawUnexpectedNodesSyntax? = nil,
1175111751
requirementLabel: RawTokenSyntax?,
1175211752
_ unexpectedBetweenRequirementLabelAndRequirementColon: RawUnexpectedNodesSyntax? = nil,
1175311753
requirementColon: RawTokenSyntax?,
1175411754
_ unexpectedBetweenRequirementColonAndRequirement: RawUnexpectedNodesSyntax? = nil,
1175511755
requirement: RawExprSyntax?,
11756-
_ unexpectedBetweenRequirementAndReqProdComma: RawUnexpectedNodesSyntax? = nil,
11757-
reqProdComma: RawTokenSyntax?,
11758-
_ unexpectedBetweenReqProdCommaAndProductLabel: RawUnexpectedNodesSyntax? = nil,
11756+
_ unexpectedBetweenRequirementAndProductComma: RawUnexpectedNodesSyntax? = nil,
11757+
productComma: RawTokenSyntax?,
11758+
_ unexpectedBetweenProductCommaAndProductLabel: RawUnexpectedNodesSyntax? = nil,
1175911759
productLabel: RawTokenSyntax?,
1176011760
_ unexpectedBetweenProductLabelAndProductColon: RawUnexpectedNodesSyntax? = nil,
1176111761
productColon: RawTokenSyntax?,
@@ -11773,17 +11773,17 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
1177311773
layout[3] = locationColon.raw
1177411774
layout[4] = unexpectedBetweenLocationColonAndLocation?.raw
1177511775
layout[5] = location.raw
11776-
layout[6] = unexpectedBetweenLocationAndLocReqComma?.raw
11777-
layout[7] = locReqComma?.raw
11778-
layout[8] = unexpectedBetweenLocReqCommaAndRequirementLabel?.raw
11776+
layout[6] = unexpectedBetweenLocationAndRequirementComma?.raw
11777+
layout[7] = requirementComma?.raw
11778+
layout[8] = unexpectedBetweenRequirementCommaAndRequirementLabel?.raw
1177911779
layout[9] = requirementLabel?.raw
1178011780
layout[10] = unexpectedBetweenRequirementLabelAndRequirementColon?.raw
1178111781
layout[11] = requirementColon?.raw
1178211782
layout[12] = unexpectedBetweenRequirementColonAndRequirement?.raw
1178311783
layout[13] = requirement?.raw
11784-
layout[14] = unexpectedBetweenRequirementAndReqProdComma?.raw
11785-
layout[15] = reqProdComma?.raw
11786-
layout[16] = unexpectedBetweenReqProdCommaAndProductLabel?.raw
11784+
layout[14] = unexpectedBetweenRequirementAndProductComma?.raw
11785+
layout[15] = productComma?.raw
11786+
layout[16] = unexpectedBetweenProductCommaAndProductLabel?.raw
1178711787
layout[17] = productLabel?.raw
1178811788
layout[18] = unexpectedBetweenProductLabelAndProductColon?.raw
1178911789
layout[19] = productColon?.raw
@@ -11812,13 +11812,13 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
1181211812
public var location: RawStringLiteralExprSyntax {
1181311813
layoutView.children[5].map(RawStringLiteralExprSyntax.init(raw:))!
1181411814
}
11815-
public var unexpectedBetweenLocationAndLocReqComma: RawUnexpectedNodesSyntax? {
11815+
public var unexpectedBetweenLocationAndRequirementComma: RawUnexpectedNodesSyntax? {
1181611816
layoutView.children[6].map(RawUnexpectedNodesSyntax.init(raw:))
1181711817
}
11818-
public var locReqComma: RawTokenSyntax? {
11818+
public var requirementComma: RawTokenSyntax? {
1181911819
layoutView.children[7].map(RawTokenSyntax.init(raw:))
1182011820
}
11821-
public var unexpectedBetweenLocReqCommaAndRequirementLabel: RawUnexpectedNodesSyntax? {
11821+
public var unexpectedBetweenRequirementCommaAndRequirementLabel: RawUnexpectedNodesSyntax? {
1182211822
layoutView.children[8].map(RawUnexpectedNodesSyntax.init(raw:))
1182311823
}
1182411824
public var requirementLabel: RawTokenSyntax? {
@@ -11836,13 +11836,13 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
1183611836
public var requirement: RawExprSyntax? {
1183711837
layoutView.children[13].map(RawExprSyntax.init(raw:))
1183811838
}
11839-
public var unexpectedBetweenRequirementAndReqProdComma: RawUnexpectedNodesSyntax? {
11839+
public var unexpectedBetweenRequirementAndProductComma: RawUnexpectedNodesSyntax? {
1184011840
layoutView.children[14].map(RawUnexpectedNodesSyntax.init(raw:))
1184111841
}
11842-
public var reqProdComma: RawTokenSyntax? {
11842+
public var productComma: RawTokenSyntax? {
1184311843
layoutView.children[15].map(RawTokenSyntax.init(raw:))
1184411844
}
11845-
public var unexpectedBetweenReqProdCommaAndProductLabel: RawUnexpectedNodesSyntax? {
11845+
public var unexpectedBetweenProductCommaAndProductLabel: RawUnexpectedNodesSyntax? {
1184611846
layoutView.children[16].map(RawUnexpectedNodesSyntax.init(raw:))
1184711847
}
1184811848
public var productLabel: RawTokenSyntax? {

Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5456,25 +5456,25 @@ public enum SyntaxFactory {
54565456
}
54575457
}
54585458
@available(*, deprecated, message: "Use initializer on PackageAttributeArgumentsSyntax")
5459-
public static func makePackageAttributeArguments(_ unexpectedBeforeLocationLabel: UnexpectedNodesSyntax? = nil, locationLabel: TokenSyntax, _ unexpectedBetweenLocationLabelAndLocationColon: UnexpectedNodesSyntax? = nil, locationColon: TokenSyntax, _ unexpectedBetweenLocationColonAndLocation: UnexpectedNodesSyntax? = nil, location: StringLiteralExprSyntax, _ 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: StringLiteralExprSyntax?, _ unexpectedAfterProductName: UnexpectedNodesSyntax? = nil) -> PackageAttributeArgumentsSyntax {
5459+
public static func makePackageAttributeArguments(_ unexpectedBeforeLocationLabel: UnexpectedNodesSyntax? = nil, locationLabel: TokenSyntax, _ unexpectedBetweenLocationLabelAndLocationColon: UnexpectedNodesSyntax? = nil, locationColon: TokenSyntax, _ unexpectedBetweenLocationColonAndLocation: UnexpectedNodesSyntax? = nil, location: StringLiteralExprSyntax, _ unexpectedBetweenLocationAndRequirementComma: UnexpectedNodesSyntax? = nil, requirementComma: TokenSyntax?, _ unexpectedBetweenRequirementCommaAndRequirementLabel: UnexpectedNodesSyntax? = nil, requirementLabel: TokenSyntax?, _ unexpectedBetweenRequirementLabelAndRequirementColon: UnexpectedNodesSyntax? = nil, requirementColon: TokenSyntax?, _ unexpectedBetweenRequirementColonAndRequirement: UnexpectedNodesSyntax? = nil, requirement: ExprSyntax?, _ unexpectedBetweenRequirementAndProductComma: UnexpectedNodesSyntax? = nil, productComma: TokenSyntax?, _ unexpectedBetweenProductCommaAndProductLabel: UnexpectedNodesSyntax? = nil, productLabel: TokenSyntax?, _ unexpectedBetweenProductLabelAndProductColon: UnexpectedNodesSyntax? = nil, productColon: TokenSyntax?, _ unexpectedBetweenProductColonAndProductName: UnexpectedNodesSyntax? = nil, productName: StringLiteralExprSyntax?, _ unexpectedAfterProductName: UnexpectedNodesSyntax? = nil) -> PackageAttributeArgumentsSyntax {
54605460
let layout: [RawSyntax?] = [
54615461
unexpectedBeforeLocationLabel?.raw,
54625462
locationLabel.raw,
54635463
unexpectedBetweenLocationLabelAndLocationColon?.raw,
54645464
locationColon.raw,
54655465
unexpectedBetweenLocationColonAndLocation?.raw,
54665466
location.raw,
5467-
unexpectedBetweenLocationAndLocReqComma?.raw,
5468-
locReqComma?.raw,
5469-
unexpectedBetweenLocReqCommaAndRequirementLabel?.raw,
5467+
unexpectedBetweenLocationAndRequirementComma?.raw,
5468+
requirementComma?.raw,
5469+
unexpectedBetweenRequirementCommaAndRequirementLabel?.raw,
54705470
requirementLabel?.raw,
54715471
unexpectedBetweenRequirementLabelAndRequirementColon?.raw,
54725472
requirementColon?.raw,
54735473
unexpectedBetweenRequirementColonAndRequirement?.raw,
54745474
requirement?.raw,
5475-
unexpectedBetweenRequirementAndReqProdComma?.raw,
5476-
reqProdComma?.raw,
5477-
unexpectedBetweenReqProdCommaAndProductLabel?.raw,
5475+
unexpectedBetweenRequirementAndProductComma?.raw,
5476+
productComma?.raw,
5477+
unexpectedBetweenProductCommaAndProductLabel?.raw,
54785478
productLabel?.raw,
54795479
unexpectedBetweenProductLabelAndProductColon?.raw,
54805480
productColon?.raw,

0 commit comments

Comments
 (0)