Skip to content

.swiftinterface-Related Fixes #751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/SwiftParser/Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,22 @@ extension Parser {
}
}

extension Parser {
mutating func parseOpaqueReturnTypeOfAttributeArguments() -> RawOpaqueReturnTypeOfAttributeArgumentsSyntax {
let (unexpectedBeforeString, mangledName) = self.expect(.stringLiteral)
let (unexpectedBeforeComma, comma) = self.expect(.comma)
let (unexpectedBeforeOrdinal, ordinal) = self.expect(.integerLiteral)
return RawOpaqueReturnTypeOfAttributeArgumentsSyntax(
unexpectedBeforeString,
mangledName: mangledName,
unexpectedBeforeComma,
comma: comma,
unexpectedBeforeOrdinal,
ordinal: ordinal,
arena: self.arena)
}
}

// MARK: Lookahead

extension Parser.Lookahead {
Expand Down
49 changes: 37 additions & 12 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension TokenConsumer {
var attributeProgress = LoopProgressCondition()
while attributeProgress.evaluate(subparser.currentToken) && subparser.at(.atSign) {
hasAttribute = true
_ = subparser.eatParseAttributeList()
_ = subparser.consumeAttributeList()
}

var modifierProgress = LoopProgressCondition()
Expand Down Expand Up @@ -130,7 +130,7 @@ extension Parser {
case (.enumKeyword, _)?:
return RawDeclSyntax(self.parseEnumDeclaration(attrs))
case (.caseKeyword, _)?:
return RawDeclSyntax(self.parseDeclEnumCase(attrs))
return RawDeclSyntax(self.parseEnumCaseDeclaration(attrs))
case (.structKeyword, _)?:
return RawDeclSyntax(self.parseStructDeclaration(attrs))
case (.protocolKeyword, _)?:
Expand Down Expand Up @@ -715,7 +715,7 @@ extension Parser {
/// raw-value-assignment → = raw-value-literal
/// raw-value-literal → numeric-literal | static-string-literal | boolean-literal
@_spi(RawSyntax)
public mutating func parseDeclEnumCase(_ attrs: DeclAttributes) -> RawEnumCaseDeclSyntax {
public mutating func parseEnumCaseDeclaration(_ attrs: DeclAttributes) -> RawEnumCaseDeclSyntax {
let (unexpectedBeforeCaseKeyword, caseKeyword) = self.expect(.caseKeyword)
var elements = [RawEnumCaseElementSyntax]()
do {
Expand All @@ -726,7 +726,7 @@ extension Parser {

let associatedValue: RawParameterClauseSyntax?
if self.at(.leftParen, where: { !$0.isAtStartOfLine }) {
associatedValue = self.parseParameterClause()
associatedValue = self.parseParameterClause(for: .enumCase)
} else {
associatedValue = nil
}
Expand Down Expand Up @@ -1142,27 +1142,52 @@ extension Parser {

extension Parser {
@_spi(RawSyntax)
public mutating func parseParameterClause(isClosure: Bool = false) -> RawParameterClauseSyntax {
public enum ParameterSubject {
case closure
case enumCase
case functionParameters
case indices

var isClosure: Bool {
switch self {
case .closure: return true
case .enumCase: return false
case .functionParameters: return false
case .indices: return false
}
}
}

@_spi(RawSyntax)
public mutating func parseParameterClause(for subject: ParameterSubject) -> RawParameterClauseSyntax {
let (unexpectedBeforeLParen, lparen) = self.expect(.leftParen)
var elements = [RawFunctionParameterSyntax]()
// If we are missing the left parenthesis and the next token doesn't appear to be an argument label, don't parse any parameters.
// If we are missing the left parenthesis and the next token doesn't appear
// to be an argument label, don't parse any parameters.
let shouldSkipParameterParsing = lparen.isMissing && (!currentToken.canBeArgumentLabel || currentToken.isKeyword)
if !shouldSkipParameterParsing {
var keepGoing = true
var loopProgress = LoopProgressCondition()
while !self.at(any: [.eof, .rightParen])
&& keepGoing
&& loopProgress.evaluate(currentToken) {
// Attributes.
let attrs = self.parseAttributeList()
// Parse any declaration attributes. The exception here is enum cases
// which only allow types, so we do not consume attributes to allow the
// type attribute grammar a chance to examine them.
let attrs: RawAttributeListSyntax?
if case .enumCase = subject {
attrs = nil
} else {
attrs = self.parseAttributeList()
}

let firstName: RawTokenSyntax?
let secondName: RawTokenSyntax?
let unexpectedBeforeColon: RawUnexpectedNodesSyntax?
let colon: RawTokenSyntax?
let shouldParseType: Bool

if self.lookahead().startsParameterName(isClosure) {
if self.lookahead().startsParameterName(subject.isClosure) {
if self.currentToken.canBeArgumentLabel {
firstName = self.parseArgumentLabel()
} else {
Expand All @@ -1174,7 +1199,7 @@ extension Parser {
} else {
secondName = nil
}
if isClosure {
if subject.isClosure {
unexpectedBeforeColon = nil
colon = self.consume(if: .colon)
shouldParseType = (colon != nil)
Expand Down Expand Up @@ -1314,7 +1339,7 @@ extension Parser {

@_spi(RawSyntax)
public mutating func parseFunctionSignature() -> RawFunctionSignatureSyntax {
let input = self.parseParameterClause()
let input = self.parseParameterClause(for: .functionParameters)

let async = self.consumeIfContextualKeyword("async")

Expand Down Expand Up @@ -1361,7 +1386,7 @@ extension Parser {
genericParameterClause = nil
}

let indices = self.parseParameterClause()
let indices = self.parseParameterClause(for: .indices)

let result: RawReturnClauseSyntax
if self.at(.arrow) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,7 @@ extension Parser {
if !self.at(.inKeyword) {
if self.at(.leftParen) {
// Parse the closure arguments.
input = RawSyntax(self.parseParameterClause(isClosure: true))
input = RawSyntax(self.parseParameterClause(for: .closure))
} else {
var params = [RawClosureParamSyntax]()
var loopProgress = LoopProgressCondition()
Expand Down
13 changes: 11 additions & 2 deletions Sources/SwiftParser/Lookahead.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,23 @@ extension Parser.Lookahead {
public mutating func eat(_ kind: RawTokenKind) -> Token {
return self.consume(if: kind)!
}
}

mutating func eatParseAttributeList() -> Bool {
extension Parser.Lookahead {
mutating func consumeAttributeList() -> Bool {
guard self.at(.atSign) else {
return false
}

while let _ = self.consume(if: .atSign) {
self.expectIdentifierOrRethrowsWithoutRecovery()
// Consume qualified names that may or may not involve generic arguments.
repeat {
self.expectIdentifierOrRethrowsWithoutRecovery()
// We don't care whether this succeeds or fails to eat generic
// parameters.
_ = self.consumeGenericArguments()
} while self.consume(if: .period) != nil

if self.consume(if: .leftParen) != nil {
while !self.at(any: [.eof, .rightParen, .poundEndifKeyword]) {
self.skipSingle()
Expand Down
26 changes: 23 additions & 3 deletions Sources/SwiftParser/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ extension Parser.Lookahead {
self.consumeAnyToken()

// Parse an optional generic argument list.
if self.currentToken.starts(with: "<") && !self.canParseGenericArguments() {
if self.currentToken.starts(with: "<") && !self.consumeGenericArguments() {
return false
}

Expand All @@ -725,13 +725,13 @@ extension Parser.Lookahead {
}

var lookahead = self.lookahead()
guard lookahead.canParseGenericArguments() else {
guard lookahead.consumeGenericArguments() else {
return false
}
return lookahead.currentToken.isGenericTypeDisambiguatingToken
}

mutating func canParseGenericArguments() -> Bool {
mutating func consumeGenericArguments() -> Bool {
// Parse the opening '<'.
guard self.currentToken.starts(with: "<") else {
return false
Expand Down Expand Up @@ -820,6 +820,26 @@ extension Parser {
arena: self.arena
)
)
case ._opaqueReturnTypeOf:
let (unexpectedBeforeAt, at) = self.expect(.atSign)
let ident = self.expectIdentifierWithoutRecovery()
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
let argument = self.parseOpaqueReturnTypeOfAttributeArguments()
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
return RawSyntax(
RawAttributeSyntax(
unexpectedBeforeAt,
atSignToken: at,
attributeName: ident,
unexpectedBeforeLeftParen,
leftParen: leftParen,
argument: RawSyntax(argument),
unexpectedBeforeRightParen,
rightParen: rightParen,
tokenList: nil,
arena: self.arena
)
)

default:
let (unexpectedBeforeAt, at) = self.expect(.atSign)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
- <doc:SwiftSyntax/BackDeployAttributeSpecListSyntax>
- <doc:SwiftSyntax/BackDeployVersionListSyntax>
- <doc:SwiftSyntax/BackDeployVersionArgumentSyntax>
- <doc:SwiftSyntax/OpaqueReturnTypeOfAttributeArgumentsSyntax>
- <doc:SwiftSyntax/SwitchCaseListSyntax>
- <doc:SwiftSyntax/WhereClauseSyntax>
- <doc:SwiftSyntax/CatchClauseListSyntax>
Expand Down
63 changes: 63 additions & 0 deletions Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10577,6 +10577,69 @@ public struct RawBackDeployVersionArgumentSyntax: RawSyntaxNodeProtocol {
}
}

@_spi(RawSyntax)
public struct RawOpaqueReturnTypeOfAttributeArgumentsSyntax: RawSyntaxNodeProtocol {
var layoutView: RawSyntaxLayoutView {
return raw.layoutView!
}

public static func isKindOf(_ raw: RawSyntax) -> Bool {
return raw.kind == .opaqueReturnTypeOfAttributeArguments
}

public var raw: RawSyntax
init(raw: RawSyntax) {
assert(Self.isKindOf(raw))
self.raw = raw
}

public init?<Node: RawSyntaxNodeProtocol>(_ other: Node) {
guard Self.isKindOf(other.raw) else { return nil }
self.init(raw: other.raw)
}

public init(
_ unexpectedBeforeMangledName: RawUnexpectedNodesSyntax? = nil,
mangledName: RawTokenSyntax,
_ unexpectedBetweenMangledNameAndComma: RawUnexpectedNodesSyntax? = nil,
comma: RawTokenSyntax,
_ unexpectedBetweenCommaAndOrdinal: RawUnexpectedNodesSyntax? = nil,
ordinal: RawTokenSyntax,
arena: __shared SyntaxArena
) {
let raw = RawSyntax.makeLayout(
kind: .opaqueReturnTypeOfAttributeArguments, uninitializedCount: 6, arena: arena) { layout in
layout.initialize(repeating: nil)
layout[0] = unexpectedBeforeMangledName?.raw
layout[1] = mangledName.raw
layout[2] = unexpectedBetweenMangledNameAndComma?.raw
layout[3] = comma.raw
layout[4] = unexpectedBetweenCommaAndOrdinal?.raw
layout[5] = ordinal.raw
}
self.init(raw: raw)
}

public var unexpectedBeforeMangledName: RawUnexpectedNodesSyntax? {
layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:))
}
public var mangledName: RawTokenSyntax {
layoutView.children[1].map(RawTokenSyntax.init(raw:))!
}
public var unexpectedBetweenMangledNameAndComma: RawUnexpectedNodesSyntax? {
layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:))
}
public var comma: RawTokenSyntax {
layoutView.children[3].map(RawTokenSyntax.init(raw:))!
}
public var unexpectedBetweenCommaAndOrdinal: RawUnexpectedNodesSyntax? {
layoutView.children[4].map(RawUnexpectedNodesSyntax.init(raw:))
}
public var ordinal: RawTokenSyntax {
layoutView.children[5].map(RawTokenSyntax.init(raw:))!
}
}

@_spi(RawSyntax)
public struct RawLabeledStmtSyntax: RawStmtSyntaxNodeProtocol {
var layoutView: RawSyntaxLayoutView {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,15 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
_verify(layout[2], as: RawUnexpectedNodesSyntax?.self)
_verify(layout[3], as: RawTokenSyntax?.self)
break
case .opaqueReturnTypeOfAttributeArguments:
assert(layout.count == 6)
_verify(layout[0], as: RawUnexpectedNodesSyntax?.self)
_verify(layout[1], as: RawTokenSyntax.self)
_verify(layout[2], as: RawUnexpectedNodesSyntax?.self)
_verify(layout[3], as: RawTokenSyntax.self)
_verify(layout[4], as: RawUnexpectedNodesSyntax?.self)
_verify(layout[5], as: RawTokenSyntax.self)
break
case .labeledStmt:
assert(layout.count == 6)
_verify(layout[0], as: RawUnexpectedNodesSyntax?.self)
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftSyntax/gyb_generated/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ extension Syntax {
return node
case .backDeployVersionArgument(let node):
return node
case .opaqueReturnTypeOfAttributeArguments(let node):
return node
case .labeledStmt(let node):
return node
case .continueStmt(let node):
Expand Down Expand Up @@ -742,6 +744,7 @@ extension SyntaxKind {
case .backDeployAttributeSpecList: return BackDeployAttributeSpecListSyntax.self
case .backDeployVersionList: return BackDeployVersionListSyntax.self
case .backDeployVersionArgument: return BackDeployVersionArgumentSyntax.self
case .opaqueReturnTypeOfAttributeArguments: return OpaqueReturnTypeOfAttributeArgumentsSyntax.self
case .labeledStmt: return LabeledStmtSyntax.self
case .continueStmt: return ContinueStmtSyntax.self
case .whileStmt: return WhileStmtSyntax.self
Expand Down
7 changes: 7 additions & 0 deletions Sources/SwiftSyntax/gyb_generated/SyntaxAnyVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,13 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
override open func visitPost(_ node: BackDeployVersionArgumentSyntax) {
visitAnyPost(node._syntaxNode)
}
override open func visit(_ node: OpaqueReturnTypeOfAttributeArgumentsSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}

override open func visitPost(_ node: OpaqueReturnTypeOfAttributeArgumentsSyntax) {
visitAnyPost(node._syntaxNode)
}
override open func visit(_ node: LabeledStmtSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftSyntax/gyb_generated/SyntaxEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public enum SyntaxEnum {
case backDeployAttributeSpecList(BackDeployAttributeSpecListSyntax)
case backDeployVersionList(BackDeployVersionListSyntax)
case backDeployVersionArgument(BackDeployVersionArgumentSyntax)
case opaqueReturnTypeOfAttributeArguments(OpaqueReturnTypeOfAttributeArgumentsSyntax)
case labeledStmt(LabeledStmtSyntax)
case continueStmt(ContinueStmtSyntax)
case whileStmt(WhileStmtSyntax)
Expand Down Expand Up @@ -639,6 +640,8 @@ public enum SyntaxEnum {
return "version list"
case .backDeployVersionArgument:
return "version"
case .opaqueReturnTypeOfAttributeArguments:
return "opaque return type arguments"
case .labeledStmt:
return "labeled statement"
case .continueStmt:
Expand Down Expand Up @@ -1179,6 +1182,8 @@ public extension Syntax {
return .backDeployVersionList(BackDeployVersionListSyntax(self)!)
case .backDeployVersionArgument:
return .backDeployVersionArgument(BackDeployVersionArgumentSyntax(self)!)
case .opaqueReturnTypeOfAttributeArguments:
return .opaqueReturnTypeOfAttributeArguments(OpaqueReturnTypeOfAttributeArgumentsSyntax(self)!)
case .labeledStmt:
return .labeledStmt(LabeledStmtSyntax(self)!)
case .continueStmt:
Expand Down
Loading