Skip to content

Fix parser issues found by asserting that self-parsing should not produce diagnostics #625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Sources/SwiftParser/Lookahead.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,9 @@ extension Parser.Lookahead {
self.consumeIdentifier()
if self.consume(if: .leftParen) != nil {
while !self.at(.eof), !self.at(.rightParen), !self.at(.poundEndifKeyword) {
if self.consume(if: .rightParen) != nil {
break
}
self.skipSingle()
}
self.consume(if: .rightParen)
}
} while self.at(.atSign)
return true
Expand Down
5 changes: 4 additions & 1 deletion Sources/swift-parser-test/swift-parser-test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ class Reduce: ParsableCommand {
printerr("Current source size \(reduced.count), reducing with chunk size \(chunkSize)")
}
reduced = try reduceImpl(source: reduced, chunkSize: chunkSize, testPasses: testPasses)
chunkSize = min(reduced.count / 2, chunkSize / 2)
chunkSize = min(
reduced.count / 2,
chunkSize / 2
)
}
return reduced
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftParserTest/Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func AssertEqualTokens(_ actual: [Lexer.Lexeme], _ expected: [Lexer.Lexeme], fil

func AssertParse<Node: RawSyntaxNodeProtocol>(
_ parseSyntax: (inout Parser) -> Node,
allowErrors: Bool = true,
allowErrors: Bool = false,
file: StaticString = #file,
line: UInt = #line,
_ source: () -> String
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftParserTest/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ final class DeclarationTests: XCTestCase {
"""
}

try AssertParse({ $0.parseSourceFile() }) {
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
"_ = foo/* */?.description"
}

Expand Down
18 changes: 9 additions & 9 deletions Tests/SwiftParserTest/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ final class ExpressionTests: XCTestCase {
"""
}

try AssertParse({ $0.parseExpression() }) {
try AssertParse({ $0.parseExpression() }, allowErrors: true) {
"[,"
}

try AssertParse({ $0.parseExpression() }) {
try AssertParse({ $0.parseExpression() }, allowErrors: true) {
"""
([1:)
"""
Expand Down Expand Up @@ -197,7 +197,7 @@ final class ExpressionTests: XCTestCase {
"""#
}

try AssertParse({ $0.parseExpression() }) {
try AssertParse({ $0.parseExpression() }, allowErrors: true) {
#"" >> \( abc } ) << ""#
}

Expand All @@ -213,7 +213,7 @@ final class ExpressionTests: XCTestCase {
"""##
}

try AssertParse({ $0.parseExpression() }) {
try AssertParse({ $0.parseExpression() }, allowErrors: true) {
#""\","#
}

Expand Down Expand Up @@ -249,35 +249,35 @@ final class ExpressionTests: XCTestCase {
"""
}

try AssertParse({ $0.parseExpression() }, allowErrors: false) {
try AssertParse({ $0.parseExpression() }) {
##"""
#"""#
"""##
}

try AssertParse({ $0.parseExpression() }, allowErrors: false) {
try AssertParse({ $0.parseExpression() }) {
##"""
#"""""#
"""##
}

try AssertParse({ $0.parseExpression() }, allowErrors: false) {
try AssertParse({ $0.parseExpression() }) {
##"""
#"""
multiline raw
"""#
"""##
}

try AssertParse({ $0.parseExpression() }, allowErrors: false) {
try AssertParse({ $0.parseExpression() }) {
#"""
"\(x)"
"""#
}
}

func testRangeSubscript() throws {
try AssertParse({ $0.parseExpression() }, allowErrors: false) {
try AssertParse({ $0.parseExpression() }) {
"""
text[...]
"""
Expand Down
14 changes: 14 additions & 0 deletions Tests/SwiftParserTest/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ public class ParserTests: XCTestCase {
let fileContents = try String(contentsOf: fileURL)
let parsed = try Parser.parse(source: fileContents)
AssertStringsEqualWithDiff("\(parsed)", fileContents)
let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: parsed)
if !diagnostics.isEmpty {
var locationAndDiagnostics: [String] = []
let locationConverter = SourceLocationConverter(file: fileURL.lastPathComponent, tree: parsed)
for diag in diagnostics {
let location = diag.location(converter: locationConverter)
let message = diag.message
locationAndDiagnostics.append("\(location): \(message)")
}
XCTFail("""
Received the following diagnostics while parsing \(fileURL)
\(locationAndDiagnostics.joined(separator: "\n"))
""")
}
}())
}
}
Expand Down
24 changes: 12 additions & 12 deletions Tests/SwiftParserTest/RecoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class RecoveryTests: XCTestCase {
}

func testBogusKeypathBaseRecovery() throws {
try AssertParse({ $0.parseSourceFile() }) {
"func nestThoseIfs() {\\n if false != true {\\n print \"\\(i)\"\\n"
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
#"func nestThoseIfs() {\n if false != true {\n print "\(i)\"\n"#
}
}

Expand All @@ -37,7 +37,7 @@ public class RecoveryTests: XCTestCase {
}

func testMissingSubscriptReturnClause() throws {
try AssertParse({ $0.parseSourceFile() }) {
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
"""
struct Foo {
subscript(x: String) {}
Expand All @@ -55,7 +55,7 @@ public class RecoveryTests: XCTestCase {
}

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

func testMissingArrowInArrowExpr() throws {
try AssertParse({ $0.parseSourceFile() }) {
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
"""
[(Int) -> throws Int]()
let _ = [Int throws Int]()
Expand Down Expand Up @@ -130,7 +130,7 @@ public class RecoveryTests: XCTestCase {
}

func testStringBogusClosingDelimiters() throws {
try AssertParse({ $0.parseSourceFile() }) {
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
#"\\("#
}

Expand All @@ -140,21 +140,21 @@ public class RecoveryTests: XCTestCase {
"""##
}

try AssertParse({ $0.parseStringLiteral() }) {
try AssertParse({ $0.parseStringLiteral() }, allowErrors: true) {
#"""
"
"""#
}

try AssertParse({ $0.parseStringLiteral() }) {
try AssertParse({ $0.parseStringLiteral() }, allowErrors: true) {
#"""
"'
"""#
}
}

func testMissingArgumentToAttribute() throws {
try AssertParse({ $0.parseSourceFile() }) {
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
"""
@_dynamicReplacement(
func test_dynamic_replacement_for2() {
Expand Down Expand Up @@ -193,7 +193,7 @@ public class RecoveryTests: XCTestCase {
}

func testExpressionMember() throws {
try AssertParse({ $0.parseSourceFile() }) {
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
"""
struct S {
/ ###line 25 "line-directive.swift"
Expand All @@ -213,7 +213,7 @@ public class RecoveryTests: XCTestCase {
}

func testExtraSyntaxInDirective() throws {
try AssertParse({ $0.parseDeclaration() }) {
try AssertParse({ $0.parseDeclaration() }, allowErrors: true) {
"""
#if os(iOS)
func foo() {}
Expand Down Expand Up @@ -343,7 +343,7 @@ public class RecoveryTests: XCTestCase {
}

func testTextRecovery() throws {
try AssertParse({ $0.parseSourceFile() }) {
try AssertParse({ $0.parseSourceFile() }, allowErrors: true) {
"""
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.
"""
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftParserTest/Statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class StatementTests: XCTestCase {
"""
}

try AssertParse({ $0.parseIfStatement() }) {
try AssertParse({ $0.parseIfStatement() }, allowErrors: true) {
"""
if case* ! = x {
bar()
Expand Down