Skip to content

Commit 6b11622

Browse files
committed
Merge remote-tracking branch 'origin/main' into add-fixed-issues-test
2 parents 493840e + c0f3642 commit 6b11622

File tree

71 files changed

+3324
-3186
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+3324
-3186
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ let package = Package(
146146
"PatternNodes.swift.gyb",
147147
"StmtNodes.swift.gyb",
148148
"SyntaxBaseKinds.swift.gyb",
149-
"Tokens.swift.gyb",
149+
"TokenSpec.swift.gyb",
150150
"Traits.swift.gyb",
151151
"Trivia.swift.gyb",
152152
"TypeNodes.swift.gyb"

Sources/SwiftParser/Diagnostics/ParserDiagnosticMessages.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public enum StaticParserError: String, DiagnosticMessage {
9090
}
9191

9292
public enum StaticParserFixIt: String, FixItMessage {
93-
case moveThrowBeforeArrow = "Move 'throws' in before of '->'"
93+
case moveThrowBeforeArrow = "Move 'throws' before '->'"
9494

9595
public var message: String { self.rawValue }
9696

Sources/SwiftParser/SwiftParser.docc/FixingBugs.md

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ Once you’ve written a test case (see below), set a breakpoint in `Parser.parse
1010

1111
1. Add a new test case in `SwiftParserTest` that looks like the following
1212
```swift
13-
try AssertParse({ $0.parseSourceFile() }) {
13+
AssertParse(
1414
"""
1515
<#your code that does not round trip#>
1616
"""
17-
}
17+
)
1818
```
1919
2. Run the test case, read the error message to figure out which part of the source file does not round-trip
20-
3. Optional: Reduce the test case even further by deleting more source code and calling into a specific production of the parser instead of `Parser.parseSourceFile`
2120

2221

2322
## Parse of Valid Source Failed
@@ -28,20 +27,14 @@ Diagnostics are produced when the parsed syntax tree contains missing or unexpec
2827

2928
1. Add a test case in `SwiftParserTest` that looks like the following
3029
```swift
31-
let source = """
32-
<#your code that produces an invalid syntax tree#>
33-
"""
34-
35-
let tree = withParser(source: source) {
36-
Syntax(raw: $0.parseSourceFile().raw)
37-
}
38-
XCTAssertHasSubstructure(
39-
tree,
40-
<#create a syntax node that you expect the tree to have#>
30+
AssertParse(
31+
"""
32+
<#your code that produces an invalid syntax tree#>
33+
""",
34+
substructure: <#create a syntax node that you expect the tree to have#>
4135
)
4236
```
43-
2. Optional: Reduce the test case even further by deleting more source code and calling into a specific production of the parser instead of `Parser.parseSourceFile`
44-
3. Run the test case and navigate the debugger to the place that produced the invalid syntax node.
37+
2. Run the test case and navigate the debugger to the place that produced the invalid syntax node.
4538

4639
## Unhelpful Diagnostic Produced
4740

@@ -51,31 +44,29 @@ Unhelpful diagnostics can result from two reasons:
5144

5245
To distinguish these cases run the following command and look at the dumped syntax tree. Use your own judgment to decide whether this models the intended meaning of the source code reasonably well.
5346
```
54-
swift-parser-test print-tree /path/to/file/with/bad/diagnostic
47+
swift-parser-test print-tree /path/to/file/with/unhelpful/diagnostic.swift
5548
```
5649
5750
Fixing the first case where the parser does not recover according to the user’s intent is similar to [Parse of Valid Source Code Produced an Invalid Syntax Tree](#Parse-of-Valid-Source-Code-Produced-an-Invalid-Syntax-Tree). See <doc:SwiftParser/ParserRecovery> for documentation how parser recovery works and determine how to recover better from the invalid source code.
5851
5952
To add a new, more contextual diagnostic, perform the following steps.
6053
61-
1. Add a test case to `DiagnosticTests.swift` like the following:
54+
1. Add a test case in `SwiftParserTest` that looks like the following
6255
6356
```swift
64-
let source = """
65-
<#your code that produces a bad diagnostic#>
66-
}
67-
"""
68-
let loop = withParser(source: source) {
69-
Syntax(raw: $0.parserSourceFile().raw)
70-
}
57+
AssertParse(
58+
"""
59+
<#your code that produced the unhelpful diagnostic#>
60+
""",
61+
diagnostics: [
62+
DiagnosticSpec(message: "<#expected diagnostic message#>")
63+
]
64+
)
7165
```
72-
2. Optional: Call a more specific production than `parseSourceFile` in the test case.
66+
2. Mark the location at which you expect the diagnostic to be produced with `#^DIAG^#`. If you expect multiple diagnostics to be produced, you can use multiple of these markers with different names and use these markers by passing a `locationMarker` to `DiagnosticSpec`.
7367
3. Determine which node encompasses all information that is necessary to produce the improved diagnostic – for example `FunctionSignatureSyntax` contains all information to diagnose if the `throws` keyword was written after the `->` instead of in front of it.
7468
4. If the diagnostic message you want to emit does not exist yet, add a case to <doc:SwiftParser/DiagnosticKind> for the new diagnostic.
7569
5. If the function does not already exist, write a new visit method on <doc:SwiftParser/ParseDiagnosticsGenerator>.
7670
6. In that visitation method, detect the pattern for which the improved diagnostic should be emitted and emit it using `diagnostics.append`.
7771
7. Mark the missing or garbage nodes that are covered by the new diagnostic as handled by adding their `SyntaxIdentifier`s to `handledNodes`.
78-
8. Assert that the new diagnostic is emitted by addding the following to your test case:
79-
```swift
80-
XCTAssertSingleDiagnostic(in: tree, line: <#expected line#>, column: <#expected column#>, expectedKind: .<#expected diagnostic kind#>)
81-
```
72+
8. If the diagnostic produces Fix-Its assert that they are generated by adding the Fix-It's message to the `DiagnosticSpec` with the `fixIt` parameter and asserting that applying the Fix-Its produces the correct source code by adding the `fixedSource` parameter to `AssertParse`.

Sources/SwiftSyntaxBuilder/CatchClauseConvenienceInitializer.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/CatchClauseConvenienceInitializer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension CatchClause {
2121
) {
2222
self.init(
2323
leadingTrivia: leadingTrivia,
24-
catchKeyword: .catchKeyword(trailingTrivia: catchItems.elements.isEmpty ? [] : .space),
24+
catchKeyword: .catch.withTrailingTrivia(catchItems.elements.isEmpty ? [] : .space),
2525
catchItems: catchItems,
2626
body: bodyBuilder()
2727
)

Sources/SwiftSyntaxBuilder/DictionaryExprConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/DictionaryExprConvenienceInitializers.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ extension DictionaryExpr {
1616
/// A convenience initializer that allows passing in members using a result builder
1717
/// instead of having to wrap them in a `DictionaryElementList`.
1818
public init(
19-
leftSquare: TokenSyntax = .`leftSquareBracket`,
20-
rightSquare: TokenSyntax = .`rightSquareBracket`,
19+
leftSquare: Token = .`leftSquareBracket`,
20+
rightSquare: Token = .`rightSquareBracket`,
2121
@DictionaryElementListBuilder contentBuilder: () -> ExpressibleAsDictionaryElementList = { [] }
2222
) {
2323
let elementList = contentBuilder().createDictionaryElementList()
2424
self.init(
2525
leftSquare: leftSquare,
26-
content: elementList.elements.isEmpty ? TokenSyntax.colonToken(trailingTrivia: []) : elementList,
26+
content: elementList.elements.isEmpty ? Token.colon.withTrailingTrivia([]) : elementList,
2727
rightSquare: rightSquare
2828
)
2929
}

Sources/SwiftSyntaxBuilder/IfStmtConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/IfStmtConvenienceInitializers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public extension IfStmt {
2727
leadingTrivia: leadingTrivia,
2828
conditions: conditions,
2929
body: body(),
30-
elseKeyword: generatedElseBody == nil ? nil : TokenSyntax.elseKeyword(leadingTrivia: .space, trailingTrivia: []),
30+
elseKeyword: generatedElseBody == nil ? nil : Token.else.withLeadingTrivia(.space).withTrailingTrivia([]),
3131
elseBody: generatedElseBody.map { CodeBlock(statements: $0) }
3232
)
3333
}

Sources/SwiftSyntaxBuilder/MemberAccessExprConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/MemberAccessExprConvenienceInitializers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension MemberAccessExpr {
1616
/// Creates a `MemberAccessExpr` using the provided parameters.
1717
public init(
1818
base: ExpressibleAsExprBuildable? = nil,
19-
dot: TokenSyntax = .period,
19+
dot: Token = .period,
2020
name: String,
2121
declNameArguments: ExpressibleAsDeclNameArguments? = nil
2222
) {

Sources/SwiftSyntaxBuilder/StringLiteralExprConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/StringLiteralExprConvenienceInitializers.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ private let rawStringPotentialEscapesPattern = try! NSRegularExpression(
2626
extension StringLiteralExpr {
2727
/// Creates a string literal, optionally specifying quotes and delimiters.
2828
public init(
29-
openDelimiter: TokenSyntax? = nil,
30-
openQuote: TokenSyntax = .stringQuote,
29+
openDelimiter: Token? = nil,
30+
openQuote: Token = .stringQuote,
3131
_ value: String,
32-
closeQuote: TokenSyntax = .stringQuote,
33-
closeDelimiter: TokenSyntax? = nil
32+
closeQuote: Token = .stringQuote,
33+
closeDelimiter: Token? = nil
3434
) {
35-
let content = TokenSyntax.stringSegment(value)
35+
let content = Token.stringSegment(value)
3636
let segment = StringSegment(content: content)
3737
let segments = StringLiteralSegments([segment])
3838

@@ -57,7 +57,7 @@ extension StringLiteralExpr {
5757
.max() ?? 0
5858

5959
// Use a delimiter that is exactly one longer
60-
let delimiter = TokenSyntax.rawStringDelimiter(String(repeating: "#", count: 1 + maxPoundCount))
60+
let delimiter = Token.rawStringDelimiter(String(repeating: "#", count: 1 + maxPoundCount))
6161

6262
self.init(
6363
openDelimiter: delimiter,

Sources/SwiftSyntaxBuilder/TernaryExprConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/TernaryExprConvenienceInitializers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ extension TernaryExpr {
2020
) {
2121
self.init(
2222
conditionExpression: condition,
23-
questionMark: .infixQuestionMarkToken(leadingTrivia: .space, trailingTrivia: .space),
23+
questionMark: .infixQuestionMark.withLeadingTrivia(.space).withTrailingTrivia(.space),
2424
firstChoice: firstChoice,
25-
colonMark: .colonToken(leadingTrivia: .space),
25+
colonMark: .colon.withLeadingTrivia(.space),
2626
secondChoice: secondChoice
2727
)
2828
}

Sources/SwiftSyntaxBuilder/TupleExprElementConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/TupleExprElementConvenienceInitializers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public extension TupleExprElement {
1717
/// The presence of the colon will be inferred based on the presence of the label.
1818
init(label: String? = nil, expression: ExpressibleAsExprBuildable) {
1919
self.init(
20-
label: label.map { TokenSyntax.identifier($0) }, colon: label == nil ? nil : .colon, expression: expression)
20+
label: label.map { Token.identifier($0) }, colon: label == nil ? nil : Token.colon, expression: expression)
2121
}
2222
}

Sources/SwiftSyntaxBuilder/VariableDeclConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/VariableDeclConvenienceInitializers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension VariableDecl {
1818
leadingTrivia: Trivia = [],
1919
attributes: ExpressibleAsAttributeList? = nil,
2020
modifiers: ExpressibleAsModifierList? = nil,
21-
_ letOrVarKeyword: TokenSyntax,
21+
_ letOrVarKeyword: Token,
2222
name: ExpressibleAsIdentifierPattern,
2323
type: ExpressibleAsTypeAnnotation? = nil,
2424
initializer: ExpressibleAsInitializerClause? = nil
@@ -50,7 +50,7 @@ extension VariableDecl {
5050
leadingTrivia: leadingTrivia,
5151
attributes: attributes,
5252
modifiers: modifiers,
53-
letOrVarKeyword: .varKeyword(leadingTrivia: attributes != nil ? .space : .zero)
53+
letOrVarKeyword: .var.withLeadingTrivia(attributes != nil ? .space : .zero)
5454
) {
5555
PatternBinding(
5656
pattern: name,

Sources/SwiftSyntaxBuilder/HasTrailingComma.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import SwiftSyntax
1414

1515
protocol HasTrailingComma {
16-
var trailingComma: TokenSyntax? { get }
16+
var trailingComma: Token? { get }
1717

1818
/// Returns this node overriding presence of the trailing comma
1919
func withTrailingComma(_ withComma: Bool) -> Self

Sources/SwiftSyntaxBuilder/generated/BuildableCollectionNodes.swift

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -955,13 +955,13 @@ extension Array: ExpressibleAsEnumCaseElementList where Element == ExpressibleAs
955955
return EnumCaseElementList(self)
956956
}
957957
}
958-
/// `IdentifierList` represents a collection of `TokenSyntax`
958+
/// `IdentifierList` represents a collection of `Token`
959959
public struct IdentifierList: ExpressibleByArrayLiteral, SyntaxBuildable, ExpressibleAsIdentifierList {
960-
let elements: [TokenSyntax]
960+
let elements: [Token]
961961
/// Creates a `IdentifierList` with the provided list of elements.
962962
/// - Parameters:
963-
/// - elements: A list of `TokenSyntax`
964-
public init (_ elements: [TokenSyntax]) {
963+
/// - elements: A list of `Token`
964+
public init (_ elements: [Token]) {
965965
self.elements = elements
966966
}
967967
/// Creates a new `IdentifierList` by flattening the elements in `lists`
@@ -970,11 +970,13 @@ public struct IdentifierList: ExpressibleByArrayLiteral, SyntaxBuildable, Expres
970970
$0.createIdentifierList().elements
971971
}
972972
}
973-
public init (arrayLiteral elements: TokenSyntax...) {
973+
public init (arrayLiteral elements: Token...) {
974974
self.init(elements)
975975
}
976976
public func buildIdentifierList(format: Format) -> IdentifierListSyntax {
977-
let result = IdentifierListSyntax(elements)
977+
let result = IdentifierListSyntax(elements.map {
978+
$0.buildToken()
979+
})
978980
return format._format(syntax: result)
979981
}
980982
public func buildSyntax(format: Format) -> Syntax {
@@ -992,7 +994,7 @@ public struct IdentifierList: ExpressibleByArrayLiteral, SyntaxBuildable, Expres
992994
return self
993995
}
994996
}
995-
extension Array: ExpressibleAsIdentifierList where Element == TokenSyntax {
997+
extension Array: ExpressibleAsIdentifierList where Element == Token {
996998
public func createIdentifierList() -> IdentifierList {
997999
return IdentifierList(self)
9981000
}
@@ -1089,13 +1091,13 @@ extension Array: ExpressibleAsPrecedenceGroupNameList where Element == Expressib
10891091
return PrecedenceGroupNameList(self)
10901092
}
10911093
}
1092-
/// `TokenList` represents a collection of `TokenSyntax`
1094+
/// `TokenList` represents a collection of `Token`
10931095
public struct TokenList: ExpressibleByArrayLiteral, SyntaxBuildable, ExpressibleAsTokenList {
1094-
let elements: [TokenSyntax]
1096+
let elements: [Token]
10951097
/// Creates a `TokenList` with the provided list of elements.
10961098
/// - Parameters:
1097-
/// - elements: A list of `TokenSyntax`
1098-
public init (_ elements: [TokenSyntax]) {
1099+
/// - elements: A list of `Token`
1100+
public init (_ elements: [Token]) {
10991101
self.elements = elements
11001102
}
11011103
/// Creates a new `TokenList` by flattening the elements in `lists`
@@ -1104,11 +1106,13 @@ public struct TokenList: ExpressibleByArrayLiteral, SyntaxBuildable, Expressible
11041106
$0.createTokenList().elements
11051107
}
11061108
}
1107-
public init (arrayLiteral elements: TokenSyntax...) {
1109+
public init (arrayLiteral elements: Token...) {
11081110
self.init(elements)
11091111
}
11101112
public func buildTokenList(format: Format) -> TokenListSyntax {
1111-
let result = TokenListSyntax(elements)
1113+
let result = TokenListSyntax(elements.map {
1114+
$0.buildToken()
1115+
})
11121116
return format._format(syntax: result)
11131117
}
11141118
public func buildSyntax(format: Format) -> Syntax {
@@ -1126,18 +1130,18 @@ public struct TokenList: ExpressibleByArrayLiteral, SyntaxBuildable, Expressible
11261130
return self
11271131
}
11281132
}
1129-
extension Array: ExpressibleAsTokenList where Element == TokenSyntax {
1133+
extension Array: ExpressibleAsTokenList where Element == Token {
11301134
public func createTokenList() -> TokenList {
11311135
return TokenList(self)
11321136
}
11331137
}
1134-
/// `NonEmptyTokenList` represents a collection of `TokenSyntax`
1138+
/// `NonEmptyTokenList` represents a collection of `Token`
11351139
public struct NonEmptyTokenList: ExpressibleByArrayLiteral, SyntaxBuildable, ExpressibleAsNonEmptyTokenList {
1136-
let elements: [TokenSyntax]
1140+
let elements: [Token]
11371141
/// Creates a `NonEmptyTokenList` with the provided list of elements.
11381142
/// - Parameters:
1139-
/// - elements: A list of `TokenSyntax`
1140-
public init (_ elements: [TokenSyntax]) {
1143+
/// - elements: A list of `Token`
1144+
public init (_ elements: [Token]) {
11411145
self.elements = elements
11421146
}
11431147
/// Creates a new `NonEmptyTokenList` by flattening the elements in `lists`
@@ -1146,11 +1150,13 @@ public struct NonEmptyTokenList: ExpressibleByArrayLiteral, SyntaxBuildable, Exp
11461150
$0.createNonEmptyTokenList().elements
11471151
}
11481152
}
1149-
public init (arrayLiteral elements: TokenSyntax...) {
1153+
public init (arrayLiteral elements: Token...) {
11501154
self.init(elements)
11511155
}
11521156
public func buildNonEmptyTokenList(format: Format) -> NonEmptyTokenListSyntax {
1153-
let result = NonEmptyTokenListSyntax(elements)
1157+
let result = NonEmptyTokenListSyntax(elements.map {
1158+
$0.buildToken()
1159+
})
11541160
return format._format(syntax: result)
11551161
}
11561162
public func buildSyntax(format: Format) -> Syntax {
@@ -1168,7 +1174,7 @@ public struct NonEmptyTokenList: ExpressibleByArrayLiteral, SyntaxBuildable, Exp
11681174
return self
11691175
}
11701176
}
1171-
extension Array: ExpressibleAsNonEmptyTokenList where Element == TokenSyntax {
1177+
extension Array: ExpressibleAsNonEmptyTokenList where Element == Token {
11721178
public func createNonEmptyTokenList() -> NonEmptyTokenList {
11731179
return NonEmptyTokenList(self)
11741180
}

0 commit comments

Comments
 (0)