svelte-syntax
A Rust crate for parsing Svelte components into typed AST and CST representations.
svelte-syntax handles the syntax layer only — it parses .svelte files and
CSS stylesheets into inspectable tree structures. It does not compile
components into JavaScript or CSS. For compilation, use svelte-compiler.
Install
[]
= "0.1.4"
Quick start
Parse a Svelte component into the modern AST (Svelte 5 runes mode):
use ;
let doc = parse?;
let root = match doc.root ;
assert!;
assert!;
# Ok::
Parse raw source into a tree-sitter CST:
use ;
let source = new;
let cst = parse_svelte?;
assert_eq!;
assert!;
# Ok::
What it provides
AST parsing
parse— parse a component into aDocumentcontaining a modern (Svelte 5 runes) or legacy (Svelte 3/4) AST root.parse_modern_root— parse directly into amodern::Rootwith typed script, template, and style blocks.parse_modern_root_incremental— reparse using a previous AST and CST, reusing unchanged subtrees for speed.parse_css— parse a standalone CSS stylesheet.
CST parsing
parse_svelte— parse source into a tree-sitter concrete syntax tree for low-level inspection.parse_svelte_incremental— incremental reparse using a previous tree and aCstEdit.CstParser— configurable tree-sitter parser with language selection.
JavaScript handles
JsProgram— a parsed JavaScript/TypeScript program. Owns its source so you can access the AST without reparsing.JsExpression— same idea for a single JS/TS expression.
Arena AST
SvelteAst— arena-allocated AST with stable node IDs, parent pointers, and position queries. Designed for language servers, linters, and formatters that need fast navigation and incremental updates.
Utilities
SourceText— borrowed source text with filename, UTF-16 offset conversion, and line/column lookups.BytePos,Span,SourceId— lightweight position primitives.- Element and attribute classification helpers:
classify_element_name,classify_attribute_name,is_component_name,is_void_element_name, and others. CompileError— structured error type with source positions and diagnostic codes.
License
MIT