Skip to main content

Crate svelte_syntax

Crate svelte_syntax 

Source
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.
CompileError
An error produced during parsing.
LineColumn
A line/column location in source text.
ParseCounters
Snapshot of thread-local parse timing counters.
ParseOptions
Options for parsing Svelte source into the public AST.
ParseTimings
Detailed timing breakdown of the parse pipeline.
SourceId
An opaque identifier for a source file, used to distinguish multiple inputs during batch processing.
SourcePosition
A byte range in source text, used by CompileError to indicate where the error occurred.
SourceText
Borrowed source text with an identifier and optional filename.
Span
A half-open byte range (start..end) in source text.

Enums§

AttributeKind
DiagnosticKind
Diagnostic codes for Svelte-specific parse and validation errors.
ElementKind
ParseMode
Selects which public AST shape the parser should return.
SvelteElementKind

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.