-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor some parts of the generator code (#73)
* Create MemberAccessExprSyntax helper to simplify chained member access * Improve EnumCaseElementSyntax with helper * misc refactor * Add SwitchCaseSyntax helper * Introduce a factory concept
- Loading branch information
1 parent
f2dd527
commit 4051043
Showing
5 changed files
with
409 additions
and
489 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
Sources/StringGenerator/Factories/StringInterpolationProtocolFactory.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,104 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
|
||
enum StringInterpolationProtocolFactory { | ||
static func initializedVariable( | ||
named variableName: TokenSyntax, | ||
type: MemberAccessExprSyntax, | ||
literalCapacity: some ExprSyntaxProtocol, | ||
interpolationCount: some ExprSyntaxProtocol | ||
) -> VariableDeclSyntax { | ||
VariableDeclSyntax(bindingSpecifier: .keyword(.var)) { | ||
PatternBindingSyntax( | ||
pattern: IdentifierPatternSyntax(identifier: variableName), | ||
initializer: InitializerClauseSyntax( | ||
value: FunctionCallExprSyntax( | ||
callee: type | ||
) { | ||
// literalCapacity: {something} | ||
LabeledExprSyntax( | ||
label: "literalCapacity", | ||
expression: literalCapacity | ||
) | ||
// interpolationCount: {something} | ||
LabeledExprSyntax( | ||
label: "interpolationCount", | ||
expression: interpolationCount | ||
) | ||
} | ||
) | ||
) | ||
} | ||
} | ||
|
||
static func appendFormatSpecifiableInterpolations( | ||
fromArguments sequenceName: TokenSyntax, | ||
intoVariable variableName: TokenSyntax | ||
) -> ForStmtSyntax { | ||
ForStmtSyntax( | ||
pattern: IdentifierPatternSyntax(identifier: "argument"), | ||
sequence: DeclReferenceExprSyntax(baseName: sequenceName), | ||
body: CodeBlockSyntax { | ||
// switch argument { ... } | ||
SwitchExprSyntax(subject: DeclReferenceExprSyntax(baseName: "argument")) { | ||
// case object(let object): | ||
// stringInterpolation.appendInterpolation(string) | ||
for placeholder in String.LocalizationValue.Placeholder.allCases { | ||
// case object(let value): | ||
SwitchCaseSyntax( | ||
singleCasePattern: ExpressionPatternSyntax( | ||
expression: FunctionCallExprSyntax( | ||
callee: MemberAccessExprSyntax(name: placeholder.caseName) | ||
) { | ||
LabeledExprSyntax( | ||
expression: PatternExprSyntax( | ||
pattern: ValueBindingPatternSyntax( | ||
bindingSpecifier: .keyword(.let), | ||
pattern: IdentifierPatternSyntax( | ||
identifier: "value" | ||
) | ||
) | ||
) | ||
) | ||
} | ||
) | ||
) { | ||
// stringInterpolation.appendInterpolation(value) | ||
FunctionCallExprSyntax( | ||
callee: MemberAccessExprSyntax(variableName, "appendInterpolation") | ||
) { | ||
LabeledExprSyntax(expression: DeclReferenceExprSyntax(baseName: "value")) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
) | ||
} | ||
|
||
// let {variableName} = {type}.init(stringInterpolation:) | ||
static func initializerClosure( | ||
bindingSpecifier: Keyword = .let, | ||
named variableName: TokenSyntax, | ||
type: MemberAccessExprSyntax | ||
) -> VariableDeclSyntax { | ||
VariableDeclSyntax(bindingSpecifier: .keyword(bindingSpecifier)) { | ||
PatternBindingSyntax( | ||
pattern: IdentifierPatternSyntax(identifier: variableName), | ||
initializer: InitializerClauseSyntax( | ||
value: MemberAccessExprSyntax( | ||
base: type, | ||
declName: DeclReferenceExprSyntax( | ||
baseName: .keyword(.`init`), | ||
argumentNames: DeclNameArgumentsSyntax( | ||
arguments: DeclNameArgumentListSyntax { | ||
DeclNameArgumentSyntax(name: "stringInterpolation") | ||
} | ||
) | ||
) | ||
) | ||
) | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.