8000 Sync swift-5.0-branch with master. by nkcsgexi · Pull Request #42 · swiftlang/swift-syntax · GitHub
[go: up one dir, main page]

Skip to content

Sync swift-5.0-branch with master. #42

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 17 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 42 additions & 0 deletions Sources/SwiftSyntax/SyntaxRewriter.swift.gyb
8000
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,45 @@ extension Syntax {
data.raw.accept(RawSyntaxVisitor(visitor, self))
}
}

public enum SyntaxVerifierError: Error, CustomStringConvertible {
case unknownSyntaxFound(node: Syntax)

public var description: String {
switch self {
case .unknownSyntaxFound(let node):
return "unknown syntax node for \"\(node)\""
}
}
}

public class SyntaxVerifier: SyntaxVisitor {

var UnknownNodes: [Syntax] = []

override public func shouldVisit(_ node: SyntaxKind) -> Bool {
return node.isUnknown
}

override public func shouldVisit(_ node: TokenKind) -> Bool {
return false
}

override public func visitPre(_ node: Syntax) {
assert(node.isUnknown)
UnknownNodes.append(node)
}

private func verify(_ node: Syntax) throws {
node.walk(self)
if !UnknownNodes.isEmpty {
throw SyntaxVerifierError.unknownSyntaxFound(node: UnknownNodes.first!)
}
}

private override init() {}

public static func verify(_ node: Syntax) throws {
try SyntaxVerifier().verify(node)
}
}
1 change: 1 addition & 0 deletions Tests/SwiftSyntaxTest/ParseFile.swift
A0EA
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ParseFileTestCase: XCTestCase {
let fileContents = try String(contentsOf: currentFile)
let parsed = try SyntaxTreeParser.parse(currentFile)
XCTAssertEqual("\(parsed)", fileContents)
try SyntaxVerifier.verify(parsed)
}())
}
}
7 changes: 5 additions & 2 deletions Tests/SwiftSyntaxTest/SyntaxFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ public class SyntaxFactoryAPITestCase: XCTestCase {
public func testUnknownSyntax() {
let expr = SyntaxFactory.makeStringLiteralExpr("Hello, world!")
XCTAssertFalse(expr.isUnknown)
XCTAssertTrue(SyntaxFactory.makeUnknownSyntax(
tokens: [SyntaxFactory.makeLeftBraceToken()]).isUnknown)
let unknown = SyntaxFactory.makeUnknownSyntax(
tokens: [SyntaxFactory.makeLeftBraceToken()])
XCTAssertTrue(unknown.isUnknown)
XCTAssertNoThrow(try SyntaxVerifier.verify(expr))
XCTAssertThrowsError(try SyntaxVerifier.verify(unknown))
}
}
0