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.2"
Quick start
Parse a Svelte component into the modern AST:
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 either a modern or legacy 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 viaArcsharing.parse_css— parse a standalone CSS stylesheet.
CST parsing
parse_svelte— parse source into a tree-sitterDocumentfor low-level syntax tree inspection.parse_svelte_incremental— incremental CST reparse using a previous tree and aCstEdit.CstParser— configurable tree-sitter parser with typestate for language selection.
JavaScript handles
ParsedJsProgram— self-contained OXC program AST that owns its source and allocator. Access the parsedProgramwithout reparsing.ParsedJsExpression— same pattern for a single JS/TS expression.
Arena AST
SvelteAst— arena-allocated AST with stableNodeIdvalues, 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. CompileErrorandCompilerDiagnosticKind— structured error types with source positions and diagnostic codes.
License
MIT