Skip to content

Commit 7012611

Browse files
authored
Merge pull request #625 from ahoppen/pr/self-parsing-fixes
Fix parser issues found by asserting that self-parsing should not produce diagnostics
2 parents 33a5780 + 49576ee commit 7012611

File tree

8 files changed

+43
-28
lines changed

8 files changed

+43
-28
lines changed

Sources/SwiftParser/Lookahead.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,9 @@ extension Parser.Lookahead {
149149
self.consumeIdentifier()
150150
if self.consume(if: .leftParen) != nil {
151151
while !self.at(.eof), !self.at(.rightParen), !self.at(.poundEndifKeyword) {
152-
if self.consume(if: .rightParen) != nil {
153-
break
154-
}
155152
self.skipSingle()
156153
}
154+
self.consume(if: .rightParen)
157155
}
158156
} while self.at(.atSign)
159157
return true

Sources/swift-parser-test/swift-parser-test.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ class Reduce: ParsableCommand {
269269
printerr("Current source size \(reduced.count), reducing with chunk size \(chunkSize)")
270270
}
271271
reduced = try reduceImpl(source: reduced, chunkSize: chunkSize, testPasses: testPasses)
272-
chunkSize = min(reduced.count / 2, chunkSize / 2)
272+
chunkSize = min(
273+
reduced.count / 2,
274+
chunkSize / 2
275+
)
273276
}
274277
return reduced
275278
}

Tests/SwiftParserTest/Assertions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func AssertEqualTokens(_ actual: [Lexer.Lexeme], _ expected: [Lexer.Lexeme], fil
5151

5252
func AssertParse<Node: RawSyntaxNodeProtocol>(
5353
_ parseSyntax: (inout Parser) -> Node,
54-
allowErrors: Bool = true,
54+
allowErrors: Bool = false,
5555
file: StaticString = #file,
5656
line: UInt = #line,
5757
_ source: () -> String

Tests/SwiftParserTest/Declarations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ final class DeclarationTests: XCTestCase {
139139
"""
140140
}
141141

142-
try AssertParse({ $0.parseSourceFile() }) {
142+
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
143143
"_ = foo/* */?.description"
144144
}
145145

Tests/SwiftParserTest/Expressions.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ final class ExpressionTests: XCTestCase {
177177
"""
178178
}
179179

180-
try AssertParse({ $0.parseExpression() }) {
180+
try AssertParse({ $0.parseExpression() }, allowErrors: true) {
181181
"[,"
182182
}
183183

184-
try AssertParse({ $0.parseExpression() }) {
184+
try AssertParse({ $0.parseExpression() }, allowErrors: true) {
185185
"""
186186
([1:)
187187
"""
@@ -210,7 +210,7 @@ final class ExpressionTests: XCTestCase {
210210
"""#
211211
}
212212

213-
try AssertParse({ $0.parseExpression() }) {
213+
try AssertParse({ $0.parseExpression() }, allowErrors: true) {
214214
#"" >> \( abc } ) << ""#
215215
}
216216

@@ -226,7 +226,7 @@ final class ExpressionTests: XCTestCase {
226226
"""##
227227
}
228228

229-
try AssertParse({ $0.parseExpression() }) {
229+
try AssertParse({ $0.parseExpression() }, allowErrors: true) {
230230
#""\","#
231231
}
232232

@@ -262,35 +262,35 @@ final class ExpressionTests: XCTestCase {
262262
"""
263263
}
264264

265-
try AssertParse({ $0.parseExpression() }, allowErrors: false) {
265+
try AssertParse({ $0.parseExpression() }) {
266266
##"""
267267
#"""#
268268
"""##
269269
}
270270

271-
try AssertParse({ $0.parseExpression() }, allowErrors: false) {
271+
try AssertParse({ $0.parseExpression() }) {
272272
##"""
273273
#"""""#
274274
"""##
275275
}
276276

277-
try AssertParse({ $0.parseExpression() }, allowErrors: false) {
277+
try AssertParse({ $0.parseExpression() }) {
278278
##"""
279279
#"""
280280
multiline raw
281281
"""#
282282
"""##
283283
}
284284

285-
try AssertParse({ $0.parseExpression() }, allowErrors: false) {
285+
try AssertParse({ $0.parseExpression() }) {
286286
#"""
287287
"\(x)"
288288
"""#
289289
}
290290
}
291291

292292
func testRangeSubscript() throws {
293-
try AssertParse({ $0.parseExpression() }, allowErrors: false) {
293+
try AssertParse({ $0.parseExpression() }) {
294294
"""
295295
text[...]
296296
"""

Tests/SwiftParserTest/ParserTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ public class ParserTests: XCTestCase {
1818
let fileContents = try String(contentsOf: fileURL)
1919
let parsed = try Parser.parse(source: fileContents)
2020
AssertStringsEqualWithDiff("\(parsed)", fileContents)
21+
let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: parsed)
22+
if !diagnostics.isEmpty {
23+
var locationAndDiagnostics: [String] = []
24+
let locationConverter = SourceLocationConverter(file: fileURL.lastPathComponent, tree: parsed)
25+
for diag in diagnostics {
26+
let location = diag.location(converter: locationConverter)
27+
let message = diag.message
28+
locationAndDiagnostics.append("\(location): \(message)")
29+
}
30+
XCTFail("""
31+
Received the following diagnostics while parsing \(fileURL)
32+
\(locationAndDiagnostics.joined(separator: "\n"))
33+
""")
34+
}
2135
}())
2236
}
2337
}

Tests/SwiftParserTest/RecoveryTests.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public class RecoveryTests: XCTestCase {
1919
}
2020

2121
func testBogusKeypathBaseRecovery() throws {
22-
try AssertParse({ $0.parseSourceFile() }) {
23-
"func nestThoseIfs() {\\n if false != true {\\n print \"\\(i)\"\\n"
22+
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
23+
#"func nestThoseIfs() {\n if false != true {\n print "\(i)\"\n"#
2424
}
2525
}
2626

@@ -37,7 +37,7 @@ public class RecoveryTests: XCTestCase {
3737
}
3838

3939
func testMissingSubscriptReturnClause() throws {
40-
try AssertParse({ $0.parseSourceFile() }) {
40+
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
4141
"""
4242
struct Foo {
4343
subscript(x: String) {}
@@ -55,7 +55,7 @@ public class RecoveryTests: XCTestCase {
5555
}
5656

5757
func testClassWithLeadingNumber() throws {
58-
try AssertParse({ $0.parseSourceFile() }) {
58+
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
5959
"""
6060
class 23class {
6161
// expected-error@-1 {{class name can only start with a letter or underscore, not a number}}
@@ -82,7 +82,7 @@ public class RecoveryTests: XCTestCase {
8282
}
8383

8484
func testMissingArrowInArrowExpr() throws {
85-
try AssertParse({ $0.parseSourceFile() }) {
85+
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
8686
"""
8787
[(Int) -> throws Int]()
8888
let _ = [Int throws Int]()
@@ -130,7 +130,7 @@ public class RecoveryTests: XCTestCase {
130130
}
131131

132132
func testStringBogusClosingDelimiters() throws {
133-
try AssertParse({ $0.parseSourceFile() }) {
133+
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
134134
#"\\("#
135135
}
136136

@@ -140,21 +140,21 @@ public class RecoveryTests: XCTestCase {
140140
"""##
141141
}
142142

143-
try AssertParse({ $0.parseStringLiteral() }) {
143+
try AssertParse({ $0.parseStringLiteral() }, allowErrors: true) {
144144
#"""
145145
"
146146
"""#
147147
}
148148

149-
try AssertParse({ $0.parseStringLiteral() }) {
149+
try AssertParse({ $0.parseStringLiteral() }, allowErrors: true) {
150150
#"""
151151
"'
152152
"""#
153153
}
154154
}
155155

156156
func testMissingArgumentToAttribute() throws {
157-
try AssertParse({ $0.parseSourceFile() }) {
157+
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
158158
"""
159159
@_dynamicReplacement(
160160
func test_dynamic_replacement_for2() {
@@ -193,7 +193,7 @@ public class RecoveryTests: XCTestCase {
193193
}
194194

195195
func testExpressionMember() throws {
196-
try AssertParse({ $0.parseSourceFile() }) {
196+
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
197197
"""
198198
struct S {
199199
/ ###line 25 "line-directive.swift"
@@ -213,7 +213,7 @@ public class RecoveryTests: XCTestCase {
213213
}
214214

215215
func testExtraSyntaxInDirective() throws {
216-
try AssertParse({ $0.parseDeclaration() }) {
216+
try AssertParse({ $0.parseDeclaration() }, allowErrors: true) {
217217
"""
218218
#if os(iOS)
219219
func foo() {}
@@ -343,7 +343,7 @@ public class RecoveryTests: XCTestCase {
343343
}
344344

345345
func testTextRecovery() throws {
346-
try AssertParse({ $0.parseSourceFile() }) {
346+
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
347347
"""
348348
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
349349
"""

Tests/SwiftParserTest/Statements.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class StatementTests: XCTestCase {
1010
"""
1111
}
1212

13-
try AssertParse({ $0.parseIfStatement() }) {
13+
try AssertParse({ $0.parseIfStatement() }, allowErrors: true) {
1414
"""
1515
if case* ! = x {
1616
bar()

0 commit comments

Comments
 (0)