-
Notifications
You must be signed in to change notification settings - Fork 440
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
kimdv
merged 2 commits into
swiftlang:main
from
kimdv:kimdv/move-raw-syntax-validation-from-gyb-to-code-gen
Feb 27, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
...neration/Sources/generate-swiftsyntax/templates/swiftsyntax/RawSyntaxValidationFile.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
kimdv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
} | ||
) | ||
) | ||
} | ||
) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.