Commit 8ea19b6
authored
[SwiftParser] Improve statement level recovery (#3156)
Consider the following code:
```swift
}
@ATTR1 func foo() {}
```
Previously, this was parsed as a `FunctionDeclSyntax` with the unexpected
code `} @attr` attached as `unexpectedBeforeFuncKeyword`. This was
problematic because `@attr` was effectively ignored. The
`TokenPrecedence`-based recovery strategy couldn't handle this well, since
`@` is not a declaration keyword and doesn't by itself signal the start
of a declaration.
One option would be to check `atStartOfDeclaration()` after each
`skipSingle()` and recognize `@` as the start of a declaration. However,
then the preceding `}` would need to be attached to the first attribute in
the attribute list, which would require threading recovery logic into
`parseAttributeList()` and only applying it to the first `parseAttribute()`.
This would make the code more complex and harder to maintain.
This PR introduces a new syntax node `UnexpectedCodeDeclSyntax`
* It represents unexpected code at the statement/declaration level.
* All statement-level recovery is now handled by `parseUnexpectedCodeDeclaration()`.
This simplifies recovery handling significantly.
---
Also:
* Improve `#if` related recovery:
* orphan `#endif` et al at top-level is now `UnexpectedCodeDecl` instead
of making everything after it "unexpected"
```swift
#endif
func foo() {}
```
* Unbalanced `#endif` et al closes code/member block with missing `}`.
```swift
struct S {
#if FOO
func foo() {
#endif
}
```
* Simplified `parsePoundDirective()`. It now receives only one closure
instead of 3.
* Don't attach ';' to compiler directives. For example:
```swift
#sourceLocation(file: "<file>", line: 100)
;
```
`;` is now diagnosed as `;`-only statement, or unexpected `;` separator
depending on the position.
* Adjust TokenPrecedence for decl/statement keyword so. Decl keyword
recovery won't go beyond `}` or statements. E.g.
```swift
func foo() {
@attr
}
struct S {}
```
```swift
func foo() {
@attr
if true {}
struct S {}
}
```1 parent baef1ff commit 8ea19b6
File tree
51 files changed
+1148
-478
lines changed- CodeGeneration
- Sources
- SyntaxSupport
- generate-swift-syntax/templates/swiftparser
- Tests/ValidateSyntaxNodes
- Sources
- SwiftParser
- generated
- SwiftSyntax
- Documentation.docc/generated
- generated
- raw
- syntaxNodes
- Tests
- SwiftDiagnosticsTest
- SwiftParserTest
- translated
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
51 files changed
+1148
-478
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
180 | | - | |
| 180 | + | |
181 | 181 | | |
182 | 182 | | |
183 | 183 | | |
| |||
401 | 401 | | |
402 | 402 | | |
403 | 403 | | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
404 | 418 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
130 | | - | |
| 130 | + | |
| 131 | + | |
131 | 132 | | |
132 | 133 | | |
133 | 134 | | |
| |||
140 | 141 | | |
141 | 142 | | |
142 | 143 | | |
143 | | - | |
| 144 | + | |
| 145 | + | |
144 | 146 | | |
145 | 147 | | |
146 | 148 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
301 | 301 | | |
302 | 302 | | |
303 | 303 | | |
| 304 | + | |
304 | 305 | | |
305 | 306 | | |
306 | 307 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
| 71 | + | |
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| |||
Lines changed: 6 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
641 | 641 | | |
642 | 642 | | |
643 | 643 | | |
644 | | - | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
645 | 650 | | |
646 | 651 | | |
647 | 652 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
| 36 | + | |
41 | 37 | | |
42 | 38 | | |
43 | 39 | | |
| |||
896 | 892 | | |
897 | 893 | | |
898 | 894 | | |
899 | | - | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
900 | 921 | | |
901 | 922 | | |
902 | 923 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
119 | | - | |
| 119 | + | |
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
| 92 | + | |
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| |||
0 commit comments