-
Notifications
You must be signed in to change notification settings - Fork 440
Move gyb files to codegen #1136
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
Changes from all commits
Commits
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
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
85 changes: 85 additions & 0 deletions
85
CodeGeneration/Sources/generate-swiftsyntax/SyntaxAnyVisitorFile.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,85 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 syntaxAnyVisitorFile = SourceFile { | ||
ClassDecl(""" | ||
\(generateCopyrightHeader(for: "generate-swiftsyntax")) | ||
|
||
/// A `SyntaxVisitor` that can visit the nodes as generic `Syntax` values. | ||
/// | ||
/// This subclass of `SyntaxVisitor` is slower than the type-specific visitation | ||
/// of `SyntaxVisitor`. Use `SyntaxAnyVisitor` if the `visitAny(_)` function | ||
/// would be useful to have, otherwise inherit from `SyntaxVisitor`. | ||
/// | ||
/// This works by overriding the type-specific visit function that delegate to | ||
/// `visitAny(_)`. A subclass that provides a custom type-specific visit | ||
/// function, should also call `visitAny(_)` in its implementation, if calling | ||
/// `visitAny` is needed: | ||
/// | ||
/// struct MyVisitor: SyntaxAnyVisitor { | ||
/// func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind { | ||
/// <code> | ||
/// } | ||
/// | ||
/// func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind { | ||
/// <code> | ||
/// // Call this to pass tokens to `visitAny(_)` as well if needed | ||
/// visitAny(token) | ||
/// } | ||
open class SyntaxAnyVisitor: SyntaxVisitor | ||
""") { | ||
FunctionDecl(""" | ||
open func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind { | ||
return .visitChildren | ||
} | ||
""") | ||
|
||
FunctionDecl(""" | ||
/// The function called after visiting the node and its descendents. | ||
/// - node: the node we just finished visiting. | ||
open func visitAnyPost(_ node: Syntax) {} | ||
""") | ||
|
||
FunctionDecl(""" | ||
// MARK: Override type specific visit methods | ||
|
||
override open func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind { | ||
return visitAny(token._syntaxNode) | ||
} | ||
""") | ||
|
||
FunctionDecl(""" | ||
override open func visitPost(_ node: TokenSyntax) { | ||
visitAnyPost(node._syntaxNode) | ||
} | ||
""") | ||
|
||
for node in SYNTAX_NODES where node.isVisitable { | ||
FunctionDecl(""" | ||
override open func visit(_ node: \(raw: node.name)) -> SyntaxVisitorContinueKind { | ||
return visitAny(node._syntaxNode) | ||
} | ||
""") | ||
|
||
FunctionDecl(""" | ||
override open func visitPost(_ node: \(raw: node.name)) { | ||
visitAnyPost(node._syntaxNode) | ||
} | ||
""") | ||
} | ||
} | ||
} |
228 changes: 228 additions & 0 deletions
228
CodeGeneration/Sources/generate-swiftsyntax/SyntaxBaseNodesFile.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,228 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 syntaxBaseNodesFile = SourceFile(leadingTrivia: [.blockComment(generateCopyrightHeader(for: "generate-swiftsyntax"))]) { | ||
for node in SYNTAX_NODES where node.isBase { | ||
ProtocolDecl(""" | ||
// MARK: - \(raw: node.name) | ||
|
||
/// Protocol to which all `\(raw: node.name)` nodes conform. Extension point to add | ||
/// common methods to all `\(raw: node.name)` nodes. | ||
/// DO NOT CONFORM TO THIS PROTOCOL YOURSELF! | ||
public protocol \(raw: node.name)Protocol: \(raw: node.baseType.baseName)Protocol {} | ||
""") | ||
|
||
ExtensionDecl("public extension Syntax") { | ||
FunctionDecl(""" | ||
/// Check whether the non-type erased version of this syntax node conforms to | ||
/// \(raw: node.name)Protocol. | ||
/// Note that this will incur an existential conversion. | ||
func isProtocol(_: \(raw: node.name)Protocol.Protocol) -> Bool { | ||
return self.asProtocol(\(raw: node.name)Protocol.self) != nil | ||
} | ||
""") | ||
|
||
FunctionDecl(""" | ||
/// Return the non-type erased version of this syntax node if it conforms to | ||
/// \(raw: node.name)Protocol. Otherwise return nil. | ||
/// Note that this will incur an existential conversion. | ||
func asProtocol(_: \(raw: node.name)Protocol.Protocol) -> \(raw: node.name)Protocol? { | ||
return self.asProtocol(SyntaxProtocol.self) as? \(raw: node.name)Protocol | ||
} | ||
""") | ||
} | ||
|
||
StructDecl(""" | ||
\(node.description ?? "") | ||
public struct \(node.name): \(node.name)Protocol, SyntaxHashable | ||
""") { | ||
VariableDecl("public let _syntaxNode: Syntax") | ||
|
||
InitializerDecl(""" | ||
/// Create a `\(raw: node.name)` node from a specialized syntax node. | ||
public init<S: \(raw: node.name)Protocol>(_ syntax: S) { | ||
// We know this cast is going to succeed. Go through init(_: SyntaxData) | ||
// to do a sanity check and verify the kind matches in debug builds and get | ||
// maximum performance in release builds. | ||
self.init(syntax._syntaxNode.data) | ||
} | ||
""") | ||
|
||
InitializerDecl(""" | ||
/// Create a `\(raw: node.name)` node from a specialized optional syntax node. | ||
public init?<S: \(raw: node.name)Protocol>(_ syntax: S?) { | ||
guard let syntax = syntax else { return nil } | ||
self.init(syntax) | ||
} | ||
""") | ||
|
||
InitializerDecl(""" | ||
public init(fromProtocol syntax: \(raw: node.name)Protocol) { | ||
// We know this cast is going to succeed. Go through init(_: SyntaxData) | ||
// to do a sanity check and verify the kind matches in debug builds and get | ||
// maximum performance in release builds. | ||
self.init(syntax._syntaxNode.data) | ||
} | ||
""") | ||
|
||
InitializerDecl(""" | ||
/// Create a `\(raw: node.name)` node from a specialized optional syntax node. | ||
public init?(fromProtocol syntax: \(raw: node.name)Protocol?) { | ||
guard let syntax = syntax else { return nil } | ||
self.init(fromProtocol: syntax) | ||
} | ||
""") | ||
|
||
InitializerDecl("public init?<S: SyntaxProtocol>(_ node: S)") { | ||
SwitchStmt(expression: MemberAccessExpr("node.raw.kind")) { | ||
SwitchCaseList { | ||
SwitchCase( | ||
label: .case(SwitchCaseLabel { | ||
for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind { | ||
CaseItem( | ||
pattern: EnumCasePattern( | ||
period: .period, | ||
caseName: .identifier(childNode.swiftSyntaxKind)) | ||
) | ||
} | ||
})) { | ||
Expr("self._syntaxNode = node._syntaxNode") | ||
} | ||
|
||
SwitchCase("default:") { | ||
ReturnStmt("return nil") | ||
} | ||
} | ||
} | ||
} | ||
|
||
InitializerDecl(""" | ||
/// Creates a `\(node.name)` node from the given `SyntaxData`. This assumes | ||
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour | ||
/// is undefined. | ||
internal init(_ data: SyntaxData) | ||
""") { | ||
IfConfigDecl( | ||
clauses: IfConfigClauseList { | ||
IfConfigClause( | ||
poundKeyword: .poundIfKeyword(), | ||
condition: Expr("DEBUG"), | ||
elements: IfConfigClauseSyntax.Elements.statements(CodeBlockItemList { | ||
SwitchStmt( | ||
expression: Expr("data.raw.kind")) { | ||
SwitchCase( | ||
label: .case(SwitchCaseLabel { | ||
for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind { | ||
CaseItem( | ||
pattern: EnumCasePatternSyntax( | ||
period: .period, | ||
caseName: .identifier(childNode.swiftSyntaxKind)) | ||
) | ||
} | ||
})) { | ||
BreakStmt() | ||
} | ||
|
||
SwitchCase("default:") { | ||
FunctionCallExpr("fatalError(\"Unable to create \(raw: node.name) from \\(data.raw.kind)\")") | ||
} | ||
} | ||
}) | ||
) | ||
} | ||
) | ||
|
||
Expr("self._syntaxNode = Syntax(data)") | ||
} | ||
|
||
FunctionDecl(""" | ||
public func `is`<S: \(raw: node.name)Protocol>(_ syntaxType: S.Type) -> Bool { | ||
return self.as(syntaxType) != nil | ||
} | ||
""") | ||
|
||
FunctionDecl(""" | ||
public func `as`<S: \(raw: node.name)Protocol>(_ syntaxType: S.Type) -> S? { | ||
return S.init(self) | ||
} | ||
""") | ||
|
||
FunctionDecl(""" | ||
public func cast<S: \(raw: node.name)Protocol>(_ syntaxType: S.Type) -> S { | ||
return self.as(S.self)! | ||
} | ||
""") | ||
|
||
FunctionDecl(""" | ||
/// Syntax nodes always conform to `\(raw: node.name)Protocol`. This API is just | ||
/// added for consistency. | ||
/// Note that this will incur an existential conversion. | ||
@available(*, deprecated, message: "Expression always evaluates to true") | ||
public func isProtocol(_: \(raw: node.name)Protocol.Protocol) -> Bool { | ||
return true | ||
} | ||
""") | ||
|
||
FunctionDecl(""" | ||
/// Return the non-type erased version of this syntax node. | ||
/// Note that this will incur an existential conversion. | ||
public func asProtocol(_: \(raw: node.name)Protocol.Protocol) -> \(raw: node.name)Protocol { | ||
return Syntax(self).asProtocol(\(raw: node.name)Protocol.self)! | ||
} | ||
""") | ||
|
||
|
||
VariableDecl( | ||
modifiers: [DeclModifier(name: .public), DeclModifier(name: .static)], | ||
name: IdentifierPattern("structure"), | ||
type: TypeAnnotation( | ||
type: TypeSyntax("SyntaxNodeStructure")) | ||
) { | ||
ReturnStmt( | ||
expression: FunctionCallExpr( | ||
callee: MemberAccessExpr(".choices")) { | ||
TupleExprElement( | ||
expression: ArrayExprSyntax { | ||
for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind { | ||
ArrayElement( | ||
expression: FunctionCallExpr("\n.node(\(raw: childNode.name).self)") | ||
) | ||
} | ||
}) | ||
} | ||
) | ||
} | ||
|
||
FunctionDecl(""" | ||
public func childNameForDiagnostics(_ index: SyntaxChildrenIndex) -> String? { | ||
return Syntax(self).childNameForDiagnostics(index) | ||
} | ||
""") | ||
} | ||
|
||
ExtensionDecl(""" | ||
extension \(raw: node.name): CustomReflectable { | ||
/// Reconstructs the real syntax type for this type from the node's kind and | ||
/// provides a mirror that reflects this type. | ||
public var customMirror: Mirror { | ||
return Mirror(reflecting: Syntax(self).asProtocol(SyntaxProtocol.self)) | ||
} | ||
} | ||
""") | ||
|
||
} | ||
} |
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
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.