# Use SwiftSyntax itself to generate SwiftSyntax’s source code

**URL:** <https://forums.swift.org/t/use-swiftsyntax-itself-to-generate-swiftsyntax-s-source-code/56599>\
**Category:** Source Tooling\
**Tags:** swift-syntax, gsoc-2022\
**Created:** [April 7, 2022, 6:14am UTC](https://forums.swift.org/t/use-swiftsyntax-itself-to-generate-swiftsyntax-s-source-code/56599 "2022-04-07T06:14:49Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![ahoppen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahoppen/32/36046_2.png) [@ahoppen](https://forums.swift.org/u/ahoppen)\
**Post date:** [April 19, 2022, 9:36am UTC](https://forums.swift.org/t/use-swiftsyntax-itself-to-generate-swiftsyntax-s-source-code/56599/4 "2022-04-19T09:36:39Z")

</div>

> [@vrjuliao](#):
>
> There is no way to generate files like the gyb ones. I mean, I could not find any way to format a file, and also comment it. So, maybe, we could provide ways to do that. I can see that there is an auto-indentation tool, but we cannot generate a file like `SwiftSyntaxBuilder/gyb_generated/Tokens.swift` since it splits the chain of member calls `SyntaxFactory.makeSpacedBinaryOperator(text).withLeadingTrivia(.spaces(1)).withTrailingTrivia(.spaces(1))` in multiple lines. I propose to add a `TrailingNewLine` attribute in some calls and declaraions in order to improve that formatting. For example: in a Chain of member calls, in an ArgumentList, or ArrayDecl for example.

I wouldn’t worry about formatting too much at this point. Formatting is a non-trivial problem to solve and I would suggest running a formatter like [swift-format](https://github.com/apple/swift-format) on the code generated by SwiftSyntaxBuilder but that would be a follow-up step after generating the actual code.

> [@vrjuliao](#):
>
> Furthermore, we don't have an way to provide comments (neither blocks or inline).

Yes, that’s a known limitation at the moment. @iMostfa has some ideas how to fix this in [[GSoC 2022] Use SwiftSyntax itself to generate SwiftSyntax’s source code instead of GYB - #9 by iMostfa](https://forums.swift.org/t/gsoc-2022-use-swiftsyntax-itself-to-generate-swiftsyntax-s-source-code-instead-of-gyb/56631/9)

> [@vrjuliao](#):
>
> To create an expression like:
> 
> ```swift
> static var `enum`: TokenSyntax{ ... }
> 
> ```
> 
> We have to nest: VariableDecl -\> PatternBinding.accessor -\> CodeBlock. Actually, I'm not sure if the expression inside the braces is an accessor (in the Tokens.swift file). It seems like an initialization. So, perhaps we could improve the InitializationClause in order to comply with it.

The code is an accessor and not an initializer because every time you access the `enum` variable, the code in the braces gets executed to generate `TokenSyntax` representing the `enum` keyword. You can see this behavior if you e.g. put a `print` statement in the accessor. An initializer would be `static var `enum`: TokenSyntax = SyntaxFactory.makeEnumKeyword().withTrailingTrivia(.spaces(1)) `

I agree that the syntax to generate variables is a little verbose at the moment. IMHO adding convenience initializers like the one you added in `Tokens.gen.swift` is the way forward here.

---

_[View the full topic](https://forums.swift.org/t/use-swiftsyntax-itself-to-generate-swiftsyntax-s-source-code/56599)._
