Skip to content

Commit 0d584d4

Browse files
committed
Generate Token.swift instead of writing it by hand
This allows up to pick up the `ExpressibleAs` conformances from ExpressibleAsConformances.py
1 parent 66134f5 commit 0d584d4

File tree

5 files changed

+354
-201
lines changed

5 files changed

+354
-201
lines changed

Sources/SwiftSyntaxBuilder/Token.swift

Lines changed: 0 additions & 76 deletions
This file was deleted.

Sources/SwiftSyntaxBuilder/generated/Tokens.swift renamed to Sources/SwiftSyntaxBuilder/generated/Token.swift

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,71 @@
1515

1616
import SwiftSyntax
1717

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+
1883
/// Namespace for commonly used tokens with default trivia.
1984
public extension Token {
2085

0 commit comments

Comments
 (0)