TextBasedRenderer.swift 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. /*
  2. * Copyright 2023, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. //===----------------------------------------------------------------------===//
  17. //
  18. // This source file is part of the SwiftOpenAPIGenerator open source project
  19. //
  20. // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
  21. // Licensed under Apache License v2.0
  22. //
  23. // See LICENSE.txt for license information
  24. // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
  25. //
  26. // SPDX-License-Identifier: Apache-2.0
  27. //
  28. //===----------------------------------------------------------------------===//
  29. /// An object for building up a generated file line-by-line.
  30. ///
  31. /// After creation, make calls such as `writeLine` to build up the file,
  32. /// and call `rendered` at the end to get the full file contents.
  33. final class StringCodeWriter {
  34. /// The stored lines of code.
  35. private var lines: [String]
  36. /// The current nesting level.
  37. private var level: Int
  38. /// The indentation for each level as the number of spaces.
  39. internal let indentation: Int
  40. /// Whether the next call to `writeLine` will continue writing to the last
  41. /// stored line. Otherwise a new line is appended.
  42. private var nextWriteAppendsToLastLine: Bool = false
  43. /// Creates a new empty writer.
  44. init(indentation: Int) {
  45. self.level = 0
  46. self.lines = []
  47. self.indentation = indentation
  48. }
  49. /// Concatenates the stored lines of code into a single string.
  50. /// - Returns: The contents of the full file in a single string.
  51. func rendered() -> String { lines.joined(separator: "\n") }
  52. /// Writes a line of code.
  53. ///
  54. /// By default, a new line is appended to the file.
  55. ///
  56. /// To continue the last line, make a call to `nextLineAppendsToLastLine`
  57. /// before calling `writeLine`.
  58. /// - Parameter line: The contents of the line to write.
  59. func writeLine(_ line: String) {
  60. let newLine: String
  61. if nextWriteAppendsToLastLine && !lines.isEmpty {
  62. let existingLine = lines.removeLast()
  63. newLine = existingLine + line
  64. } else if line.isEmpty {
  65. // Skip indentation to avoid trailing whitespace on blank lines.
  66. newLine = line
  67. } else {
  68. let indentation = Array(repeating: " ", count: self.indentation * level).joined()
  69. newLine = indentation + line
  70. }
  71. lines.append(newLine)
  72. nextWriteAppendsToLastLine = false
  73. }
  74. /// Increases the indentation level by 1.
  75. func push() { level += 1 }
  76. /// Decreases the indentation level by 1.
  77. /// - Precondition: Current level must be greater than 0.
  78. func pop() {
  79. precondition(level > 0, "Cannot pop below 0")
  80. level -= 1
  81. }
  82. /// Executes the provided closure with one level deeper indentation.
  83. /// - Parameter work: The closure to execute.
  84. /// - Returns: The result of the closure execution.
  85. func withNestedLevel<R>(_ work: () -> R) -> R {
  86. push()
  87. defer { pop() }
  88. return work()
  89. }
  90. /// Sets a flag on the writer so that the next call to `writeLine` continues
  91. /// the last stored line instead of starting a new line.
  92. ///
  93. /// Safe to call repeatedly, it gets reset by `writeLine`.
  94. func nextLineAppendsToLastLine() { nextWriteAppendsToLastLine = true }
  95. }
  96. @available(*, unavailable)
  97. extension TextBasedRenderer: Sendable {}
  98. /// A renderer that uses string interpolation and concatenation
  99. /// to convert the provided structure code into raw string form.
  100. @available(gRPCSwift 2.0, *)
  101. struct TextBasedRenderer: RendererProtocol {
  102. func render(
  103. structured: StructuredSwiftRepresentation
  104. ) throws
  105. -> SourceFile
  106. {
  107. let namedFile = structured.file
  108. renderFile(namedFile.contents)
  109. let string = writer.rendered()
  110. return SourceFile(name: namedFile.name, contents: string)
  111. }
  112. /// The underlying writer.
  113. private let writer: StringCodeWriter
  114. /// Creates a new empty renderer.
  115. static var `default`: TextBasedRenderer { .init(indentation: 4) }
  116. init(indentation: Int) {
  117. self.writer = StringCodeWriter(indentation: indentation)
  118. }
  119. // MARK: - Internals
  120. /// Returns the current contents of the writer as a string.
  121. func renderedContents() -> String { writer.rendered() }
  122. /// Renders the specified Swift file.
  123. func renderFile(_ description: FileDescription) {
  124. if let topComment = description.topComment {
  125. renderComment(topComment)
  126. writer.writeLine("")
  127. }
  128. if let imports = description.imports {
  129. renderImports(imports)
  130. writer.writeLine("")
  131. }
  132. for (codeBlock, isLast) in description.codeBlocks.enumeratedWithLastMarker() {
  133. renderCodeBlock(codeBlock)
  134. if !isLast {
  135. writer.writeLine("")
  136. }
  137. }
  138. }
  139. /// Renders the specified comment.
  140. func renderComment(_ comment: Comment) {
  141. let prefix: String
  142. let commentString: String
  143. switch comment {
  144. case .inline(let string):
  145. prefix = "//"
  146. commentString = string
  147. case .doc(let string):
  148. prefix = "///"
  149. commentString = string
  150. case .mark(let string, sectionBreak: true):
  151. prefix = "// MARK: -"
  152. commentString = string
  153. case .mark(let string, sectionBreak: false):
  154. prefix = "// MARK:"
  155. commentString = string
  156. case .preFormatted(let string):
  157. prefix = ""
  158. commentString = string
  159. }
  160. let lines = commentString.transformingLines { line, isLast in
  161. // The last line of a comment that is blank should be dropped.
  162. // Pre formatted documentation might contain such lines.
  163. if line.isEmpty && prefix.isEmpty && isLast {
  164. return nil
  165. } else {
  166. let formattedPrefix = !prefix.isEmpty && !line.isEmpty ? "\(prefix) " : prefix
  167. return "\(formattedPrefix)\(line)"
  168. }
  169. }
  170. lines.forEach(writer.writeLine)
  171. }
  172. /// Renders the specified import statements.
  173. func renderImports(_ imports: [ImportDescription]?) { (imports ?? []).forEach(renderImport) }
  174. /// Renders a single import statement.
  175. func renderImport(_ description: ImportDescription) {
  176. func render(preconcurrency: Bool) {
  177. let spiPrefix = description.spi.map { "@_spi(\($0)) " } ?? ""
  178. let preconcurrencyPrefix = preconcurrency ? "@preconcurrency " : ""
  179. let accessLevel = description.accessLevel.map { "\($0) " } ?? ""
  180. if let item = description.item {
  181. writer.writeLine(
  182. "\(preconcurrencyPrefix)\(spiPrefix)\(accessLevel)import \(item.kind) \(description.moduleName).\(item.name)"
  183. )
  184. } else if let moduleTypes = description.moduleTypes {
  185. for type in moduleTypes {
  186. writer.writeLine("\(preconcurrencyPrefix)\(spiPrefix)\(accessLevel)import \(type)")
  187. }
  188. } else {
  189. writer.writeLine(
  190. "\(preconcurrencyPrefix)\(spiPrefix)\(accessLevel)import \(description.moduleName)"
  191. )
  192. }
  193. }
  194. switch description.preconcurrency {
  195. case .always: render(preconcurrency: true)
  196. case .never: render(preconcurrency: false)
  197. case .onOS(let operatingSystems):
  198. writer.writeLine("#if \(operatingSystems.map { "os(\($0))" }.joined(separator: " || "))")
  199. render(preconcurrency: true)
  200. writer.writeLine("#else")
  201. render(preconcurrency: false)
  202. writer.writeLine("#endif")
  203. }
  204. }
  205. /// Renders the specified access modifier.
  206. func renderedAccessModifier(_ accessModifier: AccessModifier) -> String {
  207. switch accessModifier {
  208. case .public: return "public"
  209. case .package: return "package"
  210. case .internal: return "internal"
  211. case .fileprivate: return "fileprivate"
  212. case .private: return "private"
  213. }
  214. }
  215. /// Renders the specified identifier.
  216. func renderIdentifier(_ identifier: IdentifierDescription) {
  217. switch identifier {
  218. case .pattern(let string): writer.writeLine(string)
  219. case .type(let existingTypeDescription):
  220. renderExistingTypeDescription(existingTypeDescription)
  221. }
  222. }
  223. /// Renders the specified member access expression.
  224. func renderMemberAccess(_ memberAccess: MemberAccessDescription) {
  225. if let left = memberAccess.left {
  226. renderExpression(left)
  227. writer.nextLineAppendsToLastLine()
  228. }
  229. writer.writeLine(".\(memberAccess.right)")
  230. }
  231. /// Renders the specified function call argument.
  232. func renderFunctionCallArgument(_ arg: FunctionArgumentDescription) {
  233. if let left = arg.label {
  234. writer.writeLine("\(left): ")
  235. writer.nextLineAppendsToLastLine()
  236. }
  237. renderExpression(arg.expression)
  238. }
  239. /// Renders the specified function call.
  240. func renderFunctionCall(_ functionCall: FunctionCallDescription) {
  241. renderExpression(functionCall.calledExpression)
  242. writer.nextLineAppendsToLastLine()
  243. writer.writeLine("(")
  244. let arguments = functionCall.arguments
  245. if arguments.count > 1 {
  246. writer.withNestedLevel {
  247. for (argument, isLast) in arguments.enumeratedWithLastMarker() {
  248. renderFunctionCallArgument(argument)
  249. if !isLast {
  250. writer.nextLineAppendsToLastLine()
  251. writer.writeLine(",")
  252. }
  253. }
  254. }
  255. } else {
  256. writer.nextLineAppendsToLastLine()
  257. if let argument = arguments.first { renderFunctionCallArgument(argument) }
  258. writer.nextLineAppendsToLastLine()
  259. }
  260. writer.writeLine(")")
  261. if let trailingClosure = functionCall.trailingClosure {
  262. writer.nextLineAppendsToLastLine()
  263. writer.writeLine(" ")
  264. renderClosureInvocation(trailingClosure)
  265. }
  266. }
  267. /// Renders the specified assignment expression.
  268. func renderAssignment(_ assignment: AssignmentDescription) {
  269. renderExpression(assignment.left)
  270. writer.nextLineAppendsToLastLine()
  271. writer.writeLine(" = ")
  272. writer.nextLineAppendsToLastLine()
  273. renderExpression(assignment.right)
  274. }
  275. /// Renders the specified switch case kind.
  276. func renderSwitchCaseKind(_ kind: SwitchCaseKind) {
  277. switch kind {
  278. case let .`case`(expression, associatedValueNames):
  279. let associatedValues: String
  280. let maybeLet: String
  281. if !associatedValueNames.isEmpty {
  282. associatedValues = "(" + associatedValueNames.joined(separator: ", ") + ")"
  283. maybeLet = "let "
  284. } else {
  285. associatedValues = ""
  286. maybeLet = ""
  287. }
  288. writer.writeLine("case \(maybeLet)")
  289. writer.nextLineAppendsToLastLine()
  290. renderExpression(expression)
  291. writer.nextLineAppendsToLastLine()
  292. writer.writeLine(associatedValues)
  293. case .multiCase(let expressions):
  294. writer.writeLine("case ")
  295. writer.nextLineAppendsToLastLine()
  296. for (expression, isLast) in expressions.enumeratedWithLastMarker() {
  297. renderExpression(expression)
  298. writer.nextLineAppendsToLastLine()
  299. if !isLast { writer.writeLine(", ") }
  300. writer.nextLineAppendsToLastLine()
  301. }
  302. case .`default`: writer.writeLine("default")
  303. }
  304. }
  305. /// Renders the specified switch case.
  306. func renderSwitchCase(_ switchCase: SwitchCaseDescription) {
  307. renderSwitchCaseKind(switchCase.kind)
  308. writer.nextLineAppendsToLastLine()
  309. writer.writeLine(":")
  310. writer.withNestedLevel { renderCodeBlocks(switchCase.body) }
  311. }
  312. /// Renders the specified switch expression.
  313. func renderSwitch(_ switchDesc: SwitchDescription) {
  314. writer.writeLine("switch ")
  315. writer.nextLineAppendsToLastLine()
  316. renderExpression(switchDesc.switchedExpression)
  317. writer.nextLineAppendsToLastLine()
  318. writer.writeLine(" {")
  319. for caseDesc in switchDesc.cases { renderSwitchCase(caseDesc) }
  320. writer.writeLine("}")
  321. }
  322. /// Renders the specified if statement.
  323. func renderIf(_ ifDesc: IfStatementDescription) {
  324. let ifBranch = ifDesc.ifBranch
  325. writer.writeLine("if ")
  326. writer.nextLineAppendsToLastLine()
  327. renderExpression(ifBranch.condition)
  328. writer.nextLineAppendsToLastLine()
  329. writer.writeLine(" {")
  330. writer.withNestedLevel { renderCodeBlocks(ifBranch.body) }
  331. writer.writeLine("}")
  332. for branch in ifDesc.elseIfBranches {
  333. writer.nextLineAppendsToLastLine()
  334. writer.writeLine(" else if ")
  335. writer.nextLineAppendsToLastLine()
  336. renderExpression(branch.condition)
  337. writer.nextLineAppendsToLastLine()
  338. writer.writeLine(" {")
  339. writer.withNestedLevel { renderCodeBlocks(branch.body) }
  340. writer.writeLine("}")
  341. }
  342. if let elseBody = ifDesc.elseBody {
  343. writer.nextLineAppendsToLastLine()
  344. writer.writeLine(" else {")
  345. writer.withNestedLevel { renderCodeBlocks(elseBody) }
  346. writer.writeLine("}")
  347. }
  348. }
  349. /// Renders the specified switch expression.
  350. func renderDoStatement(_ description: DoStatementDescription) {
  351. writer.writeLine("do {")
  352. writer.withNestedLevel { renderCodeBlocks(description.doStatement) }
  353. if let catchBody = description.catchBody {
  354. writer.writeLine("} catch {")
  355. if !catchBody.isEmpty {
  356. writer.withNestedLevel { renderCodeBlocks(catchBody) }
  357. } else {
  358. writer.nextLineAppendsToLastLine()
  359. }
  360. }
  361. writer.writeLine("}")
  362. }
  363. /// Renders the specified value binding expression.
  364. func renderValueBinding(_ valueBinding: ValueBindingDescription) {
  365. writer.writeLine("\(renderedBindingKind(valueBinding.kind)) ")
  366. writer.nextLineAppendsToLastLine()
  367. renderFunctionCall(valueBinding.value)
  368. }
  369. /// Renders the specified keyword.
  370. func renderedKeywordKind(_ kind: KeywordKind) -> String {
  371. switch kind {
  372. case .return: return "return"
  373. case .try(hasPostfixQuestionMark: let hasPostfixQuestionMark):
  374. return "try\(hasPostfixQuestionMark ? "?" : "")"
  375. case .await: return "await"
  376. case .throw: return "throw"
  377. case .yield: return "yield"
  378. }
  379. }
  380. /// Renders the specified unary keyword expression.
  381. func renderUnaryKeywordExpression(_ expression: UnaryKeywordDescription) {
  382. writer.writeLine(renderedKeywordKind(expression.kind))
  383. guard let expr = expression.expression else { return }
  384. writer.nextLineAppendsToLastLine()
  385. writer.writeLine(" ")
  386. writer.nextLineAppendsToLastLine()
  387. renderExpression(expr)
  388. }
  389. /// Renders the specified closure invocation.
  390. func renderClosureInvocation(_ invocation: ClosureInvocationDescription) {
  391. writer.writeLine("{")
  392. if !invocation.argumentNames.isEmpty {
  393. writer.nextLineAppendsToLastLine()
  394. writer.writeLine(" \(invocation.argumentNames.joined(separator: ", ")) in")
  395. }
  396. if let body = invocation.body { writer.withNestedLevel { renderCodeBlocks(body) } }
  397. writer.writeLine("}")
  398. }
  399. /// Renders the specified binary operator.
  400. func renderedBinaryOperator(_ op: BinaryOperator) -> String { op.rawValue }
  401. /// Renders the specified binary operation.
  402. func renderBinaryOperation(_ operation: BinaryOperationDescription) {
  403. renderExpression(operation.left)
  404. writer.nextLineAppendsToLastLine()
  405. writer.writeLine(" \(renderedBinaryOperator(operation.operation)) ")
  406. writer.nextLineAppendsToLastLine()
  407. renderExpression(operation.right)
  408. }
  409. /// Renders the specified inout expression.
  410. func renderInOutDescription(_ description: InOutDescription) {
  411. writer.writeLine("&")
  412. writer.nextLineAppendsToLastLine()
  413. renderExpression(description.referencedExpr)
  414. }
  415. /// Renders the specified optional chaining expression.
  416. func renderOptionalChainingDescription(_ description: OptionalChainingDescription) {
  417. renderExpression(description.referencedExpr)
  418. writer.nextLineAppendsToLastLine()
  419. writer.writeLine("?")
  420. }
  421. /// Renders the specified tuple expression.
  422. func renderTupleDescription(_ description: TupleDescription) {
  423. writer.writeLine("(")
  424. writer.nextLineAppendsToLastLine()
  425. let members = description.members
  426. for (member, isLast) in members.enumeratedWithLastMarker() {
  427. renderExpression(member)
  428. if !isLast {
  429. writer.nextLineAppendsToLastLine()
  430. writer.writeLine(", ")
  431. }
  432. writer.nextLineAppendsToLastLine()
  433. }
  434. writer.writeLine(")")
  435. }
  436. /// Renders the specified expression.
  437. func renderExpression(_ expression: Expression) {
  438. switch expression {
  439. case .literal(let literalDescription): renderLiteral(literalDescription)
  440. case .identifier(let identifierDescription):
  441. renderIdentifier(identifierDescription)
  442. case .memberAccess(let memberAccessDescription): renderMemberAccess(memberAccessDescription)
  443. case .functionCall(let functionCallDescription): renderFunctionCall(functionCallDescription)
  444. case .assignment(let assignment): renderAssignment(assignment)
  445. case .switch(let switchDesc): renderSwitch(switchDesc)
  446. case .ifStatement(let ifDesc): renderIf(ifDesc)
  447. case .doStatement(let doStmt): renderDoStatement(doStmt)
  448. case .valueBinding(let valueBinding): renderValueBinding(valueBinding)
  449. case .unaryKeyword(let unaryKeyword): renderUnaryKeywordExpression(unaryKeyword)
  450. case .closureInvocation(let closureInvocation): renderClosureInvocation(closureInvocation)
  451. case .binaryOperation(let binaryOperation): renderBinaryOperation(binaryOperation)
  452. case .inOut(let inOut): renderInOutDescription(inOut)
  453. case .optionalChaining(let optionalChaining):
  454. renderOptionalChainingDescription(optionalChaining)
  455. case .tuple(let tuple): renderTupleDescription(tuple)
  456. }
  457. }
  458. /// Renders the specified literal expression.
  459. func renderLiteral(_ literal: LiteralDescription) {
  460. func write(_ string: String) { writer.writeLine(string) }
  461. switch literal {
  462. case let .string(string):
  463. // Use a raw literal if the string contains a quote/backslash.
  464. if string.contains("\"") || string.contains("\\") {
  465. write("#\"\(string)\"#")
  466. } else {
  467. write("\"\(string)\"")
  468. }
  469. case let .int(int): write("\(int)")
  470. case let .bool(bool): write(bool ? "true" : "false")
  471. case .nil: write("nil")
  472. case .array(let items):
  473. writer.writeLine("[")
  474. if !items.isEmpty {
  475. writer.withNestedLevel {
  476. for (item, isLast) in items.enumeratedWithLastMarker() {
  477. renderExpression(item)
  478. if !isLast {
  479. writer.nextLineAppendsToLastLine()
  480. writer.writeLine(",")
  481. }
  482. }
  483. }
  484. } else {
  485. writer.nextLineAppendsToLastLine()
  486. }
  487. writer.writeLine("]")
  488. case .dictionary(let items):
  489. writer.writeLine("[")
  490. if items.isEmpty {
  491. writer.nextLineAppendsToLastLine()
  492. writer.writeLine(":")
  493. writer.nextLineAppendsToLastLine()
  494. } else {
  495. writer.withNestedLevel {
  496. for (item, isLast) in items.enumeratedWithLastMarker() {
  497. renderExpression(item.key)
  498. writer.nextLineAppendsToLastLine()
  499. writer.writeLine(": ")
  500. writer.nextLineAppendsToLastLine()
  501. renderExpression(item.value)
  502. if !isLast {
  503. writer.nextLineAppendsToLastLine()
  504. writer.writeLine(",")
  505. }
  506. }
  507. }
  508. }
  509. writer.writeLine("]")
  510. }
  511. }
  512. /// Renders the specified where clause requirement.
  513. func renderedWhereClauseRequirement(_ requirement: WhereClauseRequirement) -> String {
  514. switch requirement {
  515. case .conformance(let left, let right): return "\(left): \(right)"
  516. }
  517. }
  518. /// Renders the specified where clause.
  519. func renderedWhereClause(_ clause: WhereClause) -> String {
  520. let renderedRequirements = clause.requirements.map(renderedWhereClauseRequirement)
  521. return "where \(renderedRequirements.joined(separator: ", "))"
  522. }
  523. /// Renders the specified extension declaration.
  524. func renderExtension(_ extensionDescription: ExtensionDescription) {
  525. if let accessModifier = extensionDescription.accessModifier {
  526. writer.writeLine(renderedAccessModifier(accessModifier) + " ")
  527. writer.nextLineAppendsToLastLine()
  528. }
  529. writer.writeLine("extension \(extensionDescription.onType)")
  530. writer.nextLineAppendsToLastLine()
  531. if !extensionDescription.conformances.isEmpty {
  532. writer.writeLine(": \(extensionDescription.conformances.joined(separator: ", "))")
  533. writer.nextLineAppendsToLastLine()
  534. }
  535. if let whereClause = extensionDescription.whereClause {
  536. writer.writeLine(" " + renderedWhereClause(whereClause))
  537. writer.nextLineAppendsToLastLine()
  538. }
  539. writer.writeLine(" {")
  540. for (declaration, isLast) in extensionDescription.declarations.enumeratedWithLastMarker() {
  541. writer.withNestedLevel {
  542. renderDeclaration(declaration)
  543. if !isLast {
  544. writer.writeLine("")
  545. }
  546. }
  547. }
  548. writer.writeLine("}")
  549. }
  550. /// Renders the specified type reference to an existing type.
  551. func renderExistingTypeDescription(_ type: ExistingTypeDescription) {
  552. switch type {
  553. case .any(let existingTypeDescription):
  554. writer.writeLine("any ")
  555. writer.nextLineAppendsToLastLine()
  556. renderExistingTypeDescription(existingTypeDescription)
  557. case .generic(let wrapper, let wrapped):
  558. renderExistingTypeDescription(wrapper)
  559. writer.nextLineAppendsToLastLine()
  560. writer.writeLine("<")
  561. writer.nextLineAppendsToLastLine()
  562. for (wrap, isLast) in wrapped.enumeratedWithLastMarker() {
  563. renderExistingTypeDescription(wrap)
  564. writer.nextLineAppendsToLastLine()
  565. if !isLast {
  566. writer.writeLine(", ")
  567. writer.nextLineAppendsToLastLine()
  568. }
  569. }
  570. writer.nextLineAppendsToLastLine()
  571. writer.writeLine(">")
  572. case .optional(let existingTypeDescription):
  573. renderExistingTypeDescription(existingTypeDescription)
  574. writer.nextLineAppendsToLastLine()
  575. writer.writeLine("?")
  576. case .member(let components):
  577. writer.writeLine(components.joined(separator: "."))
  578. case .array(let existingTypeDescription):
  579. writer.writeLine("[")
  580. writer.nextLineAppendsToLastLine()
  581. renderExistingTypeDescription(existingTypeDescription)
  582. writer.nextLineAppendsToLastLine()
  583. writer.writeLine("]")
  584. case .dictionaryValue(let existingTypeDescription):
  585. writer.writeLine("[String: ")
  586. writer.nextLineAppendsToLastLine()
  587. renderExistingTypeDescription(existingTypeDescription)
  588. writer.nextLineAppendsToLastLine()
  589. writer.writeLine("]")
  590. case .some(let existingTypeDescription):
  591. writer.writeLine("some ")
  592. writer.nextLineAppendsToLastLine()
  593. renderExistingTypeDescription(existingTypeDescription)
  594. case .closure(let closureSignatureDescription):
  595. renderClosureSignature(closureSignatureDescription)
  596. }
  597. }
  598. /// Renders the specified typealias declaration.
  599. func renderTypealias(_ alias: TypealiasDescription) {
  600. var words: [String] = []
  601. if let accessModifier = alias.accessModifier {
  602. words.append(renderedAccessModifier(accessModifier))
  603. }
  604. words.append(contentsOf: [
  605. "typealias", alias.name, "=",
  606. ])
  607. writer.writeLine(words.joinedWords() + " ")
  608. writer.nextLineAppendsToLastLine()
  609. renderExistingTypeDescription(alias.existingType)
  610. }
  611. /// Renders the specified binding kind.
  612. func renderedBindingKind(_ kind: BindingKind) -> String {
  613. switch kind {
  614. case .var: return "var"
  615. case .let: return "let"
  616. }
  617. }
  618. /// Renders the specified variable declaration.
  619. func renderVariable(_ variable: VariableDescription) {
  620. do {
  621. if let accessModifier = variable.accessModifier {
  622. writer.writeLine(renderedAccessModifier(accessModifier) + " ")
  623. writer.nextLineAppendsToLastLine()
  624. }
  625. if variable.isStatic {
  626. writer.writeLine("static ")
  627. writer.nextLineAppendsToLastLine()
  628. }
  629. writer.writeLine(renderedBindingKind(variable.kind) + " ")
  630. writer.nextLineAppendsToLastLine()
  631. renderExpression(variable.left)
  632. if let type = variable.type {
  633. writer.nextLineAppendsToLastLine()
  634. writer.writeLine(": ")
  635. writer.nextLineAppendsToLastLine()
  636. renderExistingTypeDescription(type)
  637. }
  638. }
  639. if let right = variable.right {
  640. writer.nextLineAppendsToLastLine()
  641. writer.writeLine(" = ")
  642. writer.nextLineAppendsToLastLine()
  643. renderExpression(right)
  644. }
  645. if let body = variable.getter {
  646. writer.nextLineAppendsToLastLine()
  647. writer.writeLine(" {")
  648. writer.withNestedLevel {
  649. let hasExplicitGetter =
  650. !variable.getterEffects.isEmpty || variable.setter != nil || variable.modify != nil
  651. if hasExplicitGetter {
  652. let keywords = variable.getterEffects.map(renderedFunctionKeyword).joined(separator: " ")
  653. let line = "get \(keywords) {"
  654. writer.writeLine(line)
  655. writer.push()
  656. }
  657. renderCodeBlocks(body)
  658. if hasExplicitGetter {
  659. writer.pop()
  660. writer.writeLine("}")
  661. }
  662. if let modify = variable.modify {
  663. writer.writeLine("_modify {")
  664. writer.withNestedLevel { renderCodeBlocks(modify) }
  665. writer.writeLine("}")
  666. }
  667. if let setter = variable.setter {
  668. writer.writeLine("set {")
  669. writer.withNestedLevel { renderCodeBlocks(setter) }
  670. writer.writeLine("}")
  671. }
  672. }
  673. writer.writeLine("}")
  674. }
  675. }
  676. /// Renders the specified struct declaration.
  677. func renderStruct(_ structDesc: StructDescription) {
  678. if let accessModifier = structDesc.accessModifier {
  679. writer.writeLine(renderedAccessModifier(accessModifier) + " ")
  680. writer.nextLineAppendsToLastLine()
  681. }
  682. writer.writeLine("struct \(structDesc.name)")
  683. writer.nextLineAppendsToLastLine()
  684. let generics = structDesc.generics
  685. if !generics.isEmpty {
  686. writer.nextLineAppendsToLastLine()
  687. writer.writeLine("<")
  688. for (genericType, isLast) in generics.enumeratedWithLastMarker() {
  689. writer.nextLineAppendsToLastLine()
  690. renderExistingTypeDescription(genericType)
  691. if !isLast {
  692. writer.nextLineAppendsToLastLine()
  693. writer.writeLine(", ")
  694. }
  695. }
  696. writer.nextLineAppendsToLastLine()
  697. writer.writeLine(">")
  698. writer.nextLineAppendsToLastLine()
  699. }
  700. if !structDesc.conformances.isEmpty {
  701. writer.writeLine(": \(structDesc.conformances.joined(separator: ", "))")
  702. writer.nextLineAppendsToLastLine()
  703. }
  704. if let whereClause = structDesc.whereClause {
  705. writer.nextLineAppendsToLastLine()
  706. writer.writeLine(" " + renderedWhereClause(whereClause))
  707. writer.nextLineAppendsToLastLine()
  708. }
  709. writer.writeLine(" {")
  710. if !structDesc.members.isEmpty {
  711. writer.withNestedLevel {
  712. for (member, isLast) in structDesc.members.enumeratedWithLastMarker() {
  713. renderDeclaration(member)
  714. if !isLast {
  715. writer.writeLine("")
  716. }
  717. }
  718. }
  719. } else {
  720. writer.nextLineAppendsToLastLine()
  721. }
  722. writer.writeLine("}")
  723. }
  724. /// Renders the specified protocol declaration.
  725. func renderProtocol(_ protocolDesc: ProtocolDescription) {
  726. if let accessModifier = protocolDesc.accessModifier {
  727. writer.writeLine("\(renderedAccessModifier(accessModifier)) ")
  728. writer.nextLineAppendsToLastLine()
  729. }
  730. writer.writeLine("protocol \(protocolDesc.name)")
  731. writer.nextLineAppendsToLastLine()
  732. if !protocolDesc.conformances.isEmpty {
  733. let conformances = protocolDesc.conformances.joined(separator: ", ")
  734. writer.writeLine(": \(conformances)")
  735. writer.nextLineAppendsToLastLine()
  736. }
  737. writer.writeLine(" {")
  738. if !protocolDesc.members.isEmpty {
  739. writer.withNestedLevel {
  740. for (member, isLast) in protocolDesc.members.enumeratedWithLastMarker() {
  741. renderDeclaration(member)
  742. if !isLast {
  743. writer.writeLine("")
  744. }
  745. }
  746. }
  747. } else {
  748. writer.nextLineAppendsToLastLine()
  749. }
  750. writer.writeLine("}")
  751. }
  752. /// Renders the specified enum declaration.
  753. func renderEnum(_ enumDesc: EnumDescription) {
  754. if enumDesc.isFrozen {
  755. writer.writeLine("@frozen ")
  756. writer.nextLineAppendsToLastLine()
  757. }
  758. if let accessModifier = enumDesc.accessModifier {
  759. writer.writeLine("\(renderedAccessModifier(accessModifier)) ")
  760. writer.nextLineAppendsToLastLine()
  761. }
  762. if enumDesc.isIndirect {
  763. writer.writeLine("indirect ")
  764. writer.nextLineAppendsToLastLine()
  765. }
  766. writer.writeLine("enum \(enumDesc.name)")
  767. writer.nextLineAppendsToLastLine()
  768. if !enumDesc.conformances.isEmpty {
  769. writer.writeLine(": \(enumDesc.conformances.joined(separator: ", "))")
  770. writer.nextLineAppendsToLastLine()
  771. }
  772. writer.writeLine(" {")
  773. if !enumDesc.members.isEmpty {
  774. writer.withNestedLevel { for member in enumDesc.members { renderDeclaration(member) } }
  775. } else {
  776. writer.nextLineAppendsToLastLine()
  777. }
  778. writer.writeLine("}")
  779. }
  780. /// Renders the specified enum case associated value.
  781. func renderEnumCaseAssociatedValue(_ value: EnumCaseAssociatedValueDescription) {
  782. var words: [String] = []
  783. if let label = value.label { words.append(label + ":") }
  784. writer.writeLine(words.joinedWords())
  785. writer.nextLineAppendsToLastLine()
  786. renderExistingTypeDescription(value.type)
  787. }
  788. /// Renders the specified enum case declaration.
  789. func renderEnumCase(_ enumCase: EnumCaseDescription) {
  790. writer.writeLine("case \(enumCase.name)")
  791. switch enumCase.kind {
  792. case .nameOnly: break
  793. case .nameWithRawValue(let rawValue):
  794. writer.nextLineAppendsToLastLine()
  795. writer.writeLine(" = ")
  796. writer.nextLineAppendsToLastLine()
  797. renderLiteral(rawValue)
  798. case .nameWithAssociatedValues(let values):
  799. if values.isEmpty { break }
  800. for (value, isLast) in values.enumeratedWithLastMarker() {
  801. renderEnumCaseAssociatedValue(value)
  802. if !isLast {
  803. writer.nextLineAppendsToLastLine()
  804. writer.writeLine(", ")
  805. }
  806. }
  807. }
  808. }
  809. /// Renders the specified declaration.
  810. func renderDeclaration(_ declaration: Declaration) {
  811. switch declaration {
  812. case let .commentable(comment, nestedDeclaration):
  813. renderCommentableDeclaration(comment: comment, declaration: nestedDeclaration)
  814. case let .deprecated(deprecation, nestedDeclaration):
  815. renderDeprecatedDeclaration(deprecation: deprecation, declaration: nestedDeclaration)
  816. case let .guarded(availability, nestedDeclaration):
  817. renderGuardedDeclaration(availability: availability, declaration: nestedDeclaration)
  818. case .variable(let variableDescription): renderVariable(variableDescription)
  819. case .extension(let extensionDescription): renderExtension(extensionDescription)
  820. case .struct(let structDescription): renderStruct(structDescription)
  821. case .protocol(let protocolDescription): renderProtocol(protocolDescription)
  822. case .enum(let enumDescription): renderEnum(enumDescription)
  823. case .typealias(let typealiasDescription): renderTypealias(typealiasDescription)
  824. case .function(let functionDescription): renderFunction(functionDescription)
  825. case .enumCase(let enumCase): renderEnumCase(enumCase)
  826. }
  827. }
  828. /// Renders the specified function kind.
  829. func renderedFunctionKind(_ functionKind: FunctionKind) -> String {
  830. switch functionKind {
  831. case .initializer(let isFailable): return "init\(isFailable ? "?" : "")"
  832. case .function(let name, let isStatic):
  833. return (isStatic ? "static " : "") + "func \(name)"
  834. }
  835. }
  836. /// Renders the specified function keyword.
  837. func renderedFunctionKeyword(_ keyword: FunctionKeyword) -> String {
  838. switch keyword {
  839. case .throws: return "throws"
  840. case .async: return "async"
  841. case .rethrows: return "rethrows"
  842. }
  843. }
  844. /// Renders the specified function signature.
  845. func renderClosureSignature(_ signature: ClosureSignatureDescription) {
  846. if signature.sendable {
  847. writer.writeLine("@Sendable ")
  848. writer.nextLineAppendsToLastLine()
  849. }
  850. if signature.escaping {
  851. writer.writeLine("@escaping ")
  852. writer.nextLineAppendsToLastLine()
  853. }
  854. writer.writeLine("(")
  855. let parameters = signature.parameters
  856. let separateLines = parameters.count > 1
  857. if separateLines {
  858. writer.withNestedLevel {
  859. for (parameter, isLast) in signature.parameters.enumeratedWithLastMarker() {
  860. renderClosureParameter(parameter)
  861. if !isLast {
  862. writer.nextLineAppendsToLastLine()
  863. writer.writeLine(",")
  864. }
  865. }
  866. }
  867. } else {
  868. writer.nextLineAppendsToLastLine()
  869. if let parameter = parameters.first {
  870. renderClosureParameter(parameter)
  871. writer.nextLineAppendsToLastLine()
  872. }
  873. }
  874. writer.writeLine(")")
  875. let keywords = signature.keywords
  876. for keyword in keywords {
  877. writer.nextLineAppendsToLastLine()
  878. writer.writeLine(" " + renderedFunctionKeyword(keyword))
  879. }
  880. if let returnType = signature.returnType {
  881. writer.nextLineAppendsToLastLine()
  882. writer.writeLine(" -> ")
  883. writer.nextLineAppendsToLastLine()
  884. renderExpression(returnType)
  885. }
  886. }
  887. /// Renders the specified function signature.
  888. func renderFunctionSignature(_ signature: FunctionSignatureDescription) {
  889. do {
  890. if let accessModifier = signature.accessModifier {
  891. writer.writeLine(renderedAccessModifier(accessModifier) + " ")
  892. writer.nextLineAppendsToLastLine()
  893. }
  894. let generics = signature.generics
  895. writer.writeLine(
  896. renderedFunctionKind(signature.kind)
  897. )
  898. if !generics.isEmpty {
  899. writer.nextLineAppendsToLastLine()
  900. writer.writeLine("<")
  901. for (genericType, isLast) in generics.enumeratedWithLastMarker() {
  902. writer.nextLineAppendsToLastLine()
  903. renderExistingTypeDescription(genericType)
  904. if !isLast {
  905. writer.nextLineAppendsToLastLine()
  906. writer.writeLine(", ")
  907. }
  908. }
  909. writer.nextLineAppendsToLastLine()
  910. writer.writeLine(">")
  911. }
  912. writer.nextLineAppendsToLastLine()
  913. writer.writeLine("(")
  914. let parameters = signature.parameters
  915. let separateLines = parameters.count > 1
  916. if separateLines {
  917. writer.withNestedLevel {
  918. for (parameter, isLast) in signature.parameters.enumeratedWithLastMarker() {
  919. renderParameter(parameter)
  920. if !isLast {
  921. writer.nextLineAppendsToLastLine()
  922. writer.writeLine(",")
  923. }
  924. }
  925. }
  926. } else {
  927. writer.nextLineAppendsToLastLine()
  928. if let parameter = parameters.first { renderParameter(parameter) }
  929. writer.nextLineAppendsToLastLine()
  930. }
  931. writer.writeLine(")")
  932. }
  933. do {
  934. let keywords = signature.keywords
  935. if !keywords.isEmpty {
  936. for keyword in keywords {
  937. writer.nextLineAppendsToLastLine()
  938. writer.writeLine(" " + renderedFunctionKeyword(keyword))
  939. }
  940. }
  941. }
  942. if let returnType = signature.returnType {
  943. writer.nextLineAppendsToLastLine()
  944. writer.writeLine(" -> ")
  945. writer.nextLineAppendsToLastLine()
  946. renderExpression(returnType)
  947. }
  948. if let whereClause = signature.whereClause {
  949. writer.nextLineAppendsToLastLine()
  950. writer.writeLine(" " + renderedWhereClause(whereClause))
  951. }
  952. }
  953. /// Renders the specified function declaration.
  954. func renderFunction(_ functionDescription: FunctionDescription) {
  955. renderFunctionSignature(functionDescription.signature)
  956. guard let body = functionDescription.body else { return }
  957. writer.nextLineAppendsToLastLine()
  958. writer.writeLine(" {")
  959. if !body.isEmpty {
  960. writer.withNestedLevel { renderCodeBlocks(body) }
  961. } else {
  962. writer.nextLineAppendsToLastLine()
  963. }
  964. writer.writeLine("}")
  965. }
  966. /// Renders the specified parameter declaration.
  967. func renderParameter(_ parameterDescription: ParameterDescription) {
  968. if let label = parameterDescription.label {
  969. writer.writeLine(label)
  970. } else {
  971. writer.writeLine("_")
  972. }
  973. writer.nextLineAppendsToLastLine()
  974. if let name = parameterDescription.name, name != parameterDescription.label {
  975. // If the label and name are the same value, don't repeat it.
  976. writer.writeLine(" ")
  977. writer.nextLineAppendsToLastLine()
  978. writer.writeLine(name)
  979. writer.nextLineAppendsToLastLine()
  980. }
  981. writer.writeLine(": ")
  982. writer.nextLineAppendsToLastLine()
  983. if parameterDescription.inout {
  984. writer.writeLine("inout ")
  985. writer.nextLineAppendsToLastLine()
  986. }
  987. if let type = parameterDescription.type {
  988. renderExistingTypeDescription(type)
  989. }
  990. if let defaultValue = parameterDescription.defaultValue {
  991. writer.nextLineAppendsToLastLine()
  992. writer.writeLine(" = ")
  993. writer.nextLineAppendsToLastLine()
  994. renderExpression(defaultValue)
  995. }
  996. }
  997. /// Renders the specified parameter declaration for a closure.
  998. func renderClosureParameter(_ parameterDescription: ParameterDescription) {
  999. let name = parameterDescription.name
  1000. let label: String
  1001. if let declaredLabel = parameterDescription.label {
  1002. label = declaredLabel
  1003. } else {
  1004. label = "_"
  1005. }
  1006. if let name = name {
  1007. writer.writeLine(label)
  1008. if name != parameterDescription.label {
  1009. // If the label and name are the same value, don't repeat it.
  1010. writer.writeLine(" ")
  1011. writer.nextLineAppendsToLastLine()
  1012. writer.writeLine(name)
  1013. writer.nextLineAppendsToLastLine()
  1014. }
  1015. }
  1016. if parameterDescription.inout {
  1017. writer.writeLine("inout ")
  1018. writer.nextLineAppendsToLastLine()
  1019. }
  1020. if let type = parameterDescription.type {
  1021. renderExistingTypeDescription(type)
  1022. }
  1023. if let defaultValue = parameterDescription.defaultValue {
  1024. writer.nextLineAppendsToLastLine()
  1025. writer.writeLine(" = ")
  1026. writer.nextLineAppendsToLastLine()
  1027. renderExpression(defaultValue)
  1028. }
  1029. }
  1030. /// Renders the specified declaration with a comment.
  1031. func renderCommentableDeclaration(comment: Comment?, declaration: Declaration) {
  1032. if let comment { renderComment(comment) }
  1033. renderDeclaration(declaration)
  1034. }
  1035. /// Renders the specified declaration with a deprecation annotation.
  1036. func renderDeprecatedDeclaration(deprecation: DeprecationDescription, declaration: Declaration) {
  1037. renderDeprecation(deprecation)
  1038. renderDeclaration(declaration)
  1039. }
  1040. func renderDeprecation(_ deprecation: DeprecationDescription) {
  1041. let things: [String] = [
  1042. "*", "deprecated", deprecation.message.map { "message: \"\($0)\"" },
  1043. deprecation.renamed.map { "renamed: \"\($0)\"" },
  1044. ]
  1045. .compactMap({ $0 })
  1046. let line = "@available(\(things.joined(separator: ", ")))"
  1047. writer.writeLine(line)
  1048. }
  1049. /// Renders the specified declaration with an availability guard annotation.
  1050. func renderGuardedDeclaration(availability: AvailabilityDescription, declaration: Declaration) {
  1051. renderAvailability(availability)
  1052. renderDeclaration(declaration)
  1053. }
  1054. func renderAvailability(_ availability: AvailabilityDescription) {
  1055. var line = "@available("
  1056. for osVersion in availability.osVersions {
  1057. line.append("\(osVersion.os.name) \(osVersion.version), ")
  1058. }
  1059. line.append("*)")
  1060. writer.writeLine(line)
  1061. }
  1062. /// Renders the specified code block item.
  1063. func renderCodeBlockItem(_ description: CodeBlockItem) {
  1064. switch description {
  1065. case .declaration(let declaration): renderDeclaration(declaration)
  1066. case .expression(let expression): renderExpression(expression)
  1067. }
  1068. }
  1069. /// Renders the specified code block.
  1070. func renderCodeBlock(_ description: CodeBlock) {
  1071. if let comment = description.comment { renderComment(comment) }
  1072. if let item = description.item {
  1073. renderCodeBlockItem(item)
  1074. }
  1075. }
  1076. /// Renders the specified code blocks.
  1077. func renderCodeBlocks(_ blocks: [CodeBlock]) { blocks.forEach(renderCodeBlock) }
  1078. }
  1079. extension Array {
  1080. /// Returns a collection of tuples, where the first element is
  1081. /// the collection element and the second is a Boolean value indicating
  1082. /// whether it is the last element in the collection.
  1083. /// - Returns: A collection of tuples.
  1084. fileprivate func enumeratedWithLastMarker() -> [(Element, isLast: Bool)] {
  1085. let count = count
  1086. return enumerated().map { index, element in (element, index == count - 1) }
  1087. }
  1088. }
  1089. extension Array where Element == String {
  1090. /// Returns a string where the elements of the array are joined
  1091. /// by a space character.
  1092. /// - Returns: A string with the elements of the array joined by space characters.
  1093. fileprivate func joinedWords() -> String { joined(separator: " ") }
  1094. }
  1095. extension String {
  1096. /// Returns an array of strings, where each string represents one line
  1097. /// in the current string.
  1098. /// - Returns: An array of strings, each representing one line in the original string.
  1099. fileprivate func asLines() -> [String] {
  1100. split(omittingEmptySubsequences: false, whereSeparator: \.isNewline).map(String.init)
  1101. }
  1102. /// Returns a new string where the provided closure transforms each line.
  1103. /// The closure takes a string representing one line as a parameter.
  1104. /// - Parameter work: The closure that transforms each line.
  1105. /// - Returns: A new string where each line has been transformed using the given closure.
  1106. fileprivate func transformingLines(_ work: (String, Bool) -> String?) -> [String] {
  1107. asLines().enumeratedWithLastMarker().compactMap(work)
  1108. }
  1109. }
  1110. @available(gRPCSwift 2.0, *)
  1111. extension TextBasedRenderer {
  1112. /// Returns the provided expression rendered as a string.
  1113. /// - Parameter expression: The expression.
  1114. /// - Returns: The string representation of the expression.
  1115. static func renderedExpressionAsString(_ expression: Expression) -> String {
  1116. let renderer = TextBasedRenderer.default
  1117. renderer.renderExpression(expression)
  1118. return renderer.renderedContents()
  1119. }
  1120. }