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 via Arc sharing:
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::Language;pub use cst::parse_svelte;pub use cst::parse_svelte_incremental;pub use js::ParsedJsExpression;pub use js::ParsedJsProgram;
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.
- Parse
Options - Options for parsing Svelte source into the public AST.
- Source
Id - An opaque identifier for a source file, used to distinguish multiple inputs during batch processing.
- Source
Location - A line/column location in source text.
- 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 - Compiler
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 - expression_
identifier_ name - expression_
literal_ bool - expression_
literal_ string - find_
matching_ brace_ close - is_
component_ name - is_
custom_ element_ name - is_
valid_ component_ name - is_
valid_ element_ name - is_
void_ element_ name - line_
column_ at_ offset - modern_
node_ end - modern_
node_ span - modern_
node_ start - named_
children_ vec - parse
- Parse a Svelte component into the public AST.
- parse_
css - 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_
svelte_ ignores