Skip to content

Commit f144b86

Browse files
committed
improve implementation of without operator
- changes "hasWithout" to "withoutTilde" for now, though the tilde is likely to change to the contextual keyword `without` soon. - improves a test with structural verification - simplifies parsing a bit
1 parent a283982 commit f144b86

File tree

6 files changed

+62
-36
lines changed

6 files changed

+62
-36
lines changed

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ public let DECL_NODES: [Node] = [
10331033
/// indicate the suppression of implicit conformance to this type.
10341034
/// This child stores the token representing the 'without' operator.
10351035
Child(
1036-
name: "HasWithout",
1036+
name: "WithoutTilde",
10371037
kind: .token(choices: [.token(tokenKind: "PrefixOperatorToken")]),
10381038
isOptional: true
10391039
),

Sources/SwiftParser/Nominals.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,14 @@ extension Parser {
289289
)
290290
)
291291
} else {
292-
if self.currentToken.starts(with: "~") {
293-
withoutToken = self.consumePrefix("~", as: .prefixOperator)
294-
}
292+
withoutToken = self.consumeIfContextualPunctuator("~", remapping: .prefixOperator)
295293
type = self.parseType()
296294
}
297295

298296
keepGoing = self.consume(if: .comma)
299297
elements.append(
300298
RawInheritedTypeSyntax(
301-
hasWithout: withoutToken,
299+
withoutTilde: withoutToken,
302300
typeName: type,
303301
trailingComma: keepGoing,
304302
arena: self.arena

Sources/SwiftSyntax/generated/ChildNameForKeyPath.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,12 +1713,12 @@ public func childName(_ keyPath: AnyKeyPath) -> String? {
17131713
return "rightOperand"
17141714
case \InfixOperatorExprSyntax.unexpectedAfterRightOperand:
17151715
return "unexpectedAfterRightOperand"
1716-
case \InheritedTypeSyntax.unexpectedBeforeHasWithout:
1717-
return "unexpectedBeforeHasWithout"
1718-
case \InheritedTypeSyntax.hasWithout:
1719-
return "hasWithout"
1720-
case \InheritedTypeSyntax.unexpectedBetweenHasWithoutAndTypeName:
1721-
return "unexpectedBetweenHasWithoutAndTypeName"
1716+
case \InheritedTypeSyntax.unexpectedBeforeWithoutTilde:
1717+
return "unexpectedBeforeWithoutTilde"
1718+
case \InheritedTypeSyntax.withoutTilde:
1719+
return "withoutTilde"
1720+
case \InheritedTypeSyntax.unexpectedBetweenWithoutTildeAndTypeName:
1721+
return "unexpectedBetweenWithoutTildeAndTypeName"
17221722
case \InheritedTypeSyntax.typeName:
17231723
return "typeName"
17241724
case \InheritedTypeSyntax.unexpectedBetweenTypeNameAndTrailingComma:

Sources/SwiftSyntax/generated/raw/RawSyntaxNodes.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11708,9 +11708,9 @@ public struct RawInheritedTypeSyntax: RawSyntaxNodeProtocol {
1170811708
}
1170911709

1171011710
public init(
11711-
_ unexpectedBeforeHasWithout: RawUnexpectedNodesSyntax? = nil,
11712-
hasWithout: RawTokenSyntax?,
11713-
_ unexpectedBetweenHasWithoutAndTypeName: RawUnexpectedNodesSyntax? = nil,
11711+
_ unexpectedBeforeWithoutTilde: RawUnexpectedNodesSyntax? = nil,
11712+
withoutTilde: RawTokenSyntax?,
11713+
_ unexpectedBetweenWithoutTildeAndTypeName: RawUnexpectedNodesSyntax? = nil,
1171411714
typeName: RawTypeSyntax,
1171511715
_ unexpectedBetweenTypeNameAndTrailingComma: RawUnexpectedNodesSyntax? = nil,
1171611716
trailingComma: RawTokenSyntax?,
@@ -11720,9 +11720,9 @@ public struct RawInheritedTypeSyntax: RawSyntaxNodeProtocol {
1172011720
let raw = RawSyntax.makeLayout(
1172111721
kind: .inheritedType, uninitializedCount: 7, arena: arena) { layout in
1172211722
layout.initialize(repeating: nil)
11723-
layout[0] = unexpectedBeforeHasWithout?.raw
11724-
layout[1] = hasWithout?.raw
11725-
layout[2] = unexpectedBetweenHasWithoutAndTypeName?.raw
11723+
layout[0] = unexpectedBeforeWithoutTilde?.raw
11724+
layout[1] = withoutTilde?.raw
11725+
layout[2] = unexpectedBetweenWithoutTildeAndTypeName?.raw
1172611726
layout[3] = typeName.raw
1172711727
layout[4] = unexpectedBetweenTypeNameAndTrailingComma?.raw
1172811728
layout[5] = trailingComma?.raw
@@ -11731,15 +11731,15 @@ public struct RawInheritedTypeSyntax: RawSyntaxNodeProtocol {
1173111731
self.init(unchecked: raw)
1173211732
}
1173311733

11734-
public var unexpectedBeforeHasWithout: RawUnexpectedNodesSyntax? {
11734+
public var unexpectedBeforeWithoutTilde: RawUnexpectedNodesSyntax? {
1173511735
layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:))
1173611736
}
1173711737

11738-
public var hasWithout: RawTokenSyntax? {
11738+
public var withoutTilde: RawTokenSyntax? {
1173911739
layoutView.children[1].map(RawTokenSyntax.init(raw:))
1174011740
}
1174111741

11742-
public var unexpectedBetweenHasWithoutAndTypeName: RawUnexpectedNodesSyntax? {
11742+
public var unexpectedBetweenWithoutTildeAndTypeName: RawUnexpectedNodesSyntax? {
1174311743
layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:))
1174411744
}
1174511745

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodes.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10564,9 +10564,9 @@ public struct InheritedTypeSyntax: SyntaxProtocol, SyntaxHashable {
1056410564

1056510565
public init<T: TypeSyntaxProtocol>(
1056610566
leadingTrivia: Trivia? = nil,
10567-
_ unexpectedBeforeHasWithout: UnexpectedNodesSyntax? = nil,
10568-
hasWithout: TokenSyntax? = nil,
10569-
_ unexpectedBetweenHasWithoutAndTypeName: UnexpectedNodesSyntax? = nil,
10567+
_ unexpectedBeforeWithoutTilde: UnexpectedNodesSyntax? = nil,
10568+
withoutTilde: TokenSyntax? = nil,
10569+
_ unexpectedBetweenWithoutTildeAndTypeName: UnexpectedNodesSyntax? = nil,
1057010570
typeName: T,
1057110571
_ unexpectedBetweenTypeNameAndTrailingComma: UnexpectedNodesSyntax? = nil,
1057210572
trailingComma: TokenSyntax? = nil,
@@ -10577,18 +10577,18 @@ public struct InheritedTypeSyntax: SyntaxProtocol, SyntaxHashable {
1057710577
// Extend the lifetime of all parameters so their arenas don't get destroyed
1057810578
// before they can be added as children of the new arena.
1057910579
let data: SyntaxData = withExtendedLifetime((SyntaxArena(), (
10580-
unexpectedBeforeHasWithout,
10581-
hasWithout,
10582-
unexpectedBetweenHasWithoutAndTypeName,
10580+
unexpectedBeforeWithoutTilde,
10581+
withoutTilde,
10582+
unexpectedBetweenWithoutTildeAndTypeName,
1058310583
typeName,
1058410584
unexpectedBetweenTypeNameAndTrailingComma,
1058510585
trailingComma,
1058610586
unexpectedAfterTrailingComma
1058710587
))) {(arena, _) in
1058810588
let layout: [RawSyntax?] = [
10589-
unexpectedBeforeHasWithout?.raw,
10590-
hasWithout?.raw,
10591-
unexpectedBetweenHasWithoutAndTypeName?.raw,
10589+
unexpectedBeforeWithoutTilde?.raw,
10590+
withoutTilde?.raw,
10591+
unexpectedBetweenWithoutTildeAndTypeName?.raw,
1059210592
typeName.raw,
1059310593
unexpectedBetweenTypeNameAndTrailingComma?.raw,
1059410594
trailingComma?.raw,
@@ -10607,7 +10607,7 @@ public struct InheritedTypeSyntax: SyntaxProtocol, SyntaxHashable {
1060710607
self.init(data)
1060810608
}
1060910609

10610-
public var unexpectedBeforeHasWithout: UnexpectedNodesSyntax? {
10610+
public var unexpectedBeforeWithoutTilde: UnexpectedNodesSyntax? {
1061110611
get {
1061210612
return data.child(at: 0, parent: Syntax(self)).map(UnexpectedNodesSyntax.init)
1061310613
}
@@ -10616,7 +10616,7 @@ public struct InheritedTypeSyntax: SyntaxProtocol, SyntaxHashable {
1061610616
}
1061710617
}
1061810618

10619-
public var hasWithout: TokenSyntax? {
10619+
public var withoutTilde: TokenSyntax? {
1062010620
get {
1062110621
return data.child(at: 1, parent: Syntax(self)).map(TokenSyntax.init)
1062210622
}
@@ -10625,7 +10625,7 @@ public struct InheritedTypeSyntax: SyntaxProtocol, SyntaxHashable {
1062510625
}
1062610626
}
1062710627

10628-
public var unexpectedBetweenHasWithoutAndTypeName: UnexpectedNodesSyntax? {
10628+
public var unexpectedBetweenWithoutTildeAndTypeName: UnexpectedNodesSyntax? {
1062910629
get {
1063010630
return data.child(at: 2, parent: Syntax(self)).map(UnexpectedNodesSyntax.init)
1063110631
}
@@ -10672,9 +10672,9 @@ public struct InheritedTypeSyntax: SyntaxProtocol, SyntaxHashable {
1067210672

1067310673
public static var structure: SyntaxNodeStructure {
1067410674
return .layout([
10675-
\Self.unexpectedBeforeHasWithout,
10676-
\Self.hasWithout,
10677-
\Self.unexpectedBetweenHasWithoutAndTypeName,
10675+
\Self.unexpectedBeforeWithoutTilde,
10676+
\Self.withoutTilde,
10677+
\Self.unexpectedBetweenWithoutTildeAndTypeName,
1067810678
\Self.typeName,
1067910679
\Self.unexpectedBetweenTypeNameAndTrailingComma,
1068010680
\Self.trailingComma,

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,9 +1566,37 @@ final class DeclarationTests: XCTestCase {
15661566
assertParse(
15671567
"""
15681568
struct Hello: ~Copyable {}
1569+
""",
1570+
substructure: Syntax(
1571+
InheritedTypeSyntax(
1572+
withoutTilde: .prefixOperator("~"),
1573+
typeName: TypeSyntax(stringLiteral: "Copyable")
1574+
)
1575+
)
1576+
)
15691577

1570-
enum Whatever: Int, ~ Hashable, Equatable {}
1578+
assertParse(
15711579
"""
1580+
enum Whatever: Int, ~ Hashable, Equatable {}
1581+
""",
1582+
substructure:
1583+
Syntax(
1584+
TypeInheritanceClauseSyntax(
1585+
colon: .colonToken(),
1586+
inheritedTypeCollection: InheritedTypeListSyntax([
1587+
InheritedTypeSyntax(
1588+
typeName: TypeSyntax(stringLiteral: "Int"),
1589+
trailingComma: .commaToken()
1590+
),
1591+
InheritedTypeSyntax(
1592+
withoutTilde: .prefixOperator("~"),
1593+
typeName: TypeSyntax(stringLiteral: "Hashable"),
1594+
trailingComma: .commaToken()
1595+
),
1596+
InheritedTypeSyntax(typeName: TypeSyntax(stringLiteral: "Equatable")),
1597+
])
1598+
)
1599+
)
15721600
)
15731601
}
15741602
}

0 commit comments

Comments
 (0)