-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeGeneration] improved creation of syntax nodes across raw and non…
…-raw - introduced `TypeConvertible`, `ParameterConvertible` and `SyntaxNodeConvertible` - introduced raw representations of `SyntaxNodeKind`, `Node` and `Child` for raw - removed `SyntaxBuildableType` - fixed the typing of raw node choices
- Loading branch information
1 parent
a7b91e6
commit 2d8a3cd
Showing
53 changed files
with
814 additions
and
806 deletions.
There are no files selected for viewing
This file contains 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 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 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
35 changes: 35 additions & 0 deletions
35
CodeGeneration/Sources/SyntaxSupport/ParameterConvertible.swift
This file contains 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,35 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 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 | ||
|
||
/// Implementations should provide necessary information for generating code of a parameter. | ||
public protocol ParameterConvertible { | ||
/// The type that is used for parameters in SwiftSyntaxBuilder that take this | ||
/// type of syntax node and expect an existential type if the parameter type is a protocol. | ||
var parameterAnyType: TypeSyntax { | ||
get | ||
} | ||
|
||
/// The type that is used for parameters in SwiftSyntaxBuilder that take this | ||
/// type of syntax node and expect a generic type if the parameter type is a protocol. | ||
var parameterSomeType: TypeSyntax { | ||
get | ||
} | ||
|
||
/// If the type has a default value (because it is optional or a token | ||
/// with fixed test), return an expression that can be used as the | ||
/// default value for a function parameter. Otherwise, return `nil`. | ||
var defaultValue: ExprSyntax? { | ||
get | ||
} | ||
} |
This file contains 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 - 2024 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 | ||
|
||
public extension Child { | ||
/// The raw representation of ``Child``. | ||
struct Raw: SyntaxNodeConvertible, NodeChoiceConvertible, ParameterConvertible { | ||
var child: Child | ||
|
||
public var isOptional: Bool { | ||
self.child.isOptional | ||
} | ||
|
||
public var isNode: Bool { | ||
self.child.isNode | ||
} | ||
|
||
public var syntaxType: TypeSyntax { | ||
switch self.child.kind { | ||
case .node(let kind), .collection(let kind, _, _, _): | ||
return kind.raw.syntaxType | ||
case .nodeChoices: | ||
return self.child.syntaxChoicesType | ||
case .token: | ||
return "RawTokenSyntax" | ||
} | ||
} | ||
|
||
public var syntaxNodeKind: SyntaxNodeKind.Raw { | ||
self.child.syntaxNodeKind.raw | ||
} | ||
|
||
public var documentation: SwiftSyntax.Trivia { | ||
self.child.documentation | ||
} | ||
|
||
public var experimentalFeature: ExperimentalFeature? { | ||
self.child.experimentalFeature | ||
} | ||
|
||
public var apiAttributes: AttributeListSyntax { | ||
self.child.apiAttributes | ||
} | ||
|
||
public var identifier: TokenSyntax { | ||
self.child.identifier | ||
} | ||
|
||
public var parameterAnyType: TypeSyntax { | ||
self.child.parameterType(specifier: "any", protocolType: self.protocolType, syntaxType: self.syntaxType) | ||
} | ||
|
||
public var parameterSomeType: TypeSyntax { | ||
if self.child.isBaseNode && !self.child.isOptional { | ||
// we restrict the use of generic type to non-optional parameter types, otherwise call sites would no longer be | ||
// able to just pass `nil` to this parameter without specializing `(some Raw<Kind>SyntaxNodeProtocol)?` | ||
// | ||
// we've opted out of providing a default value to the parameter (e.g. `RawExprSyntax?.none`) as a workaround, | ||
// as passing an explicit `nil` would prompt developers to think clearly whether this parameter should be parsed | ||
return "some \(self.protocolType)" | ||
} else { | ||
return self.actualType | ||
} | ||
} | ||
|
||
public var defaultValue: ExprSyntax? { | ||
self.child.defaultValue(syntaxType: self.syntaxType) | ||
} | ||
} | ||
|
||
/// The raw representation of this child. | ||
var raw: Raw { | ||
Raw(child: self) | ||
} | ||
} |
This file contains 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,54 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 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 | ||
|
||
public extension Node { | ||
/// The raw representation of ``Node``. The definition of a syntax node that, when generated, conforms to | ||
/// `RawSyntaxNodeProtocol`. | ||
struct Raw: SyntaxNodeConvertible, NodeChoiceConvertible { | ||
var node: Node | ||
|
||
public var isOptional: Bool { | ||
self.node.isOptional | ||
} | ||
|
||
public var syntaxNodeKind: SyntaxNodeKind.Raw { | ||
self.node.syntaxNodeKind.raw | ||
} | ||
|
||
public var isNode: Bool { | ||
self.node.isNode | ||
} | ||
|
||
public var documentation: SwiftSyntax.Trivia { | ||
self.node.documentation | ||
} | ||
|
||
public var experimentalFeature: ExperimentalFeature? { | ||
self.node.experimentalFeature | ||
} | ||
|
||
public var apiAttributes: AttributeListSyntax { | ||
self.node.apiAttributes | ||
} | ||
|
||
public var identifier: TokenSyntax { | ||
self.node.identifier | ||
} | ||
} | ||
|
||
/// The raw representation of this node. | ||
var raw: Raw { | ||
Raw(node: self) | ||
} | ||
} |
Oops, something went wrong.