Skip to content

Commit c52427a

Browse files
authored
Merge pull request #1458 from hamishknight/outliner-5.9
2 parents 8233b18 + 35a7334 commit c52427a

File tree

141 files changed

+2454
-2391
lines changed

Some content is hidden

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

141 files changed

+2454
-2391
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,20 +1768,14 @@ extension Parser {
17681768
)
17691769
}
17701770

1771-
var body = [RawCodeBlockItemSyntax]()
1772-
var codeBlockProgress = LoopProgressCondition()
1773-
while !self.at(.rightBrace),
1774-
let newItem = self.parseCodeBlockItem(),
1775-
codeBlockProgress.evaluate(currentToken)
1776-
{
1777-
body.append(newItem)
1778-
}
1771+
let body = parseCodeBlockItemList(until: { $0.at(.rightBrace) })
1772+
17791773
let (unexpectedBeforeRBrace, rbrace) = self.expect(.rightBrace)
17801774
return .getter(
17811775
RawCodeBlockSyntax(
17821776
unexpectedBeforeLBrace,
17831777
leftBrace: lbrace,
1784-
statements: RawCodeBlockItemListSyntax(elements: body, arena: self.arena),
1778+
statements: body,
17851779
unexpectedBeforeRBrace,
17861780
rightBrace: rbrace,
17871781
arena: self.arena

Sources/SwiftParser/Expressions.swift

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,21 +1710,15 @@ extension Parser {
17101710
let signature = self.parseClosureSignatureIfPresent()
17111711

17121712
// Parse the body.
1713-
var elements = [RawCodeBlockItemSyntax]()
1714-
do {
1715-
var loopProgress = LoopProgressCondition()
1716-
while !self.at(.rightBrace), let newItem = self.parseCodeBlockItem(), loopProgress.evaluate(currentToken) {
1717-
elements.append(newItem)
1718-
}
1719-
}
1713+
let elements = parseCodeBlockItemList(until: { $0.at(.rightBrace) })
17201714

17211715
// Parse the closing '}'.
17221716
let (unexpectedBeforeRBrace, rbrace) = self.expect(.rightBrace)
17231717
return RawClosureExprSyntax(
17241718
unexpectedBeforeLBrace,
17251719
leftBrace: lbrace,
17261720
signature: signature,
1727-
statements: RawCodeBlockItemListSyntax(elements: elements, arena: arena),
1721+
statements: elements,
17281722
unexpectedBeforeRBrace,
17291723
rightBrace: rbrace,
17301724
arena: self.arena
@@ -2326,16 +2320,9 @@ extension Parser {
23262320
}
23272321

23282322
mutating func parseSwitchCaseBody() -> RawCodeBlockItemListSyntax {
2329-
var items = [RawCodeBlockItemSyntax]()
2330-
var loopProgress = LoopProgressCondition()
2331-
while !self.at(.rightBrace) && !self.at(.poundEndifKeyword, .poundElseifKeyword, .poundElseKeyword)
2332-
&& !self.withLookahead({ $0.isStartOfConditionalSwitchCases() }),
2333-
let newItem = self.parseCodeBlockItem(),
2334-
loopProgress.evaluate(currentToken)
2335-
{
2336-
items.append(newItem)
2337-
}
2338-
return RawCodeBlockItemListSyntax(elements: items, arena: self.arena)
2323+
parseCodeBlockItemList(until: {
2324+
$0.at(.rightBrace) || $0.at(.poundEndifKeyword, .poundElseifKeyword, .poundElseKeyword) || $0.withLookahead({ $0.isStartOfConditionalSwitchCases() })
2325+
})
23392326
}
23402327

23412328
/// Parse a single switch case clause.

Sources/SwiftParser/SwiftParser.docc/FixingBugs.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ 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-
AssertParse(
13+
assertParse(
1414
"""
1515
<#your code that does not round trip#>
1616
"""
@@ -27,7 +27,7 @@ Diagnostics are produced when the parsed syntax tree contains missing or unexpec
2727

2828
1. Add a test case in `SwiftParserTest` that looks like the following
2929
```swift
30-
AssertParse(
30+
assertParse(
3131
"""
3232
<#your code that produces an invalid syntax tree#>
3333
""",
@@ -54,7 +54,7 @@ To add a new, more contextual diagnostic, perform the following steps.
5454
1. Add a test case in `SwiftParserTest` that looks like the following
5555
5656
```swift
57-
AssertParse(
57+
assertParse(
5858
"""
5959
<#your code that produced the unhelpful diagnostic#>
6060
""",
@@ -69,7 +69,7 @@ To add a new, more contextual diagnostic, perform the following steps.
6969
5. If the function does not already exist, write a new visit method on <doc:SwiftParser/ParseDiagnosticsGenerator>.
7070
6. In that visitation method, detect the pattern for which the improved diagnostic should be emitted and emit it using `diagnostics.append`.
7171
7. Mark the missing or garbage nodes that are covered by the new diagnostic as handled by adding their `SyntaxIdentifier`s to `handledNodes`.
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`.
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`.
7373
7474
> 💡 Tip: To make typing the marker emojis more convienient. you can set up code snippets in Xcode. To do this, perform the following steps:
7575
> 1. Type the marker in any Xcode window or find it in some test case

Sources/SwiftParser/TopLevel.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extension Parser {
5959
}
6060

6161
extension Parser {
62-
mutating func parseCodeBlockItemList(isAtTopLevel: Bool, allowInitDecl: Bool = true, stopCondition: (inout Parser) -> Bool) -> RawCodeBlockItemListSyntax {
62+
mutating func parseCodeBlockItemList(isAtTopLevel: Bool = false, allowInitDecl: Bool = true, until stopCondition: (inout Parser) -> Bool) -> RawCodeBlockItemListSyntax {
6363
var elements = [RawCodeBlockItemSyntax]()
6464
var loopProgress = LoopProgressCondition()
6565
while !stopCondition(&self), loopProgress.evaluate(currentToken) {
@@ -89,7 +89,7 @@ extension Parser {
8989
///
9090
/// top-level-declaration → statements?
9191
mutating func parseTopLevelCodeBlockItems() -> RawCodeBlockItemListSyntax {
92-
return parseCodeBlockItemList(isAtTopLevel: true, stopCondition: { _ in false })
92+
return parseCodeBlockItemList(isAtTopLevel: true, until: { _ in false })
9393
}
9494

9595
/// The optional form of `parseCodeBlock` that checks to see if the parser has
@@ -116,7 +116,7 @@ extension Parser {
116116
/// indented to close this code block or a surrounding context. See `expectRightBrace`.
117117
mutating func parseCodeBlock(introducer: RawTokenSyntax? = nil, allowInitDecl: Bool = true) -> RawCodeBlockSyntax {
118118
let (unexpectedBeforeLBrace, lbrace) = self.expect(.leftBrace)
119-
let itemList = parseCodeBlockItemList(isAtTopLevel: false, allowInitDecl: allowInitDecl, stopCondition: { $0.at(.rightBrace) })
119+
let itemList = parseCodeBlockItemList(allowInitDecl: allowInitDecl, until: { $0.at(.rightBrace) })
120120
let (unexpectedBeforeRBrace, rbrace) = self.expectRightBrace(leftBrace: lbrace, introducer: introducer)
121121

122122
return .init(
@@ -148,7 +148,7 @@ extension Parser {
148148
/// statement → compiler-control-statement
149149
/// statements → statement statements?
150150
@_spi(RawSyntax)
151-
public mutating func parseCodeBlockItem(isAtTopLevel: Bool = false, allowInitDecl: Bool = true) -> RawCodeBlockItemSyntax? {
151+
public mutating func parseCodeBlockItem(isAtTopLevel: Bool, allowInitDecl: Bool) -> RawCodeBlockItemSyntax? {
152152
if let remainingTokens = remainingTokensIfMaximumNestingLevelReached() {
153153
return RawCodeBlockItemSyntax(
154154
remainingTokens,
@@ -229,7 +229,7 @@ extension Parser {
229229
// If config of attributes is parsed as part of declaration parsing as it
230230
// doesn't constitute its own code block item.
231231
let directive = self.parsePoundIfDirective { (parser, _) in
232-
parser.parseCodeBlockItem()
232+
parser.parseCodeBlockItem(isAtTopLevel: isAtTopLevel, allowInitDecl: allowInitDecl)
233233
} addSemicolonIfNeeded: { lastElement, newItemAtStartOfLine, parser in
234234
if lastElement.semicolon == nil && !newItemAtStartOfLine {
235235
return RawCodeBlockItemSyntax(

Sources/_SwiftSyntaxTestSupport/AssertEqualWithDiff.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import XCTest
2323
/// which this function was called.
2424
/// - line: The line number on which failure occurred. Defaults to the line number on which this
2525
/// function was called.
26-
public func AssertStringsEqualWithDiff(
26+
public func assertStringsEqualWithDiff(
2727
_ actual: String,
2828
_ expected: String,
2929
_ message: String = "",
@@ -34,7 +34,7 @@ public func AssertStringsEqualWithDiff(
3434
if actual == expected {
3535
return
3636
}
37-
FailStringsEqualWithDiff(
37+
failStringsEqualWithDiff(
3838
actual,
3939
expected,
4040
message,
@@ -55,7 +55,7 @@ public func AssertStringsEqualWithDiff(
5555
/// which this function was called.
5656
/// - line: The line number on which failure occurred. Defaults to the line number on which this
5757
/// function was called.
58-
public func AssertDataEqualWithDiff(
58+
public func assertDataEqualWithDiff(
5959
_ actual: Data,
6060
_ expected: Data,
6161
_ message: String = "",
@@ -69,7 +69,7 @@ public func AssertDataEqualWithDiff(
6969

7070
// NOTE: Converting to `Stirng` here looses invalid UTF8 sequence difference,
7171
// but at least we can see something is different.
72-
FailStringsEqualWithDiff(
72+
failStringsEqualWithDiff(
7373
String(decoding: actual, as: UTF8.self),
7474
String(decoding: expected, as: UTF8.self),
7575
message,
@@ -80,7 +80,7 @@ public func AssertDataEqualWithDiff(
8080
}
8181

8282
/// `XCTFail` with `diff`-style output.
83-
public func FailStringsEqualWithDiff(
83+
public func failStringsEqualWithDiff(
8484
_ actual: String,
8585
_ expected: String,
8686
_ message: String = "",

Tests/SwiftDiagnosticsTest/DiagnosticsFormatterTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class DiagnosticsFormatterTests: XCTestCase {
3333
│ ╰─ error: expected expression after operator
3434
3535
"""
36-
AssertStringsEqualWithDiff(annotate(source: source), expectedOutput)
36+
assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
3737
}
3838

3939
func testMultipleDiagnosticsInOneLine() {
@@ -47,7 +47,7 @@ final class DiagnosticsFormatterTests: XCTestCase {
4747
│ ╰─ error: expected name in member access
4848
4949
"""
50-
AssertStringsEqualWithDiff(annotate(source: source), expectedOutput)
50+
assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
5151
}
5252

5353
func testLineSkipping() {
@@ -78,7 +78,7 @@ final class DiagnosticsFormatterTests: XCTestCase {
7878
│ ╰─ error: expected value and ')' to end function call
7979
8080
"""
81-
AssertStringsEqualWithDiff(annotate(source: source), expectedOutput)
81+
assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
8282
}
8383

8484
func testTwoDiagnosticsAtSameLocation() throws {
@@ -91,7 +91,7 @@ final class DiagnosticsFormatterTests: XCTestCase {
9191
9292
"""
9393

94-
AssertStringsEqualWithDiff(annotate(source: source), expectedOutput)
94+
assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
9595
}
9696

9797
func testAddsColoringToSingleErrorDiagnostic() {
@@ -104,7 +104,7 @@ final class DiagnosticsFormatterTests: XCTestCase {
104104
\u{001B}[0;36m│\u{001B}[0;0m ╰─ \u{001B}[1;31merror: expected expression after operator\u{001B}[0;0m
105105
106106
"""
107-
AssertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
107+
assertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
108108
}
109109

110110
func testAddsColoringToMultipleDiagnosticsInOneLine() {
@@ -118,7 +118,7 @@ final class DiagnosticsFormatterTests: XCTestCase {
118118
\u{001B}[0;36m│\u{001B}[0;0m ╰─ \u{001B}[1;31merror: expected name in member access\u{001B}[0;0m
119119
120120
"""
121-
AssertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
121+
assertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
122122
}
123123

124124
func testColoringWithHighlights() {
@@ -133,6 +133,6 @@ final class DiagnosticsFormatterTests: XCTestCase {
133133
134134
"""
135135

136-
AssertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
136+
assertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
137137
}
138138
}

Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
109109
)
110110

111111
let annotated = DiagnosticsFormatter.annotateSources(in: group)
112-
AssertStringsEqualWithDiff(
112+
assertStringsEqualWithDiff(
113113
annotated,
114114
"""
115115
=== main.swift:5 ===
@@ -178,7 +178,7 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
178178
)
179179

180180
let annotated = DiagnosticsFormatter.annotateSources(in: group)
181-
AssertStringsEqualWithDiff(
181+
assertStringsEqualWithDiff(
182182
annotated,
183183
"""
184184
=== main.swift:2 ===

0 commit comments

Comments
 (0)