|
15 | 15 |
|
16 | 16 | import SwiftSyntax
|
17 | 17 |
|
| 18 | +/// Represents `TokenSyntax` in `SwiftSyntaxBuilder`. |
| 19 | +/// At the moment, this just wraps `TokenSyntax`, but we can make it store just the information necessary to build a `TokenSyntax` in the future. |
| 20 | +public struct Token: SyntaxBuildable, ExpressibleAsBinaryOperatorExpr, ExpressibleAsDeclModifier, ExpressibleAsIdentifierExpr, ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList { |
| 21 | + let tokenSyntax: TokenSyntax |
| 22 | + |
| 23 | + var text: String { |
| 24 | + tokenSyntax.text |
| 25 | + } |
| 26 | + |
| 27 | + public init (tokenSyntax: TokenSyntax) { |
| 28 | + self.tokenSyntax = tokenSyntax |
| 29 | + } |
| 30 | + |
| 31 | + public func withLeadingTrivia(_ trivia: Trivia) -> Token { |
| 32 | + Token(tokenSyntax: tokenSyntax.withLeadingTrivia(trivia)) |
| 33 | + } |
| 34 | + |
| 35 | + public func withTrailingTrivia(_ trivia: Trivia) -> Token { |
| 36 | + Token(tokenSyntax: tokenSyntax.withTrailingTrivia(trivia)) |
| 37 | + } |
| 38 | + |
| 39 | + public func buildToken() -> TokenSyntax { |
| 40 | + tokenSyntax |
| 41 | + } |
| 42 | + |
| 43 | + public func buildSyntax(format: Format, leadingTrivia: Trivia?) -> Syntax { |
| 44 | + Syntax(tokenSyntax) |
| 45 | + } |
| 46 | + |
| 47 | + /// Conformance to ExpressibleAsTokenList |
| 48 | + public func createTokenList() -> TokenList { |
| 49 | + return TokenList([self]) |
| 50 | + } |
| 51 | + |
| 52 | + /// Conformance to ExpressibleAsNonEmptyTokenList |
| 53 | + public func createNonEmptyTokenList() -> NonEmptyTokenList { |
| 54 | + return NonEmptyTokenList([self]) |
| 55 | + } |
| 56 | + |
| 57 | + /// Conformance to ExpressibleAsBinaryOperatorExpr |
| 58 | + public func createBinaryOperatorExpr() -> BinaryOperatorExpr { |
| 59 | + return BinaryOperatorExpr(operatorToken: self) |
| 60 | + } |
| 61 | + |
| 62 | + /// Conformance to ExpressibleAsDeclModifier |
| 63 | + public func createDeclModifier() -> DeclModifier { |
| 64 | + return DeclModifier(name: self) |
| 65 | + } |
| 66 | + |
| 67 | + /// Conformance to ExpressibleAsIdentifierExpr |
| 68 | + public func createIdentifierExpr() -> IdentifierExpr { |
| 69 | + return IdentifierExpr(identifier: self) |
| 70 | + } |
| 71 | + |
| 72 | + /// `Token` conforms to `SyntaxBuildable` via different paths, so we need to pick one default conversion path. |
| 73 | + public func createSyntaxBuildable() -> SyntaxBuildable { |
| 74 | + return createIdentifierExpr() |
| 75 | + } |
| 76 | + |
| 77 | + /// `Token` conforms to `ExprBuildable` via different paths, so we need to pick one default conversion path. |
| 78 | + public func createExprBuildable() -> ExprBuildable { |
| 79 | + return createIdentifierExpr() |
| 80 | + } |
| 81 | +} |
| 82 | + |
18 | 83 | /// Namespace for commonly used tokens with default trivia.
|
19 | 84 | public extension Token {
|
20 | 85 |
|
|
0 commit comments