Tuesday, August 23, 2022

Implementing the Swift Parser in Swift

Mattt Thompson:

SwiftSyntax is a Swift library that lets you parse, analyze, generate, and transform Swift source code. It’s based on the libSyntax library, and was spun out from the main Swift language repository in August 2017.

Douglas Gregor:

We have started a project to reimplement the Swift parser in Swift, to become part of the SwiftSyntax library. The current implementation can parse much of the Swift grammar already.

[…]

The main goal of this project is to fully replace the C++ implementation of the Swift parser for all clients.

[…]

The parser will attempt to recover from syntax errors, maintaining as much of the program structure as is feasible. It has no side effects, and in particular produces no errors regardless of how ill-formed the input source text is. Instead, all errors are described in the syntax tree itself, and can be diagnosed by a separate pass that identifies such errors.

[…]

SwiftSyntax is designed to maintain all “trivia” (including whitespace, comments, etc.) precisely as it occurs in the source text, so that a syntax tree can be rendered back into text that is byte-for-byte identical to the original source.

See also: Swift Abstract Syntax Tree (via Helge Heß).

Previously:

Comments RSS · Twitter

Leave a Comment