diff --git a/CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift b/CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift index 7326c1d8f3a..28a715a4a9f 100644 --- a/CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift +++ b/CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift @@ -96,7 +96,7 @@ public let KEYWORDS: [KeywordSpec] = [ KeywordSpec("autoclosure"), KeywordSpec("availability"), KeywordSpec("available"), - KeywordSpec("await"), + KeywordSpec("await", requiresTrailingSpace: true), KeywordSpec("backDeployed"), KeywordSpec("before"), KeywordSpec("block"), diff --git a/CodeGeneration/Sources/SyntaxSupport/StmtNodes.swift b/CodeGeneration/Sources/SyntaxSupport/StmtNodes.swift index 9cbc278e8e5..fd745ea4a3a 100644 --- a/CodeGeneration/Sources/SyntaxSupport/StmtNodes.swift +++ b/CodeGeneration/Sources/SyntaxSupport/StmtNodes.swift @@ -290,18 +290,17 @@ public let STMT_NODES: [Node] = [ ), Child( name: "TryKeyword", - kind: .node(kind: "TryToken"), + kind: .token(choices: [.keyword(text: "try")]), isOptional: true ), Child( name: "AwaitKeyword", kind: .token(choices: [.keyword(text: "await")]), - isOptional: true, - classification: "Keyword" + isOptional: true ), Child( name: "CaseKeyword", - kind: .node(kind: "CaseToken"), + kind: .token(choices: [.keyword(text: "case")]), isOptional: true ), Child( diff --git a/CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift b/CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift index 07b3260183b..c6c98ca75ef 100644 --- a/CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift +++ b/CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift @@ -45,21 +45,34 @@ public extension Child { flattened(indentedDocumentation: description ?? "") } - var defaultInitialization: ExprSyntax? { - switch kind { - case .token(choices: let choices, requiresLeadingSpace: _, requiresTrailingSpace: _): - if choices.count == 1, case .keyword(text: let text) = choices.first { - var textChoice = text - if textChoice == "init" { - textChoice = "`init`" - } - return ExprSyntax(".keyword(.\(raw: textChoice))") - } else { - return type.defaultInitialization - } - default: - return type.defaultInitialization + /// If the child node has a default value, return an expression of the form + /// ` = default_value` that can be used as the default value to for a + /// function parameter. Otherwise, return `nil`. + var defaultInitialization: InitializerClauseSyntax? { + if isOptional || isUnexpectedNodes { + return InitializerClauseSyntax(value: NilLiteralExprSyntax()) + } + guard let token = token, isToken else { + return type.defaultValue.map { InitializerClauseSyntax(value: $0) } + } + if token.isKeyword { + return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)()")) + } + if token.text != nil { + return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)Token()")) + } + guard case .token(let choices, _, _) = kind, choices.count == 1, token.associatedValueClass != nil else { + return nil + } + var textChoice: String + switch choices[0] { + case .keyword(let text), .token(let text): + textChoice = text + } + if textChoice == "init" { + textChoice = "`init`" } + return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)(.\(raw: textChoice))")) } /// If this node is a token that can't contain arbitrary text, generate a Swift diff --git a/CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift b/CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift index de92776e701..6f80ec6d1ff 100644 --- a/CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift +++ b/CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift @@ -50,7 +50,7 @@ public extension Node { /// Assuming this node has a single child without a default value, that child. var singleNonDefaultedChild: Child { - let nonDefaultedParams = children.filter { $0.type.defaultInitialization == nil } + let nonDefaultedParams = children.filter { $0.defaultInitialization == nil } precondition(nonDefaultedParams.count == 1) return nonDefaultedParams[0] } diff --git a/CodeGeneration/Sources/Utils/SyntaxBuildableType.swift b/CodeGeneration/Sources/Utils/SyntaxBuildableType.swift index 7f6bb10f3b5..83fb56f2149 100644 --- a/CodeGeneration/Sources/Utils/SyntaxBuildableType.swift +++ b/CodeGeneration/Sources/Utils/SyntaxBuildableType.swift @@ -50,10 +50,9 @@ public struct SyntaxBuildableType: Hashable { } /// If the type has a default value (because it is optional or a token - /// with fixed test), return an expression of the form ` = defaultValue` - /// that can be used as the default value for a function parameter. - /// Otherwise, return the empty string. - public var defaultInitialization: ExprSyntax? { + /// with fixed test), return an expression that can be used as the + /// default value for a function parameter. Otherwise, return `nil`. + public var defaultValue: ExprSyntax? { if isOptional { return ExprSyntax(NilLiteralExprSyntax()) } else if let token = token { diff --git a/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/RawSyntaxNodesFile.swift b/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/RawSyntaxNodesFile.swift index e98d5280a54..1039fd01630 100644 --- a/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/RawSyntaxNodesFile.swift +++ b/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/RawSyntaxNodesFile.swift @@ -197,7 +197,7 @@ let rawSyntaxNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { secondName: .identifier(child.swiftName), colon: .colonToken(), type: child.rawParameterType, - defaultArgument: child.isUnexpectedNodes ? child.defaultInitialization.map { InitializerClauseSyntax(value: $0) } : nil + defaultArgument: child.isUnexpectedNodes ? child.defaultInitialization : nil ) } diff --git a/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SyntaxNodeFile.swift b/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SyntaxNodeFile.swift index c0f00930116..da6b775524b 100644 --- a/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SyntaxNodeFile.swift +++ b/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SyntaxNodeFile.swift @@ -438,57 +438,3 @@ fileprivate extension Node { } } } - -fileprivate extension Child { - /// If the type has a default value, return an expression of the form - /// ` = default_value` that can be used as the default value to for a - /// function parameter. Otherwise, return an empty string. - var defaultInitialization: InitializerClauseSyntax? { - // Note that this should be Optional.none for defaulted generic, - // but that doesn't work in Swift 5.6. To keep source compatibility with - // previous SwiftSyntax, we instead create a second initializer that uses - // `Missing` and defaults that to `nil` instead (and `Missing` is - // used so that they can't be implicitly converted from a literal). - - if isOptional || isUnexpectedNodes { - return InitializerClauseSyntax(value: NilLiteralExprSyntax()) - } - - guard let token = token, isToken else { - return nil - } - - if token.isKeyword { - return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)()")) - } - - if token.text != nil { - return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)Token()")) - } - - guard case .token(let choices, _, _) = kind else { - return nil - } - - guard choices.count == 1 else { - return nil - } - - var textChoice: String - - switch choices[0] { - case .keyword(let text), - .token(let text): - textChoice = text - } - - if token.associatedValueClass != nil { - if textChoice == "init" { - textChoice = "`init`" - } - return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)(.\(raw: textChoice))")) - } else { - return InitializerClauseSyntax(value: ExprSyntax(#".\#(raw: token.swiftKind)("\#(raw: textChoice)")"#)) - } - } -} diff --git a/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/BuildableNodesFile.swift b/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/BuildableNodesFile.swift index bcfddab0254..e816ca975ba 100644 --- a/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/BuildableNodesFile.swift +++ b/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/BuildableNodesFile.swift @@ -83,7 +83,7 @@ private func createConvenienceInitializer(node: Node) throws -> InitializerDeclS firstName: .identifier(child.swiftName), colon: .colonToken(), type: child.parameterType, - defaultArgument: child.defaultInitialization.map { InitializerClauseSyntax(value: $0) } + defaultArgument: child.defaultInitialization ) ) } diff --git a/Sources/IDEUtils/generated/SyntaxClassification.swift b/Sources/IDEUtils/generated/SyntaxClassification.swift index 0a5076ebdba..12db3b80da2 100644 --- a/Sources/IDEUtils/generated/SyntaxClassification.swift +++ b/Sources/IDEUtils/generated/SyntaxClassification.swift @@ -79,8 +79,6 @@ extension SyntaxClassification { return (.stringInterpolationAnchor, true) case \ExpressionSegmentSyntax.rightParen: return (.stringInterpolationAnchor, true) - case \ForInStmtSyntax.awaitKeyword: - return (.keyword, false) case \IfConfigClauseSyntax.poundKeyword: return (.buildConfigId, false) case \IfConfigClauseSyntax.condition: diff --git a/Sources/SwiftBasicFormat/generated/BasicFormat.swift b/Sources/SwiftBasicFormat/generated/BasicFormat.swift index 21958a7f635..44a5ad660da 100644 --- a/Sources/SwiftBasicFormat/generated/BasicFormat.swift +++ b/Sources/SwiftBasicFormat/generated/BasicFormat.swift @@ -285,6 +285,8 @@ open class BasicFormat: SyntaxRewriter { return true case .keyword(.async): return true + case .keyword(.await): + return true case .keyword(.`break`): return true case .keyword(.`case`): diff --git a/Sources/SwiftParser/Statements.swift b/Sources/SwiftParser/Statements.swift index c1743dc600d..102c6015267 100644 --- a/Sources/SwiftParser/Statements.swift +++ b/Sources/SwiftParser/Statements.swift @@ -614,7 +614,6 @@ extension Parser { public mutating func parseForEachStatement(forHandle: RecoveryConsumptionHandle) -> RawForInStmtSyntax { let (unexpectedBeforeForKeyword, forKeyword) = self.eat(forHandle) let tryKeyword = self.consume(if: .keyword(.try)) - let awaitKeyword = self.consume(if: .keyword(.await)) // Parse the pattern. This is either 'case ' or just a diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift index 717235f3916..e9b576d7fb0 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift @@ -1200,11 +1200,11 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax.self, tokenChoices: [.keyword("for")])) assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) - assertNoError(kind, 3, verify(layout[3], as: RawTokenSyntax?.self)) + assertNoError(kind, 3, verify(layout[3], as: RawTokenSyntax?.self, tokenChoices: [.keyword("try")])) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 5, verify(layout[5], as: RawTokenSyntax?.self, tokenChoices: [.keyword("await")])) assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) - assertNoError(kind, 7, verify(layout[7], as: RawTokenSyntax?.self)) + assertNoError(kind, 7, verify(layout[7], as: RawTokenSyntax?.self, tokenChoices: [.keyword("case")])) assertNoError(kind, 8, verify(layout[8], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 9, verify(layout[9], as: RawPatternSyntax.self)) assertNoError(kind, 10, verify(layout[10], as: RawUnexpectedNodesSyntax?.self)) diff --git a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxDeclNodes.swift b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxDeclNodes.swift index 23740f3e3f4..f9b25f9e737 100644 --- a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxDeclNodes.swift +++ b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxDeclNodes.swift @@ -300,7 +300,7 @@ public struct ActorDeclSyntax: DeclSyntaxProtocol, SyntaxHashable { _ unexpectedBetweenModifiersAndActorKeyword: UnexpectedNodesSyntax? = nil, actorKeyword: TokenSyntax = .keyword(.actor), _ unexpectedBetweenActorKeywordAndIdentifier: UnexpectedNodesSyntax? = nil, - identifier: TokenSyntax = .identifier("IdentifierToken"), + identifier: TokenSyntax, _ unexpectedBetweenIdentifierAndGenericParameterClause: UnexpectedNodesSyntax? = nil, genericParameterClause: GenericParameterClauseSyntax? = nil, _ unexpectedBetweenGenericParameterClauseAndInheritanceClause: UnexpectedNodesSyntax? = nil, @@ -633,7 +633,7 @@ public struct AssociatedtypeDeclSyntax: DeclSyntaxProtocol, SyntaxHashable { _ unexpectedBetweenModifiersAndAssociatedtypeKeyword: UnexpectedNodesSyntax? = nil, associatedtypeKeyword: TokenSyntax = .keyword(.associatedtype), _ unexpectedBetweenAssociatedtypeKeywordAndIdentifier: UnexpectedNodesSyntax? = nil, - identifier: TokenSyntax = .identifier("IdentifierToken"), + identifier: TokenSyntax, _ unexpectedBetweenIdentifierAndInheritanceClause: UnexpectedNodesSyntax? = nil, inheritanceClause: TypeInheritanceClauseSyntax? = nil, _ unexpectedBetweenInheritanceClauseAndInitializer: UnexpectedNodesSyntax? = nil, @@ -938,7 +938,7 @@ public struct ClassDeclSyntax: DeclSyntaxProtocol, SyntaxHashable { _ unexpectedBetweenModifiersAndClassKeyword: UnexpectedNodesSyntax? = nil, classKeyword: TokenSyntax = .keyword(.class), _ unexpectedBetweenClassKeywordAndIdentifier: UnexpectedNodesSyntax? = nil, - identifier: TokenSyntax = .identifier("IdentifierToken"), + identifier: TokenSyntax, _ unexpectedBetweenIdentifierAndGenericParameterClause: UnexpectedNodesSyntax? = nil, genericParameterClause: GenericParameterClauseSyntax? = nil, _ unexpectedBetweenGenericParameterClauseAndInheritanceClause: UnexpectedNodesSyntax? = nil, @@ -1486,7 +1486,7 @@ public struct EditorPlaceholderDeclSyntax: DeclSyntaxProtocol, SyntaxHashable { public init( leadingTrivia: Trivia? = nil, _ unexpectedBeforeIdentifier: UnexpectedNodesSyntax? = nil, - identifier: TokenSyntax = .identifier("IdentifierToken"), + identifier: TokenSyntax, _ unexpectedAfterIdentifier: UnexpectedNodesSyntax? = nil, trailingTrivia: Trivia? = nil @@ -1822,7 +1822,7 @@ public struct EnumDeclSyntax: DeclSyntaxProtocol, SyntaxHashable { _ unexpectedBetweenModifiersAndEnumKeyword: UnexpectedNodesSyntax? = nil, enumKeyword: TokenSyntax = .keyword(.enum), _ unexpectedBetweenEnumKeywordAndIdentifier: UnexpectedNodesSyntax? = nil, - identifier: TokenSyntax = .identifier("IdentifierToken"), + identifier: TokenSyntax, _ unexpectedBetweenIdentifierAndGenericParameters: UnexpectedNodesSyntax? = nil, genericParameters: GenericParameterClauseSyntax? = nil, _ unexpectedBetweenGenericParametersAndInheritanceClause: UnexpectedNodesSyntax? = nil, @@ -3548,7 +3548,7 @@ public struct MacroDeclSyntax: DeclSyntaxProtocol, SyntaxHashable { _ unexpectedBetweenModifiersAndMacroKeyword: UnexpectedNodesSyntax? = nil, macroKeyword: TokenSyntax = .keyword(.macro), _ unexpectedBetweenMacroKeywordAndIdentifier: UnexpectedNodesSyntax? = nil, - identifier: TokenSyntax = .identifier("IdentifierToken"), + identifier: TokenSyntax, _ unexpectedBetweenIdentifierAndGenericParameterClause: UnexpectedNodesSyntax? = nil, genericParameterClause: GenericParameterClauseSyntax? = nil, _ unexpectedBetweenGenericParameterClauseAndSignature: UnexpectedNodesSyntax? = nil, @@ -3877,7 +3877,7 @@ public struct MacroExpansionDeclSyntax: DeclSyntaxProtocol, SyntaxHashable { _ unexpectedBeforePoundToken: UnexpectedNodesSyntax? = nil, poundToken: TokenSyntax = .poundToken(), _ unexpectedBetweenPoundTokenAndMacro: UnexpectedNodesSyntax? = nil, - macro: TokenSyntax = .identifier("IdentifierToken"), + macro: TokenSyntax, _ unexpectedBetweenMacroAndGenericArguments: UnexpectedNodesSyntax? = nil, genericArguments: GenericArgumentClauseSyntax? = nil, _ unexpectedBetweenGenericArgumentsAndLeftParen: UnexpectedNodesSyntax? = nil, @@ -4815,7 +4815,7 @@ public struct PrecedenceGroupDeclSyntax: DeclSyntaxProtocol, SyntaxHashable { _ unexpectedBetweenModifiersAndPrecedencegroupKeyword: UnexpectedNodesSyntax? = nil, precedencegroupKeyword: TokenSyntax = .keyword(.precedencegroup), _ unexpectedBetweenPrecedencegroupKeywordAndIdentifier: UnexpectedNodesSyntax? = nil, - identifier: TokenSyntax = .identifier("IdentifierToken"), + identifier: TokenSyntax, _ unexpectedBetweenIdentifierAndLeftBrace: UnexpectedNodesSyntax? = nil, leftBrace: TokenSyntax = .leftBraceToken(), _ unexpectedBetweenLeftBraceAndGroupAttributes: UnexpectedNodesSyntax? = nil, @@ -5143,7 +5143,7 @@ public struct ProtocolDeclSyntax: DeclSyntaxProtocol, SyntaxHashable { _ unexpectedBetweenModifiersAndProtocolKeyword: UnexpectedNodesSyntax? = nil, protocolKeyword: TokenSyntax = .keyword(.protocol), _ unexpectedBetweenProtocolKeywordAndIdentifier: UnexpectedNodesSyntax? = nil, - identifier: TokenSyntax = .identifier("IdentifierToken"), + identifier: TokenSyntax, _ unexpectedBetweenIdentifierAndPrimaryAssociatedTypeClause: UnexpectedNodesSyntax? = nil, primaryAssociatedTypeClause: PrimaryAssociatedTypeClauseSyntax? = nil, _ unexpectedBetweenPrimaryAssociatedTypeClauseAndInheritanceClause: UnexpectedNodesSyntax? = nil, @@ -5476,7 +5476,7 @@ public struct StructDeclSyntax: DeclSyntaxProtocol, SyntaxHashable { _ unexpectedBetweenModifiersAndStructKeyword: UnexpectedNodesSyntax? = nil, structKeyword: TokenSyntax = .keyword(.struct), _ unexpectedBetweenStructKeywordAndIdentifier: UnexpectedNodesSyntax? = nil, - identifier: TokenSyntax = .identifier("IdentifierToken"), + identifier: TokenSyntax, _ unexpectedBetweenIdentifierAndGenericParameterClause: UnexpectedNodesSyntax? = nil, genericParameterClause: GenericParameterClauseSyntax? = nil, _ unexpectedBetweenGenericParameterClauseAndInheritanceClause: UnexpectedNodesSyntax? = nil, @@ -6184,7 +6184,7 @@ public struct TypealiasDeclSyntax: DeclSyntaxProtocol, SyntaxHashable { _ unexpectedBetweenModifiersAndTypealiasKeyword: UnexpectedNodesSyntax? = nil, typealiasKeyword: TokenSyntax = .keyword(.typealias), _ unexpectedBetweenTypealiasKeywordAndIdentifier: UnexpectedNodesSyntax? = nil, - identifier: TokenSyntax = .identifier("IdentifierToken"), + identifier: TokenSyntax, _ unexpectedBetweenIdentifierAndGenericParameterClause: UnexpectedNodesSyntax? = nil, genericParameterClause: GenericParameterClauseSyntax? = nil, _ unexpectedBetweenGenericParameterClauseAndInitializer: UnexpectedNodesSyntax? = nil, diff --git a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxExprNodes.swift b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxExprNodes.swift index 6a062c3adf3..8707e257ac9 100644 --- a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxExprNodes.swift +++ b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxExprNodes.swift @@ -733,7 +733,7 @@ public struct BinaryOperatorExprSyntax: ExprSyntaxProtocol, SyntaxHashable { public init( leadingTrivia: Trivia? = nil, _ unexpectedBeforeOperatorToken: UnexpectedNodesSyntax? = nil, - operatorToken: TokenSyntax = .binaryOperator("BinaryOperatorToken"), + operatorToken: TokenSyntax, _ unexpectedAfterOperatorToken: UnexpectedNodesSyntax? = nil, trailingTrivia: Trivia? = nil @@ -1517,7 +1517,7 @@ public struct EditorPlaceholderExprSyntax: ExprSyntaxProtocol, SyntaxHashable { public init( leadingTrivia: Trivia? = nil, _ unexpectedBeforeIdentifier: UnexpectedNodesSyntax? = nil, - identifier: TokenSyntax = .identifier("IdentifierToken"), + identifier: TokenSyntax, _ unexpectedAfterIdentifier: UnexpectedNodesSyntax? = nil, trailingTrivia: Trivia? = nil @@ -1603,7 +1603,7 @@ public struct FloatLiteralExprSyntax: ExprSyntaxProtocol, SyntaxHashable { public init( leadingTrivia: Trivia? = nil, _ unexpectedBeforeFloatingDigits: UnexpectedNodesSyntax? = nil, - floatingDigits: TokenSyntax = .floatingLiteral("FloatingLiteralToken"), + floatingDigits: TokenSyntax, _ unexpectedAfterFloatingDigits: UnexpectedNodesSyntax? = nil, trailingTrivia: Trivia? = nil @@ -2774,7 +2774,7 @@ public struct IntegerLiteralExprSyntax: ExprSyntaxProtocol, SyntaxHashable { public init( leadingTrivia: Trivia? = nil, _ unexpectedBeforeDigits: UnexpectedNodesSyntax? = nil, - digits: TokenSyntax = .integerLiteral("IntegerLiteralToken"), + digits: TokenSyntax, _ unexpectedAfterDigits: UnexpectedNodesSyntax? = nil, trailingTrivia: Trivia? = nil @@ -3226,7 +3226,7 @@ public struct MacroExpansionExprSyntax: ExprSyntaxProtocol, SyntaxHashable { _ unexpectedBeforePoundToken: UnexpectedNodesSyntax? = nil, poundToken: TokenSyntax = .poundToken(), _ unexpectedBetweenPoundTokenAndMacro: UnexpectedNodesSyntax? = nil, - macro: TokenSyntax = .identifier("IdentifierToken"), + macro: TokenSyntax, _ unexpectedBetweenMacroAndGenericArguments: UnexpectedNodesSyntax? = nil, genericArguments: GenericArgumentClauseSyntax? = nil, _ unexpectedBetweenGenericArgumentsAndLeftParen: UnexpectedNodesSyntax? = nil, @@ -4597,7 +4597,7 @@ public struct PostfixUnaryExprSyntax: ExprSyntaxProtocol, SyntaxHashable { _ unexpectedBeforeExpression: UnexpectedNodesSyntax? = nil, expression: E, _ unexpectedBetweenExpressionAndOperatorToken: UnexpectedNodesSyntax? = nil, - operatorToken: TokenSyntax = .postfixOperator("PostfixOperatorToken"), + operatorToken: TokenSyntax, _ unexpectedAfterOperatorToken: UnexpectedNodesSyntax? = nil, trailingTrivia: Trivia? = nil @@ -4853,7 +4853,7 @@ public struct RegexLiteralExprSyntax: ExprSyntaxProtocol, SyntaxHashable { _ unexpectedBetweenOpeningPoundsAndOpenSlash: UnexpectedNodesSyntax? = nil, openSlash: TokenSyntax = .regexSlashToken(), _ unexpectedBetweenOpenSlashAndRegexPattern: UnexpectedNodesSyntax? = nil, - regexPattern: TokenSyntax = .regexLiteralPattern("RegexLiteralPatternToken"), + regexPattern: TokenSyntax, _ unexpectedBetweenRegexPatternAndCloseSlash: UnexpectedNodesSyntax? = nil, closeSlash: TokenSyntax = .regexSlashToken(), _ unexpectedBetweenCloseSlashAndClosingPounds: UnexpectedNodesSyntax? = nil, diff --git a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodes.swift b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodes.swift index 08e44ba5545..d24474d2a06 100644 --- a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodes.swift +++ b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodes.swift @@ -466,7 +466,7 @@ public struct AccessorParameterSyntax: SyntaxProtocol, SyntaxHashable { _ unexpectedBeforeLeftParen: UnexpectedNodesSyntax? = nil, leftParen: TokenSyntax = .leftParenToken(), _ unexpectedBetweenLeftParenAndName: UnexpectedNodesSyntax? = nil, - name: TokenSyntax = .identifier("IdentifierToken"), + name: TokenSyntax, _ unexpectedBetweenNameAndRightParen: UnexpectedNodesSyntax? = nil, rightParen: TokenSyntax = .rightParenToken(), _ unexpectedAfterRightParen: UnexpectedNodesSyntax? = nil, @@ -2139,7 +2139,7 @@ public struct AvailabilityVersionRestrictionSyntax: SyntaxProtocol, SyntaxHashab public init( leadingTrivia: Trivia? = nil, _ unexpectedBeforePlatform: UnexpectedNodesSyntax? = nil, - platform: TokenSyntax = .identifier("IdentifierToken"), + platform: TokenSyntax, _ unexpectedBetweenPlatformAndVersion: UnexpectedNodesSyntax? = nil, version: VersionTupleSyntax? = nil, _ unexpectedAfterVersion: UnexpectedNodesSyntax? = nil, @@ -5363,7 +5363,7 @@ public struct ConventionAttributeArgumentsSyntax: SyntaxProtocol, SyntaxHashable public init( leadingTrivia: Trivia? = nil, _ unexpectedBeforeConventionLabel: UnexpectedNodesSyntax? = nil, - conventionLabel: TokenSyntax = .identifier("IdentifierToken"), + conventionLabel: TokenSyntax, _ unexpectedBetweenConventionLabelAndComma: UnexpectedNodesSyntax? = nil, comma: TokenSyntax? = nil, _ unexpectedBetweenCommaAndCTypeLabel: UnexpectedNodesSyntax? = nil, @@ -5579,7 +5579,7 @@ public struct ConventionWitnessMethodAttributeArgumentsSyntax: SyntaxProtocol, S _ unexpectedBetweenWitnessMethodLabelAndColon: UnexpectedNodesSyntax? = nil, colon: TokenSyntax = .colonToken(), _ unexpectedBetweenColonAndProtocolName: UnexpectedNodesSyntax? = nil, - protocolName: TokenSyntax = .identifier("IdentifierToken"), + protocolName: TokenSyntax, _ unexpectedAfterProtocolName: UnexpectedNodesSyntax? = nil, trailingTrivia: Trivia? = nil @@ -8119,7 +8119,7 @@ public struct EnumCaseElementSyntax: SyntaxProtocol, SyntaxHashable { public init( leadingTrivia: Trivia? = nil, _ unexpectedBeforeIdentifier: UnexpectedNodesSyntax? = nil, - identifier: TokenSyntax = .identifier("IdentifierToken"), + identifier: TokenSyntax, _ unexpectedBetweenIdentifierAndAssociatedValue: UnexpectedNodesSyntax? = nil, associatedValue: EnumCaseParameterClauseSyntax? = nil, _ unexpectedBetweenAssociatedValueAndRawValue: UnexpectedNodesSyntax? = nil, @@ -10308,7 +10308,7 @@ public struct GenericParameterSyntax: SyntaxProtocol, SyntaxHashable { _ unexpectedBetweenAttributesAndEach: UnexpectedNodesSyntax? = nil, each: TokenSyntax? = nil, _ unexpectedBetweenEachAndName: UnexpectedNodesSyntax? = nil, - name: TokenSyntax = .identifier("IdentifierToken"), + name: TokenSyntax, _ unexpectedBetweenNameAndColon: UnexpectedNodesSyntax? = nil, colon: TokenSyntax? = nil, _ unexpectedBetweenColonAndInheritedType: UnexpectedNodesSyntax? = nil, @@ -10380,7 +10380,7 @@ public struct GenericParameterSyntax: SyntaxProtocol, SyntaxHashable { _ unexpectedBetweenAttributesAndEach: UnexpectedNodesSyntax? = nil, each: TokenSyntax? = nil, _ unexpectedBetweenEachAndName: UnexpectedNodesSyntax? = nil, - name: TokenSyntax = .identifier("IdentifierToken"), + name: TokenSyntax, _ unexpectedBetweenNameAndColon: UnexpectedNodesSyntax? = nil, colon: TokenSyntax? = nil, _ unexpectedBetweenColonAndInheritedType: UnexpectedNodesSyntax? = nil, @@ -13587,7 +13587,7 @@ public struct OpaqueReturnTypeOfAttributeArgumentsSyntax: SyntaxProtocol, Syntax _ unexpectedBetweenMangledNameAndComma: UnexpectedNodesSyntax? = nil, comma: TokenSyntax = .commaToken(), _ unexpectedBetweenCommaAndOrdinal: UnexpectedNodesSyntax? = nil, - ordinal: TokenSyntax = .integerLiteral("IntegerLiteralToken"), + ordinal: TokenSyntax, _ unexpectedAfterOrdinal: UnexpectedNodesSyntax? = nil, trailingTrivia: Trivia? = nil @@ -13742,7 +13742,7 @@ public struct OperatorPrecedenceAndTypesSyntax: SyntaxProtocol, SyntaxHashable { _ unexpectedBeforeColon: UnexpectedNodesSyntax? = nil, colon: TokenSyntax = .colonToken(), _ unexpectedBetweenColonAndPrecedenceGroup: UnexpectedNodesSyntax? = nil, - precedenceGroup: TokenSyntax = .identifier("IdentifierToken"), + precedenceGroup: TokenSyntax, _ unexpectedBetweenPrecedenceGroupAndDesignatedTypes: UnexpectedNodesSyntax? = nil, designatedTypes: DesignatedTypeListSyntax, _ unexpectedAfterDesignatedTypes: UnexpectedNodesSyntax? = nil, @@ -14768,7 +14768,7 @@ public struct PoundSourceLocationArgsSyntax: SyntaxProtocol, SyntaxHashable { _ unexpectedBetweenLineArgLabelAndLineArgColon: UnexpectedNodesSyntax? = nil, lineArgColon: TokenSyntax = .colonToken(), _ unexpectedBetweenLineArgColonAndLineNumber: UnexpectedNodesSyntax? = nil, - lineNumber: TokenSyntax = .integerLiteral("IntegerLiteralToken"), + lineNumber: TokenSyntax, _ unexpectedAfterLineNumber: UnexpectedNodesSyntax? = nil, trailingTrivia: Trivia? = nil @@ -15335,7 +15335,7 @@ public struct PrecedenceGroupNameElementSyntax: SyntaxProtocol, SyntaxHashable { public init( leadingTrivia: Trivia? = nil, _ unexpectedBeforeName: UnexpectedNodesSyntax? = nil, - name: TokenSyntax = .identifier("IdentifierToken"), + name: TokenSyntax, _ unexpectedBetweenNameAndTrailingComma: UnexpectedNodesSyntax? = nil, trailingComma: TokenSyntax? = nil, _ unexpectedAfterTrailingComma: UnexpectedNodesSyntax? = nil, @@ -15812,7 +15812,7 @@ public struct PrimaryAssociatedTypeSyntax: SyntaxProtocol, SyntaxHashable { public init( leadingTrivia: Trivia? = nil, _ unexpectedBeforeName: UnexpectedNodesSyntax? = nil, - name: TokenSyntax = .identifier("IdentifierToken"), + name: TokenSyntax, _ unexpectedBetweenNameAndTrailingComma: UnexpectedNodesSyntax? = nil, trailingComma: TokenSyntax? = nil, _ unexpectedAfterTrailingComma: UnexpectedNodesSyntax? = nil, @@ -16448,7 +16448,7 @@ public struct SourceFileSyntax: SyntaxProtocol, SyntaxHashable { _ unexpectedBeforeStatements: UnexpectedNodesSyntax? = nil, statements: CodeBlockItemListSyntax, _ unexpectedBetweenStatementsAndEOFToken: UnexpectedNodesSyntax? = nil, - eofToken: TokenSyntax, + eofToken: TokenSyntax = .eof(), _ unexpectedAfterEOFToken: UnexpectedNodesSyntax? = nil, trailingTrivia: Trivia? = nil @@ -16592,7 +16592,7 @@ public struct StringSegmentSyntax: SyntaxProtocol, SyntaxHashable { public init( leadingTrivia: Trivia? = nil, _ unexpectedBeforeContent: UnexpectedNodesSyntax? = nil, - content: TokenSyntax = .stringSegment("StringSegmentToken"), + content: TokenSyntax, _ unexpectedAfterContent: UnexpectedNodesSyntax? = nil, trailingTrivia: Trivia? = nil @@ -18880,7 +18880,7 @@ public struct VersionTupleSyntax: SyntaxProtocol, SyntaxHashable { public init( leadingTrivia: Trivia? = nil, _ unexpectedBeforeMajor: UnexpectedNodesSyntax? = nil, - major: TokenSyntax = .integerLiteral("IntegerLiteralToken"), + major: TokenSyntax, _ unexpectedBetweenMajorAndMinorPeriod: UnexpectedNodesSyntax? = nil, minorPeriod: TokenSyntax? = nil, _ unexpectedBetweenMinorPeriodAndMinor: UnexpectedNodesSyntax? = nil, diff --git a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxStmtNodes.swift b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxStmtNodes.swift index ab7ca1abcaf..e1aac894b6b 100644 --- a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxStmtNodes.swift +++ b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxStmtNodes.swift @@ -1443,7 +1443,7 @@ public struct LabeledStmtSyntax: StmtSyntaxProtocol, SyntaxHashable { public init( leadingTrivia: Trivia? = nil, _ unexpectedBeforeLabelName: UnexpectedNodesSyntax? = nil, - labelName: TokenSyntax = .identifier("IdentifierToken"), + labelName: TokenSyntax, _ unexpectedBetweenLabelNameAndLabelColon: UnexpectedNodesSyntax? = nil, labelColon: TokenSyntax = .colonToken(), _ unexpectedBetweenLabelColonAndStatement: UnexpectedNodesSyntax? = nil, diff --git a/Sources/SwiftSyntaxBuilder/generated/BuildableNodes.swift b/Sources/SwiftSyntaxBuilder/generated/BuildableNodes.swift index 1f01971df8d..371cbc63364 100644 --- a/Sources/SwiftSyntaxBuilder/generated/BuildableNodes.swift +++ b/Sources/SwiftSyntaxBuilder/generated/BuildableNodes.swift @@ -538,7 +538,7 @@ extension ForInStmtSyntax { unexpectedBetweenForKeywordAndTryKeyword: UnexpectedNodesSyntax? = nil, tryKeyword: TokenSyntax? = nil, unexpectedBetweenTryKeywordAndAwaitKeyword: UnexpectedNodesSyntax? = nil, - awaitKeyword: TokenSyntax? = .keyword(.await), + awaitKeyword: TokenSyntax? = nil, unexpectedBetweenAwaitKeywordAndCaseKeyword: UnexpectedNodesSyntax? = nil, caseKeyword: TokenSyntax? = nil, unexpectedBetweenCaseKeywordAndPattern: UnexpectedNodesSyntax? = nil, diff --git a/Tests/SwiftSyntaxBuilderTest/ForInStmtTests.swift b/Tests/SwiftSyntaxBuilderTest/ForInStmtTests.swift new file mode 100644 index 00000000000..bfce5852a98 --- /dev/null +++ b/Tests/SwiftSyntaxBuilderTest/ForInStmtTests.swift @@ -0,0 +1,84 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import XCTest +import SwiftSyntax +import SwiftSyntaxBuilder + +final class ForInStmtTests: XCTestCase { + func testEmptyForInStmtSyntax() throws { + let buildable = ForInStmtSyntax(pattern: PatternSyntax("foo"), sequenceExpr: ExprSyntax("bar")) {} + assertBuildResult( + buildable, + """ + for foo in bar { + } + """ + ) + } + + func testForInStmtSyntax() throws { + let testCases: [UInt: (ForInStmtSyntax, String)] = [ + #line: ( + StmtSyntax( + """ + for foo in bar { + _ = foo + } + """ + ).cast(ForInStmtSyntax.self), + """ + for foo in bar { + _ = foo + } + """ + ), + #line: ( + try ForInStmtSyntax("for try await foo in bar") { ExprSyntax("print(foo)") }, + """ + for try await foo in bar { + print(foo) + } + """ + ), + ] + + for (line, testCase) in testCases { + let (builder, expected) = testCase + assertBuildResult(builder, expected, line: line) + } + } + + func testEffectiveForInStmtSyntax() throws { + let buildable = ForInStmtSyntax( + tryKeyword: .keyword(.try), + awaitKeyword: .keyword(.await), + pattern: PatternSyntax("foo"), + sequenceExpr: ExprSyntax("bar"), + body: CodeBlockSyntax( + statements: [ + .init(item: .decl("let baz = await foo.baz")), + .init(item: .expr("print(foo.baz)")), + ] + ) + ) + assertBuildResult( + buildable, + """ + for try await foo in bar { + let baz = await foo.baz + print(foo.baz) + } + """ + ) + } +}