gitmyhub

swift-syntax

Swift ★ 3.7k updated 4d ago

A set of Swift libraries for parsing, inspecting, generating, and transforming Swift source code.

Swift Syntax is a set of libraries, maintained by the Swift project itself, for reading and working with Swift source code as structured data. Instead of treating a Swift file as plain text, these libraries parse it into a tree of nodes where every piece of syntax, every keyword, operator, and expression, has a named place in the structure. This kind of representation is called an abstract syntax tree, and it lets programs reason about code rather than just store it.

The primary use of this library is as the foundation for Swift's macro system. When you write a Swift macro, the macro expansion mechanism uses SwiftSyntax nodes to represent what the macro receives as input and what it produces as output. The library also powers tools that analyze, reformat, or automatically rewrite Swift source files.

For developers who want to explore what the tree looks like for a given piece of code, the README points to an interactive web tool at swift-ast-explorer.com where you can paste Swift code and see the parsed tree structure in real time.

The versioning scheme ties each release to the corresponding Swift language version. For example, the major version 509 of the package corresponds to Swift 5.9. To add the library to a Swift project managed by Swift Package Manager, you declare it as a dependency in the package manifest file. It can also be added through Xcode's package dependency interface. An experimental Bazel build configuration is available for teams using the Bazel build system.

The library is developed by the open-source Swift community. Documentation is hosted on Swift Package Index and the repository welcomes contributions via the contributing guide.