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 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.
CompileError
An error produced during parsing.
ParseOptions
Options for parsing Svelte source into the public AST.
SourceId
An opaque identifier for a source file, used to distinguish multiple inputs during batch processing.
SourceLocation
A line/column location in source text.
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
CompilerDiagnosticKind
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
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