tree-sitter-swift

repository·main·Indexed 42 days ago

https://github.com/alex-pinkus/tree-sitter-swift

A Tree-sitter grammar for the Swift programming language, version 0.7.3, enabling high-performance incremental parsing for editors and tools. Supports integration with Rust, JavaScript (NPM), and WebAssembly (WASM).

Tokens
1.3K
Snippets
5
Records
7
Agent score
3%

What's inside tree-sitter-swift

  1. Use tree-sitter-swift in Rust

    main

    To use the Swift parser in a Rust project, add tree-sitter and tree-sitter-swift to your Cargo.toml. You can then initialize a tree_sitter::Parser and set its language using tree_sitter_swift::language().

    tree-sitter = "0.23.0"
    tree-sitter-swift = "=0.7.3"
    let mut parser = tree_sitter::Parser::new();
    parser.set_language(tree_sitter_swift::language())?;
    
    // ...
    
    let tree = parser.parse(&my_source_code, None)
        .ok_or_else(|| /* error handling code */)?;
  2. Use tree-sitter-swift in Javascript (NPM)

    main

    To use the Swift parser in a Node.js environment, add tree-sitter and tree-sitter-swift to your package.json. Use the tree-sitter library to instantiate a parser and pass the tree-sitter-swift module to setLanguage().

    "dependencies": {
      "tree-sitter-swift": "0.7.3",
      "tree-sitter": "^0.22.1"
    }
    const Parser = require("tree-sitter");
    const Swift = require("tree-sitter-swift");
    
    const parser = new Parser();
    parser.setLanguage(Swift);
    
    // ...
    
    const tree = parser.parse(mySourceCode);
  3. Use tree-sitter-swift in Web Assembly (WASM)

    main

    To use the Swift parser in a web environment via web-tree-sitter, you must first build a WASM bundle from the installed node modules using the tree-sitter CLI.

    1. Install dependencies: npm install web-tree-sitter tree-sitter-swift.
    2. Build the WASM bundle: npx tree-sitter build-asm ./node_modules/tree-sitter.
    3. Initialize the parser in your application by calling Parser.init() and loading the generated .wasm file.
    $ npx tree-sitter build-asm ./node_modules/tree-sitter
    const Parser = require("web-tree-sitter");
    async function run() {
      // needs to happen first
      await Parser.init();
      // wait for the load of swift
      const Swift = await Parser.Language.load("./tree-sitter-swift.wasm");
    
      const parser = new Parser();
      parser.setLanguage(Swift);
    
      // Parse your swift code here.
      const tree = parser.parse('print("Hello, World!")');
    }
    
    run().then(console.log, console.error);
  4. Workflow for editing the Swift grammar

    main

    If you are developing or modifying the grammar itself, follow this workflow:

    1. Modify grammar.ts.
    2. Run npm install && npm test to verify existing parsing behavior. Note: npm test defaults to using valgrind; if unavailable, use tree-sitter test instead.
    3. Run tree-sitter parse on a real Swift codebase to identify regressions.
    4. Create new corpus test cases based on any parsing failures.
  5. How to obtain the autogenerated parser.c and grammar.json

    main

    This repository does not check in autogenerated files like parser.c or grammar.json to reduce noise and ensure compatibility.

    If you need these files without setting up a local build environment, you can download them from GitHub Actions:

    1. Navigate to the Actions page of this repository.
    2. Select the "Publish grammar.json and parser.c" workflow run for your desired commit.
    3. Under Artifacts, download generated-parser-src.
  6. Use tree-sitter-swift in Node.js

    main

    The tree-sitter-swift package provides a compiled Node.js binding for the Swift grammar. It is exported via node-gyp-build to ensure compatibility with the local environment.

    In addition to the standard Tree-sitter language object, this module may export a nodeTypeInfo property containing metadata about Swift node types from src/node-types.json. This is useful for mapping tree nodes to specific Swift language constructs.

    const Language = require('tree-sitter');
    const Swift = require('tree-sitter-swift');
    
    // Use the exported language object with a Tree-sitter Parser
    const parser = new Language(Swift);
  7. Access Swift node type information

    main

    The tree-sitter-swift module optionally exports nodeTypeInfo, which contains a JSON mapping of Swift node types. This can be used to programmatically inspect the grammar's node structure.

    const Swift = require('tree-sitter-swift');
    
    if (Swift.nodeTypeInfo) {
      console.log('Available node types:', Swift.nodeTypeInfo);
    }