swift-syntax

repository·main·Indexed 25 days ago

https://github.com/swiftlang/swift-syntax

A library for working with a source-accurate tree representation of Swift code, serving as the core infrastructure for the Swift macro system. It includes SwiftParser for parsing source code into a SourceFileSyntax tree, SwiftSyntaxBuilder for programmatic code generation, and tools for creating Xcode Source Editor Extensions via the SwiftRefactor library.

Tokens
19.8K
Snippets
37
Records
156
Agent score
86%

What's inside swift-syntax

  1. Overview of Swift Syntax

    main
    The swift-syntax package provides libraries for working with a source-accurate tree representation of Swift source code, known as the SwiftSyntax tree. This tree is the foundation of the Swift macro system: macro expansion nodes are represented as SwiftSyntax nodes, and macros generate SwiftSyntax trees to be inserted into source files.
  2. Key design principles of SwiftParser

    main

    SwiftParser follows several core design principles:

    • Resilient: Recovers from syntax errors by using unexpected nodes and missing tokens instead of failing.
    • Efficient: Aiming for performance comparable to the C++ parser implementation.
    • Source-preserving: Maintains all "trivia" (whitespace, comments, etc.) so that the syntax tree can be rendered back into byte-for-byte identical source text.
    • Minimal context: Requires only minimal context (such as dialect support for regex literals) and can be invoked on any major grammar production (e.g., a full file, a type, or an expression).
    • Incremental: Supports incremental updates to a parse tree for a new version of a source file, reusing existing nodes to reduce memory and computation overhead.
  3. Understand Swift Parser Recovery mechanisms

    main

    The Swift parser uses recovery mechanisms to handle malformed or partial Swift code, aiming to produce as much syntax tree structure as possible. This is particularly useful for tools like formatters and editors that process incomplete input. The parser focuses on two primary error classes:

    1. Unexpected Syntax: When the parser encounters a token it didn't expect, it uses a token precedence model (TokenPrecedence) to look ahead for the expected token. It attempts to skip the minimum number of tokens required to resume valid parsing.
    2. Missing Syntax: When lookahead fails or is impossible, the parser synthesizes 'missing' elements to maintain tree structure. These elements have no textual content but allow the parser to continue and provide clear signals for diagnostic tools.
  4. Evaluate #if conditionals with SwiftIfConfig

    main
    The SwiftIfConfig library allows you to determine which parts of a Swift syntax tree are active under a specific build configuration. Because the standard swift-syntax parser does not reason about build configurations, it includes all branches of #if statements as IfConfigDeclSyntax nodes. SwiftIfConfig provides utilities to filter these nodes based on a type conforming to the BuildConfiguration protocol.
  5. Understand SwiftSyntax core tenets

    main

    SwiftSyntax is designed around three core principles:

    1. Immutability: The syntax tree is a persistent data structure. Once created, it cannot be changed. Modifications return a new tree that shares as much structure as possible with the original to minimize memory overhead. This makes the tree safe for concurrent use across threads without locks.
    2. Source Fidelity: The library preserves every byte of the source text, including whitespace, comments, compiler directives, and the Unicode byte order mark (BOM). This is referred to as 'trivia'.
    3. Resilience: The tree can represent both well-formed and ill-formed code. It handles missing syntax (represented by tokens with SourcePresence/missing) and unexpected syntax (superfluous text), allowing tools like IDEs and linters to recover gracefully during parsing.
  6. Understand the relationship between Swift Grammar and SwiftSyntax

    main

    The Swift parser uses a recursive descent approach to transform Swift source code into a syntax tree.

    • Grammar Productions: Rules that define valid Swift syntax (e.g., optional-type → type '?').
    • SwiftSyntax: A concrete syntax tree (CST) that faithfully represents the source text. Each grammar production corresponds to a specific syntax node type (e.g., RawOptionalTypeSyntax).
    • Syntax Nodes: These nodes contain properties to access child nodes (e.g., wrappedType) and associated tokens (e.g., questionMark).
    • Syntax Collections: For sequences of elements (like a list of class members), SwiftSyntax provides SyntaxCollection types.
  7. Understand SwiftParser error resilience and recovery

    main

    The SwiftParser is designed to be resilient, meaning it attempts to recover from syntax errors to maintain program structure without producing side effects or failing. Errors are embedded directly in the syntax tree in two forms:

    • Unexpected nodes: Syntax that does not match the Swift grammar is stored in "unexpected" child nodes that can be queried.
    • Missing tokens: Required syntax that is absent in the source (e.g., a missing closing parenthesis )) is inserted into the tree as "missing" tokens. These tokens are skipped when rendering back to source text but can be used by tools to suggest or apply fixes.
  8. Generate Swift code using SwiftSyntaxBuilder

    main
    For developers who want to generate Swift code programmatically using Swift itself, use the SwiftSyntaxBuilder library located in ../Sources/SwiftSyntaxBuilder. This library is used by the CodeGeneration package to produce parts of the SwiftSyntax source code.
  9. Understand Parser Resilience and Recovery

    main

    Resilience in SwiftParser refers to the ability to accept ill-formed inputs and still produce a well-formed, structured output.

    • Structural Well-formedness: The goal is to output as much syntactic structure as possible so clients can still interpret parts of the code.
    • Recovery Mechanisms: The parser uses facilities to recover from invalid, unexpected, or missing tokens.
    • Missing Nodes: The syntax tree can explicitly insert "missing" syntax elements as nodes. SwiftSyntax provides different "views" of the tree that can include these missing or unexpected nodes on demand.