Skip to content

Move raw syntax validation from gyb to code gen #1332

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
TemplateSpec(sourceFile: keywordFile, module: swiftSyntaxDir, filename: "Keyword.swift"),
TemplateSpec(sourceFile: miscFile, module: swiftSyntaxDir, filename: "Misc.swift"),
TemplateSpec(sourceFile: rawSyntaxNodesFile, module: swiftSyntaxDir, filename: "raw/RawSyntaxNodes.swift"),
TemplateSpec(sourceFile: rawSyntaxValidationFile, module: swiftSyntaxDir, filename: "raw/RawSyntaxValidation.swift"),
TemplateSpec(sourceFile: syntaxAnyVisitorFile, module: swiftSyntaxDir, filename: "SyntaxAnyVisitor.swift"),
TemplateSpec(sourceFile: syntaxBaseNodesFile, module: swiftSyntaxDir, filename: "SyntaxBaseNodes.swift"),
TemplateSpec(sourceFile: syntaxCollectionsFile, module: swiftSyntaxDir, filename: "SyntaxCollections.swift"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
import SwiftSyntaxBuilder
import SyntaxSupport
import Utils

let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: "\(generateCopyrightHeader(for: "generate-swiftsyntax"))" + .newline) {
try FunctionDeclSyntax(
"""
/// Check that the `layout` is valid for the given 'SyntaxKind'.
///
/// Note that this only validates the immediate children.
/// Results in an assertion failure if the layout is invalid.
func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind)
"""
) {
IfConfigDeclSyntax(
clauses: IfConfigClauseListSyntax {
IfConfigClauseSyntax(
poundKeyword: .poundIfKeyword(),
condition: ExprSyntax("DEBUG"),
elements: .statements(
CodeBlockItemListSyntax {
DeclSyntax(
#"""
enum ValidationError: CustomStringConvertible {
case expectedNonNil(expectedKind: RawSyntaxNodeProtocol.Type, file: StaticString, line: UInt)
case kindMismatch(expectedKind: RawSyntaxNodeProtocol.Type, actualKind: SyntaxKind, file: StaticString, line: UInt)

var description: String {
switch self {
case .expectedNonNil(expectedKind: let expectedKind, file: _, line: _):
return "Expected non-nil node of type \(expectedKind) but received nil"
case .kindMismatch(expectedKind: let expectedKind, actualKind: let actualKind, file: _, line: _):
return "Expected node of type \(expectedKind) but received \(actualKind)"
}
}

var fileAndLine: (StaticString, UInt) {
switch self {
case .expectedNonNil(expectedKind: _, file: let file, line: let line):
return (file, line)
case .kindMismatch(expectedKind: _, actualKind: _, file: let file, line: let line):
return (file, line)
}
}
}
"""#
)

DeclSyntax(
"""
func verify<Node: RawSyntaxNodeProtocol>(_ raw: RawSyntax?, as _: Node.Type, file: StaticString = #file, line: UInt = #line) -> ValidationError? {
guard let raw = raw else {
return .expectedNonNil(expectedKind: Node.self, file: file, line: line)
}
guard Node.isKindOf(raw) else {
return .kindMismatch(expectedKind: Node.self, actualKind: raw.kind, file: file, line: line)
}
return nil
}
"""
)

DeclSyntax(
"""
func verify<Node: RawSyntaxNodeProtocol>(_ raw: RawSyntax?, as _: Node?.Type, file: StaticString = #file, line: UInt = #line) -> ValidationError? {
if raw != nil {
return verify(raw, as: Node.self, file: file, line: line)
}
return nil
}
"""
)

DeclSyntax(
#"""
func assertNoError(_ nodeKind: SyntaxKind, _ index: Int, _ error: ValidationError?) {
if let error = error {
let (file, line) = error.fileAndLine
assertionFailure("""
Error validating child at index \(index) of \(nodeKind):
\(error.description)
""", file: file, line: line)
_ = 1
}
}
"""#
)

DeclSyntax(
#"""
func assertAnyHasNoError(_ nodeKind: SyntaxKind, _ index: Int, _ errors: [ValidationError?]) {
let nonNilErrors = errors.compactMap({ $0 })
if nonNilErrors.count == errors.count, let firstError = nonNilErrors.first {
let (file, line) = firstError.fileAndLine
assertionFailure("""
Error validating child at index \(index) of \(nodeKind):
Node did not satisfy any node choice requirement.
Validation failures:
\(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n"))
""", file: file, line: line)
_ = 1
}
}
"""#
)

try! SwitchExprSyntax("switch kind") {
SwitchCaseSyntax(
"""
case .token:
assertionFailure("validateLayout for .token kind is not supported")
"""
)

for node in NON_BASE_SYNTAX_NODES {
SwitchCaseSyntax("case .\(raw: node.swiftSyntaxKind):") {
if node.isBuildable || node.isMissing {
ExprSyntax("assert(layout.count == \(raw: node.children.count))")
for (index, child) in node.children.enumerated() {
switch child.kind {
case .nodeChoices(let choices):
let verifiedChoices = ArrayExprSyntax {
ArrayElementSyntax(
leadingTrivia: .newline,
expression: ExprSyntax("verify(layout[\(raw: index)], as: Raw\(raw: child.type.buildable).self)")
)
}

ExprSyntax("assertAnyHasNoError(kind, \(raw: index), \(verifiedChoices))")
default:
ExprSyntax("assertNoError(kind, \(raw: index), verify(layout[\(raw: index)], as: Raw\(raw: child.type.buildable).self))")
}
}
} else if node.isSyntaxCollection {
try! ForInStmtSyntax("for (index, element) in layout.enumerated()") {
if let collectionElementChoices = node.collectionElementChoices, !collectionElementChoices.isEmpty {
let verifiedChoices = ArrayExprSyntax {
for choiceName in node.collectionElementChoices! {
let choice = SYNTAX_NODE_MAP[choiceName]!
ArrayElementSyntax(
leadingTrivia: .newline,
expression: ExprSyntax("verify(element, as: Raw\(raw: choice.name).self)")
)
}
}
ExprSyntax("assertAnyHasNoError(kind, index, \(verifiedChoices))")
} else {
ExprSyntax("assertNoError(kind, index, verify(element, as: Raw\(node.collectionElementType.buildable).self))")
}
}
}

BreakStmtSyntax()
}
}
}
}
)
)
}
)
}
}
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ let package = Package(
name: "SwiftSyntax",
dependencies: [],
exclude: [
"CMakeLists.txt",
"Raw/RawSyntaxValidation.swift.gyb",
"CMakeLists.txt"
],
swiftSettings: swiftSyntaxSwiftSettings
),
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ add_swift_host_library(SwiftSyntax
Raw/RawSyntaxTokenView.swift

generated/raw/RawSyntaxNodes.swift
Raw/gyb_generated/RawSyntaxValidation.swift
generated/raw/RawSyntaxValidation.swift

generated/Keyword.swift
generated/Misc.swift
Expand Down
129 changes: 0 additions & 129 deletions Sources/SwiftSyntax/Raw/RawSyntaxValidation.swift.gyb

This file was deleted.

Loading