Skip to content

Commit

Permalink
Simplify @_package parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
stevapple committed Feb 4, 2023
1 parent 0f4cfba commit 310c9b8
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ public let ATTRIBUTE_NODES: [Node] = [
Child(name: "Location",
kind: .node(kind: "StringLiteralExpr"),
description: "The location/identifier of package."),
Child(name: "LocReqComma",
Child(name: "RequirementComma",
kind: .token(choices: [.token(tokenKind: "CommaToken")]),
description: "The comma separating the location and requirement",
description: "The comma before the package requirement, if it exists.",
isOptional: true),
Child(name: "RequirementLabel",
kind: .token(choices: [.keyword(text: "branch"), .keyword(text: "from"), .keyword(text: "revision")]),
Expand All @@ -223,9 +223,9 @@ public let ATTRIBUTE_NODES: [Node] = [
kind: .node(kind: "Expr"),
description: "The version requirement of package.",
isOptional: true),
Child(name: "ReqProdComma",
Child(name: "ProductComma",
kind: .token(choices: [.token(tokenKind: "CommaToken")]),
description: "The comma separating the requirement and product name",
description: "The comma before the package product, if it exists.",
isOptional: true),
Child(name: "ProductLabel",
kind: .token(choices: [.keyword(text: "product")]),
Expand Down
125 changes: 24 additions & 101 deletions Sources/SwiftParser/Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -873,143 +873,66 @@ extension Parser {
}

extension Parser {
mutating func parsePackageAttribute() -> RawAttributeSyntax {
let (unexpectedBeforeAtSign, atSign) = self.expect(.atSign)
let (unexpectedBeforePackageToken, packageToken) = self.expect(.keyword(._package))
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
let arguments = self.parsePackageAttributeArguments()
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
return RawAttributeSyntax(
unexpectedBeforeAtSign,
atSignToken: atSign,
unexpectedBeforePackageToken,
attributeName: RawTypeSyntax(RawSimpleTypeIdentifierSyntax(name: packageToken, genericArgumentClause: nil, arena: self.arena)),
unexpectedBeforeLeftParen,
leftParen: leftParen,
argument: .packageAttributeArguments(arguments),
unexpectedBeforeRightParen,
rightParen: rightParen,
arena: self.arena
)
}

enum PackageLocationLabel: RawTokenKindSubset {
case id
case path
case url

init?(lexeme: Lexer.Lexeme) {
switch lexeme {
case RawTokenKindMatch(.id): self = .id
case RawTokenKindMatch(.path): self = .path
case RawTokenKindMatch(.url): self = .url
default: return nil
}
}

var rawTokenKind: RawTokenKind {
switch self {
case .id: return .keyword(.id)
case .path: return .keyword(.path)
case .url: return .keyword(.url)
}
}
}

enum PackageRequirementLabel: RawTokenKindSubset {
case branch
case from
case revision

init?(lexeme: Lexer.Lexeme) {
switch lexeme {
case RawTokenKindMatch(.branch): self = .branch
case RawTokenKindMatch(.from): self = .from
case RawTokenKindMatch(.revision): self = .revision
default: return nil
}
}

var rawTokenKind: RawTokenKind {
switch self {
case .branch: return .keyword(.branch)
case .from: return .keyword(.from)
case .revision: return .keyword(.revision)
}
}
}

mutating func parsePackageAttributeArguments() -> RawPackageAttributeArgumentsSyntax {
// Parsing package location.
let locationLabel = self.consume(ifAnyIn: PackageLocationLabel.self) ?? missingToken(.identifier)
let (unexpectedBeforeLocationLabel, locationLabel) = self.expectAny([.keyword(.id), .keyword(.path), .keyword(.url)], default: .keyword(.id))
let (unexpectedBeforeLocationColon, locationColon) = self.expect(.colon)
let location = self.parseStringLiteral()
// Parsing package requirement.
let (unexpectedBeforeLocReqComma, locReqComma): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
let (unexpectedBeforeRequirementComma, requirementComma): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
let (unexpectedBeforeRequirementLabel, requirementLabel): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
let (unexpectedBeforeRequirementColon, requirementColon): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
let requirement: RawExprSyntax?
if locationLabel.tokenKind != .keyword(.path) {
(unexpectedBeforeLocReqComma, locReqComma) = self.expect(.comma)
if let label = self.consume(ifAnyIn: PackageRequirementLabel.self) {
(unexpectedBeforeRequirementLabel, requirementLabel) = (nil, label)
(unexpectedBeforeRequirementComma, requirementComma) = self.expect(.comma)
if self.at(any: [.keyword(.from), .keyword(.branch), .keyword(.revision)]) {
(unexpectedBeforeRequirementLabel, requirementLabel) = self.expectAny([.keyword(.from), .keyword(.branch), .keyword(.revision)], default: .keyword(.from))
(unexpectedBeforeRequirementColon, requirementColon) = self.expect(.colon)
} else {
(unexpectedBeforeRequirementLabel, requirementLabel) = (nil, nil)
(unexpectedBeforeRequirementColon, requirementColon) = (nil, nil)
}
requirement = self.parseExpression()
} else {
(unexpectedBeforeLocReqComma, locReqComma) = (nil, nil)
(unexpectedBeforeRequirementComma, requirementComma) = (nil, nil)
(unexpectedBeforeRequirementLabel, requirementLabel) = (nil, nil)
(unexpectedBeforeRequirementColon, requirementColon) = (nil, nil)
requirement = nil
}
guard self.at(.comma) else {
return RawPackageAttributeArgumentsSyntax(
locationLabel: locationLabel,
unexpectedBeforeLocationColon,
locationColon: locationColon,
location: location,
unexpectedBeforeLocReqComma,
locReqComma: locReqComma,
unexpectedBeforeRequirementLabel,
requirementLabel: requirementLabel,
unexpectedBeforeRequirementColon,
requirementColon: requirementColon,
requirement: requirement,
reqProdComma: nil,
productLabel: nil,
productColon: nil,
productName: nil,
self.remainingTokensIfMaximumNestingLevelReached(),
arena: self.arena
)
// Parsing package requirement.
let productComma = self.consume(if: .comma)
let (unexpectedBeforeProductLabel, productLabel): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
let (unexpectedBeforeProductColon, productColon): (RawUnexpectedNodesSyntax?, RawTokenSyntax?)
let productName: RawStringLiteralExprSyntax?
if productComma != nil {
(unexpectedBeforeProductLabel, productLabel) = self.expect(.keyword(.product))
(unexpectedBeforeProductColon, productColon) = self.expect(.colon)
productName = self.parseStringLiteral()
} else {
(unexpectedBeforeProductLabel, productLabel) = (nil, nil)
(unexpectedBeforeProductColon, productColon) = (nil, nil)
productName = nil
}
let (unexpectedBeforeReqProdComma, reqProdComma) = self.expect(.comma)
let (unexpectedBeforeProductLabel, productLabel) = self.expect(.keyword(.product))
let (unexpectedBeforeProductColon, productColon) = self.expect(.colon)
let productName = self.parseStringLiteral()
// Returning @_package argument list
return RawPackageAttributeArgumentsSyntax(
unexpectedBeforeLocationLabel,
locationLabel: locationLabel,
unexpectedBeforeLocationColon,
locationColon: locationColon,
location: location,
unexpectedBeforeLocReqComma,
locReqComma: locReqComma,
unexpectedBeforeRequirementComma,
requirementComma: requirementComma,
unexpectedBeforeRequirementLabel,
requirementLabel: requirementLabel,
unexpectedBeforeRequirementColon,
requirementColon: requirementColon,
requirement: requirement,
unexpectedBeforeReqProdComma,
reqProdComma: reqProdComma,
productComma: productComma,
unexpectedBeforeProductLabel,
productLabel: productLabel,
unexpectedBeforeProductColon,
productColon: productColon,
productName: productName,
self.remainingTokensIfMaximumNestingLevelReached(),
arena: self.arena
)
}
Expand Down
36 changes: 18 additions & 18 deletions Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11745,17 +11745,17 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
locationColon: RawTokenSyntax,
_ unexpectedBetweenLocationColonAndLocation: RawUnexpectedNodesSyntax? = nil,
location: RawStringLiteralExprSyntax,
_ unexpectedBetweenLocationAndLocReqComma: RawUnexpectedNodesSyntax? = nil,
locReqComma: RawTokenSyntax?,
_ unexpectedBetweenLocReqCommaAndRequirementLabel: RawUnexpectedNodesSyntax? = nil,
_ unexpectedBetweenLocationAndRequirementComma: RawUnexpectedNodesSyntax? = nil,
requirementComma: RawTokenSyntax?,
_ unexpectedBetweenRequirementCommaAndRequirementLabel: RawUnexpectedNodesSyntax? = nil,
requirementLabel: RawTokenSyntax?,
_ unexpectedBetweenRequirementLabelAndRequirementColon: RawUnexpectedNodesSyntax? = nil,
requirementColon: RawTokenSyntax?,
_ unexpectedBetweenRequirementColonAndRequirement: RawUnexpectedNodesSyntax? = nil,
requirement: RawExprSyntax?,
_ unexpectedBetweenRequirementAndReqProdComma: RawUnexpectedNodesSyntax? = nil,
reqProdComma: RawTokenSyntax?,
_ unexpectedBetweenReqProdCommaAndProductLabel: RawUnexpectedNodesSyntax? = nil,
_ unexpectedBetweenRequirementAndProductComma: RawUnexpectedNodesSyntax? = nil,
productComma: RawTokenSyntax?,
_ unexpectedBetweenProductCommaAndProductLabel: RawUnexpectedNodesSyntax? = nil,
productLabel: RawTokenSyntax?,
_ unexpectedBetweenProductLabelAndProductColon: RawUnexpectedNodesSyntax? = nil,
productColon: RawTokenSyntax?,
Expand All @@ -11773,17 +11773,17 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
layout[3] = locationColon.raw
layout[4] = unexpectedBetweenLocationColonAndLocation?.raw
layout[5] = location.raw
layout[6] = unexpectedBetweenLocationAndLocReqComma?.raw
layout[7] = locReqComma?.raw
layout[8] = unexpectedBetweenLocReqCommaAndRequirementLabel?.raw
layout[6] = unexpectedBetweenLocationAndRequirementComma?.raw
layout[7] = requirementComma?.raw
layout[8] = unexpectedBetweenRequirementCommaAndRequirementLabel?.raw
layout[9] = requirementLabel?.raw
layout[10] = unexpectedBetweenRequirementLabelAndRequirementColon?.raw
layout[11] = requirementColon?.raw
layout[12] = unexpectedBetweenRequirementColonAndRequirement?.raw
layout[13] = requirement?.raw
layout[14] = unexpectedBetweenRequirementAndReqProdComma?.raw
layout[15] = reqProdComma?.raw
layout[16] = unexpectedBetweenReqProdCommaAndProductLabel?.raw
layout[14] = unexpectedBetweenRequirementAndProductComma?.raw
layout[15] = productComma?.raw
layout[16] = unexpectedBetweenProductCommaAndProductLabel?.raw
layout[17] = productLabel?.raw
layout[18] = unexpectedBetweenProductLabelAndProductColon?.raw
layout[19] = productColon?.raw
Expand Down Expand Up @@ -11812,13 +11812,13 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
public var location: RawStringLiteralExprSyntax {
layoutView.children[5].map(RawStringLiteralExprSyntax.init(raw:))!
}
public var unexpectedBetweenLocationAndLocReqComma: RawUnexpectedNodesSyntax? {
public var unexpectedBetweenLocationAndRequirementComma: RawUnexpectedNodesSyntax? {
layoutView.children[6].map(RawUnexpectedNodesSyntax.init(raw:))
}
public var locReqComma: RawTokenSyntax? {
public var requirementComma: RawTokenSyntax? {
layoutView.children[7].map(RawTokenSyntax.init(raw:))
}
public var unexpectedBetweenLocReqCommaAndRequirementLabel: RawUnexpectedNodesSyntax? {
public var unexpectedBetweenRequirementCommaAndRequirementLabel: RawUnexpectedNodesSyntax? {
layoutView.children[8].map(RawUnexpectedNodesSyntax.init(raw:))
}
public var requirementLabel: RawTokenSyntax? {
Expand All @@ -11836,13 +11836,13 @@ public struct RawPackageAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
public var requirement: RawExprSyntax? {
layoutView.children[13].map(RawExprSyntax.init(raw:))
}
public var unexpectedBetweenRequirementAndReqProdComma: RawUnexpectedNodesSyntax? {
public var unexpectedBetweenRequirementAndProductComma: RawUnexpectedNodesSyntax? {
layoutView.children[14].map(RawUnexpectedNodesSyntax.init(raw:))
}
public var reqProdComma: RawTokenSyntax? {
public var productComma: RawTokenSyntax? {
layoutView.children[15].map(RawTokenSyntax.init(raw:))
}
public var unexpectedBetweenReqProdCommaAndProductLabel: RawUnexpectedNodesSyntax? {
public var unexpectedBetweenProductCommaAndProductLabel: RawUnexpectedNodesSyntax? {
layoutView.children[16].map(RawUnexpectedNodesSyntax.init(raw:))
}
public var productLabel: RawTokenSyntax? {
Expand Down
14 changes: 7 additions & 7 deletions Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5456,25 +5456,25 @@ public enum SyntaxFactory {
}
}
@available(*, deprecated, message: "Use initializer on PackageAttributeArgumentsSyntax")
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 {
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 {
let layout: [RawSyntax?] = [
unexpectedBeforeLocationLabel?.raw,
locationLabel.raw,
unexpectedBetweenLocationLabelAndLocationColon?.raw,
locationColon.raw,
unexpectedBetweenLocationColonAndLocation?.raw,
location.raw,
unexpectedBetweenLocationAndLocReqComma?.raw,
locReqComma?.raw,
unexpectedBetweenLocReqCommaAndRequirementLabel?.raw,
unexpectedBetweenLocationAndRequirementComma?.raw,
requirementComma?.raw,
unexpectedBetweenRequirementCommaAndRequirementLabel?.raw,
requirementLabel?.raw,
unexpectedBetweenRequirementLabelAndRequirementColon?.raw,
requirementColon?.raw,
unexpectedBetweenRequirementColonAndRequirement?.raw,
requirement?.raw,
unexpectedBetweenRequirementAndReqProdComma?.raw,
reqProdComma?.raw,
unexpectedBetweenReqProdCommaAndProductLabel?.raw,
unexpectedBetweenRequirementAndProductComma?.raw,
productComma?.raw,
unexpectedBetweenProductCommaAndProductLabel?.raw,
productLabel?.raw,
unexpectedBetweenProductLabelAndProductColon?.raw,
productColon?.raw,
Expand Down
Loading

0 comments on commit 310c9b8

Please sign in to comment.