Home > Software engineering >  Is there a standard Swift AST like there is for JavaScript?
Is there a standard Swift AST like there is for JavaScript?

Time:11-23

In JavaScript we have estree which is the AST definition evolving from Mozilla's implementation. But nowadays if you build an AST transformer in JS of a JS AST, you probably use this structure. Do we have anything like this for Swift, of the Swift AST?

I see we have a grammar, but what about an AST? I guess I can make one from that, but still.

If nothing standard, do we have any AST examples?

CodePudding user response:

The Swift project offers SwiftSyntax as a package for working with Swift source code, in Swift. Under the hood, it's powered by the compiler's own libSyntax, written in C .

Note that this currently isn't the representation that the Swift compiler proper uses for actually compiling Swift code — libSyntax focuses on source code itself for rewriting, formatting, transformations, etc., but is largely void of the semantic information that you may find in a compiler AST necessary for transforming the source into machine code. If you're just looking to operate on the AST without those semantics, this may be sufficient for your use case.

The repo README should have some info to get you started, an example, and some real-world use cases showing concrete usage of the library.

  • Related