Skip to content

Create local functions to validate the RawSyntax layout of nodes #2908

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 1 commit into from
Dec 6, 2024
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 @@ -190,75 +190,83 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead
"""#
)

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

for node in NON_BASE_SYNTAX_NODES {
try SwitchCaseSyntax("case .\(node.enumCaseCallName):") {
if let node = node.layoutNode {
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\(child.buildableType.buildable).self)"
)
for node in NON_BASE_SYNTAX_NODES {
try FunctionDeclSyntax(
"func validate\(node.kind.syntaxType)(kind: SyntaxKind, layout: RawSyntaxBuffer)"
) {
if let node = node.layoutNode {
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\(child.buildableType.buildable).self)"
)
}
)
}

ExprSyntax("assertAnyHasNoError(kind, \(raw: index), \(verifiedChoices))")
case .token(choices: let choices, requiresLeadingSpace: _, requiresTrailingSpace: _):
let choices = ArrayExprSyntax {
for choice in choices {
switch choice {
case .keyword(let keyword):
ArrayElementSyntax(expression: ExprSyntax(".keyword(\(literal: keyword.spec.name))"))
case .token(let token):
ArrayElementSyntax(expression: ExprSyntax(".tokenKind(.\(token.spec.memberCallName))"))
}
ExprSyntax("assertAnyHasNoError(kind, \(raw: index), \(verifiedChoices))")
case .token(choices: let choices, requiresLeadingSpace: _, requiresTrailingSpace: _):
let choices = ArrayExprSyntax {
for choice in choices {
switch choice {
case .keyword(let keyword):
ArrayElementSyntax(expression: ExprSyntax(".keyword(\(literal: keyword.spec.name))"))
case .token(let token):
ArrayElementSyntax(expression: ExprSyntax(".tokenKind(.\(token.spec.memberCallName))"))
}
}
let verifyCall = ExprSyntax(
"verify(layout[\(raw: index)], as: Raw\(child.buildableType.buildable).self, tokenChoices: \(choices))"
)
ExprSyntax("assertNoError(kind, \(raw: index), \(verifyCall))")
default:
ExprSyntax(
"assertNoError(kind, \(raw: index), verify(layout[\(raw: index)], as: Raw\(child.buildableType.buildable).self))"
)
}
let verifyCall = ExprSyntax(
"verify(layout[\(raw: index)], as: Raw\(child.buildableType.buildable).self, tokenChoices: \(choices))"
)
ExprSyntax("assertNoError(kind, \(raw: index), \(verifyCall))")
default:
ExprSyntax(
"assertNoError(kind, \(raw: index), verify(layout[\(raw: index)], as: Raw\(child.buildableType.buildable).self))"
)
}
} else if let node = node.collectionNode {
try ForStmtSyntax("for (index, element) in layout.enumerated()") {
if let onlyElement = node.elementChoices.only {
ExprSyntax(
"assertNoError(kind, index, verify(element, as: \(onlyElement.raw.syntaxType).self))"
)
} else {
let verifiedChoices = ArrayExprSyntax {
for choiceName in node.elementChoices {
let choice = SYNTAX_NODE_MAP[choiceName]!
ArrayElementSyntax(
leadingTrivia: .newline,
expression: ExprSyntax("verify(element, as: \(choice.kind.raw.syntaxType).self)")
)
}
}
} else if let node = node.collectionNode {
try ForStmtSyntax("for (index, element) in layout.enumerated()") {
if let onlyElement = node.elementChoices.only {
ExprSyntax(
"assertNoError(kind, index, verify(element, as: \(onlyElement.raw.syntaxType).self))"
)
} else {
let verifiedChoices = ArrayExprSyntax {
for choiceName in node.elementChoices {
let choice = SYNTAX_NODE_MAP[choiceName]!
ArrayElementSyntax(
leadingTrivia: .newline,
expression: ExprSyntax("verify(element, as: \(choice.kind.raw.syntaxType).self)")
)
}
ExprSyntax("assertAnyHasNoError(kind, index, \(verifiedChoices))")
}
ExprSyntax("assertAnyHasNoError(kind, index, \(verifiedChoices))")
}
}
}
}
}

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

for node in NON_BASE_SYNTAX_NODES {
SwitchCaseSyntax("case .\(node.enumCaseCallName):") {
ExprSyntax("validate\(node.kind.syntaxType)(kind: kind, layout: layout)")
}
}
}
}
)
)
Expand Down
Loading