Skip to content

WIP: Validate node_choices and token_choices #1032

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

Closed
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 @@ -94,6 +94,8 @@ public let ATTRIBUTE_NODES: [Node] = [
tokenChoices: [
"StringLiteral"
]),
Child(name: "StringExpr",
kind: "StringLiteralExpr"),
Child(name: "Integer",
kind: "IntegerLiteralToken",
tokenChoices: [
Expand Down Expand Up @@ -188,10 +190,11 @@ public let ATTRIBUTE_NODES: [Node] = [
],
children: [
Child(name: "Label",
kind: "IdentifierToken",
kind: "Token",
description: "The label of the argument",
tokenChoices: [
"Identifier"
"Identifier",
"ContextualKeyword"
]),
Child(name: "Colon",
kind: "ColonToken",
Expand Down Expand Up @@ -348,10 +351,10 @@ public let ATTRIBUTE_NODES: [Node] = [
kind: "Syntax",
children: [
Child(name: "DiffKind",
kind: "IdentifierToken",
kind: "ContextualKeywordToken",
isOptional: true,
tokenChoices: [
"Identifier"
"ContextualKeyword"
],
textChoices: [
"forward",
Expand Down Expand Up @@ -477,10 +480,10 @@ public let ATTRIBUTE_NODES: [Node] = [
kind: "Syntax",
children: [
Child(name: "OfLabel",
kind: "IdentifierToken",
kind: "ContextualKeywordToken",
description: "The \"of\" label.",
tokenChoices: [
"Identifier"
"ContextualKeyword"
],
textChoices: [
"of"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ public let AVAILABILITY_NODES: [Node] = [
"*"
]),
Child(name: "IdentifierRestriction",
kind: "IdentifierToken",
kind: "ContextualKeywordToken",
tokenChoices: [
"Identifier"
"ContextualKeyword"
]),
Child(name: "AvailabilityVersionRestriction",
kind: "AvailabilityVersionRestriction"),
Child(name: "AvailabilityLabeledArgument",
kind: "AvailabilityLabeledArgument")
kind: "AvailabilityLabeledArgument"),
Child(name: "TokenList",
kind: "TokenList")
]),
Child(name: "TrailingComma",
kind: "CommaToken",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,13 +781,11 @@ public let EXPR_NODES: [Node] = [
kind: "ParameterClause")
]),
Child(name: "AsyncKeyword",
kind: "ContextualKeywordToken",
kind: "Token",
isOptional: true,
tokenChoices: [
"ContextualKeyword"
],
textChoices: [
"async"
"ContextualKeyword",
"Throws"
]),
Child(name: "ThrowsTok",
kind: "ThrowsToken",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public let GENERIC_NODES: [Node] = [
"SpacedBinaryOperator",
"UnspacedBinaryOperator",
"PrefixOperator",
"PostfixOperator"
"PostfixOperator",
"Equal"
]),
Child(name: "RightTypeIdentifier",
kind: "Type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,19 @@ public let TYPE_NODES: [Node] = [
"RightParen"
]),
Child(name: "AsyncKeyword",
kind: "ContextualKeyworkToken",
kind: "Token",
isOptional: true,
textChoices: [
"async"
tokenChoices: [
"ContextualKeyword",
"Throws",
"Rethrows",
"Throw"
]),
Child(name: "ThrowsOrRethrowsKeyword",
kind: "Token",
isOptional: true,
tokenChoices: [
"ContextualKeyword",
"Throws",
"Rethrows",
"Throw"
Expand Down
6 changes: 5 additions & 1 deletion Sources/SwiftParser/Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ extension Parser {
guard let leftParen = self.consume(if: .leftParen) else {
// If no opening '(' for parameter list, parse a single parameter.
let param = self.parseDifferentiabilityParameter().map(RawSyntax.init(_:))
?? RawSyntax(RawMissingSyntax(arena: self.arena))
?? RawSyntax(RawDifferentiabilityParamSyntax(
parameter: RawSyntax(missingToken(.identifier)),
trailingComma: nil,
arena: self.arena
))
return RawDifferentiabilityParamsClauseSyntax(
unexpectedBeforeWrt,
wrtLabel: wrt,
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Names.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension Parser {
self.missingToken(.identifier, text: nil)
)
} else {
return (nil, self.consumeAnyToken())
return (nil, self.consumeAnyToken(remapping: .identifier))
}
}
}
Expand Down
123 changes: 115 additions & 8 deletions Sources/SwiftSyntax/Raw/RawSyntaxValidation.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,119 @@
//
//===----------------------------------------------------------------------===//

%{
def get_verify_call(idx, child, is_optional):
question_mark = "?" if is_optional else ""
if child.is_token():
token_choices = ', '.join([f'.{token.swift_kind()}' for token in child.token_choices])
text_choices = ', '.join([f'"{text}"' for text in child.text_choices])
return f"verify(layout[{idx}], as: Raw{child.type_name}{question_mark}.self, tokenChoices: [{token_choices}], textChoices: [{text_choices}])"
else:
return f"verify(layout[{idx}], as: Raw{child.type_name}{question_mark}.self)"
}%

/// 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) {
#if DEBUG
func _verify<Node: RawSyntaxNodeProtocol>(_ raw: RawSyntax?, as _: Node.Type, file: StaticString = #file, line: UInt = #line) {
assert(raw != nil, file: file, line: line)
assert(Node.isKindOf(raw!), file: file, line: line)
enum ValidationError: CustomStringConvertible {
case expectedNonNil(expectedKind: RawSyntaxNodeProtocol.Type, file: StaticString, line: UInt)
case kindMismatch(expectedKind: RawSyntaxNodeProtocol.Type, actualKind: SyntaxKind, file: StaticString, line: UInt)
case mismatchedTokenKindChoices(expectedKinds: [RawTokenKind], actualKind: RawTokenKind, file: StaticString, line: UInt)
case mismatchedTokenTextChoices(expectedTexts: [SyntaxText], actualText: SyntaxText, 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)"
case .mismatchedTokenKindChoices(expectedKinds: let expectedKinds, actualKind: let actualKind, file: _, line: _):
return "Expected token of kind \(expectedKinds) but received \(actualKind)"
case .mismatchedTokenTextChoices(expectedTexts: let expectedTexts, actualText: let actualText, file: _, line: _):
return "Expected token with text \(expectedTexts) but received \(actualText)"
}
}

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)
case .mismatchedTokenKindChoices(expectedKinds: _, actualKind: _, file: let file, line: let line):
return (file, line)
case .mismatchedTokenTextChoices(expectedTexts: _, actualText: _, file: let file, line: let line):
return (file, line)
}
}
}

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
}

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
}

func verify(_ raw: RawSyntax?, as: RawTokenSyntax.Type, tokenChoices: [RawTokenKind], textChoices: [SyntaxText], file: StaticString = #file, line: UInt = #line) -> ValidationError? {
guard let raw = raw else {
return .expectedNonNil(expectedKind: RawTokenSyntax.self, file: file, line: line)
}
guard let token = raw.as(RawTokenSyntax.self) else {
return .kindMismatch(expectedKind: RawTokenSyntax.self, actualKind: raw.kind, file: file, line: line)
}
guard tokenChoices.isEmpty || tokenChoices.contains(token.tokenKind) else {
return .mismatchedTokenKindChoices(expectedKinds: tokenChoices, actualKind: token.tokenKind, file: file, line: line)
}
guard textChoices.isEmpty || textChoices.contains(token.tokenText) else {
return .mismatchedTokenTextChoices(expectedTexts: textChoices, actualText: token.tokenText, file: file, line: line)
}
return nil
}

func verify(_ raw: RawSyntax?, as: RawTokenSyntax?.Type, tokenChoices: [RawTokenKind], textChoices: [SyntaxText], file: StaticString = #file, line: UInt = #line) -> ValidationError? {
if raw != nil {
return verify(raw, as: RawTokenSyntax.self, tokenChoices: tokenChoices, textChoices: textChoices, file: file, line: line)
}
return nil
}
func _verify<Node: RawSyntaxNodeProtocol>(_ raw: RawSyntax?, as _: Node?.Type, file: StaticString = #file, line: UInt = #line) {
raw.map { assert(Node.isKindOf($0), file: file, line: line) }

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)
}
}

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)
}
}

switch kind {
case .unknown:
break
Expand All @@ -41,11 +140,19 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
% if node.is_buildable() or node.is_missing():
assert(layout.count == ${len(node.children)})
% for (idx, child) in enumerate(node.children):
_verify(layout[${idx}], as: Raw${child.type_name}${"?" if child.is_optional else ""}.self)
% if child.node_choices:
assertAnyHasNoError(kind, ${idx}, [
% for node_choice in child.node_choices:
${get_verify_call(idx, node_choice, child.is_optional)},
% end
])
% else:
assertNoError(kind, ${idx}, ${get_verify_call(idx, child, child.is_optional)})
% end
% end
% elif node.is_syntax_collection():
for element in layout {
_verify(element, as: Raw${node.collection_element_type}.self)
for (index, element) in layout.enumerated() {
assertNoError(kind, index, verify(element, as: Raw${node.collection_element_type}.self))
}
% end
break
Expand Down
Loading