9 unstable releases (3 breaking)

new 0.13.0 Feb 19, 2025
0.13.0-rc1 Feb 5, 2025
0.12.0 Oct 18, 2024
0.11.1 May 17, 2024
0.1.0 Jul 18, 2023

#901 in Programming languages

Download history 1634/week @ 2024-10-30 1261/week @ 2024-11-06 1526/week @ 2024-11-13 1640/week @ 2024-11-20 2264/week @ 2024-11-27 1953/week @ 2024-12-04 2101/week @ 2024-12-11 1519/week @ 2024-12-18 1066/week @ 2024-12-25 1335/week @ 2025-01-01 2070/week @ 2025-01-08 3266/week @ 2025-01-15 4252/week @ 2025-01-22 3773/week @ 2025-01-29 5933/week @ 2025-02-05 4052/week @ 2025-02-12

18,736 downloads per month
Used in 63 crates (15 directly)

Apache-2.0

400KB
9K SLoC

typst-syntax

Welcome to the Typst Syntax crate! This crate manages the syntactical structure of Typst by holding some core abstractions like assigning source file ids, parsing Typst syntax, creating an Abstract Syntax Tree (AST), initializing source "spans" (for linking AST elements to their outputs in a document), and syntax highlighting.

Below are quick descriptions of the files you might be editing if you find yourself here :)

  • lexer.rs: The lexical foundation of the parser, which converts a string of characters into tokens.
  • parser.rs: The main parser definition, preparing a Concrete Syntax Tree made of nested vectors of SyntaxNodes.
  • reparser.rs: The algorithm for reparsing the minimal required amount of source text for efficient incremental compilation.
  • ast.rs: The conversion layer between the Concrete Syntax Tree of the parser and the Abstract Syntax Tree used for code evaluation.
  • node.rs & span.rs: The underlying data structure for the Concrete Syntax Tree and the definitions of source spans used for efficiently pointing to a syntax node in things like diagnostics.
  • kind.rs & set.rs: An enum with all syntactical tokens and nodes and bit-set data structure for sets of SyntaxKinds.
  • highlight.rs: Extracting of syntax highlighting information out of the Concrete Syntax Tree (and outputting as HTML).
  • path.rs, file.rs, package.rs: The system for interning project and package paths as unique file IDs and resolving them in a virtual filesystem (not actually for opening files).

The structure of the parser is largely adapted from Rust Analyzer. Their documentation is a good reference for a number of the design decisions around the parser and AST.

The reparsing algorithm is explained in Section 4 of Martin's thesis (though it changed a bit since).

Dependencies

~6–12MB
~157K SLoC