Expand description
Parse Svelte components into typed AST and CST representations.
This crate provides the syntax layer for working with .svelte files in
Rust. It parses components into inspectable tree structures without
compiling them into JavaScript or CSS.
§Parsing a component
Use parse or parse_modern_root to obtain a typed AST:
use svelte_syntax::{parse, ParseMode, ParseOptions};
let doc = parse(
"<script>let count = 0;</script><button>{count}</button>",
ParseOptions {
mode: ParseMode::Modern,
..ParseOptions::default()
},
)?;
let root = match doc.root {
svelte_syntax::ast::Root::Modern(root) => root,
_ => unreachable!(),
};
// Access the instance script and template fragment.
assert!(root.instance.is_some());
assert!(!root.fragment.nodes.is_empty());§Parsing into a CST
Use parse_svelte for a tree-sitter concrete syntax tree:
use svelte_syntax::{SourceId, SourceText, parse_svelte};
let source = SourceText::new(SourceId::new(0), "<div>hello</div>", None);
let cst = parse_svelte(source)?;
assert_eq!(cst.root_kind(), "document");
assert!(!cst.has_error());§Incremental reparsing
Both the CST and AST support incremental reparsing. Provide the previous
parse result and a CstEdit describing the change, and unchanged
subtrees are reused automatically:
use svelte_syntax::{
SourceId, SourceText, CstEdit,
parse_svelte, parse_modern_root, parse_modern_root_incremental,
};
let before = "<script>let x = 1;</script><div>Hello</div>";
let after = "<script>let x = 1;</script><div>World</div>";
let old_root = parse_modern_root(before)?;
let old_cst = parse_svelte(SourceText::new(SourceId::new(0), before, None))?;
let edit = CstEdit::replace(before, 37, 42, "World");
let new_root = parse_modern_root_incremental(after, before, &old_root, &old_cst, edit)?;
// The script was not in the changed range, so it is Arc-reused.
assert!(std::sync::Arc::ptr_eq(
&old_root.instance.as_ref().unwrap().content,
&new_root.instance.as_ref().unwrap().content,
));Re-exports§
pub use cst::CstEdit;pub use cst::CstParser;pub use cst::Document;pub use cst::ExpressionCache;pub use cst::Language;pub use cst::ParsedDocument;pub use cst::parse_svelte;pub use cst::parse_svelte_incremental;pub use cst::Root;pub use cst::Element;pub use cst::TextNode;pub use cst::CommentNode;pub use cst::IfBlock;pub use cst::EachBlock;pub use cst::AwaitBlock;pub use cst::KeyBlock;pub use cst::SnippetBlock;pub use cst::ExpressionTag;pub use cst::HtmlTag;pub use cst::ConstTag;pub use cst::DebugTag;pub use cst::RenderTag;pub use cst::AttachTag;pub use cst::AttributeNode;pub use cst::AttributeValuePart;pub use cst::StartTag;pub use cst::ElseClause;pub use cst::Alternate;pub use cst::TemplateNode;pub use cst::ChildIter;pub use cst::AttributeIter;pub use cst::classify_node;pub use js::JsExpression;pub use js::JsProgram;
Modules§
- arena
- Arena-based Svelte AST with stable node IDs, parent pointers, and position queries.
- ast
- Svelte AST types.
- compat
- Compatibility helpers for legacy projections.
- cst
- js
Structs§
- BytePos
- A byte offset into source text, stored as a
u32. - Compile
Error - An error produced during parsing.
- Line
Column - A line/column location in source text.
- Parse
Counters - Snapshot of thread-local parse timing counters.
- Parse
Options - Options for parsing Svelte source into the public AST.
- Parse
Timings - Detailed timing breakdown of the parse pipeline.
- Source
Id - An opaque identifier for a source file, used to distinguish multiple inputs during batch processing.
- Source
Position - A byte range in source text, used by
CompileErrorto indicate where the error occurred. - Source
Text - Borrowed source text with an identifier and optional filename.
- Span
- A half-open byte range (
start..end) in source text.
Enums§
- Attribute
Kind - Diagnostic
Kind - Diagnostic codes for Svelte-specific parse and validation errors.
- Element
Kind - Parse
Mode - Selects which public AST shape the parser should return.
- Svelte
Element Kind
Functions§
- classify_
attribute_ name - classify_
element_ name - find_
matching_ brace_ close - is_
component_ name - is_
custom_ element_ name - is_
valid_ component_ name - is_
valid_ element_ name - is_
void_ element_ name - legacy_
root_ from_ modern - Convert a modern AST root to a legacy AST root without re-parsing the CST.
- line_
column_ at_ offset - parse
- Parse a Svelte component into the public AST.
- parse_
css - parse_
legacy_ root_ from_ cst - parse_
modern_ css_ nodes - parse_
modern_ expression_ from_ text - parse_
modern_ expression_ tag - parse_
modern_ root - Parse a Svelte component directly into the modern AST root.
- parse_
modern_ root_ incremental - Parse a Svelte component into the modern AST root incrementally, reusing unchanged subtrees from a previous parse. Requires a previous CST document and the CST edit that was applied so tree-sitter can compute changed ranges.
- parse_
modern_ root_ timed - Parse with detailed timing for each sub-phase.
- parse_
svelte_ ignores - read_
parse_ counters - Read current thread-local parse timing counters.
- reset_
parse_ counters - Reset all thread-local parse timing counters.