Skip to content

Handle pattern as editor placeholder #2150

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
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
18 changes: 18 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/PatternNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,22 @@ public let PATTERN_NODES: [Node] = [
]
),

Node(
kind: .editorPlaceholderPattern,
base: .pattern,
nameForDiagnostics: "editor placeholder",
documentation: """
An editor placeholder, e.g. `<#pattern#>` that is used in a position that expects a pattern.
""",
children: [
Child(
name: "placeholder",
kind: .token(choices: [.token(.identifier)]),
documentation: """
The actual editor placeholder that starts with `<#` and ends with `#>`.
"""
)
]
),

]
2 changes: 2 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public enum SyntaxNodeKind: String, CaseIterable {
case dynamicReplacementAttributeArguments
case editorPlaceholderDecl
case editorPlaceholderExpr
case editorPlaceholderPattern
case editorPlaceholderType
case effectsAttributeArgumentList
case enumCaseDecl
case enumCaseElement
Expand Down
24 changes: 24 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -530,4 +530,28 @@ public let TYPE_NODES: [Node] = [
]
),

Node(
kind: .editorPlaceholderType,
base: .type,
nameForDiagnostics: "editor placeholder",
documentation: """
An editor placeholder, e.g. `<#Type#>` that is used in a position that expects a type.
""",
children: [
Child(
name: "placeholder",
kind: .token(choices: [.token(.identifier)]),
documentation: """
The actual editor placeholder that starts with `<#` and ends with `#>`.
"""
),
Child(
name: "genericArgumentClause",
kind: .node(kind: .genericArgumentClause),
documentation: "Generic arguments that is attatched to the type.",
isOptional: true
),
]
),

]
10 changes: 10 additions & 0 deletions Release Notes/510.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
- Description: Remarks are used by the Swift compiler and other tools to describe some aspect of translation that doesn't reflect correctness, but may be useful for the user. Remarks have been added to the diagnostic severity enums to align with the Swift compiler.
- Pull Request: https://github.com/apple/swift-syntax/pull/2143

- New pattern node `EditorPlaceholderPatternSyntax`
- Description: This node type will be placeholder that is used in a position that expects a pattern.
- Issue: https://github.com/apple/swift-syntax/issues/2147
- Pull Request: https://github.com/apple/swift-syntax/pull/2150

- New type node `EditorPlaceholderTypeSyntax`
- Description: This node type will be placeholder that is used in a position that expects a type.
- Issue: https://github.com/apple/swift-syntax/issues/2156
- Pull Request: https://github.com/apple/swift-syntax/pull/2150

## API Behavior Changes

## Deprecations
Expand Down
19 changes: 14 additions & 5 deletions Sources/SwiftParser/Patterns.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,21 @@ extension Parser {
)
case (.lhs(.identifier), let handle)?:
let identifier = self.eat(handle)
return RawPatternSyntax(
RawIdentifierPatternSyntax(
identifier: identifier,
arena: self.arena
if identifier.tokenText.isEditorPlaceholder {
return RawPatternSyntax(
RawEditorPlaceholderPatternSyntax(
placeholder: identifier,
arena: self.arena
)
)
)
} else {
return RawPatternSyntax(
RawIdentifierPatternSyntax(
identifier: identifier,
arena: self.arena
)
)
}
case (.lhs(.dollarIdentifier), let handle)?:
let dollarIdent = self.eat(handle)
let unexpectedBeforeIdentifier = RawUnexpectedNodesSyntax(elements: [RawSyntax(dollarIdent)], arena: self.arena)
Expand Down
29 changes: 21 additions & 8 deletions Sources/SwiftParser/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,25 @@ extension Parser {
generics = nil
}

return RawTypeSyntax(
RawIdentifierTypeSyntax(
unexpectedBeforeName,
name: name,
genericArgumentClause: generics,
arena: self.arena
if name.tokenText.isEditorPlaceholder {
return RawTypeSyntax(
RawEditorPlaceholderTypeSyntax(
placeholder: name,
genericArgumentClause: generics,
arena: self.arena
)
)
)
} else {
return RawTypeSyntax(
RawIdentifierTypeSyntax(
unexpectedBeforeName,
name: name,
genericArgumentClause: generics,
arena: self.arena
)
)
}

}

/// Parse the existential `Any` type.
Expand Down Expand Up @@ -938,7 +949,9 @@ extension Parser {

extension Parser {
mutating func parseResultType() -> RawTypeSyntax {
if self.at(prefix: "<") {
if self.at(prefix: "<#") {
return self.parseTypeIdentifier()
} else if self.at(prefix: "<") {
let generics = self.parseGenericParameters()
let baseType = self.parseType()
return RawTypeSyntax(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ extension SyntaxKind {
return "editor placeholder"
case .editorPlaceholderExpr:
return "editor placeholder"
case .editorPlaceholderPattern:
return "editor placeholder"
case .editorPlaceholderType:
return "editor placeholder"
case .effectsAttributeArgumentList:
return "@_effects arguments"
case .enumCaseDecl:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ These articles are intended for developers wishing to contribute to SwiftSyntax
- <doc:SwiftSyntax/PatternSyntax>
- <doc:SwiftSyntax/PatternSyntaxProtocol>
- <doc:SwiftSyntax/MissingPatternSyntax>
- <doc:SwiftSyntax/EditorPlaceholderPatternSyntax>
- <doc:SwiftSyntax/ExpressionPatternSyntax>
- <doc:SwiftSyntax/IdentifierPatternSyntax>
- <doc:SwiftSyntax/IsTypePatternSyntax>
Expand Down Expand Up @@ -190,6 +191,7 @@ These articles are intended for developers wishing to contribute to SwiftSyntax
- <doc:SwiftSyntax/ClassRestrictionTypeSyntax>
- <doc:SwiftSyntax/CompositionTypeSyntax>
- <doc:SwiftSyntax/DictionaryTypeSyntax>
- <doc:SwiftSyntax/EditorPlaceholderTypeSyntax>
- <doc:SwiftSyntax/FunctionTypeSyntax>
- <doc:SwiftSyntax/IdentifierTypeSyntax>
- <doc:SwiftSyntax/ImplicitlyUnwrappedOptionalTypeSyntax>
Expand Down
16 changes: 16 additions & 0 deletions Sources/SwiftSyntax/generated/ChildNameForKeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,22 @@ public func childName(_ keyPath: AnyKeyPath) -> String? {
return "placeholder"
case \EditorPlaceholderExprSyntax.unexpectedAfterPlaceholder:
return "unexpectedAfterPlaceholder"
case \EditorPlaceholderPatternSyntax.unexpectedBeforePlaceholder:
return "unexpectedBeforePlaceholder"
case \EditorPlaceholderPatternSyntax.placeholder:
return "placeholder"
case \EditorPlaceholderPatternSyntax.unexpectedAfterPlaceholder:
return "unexpectedAfterPlaceholder"
case \EditorPlaceholderTypeSyntax.unexpectedBeforePlaceholder:
return "unexpectedBeforePlaceholder"
case \EditorPlaceholderTypeSyntax.placeholder:
return "placeholder"
case \EditorPlaceholderTypeSyntax.unexpectedBetweenPlaceholderAndGenericArgumentClause:
return "unexpectedBetweenPlaceholderAndGenericArgumentClause"
case \EditorPlaceholderTypeSyntax.genericArgumentClause:
return "genericArgumentClause"
case \EditorPlaceholderTypeSyntax.unexpectedAfterGenericArgumentClause:
return "unexpectedAfterGenericArgumentClause"
case \EnumCaseDeclSyntax.unexpectedBeforeAttributes:
return "unexpectedBeforeAttributes"
case \EnumCaseDeclSyntax.attributes:
Expand Down
16 changes: 16 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxAnyVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,22 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
visitAnyPost(node._syntaxNode)
}

override open func visit(_ node: EditorPlaceholderPatternSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}

override open func visitPost(_ node: EditorPlaceholderPatternSyntax) {
visitAnyPost(node._syntaxNode)
}

override open func visit(_ node: EditorPlaceholderTypeSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}

override open func visitPost(_ node: EditorPlaceholderTypeSyntax) {
visitAnyPost(node._syntaxNode)
}

override open func visit(_ node: EffectsAttributeArgumentListSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/SwiftSyntax/generated/SyntaxBaseNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {

public init?(_ node: some SyntaxProtocol) {
switch node.raw.kind {
case .expressionPattern, .identifierPattern, .isTypePattern, .missingPattern, .tuplePattern, .valueBindingPattern, .wildcardPattern:
case .editorPlaceholderPattern, .expressionPattern, .identifierPattern, .isTypePattern, .missingPattern, .tuplePattern, .valueBindingPattern, .wildcardPattern:
self._syntaxNode = node._syntaxNode
default:
return nil
Expand All @@ -795,6 +795,7 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {

public static var structure: SyntaxNodeStructure {
return .choices([
.node(EditorPlaceholderPatternSyntax.self),
.node(ExpressionPatternSyntax.self),
.node(IdentifierPatternSyntax.self),
.node(IsTypePatternSyntax.self),
Expand Down Expand Up @@ -1298,7 +1299,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {

public init?(_ node: some SyntaxProtocol) {
switch node.raw.kind {
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .dictionaryType, .functionType, .identifierType, .implicitlyUnwrappedOptionalType, .memberType, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packElementType, .packExpansionType, .someOrAnyType, .suppressedType, .tupleType:
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .dictionaryType, .editorPlaceholderType, .functionType, .identifierType, .implicitlyUnwrappedOptionalType, .memberType, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packElementType, .packExpansionType, .someOrAnyType, .suppressedType, .tupleType:
self._syntaxNode = node._syntaxNode
default:
return nil
Expand Down Expand Up @@ -1328,6 +1329,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
.node(ClassRestrictionTypeSyntax.self),
.node(CompositionTypeSyntax.self),
.node(DictionaryTypeSyntax.self),
.node(EditorPlaceholderTypeSyntax.self),
.node(FunctionTypeSyntax.self),
.node(IdentifierTypeSyntax.self),
.node(ImplicitlyUnwrappedOptionalTypeSyntax.self),
Expand Down Expand Up @@ -1486,6 +1488,8 @@ extension Syntax {
.node(DynamicReplacementAttributeArgumentsSyntax.self),
.node(EditorPlaceholderDeclSyntax.self),
.node(EditorPlaceholderExprSyntax.self),
.node(EditorPlaceholderPatternSyntax.self),
.node(EditorPlaceholderTypeSyntax.self),
.node(EffectsAttributeArgumentListSyntax.self),
.node(EnumCaseDeclSyntax.self),
.node(EnumCaseElementListSyntax.self),
Expand Down
6 changes: 6 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public enum SyntaxEnum {
case dynamicReplacementAttributeArguments(DynamicReplacementAttributeArgumentsSyntax)
case editorPlaceholderDecl(EditorPlaceholderDeclSyntax)
case editorPlaceholderExpr(EditorPlaceholderExprSyntax)
case editorPlaceholderPattern(EditorPlaceholderPatternSyntax)
case editorPlaceholderType(EditorPlaceholderTypeSyntax)
case effectsAttributeArgumentList(EffectsAttributeArgumentListSyntax)
case enumCaseDecl(EnumCaseDeclSyntax)
case enumCaseElementList(EnumCaseElementListSyntax)
Expand Down Expand Up @@ -479,6 +481,10 @@ public extension Syntax {
return .editorPlaceholderDecl(EditorPlaceholderDeclSyntax(self)!)
case .editorPlaceholderExpr:
return .editorPlaceholderExpr(EditorPlaceholderExprSyntax(self)!)
case .editorPlaceholderPattern:
return .editorPlaceholderPattern(EditorPlaceholderPatternSyntax(self)!)
case .editorPlaceholderType:
return .editorPlaceholderType(EditorPlaceholderTypeSyntax(self)!)
case .effectsAttributeArgumentList:
return .effectsAttributeArgumentList(EffectsAttributeArgumentListSyntax(self)!)
case .enumCaseDecl:
Expand Down
6 changes: 6 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public enum SyntaxKind: CaseIterable {
case dynamicReplacementAttributeArguments
case editorPlaceholderDecl
case editorPlaceholderExpr
case editorPlaceholderPattern
case editorPlaceholderType
case effectsAttributeArgumentList
case enumCaseDecl
case enumCaseElementList
Expand Down Expand Up @@ -600,6 +602,10 @@ public enum SyntaxKind: CaseIterable {
return EditorPlaceholderDeclSyntax.self
case .editorPlaceholderExpr:
return EditorPlaceholderExprSyntax.self
case .editorPlaceholderPattern:
return EditorPlaceholderPatternSyntax.self
case .editorPlaceholderType:
return EditorPlaceholderTypeSyntax.self
case .effectsAttributeArgumentList:
return EffectsAttributeArgumentListSyntax.self
case .enumCaseDecl:
Expand Down
26 changes: 26 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,20 @@ open class SyntaxRewriter {
return ExprSyntax(visitChildren(node))
}

/// Visit a ``EditorPlaceholderPatternSyntax``.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
open func visit(_ node: EditorPlaceholderPatternSyntax) -> PatternSyntax {
return PatternSyntax(visitChildren(node))
}

/// Visit a ``EditorPlaceholderTypeSyntax``.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
open func visit(_ node: EditorPlaceholderTypeSyntax) -> TypeSyntax {
return TypeSyntax(visitChildren(node))
}

/// Visit a ``EffectsAttributeArgumentListSyntax``.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
Expand Down Expand Up @@ -2456,6 +2470,14 @@ open class SyntaxRewriter {
return {
self.visitImpl($0, EditorPlaceholderExprSyntax.self, self.visit)
}
case .editorPlaceholderPattern:
return {
self.visitImpl($0, EditorPlaceholderPatternSyntax.self, self.visit)
}
case .editorPlaceholderType:
return {
self.visitImpl($0, EditorPlaceholderTypeSyntax.self, self.visit)
}
case .effectsAttributeArgumentList:
return {
self.visitImpl($0, EffectsAttributeArgumentListSyntax.self, self.visit)
Expand Down Expand Up @@ -3390,6 +3412,10 @@ open class SyntaxRewriter {
return visitImpl(node, EditorPlaceholderDeclSyntax.self, visit)
case .editorPlaceholderExpr:
return visitImpl(node, EditorPlaceholderExprSyntax.self, visit)
case .editorPlaceholderPattern:
return visitImpl(node, EditorPlaceholderPatternSyntax.self, visit)
case .editorPlaceholderType:
return visitImpl(node, EditorPlaceholderTypeSyntax.self, visit)
case .effectsAttributeArgumentList:
return visitImpl(node, EffectsAttributeArgumentListSyntax.self, visit)
case .enumCaseDecl:
Expand Down
Loading