A Svelte markup parser in Rust: lossless, never-failing, span-based, and
built to interoperate with the oxc toolchain. Sibling to
vue_sfc_parser, under the same
contract.
parse_markup parses a whole .svelte file (Svelte components are
markup at the top level) into a spanned AST:
- Elements with fully decomposed attributes: directives with
|modifiers(on:,bind:,class:,style:,use:,transition:,in:,out:,animate:,let:),{shorthand},{...spread}, and text/expression value part sequences (class="a {b} c"). - Mustaches
{expr}and the tag family{@html},{@debug},{@const},{@render},{@attach}— unknown{@word}forms degrade to a parsed node, not raw text. - Logic blocks with their full branch structure:
{#if}/{:else if}/{:else},{#each expr as pattern, index (key)}in all its partial forms (including Svelte 5's{#each expr}),{#await}with{:then}/{:catch}and the inline shorthands,{#key}, and{#snippet name(params)}. <script>and<style>are raw-text elements whose contents are exposed as spans for the consumer's JS/CSS parsers.<textarea>children parse as markup (Svelte allows mustaches there).<svelte:*>special elements, dotted component names (foo.Widget), and<!-- svelte-ignore … -->comments (exposed viaComment::svelte_ignore).
The parser never fails. Malformed markup degrades to Node::Raw
pass-through spans, unclosed elements and blocks recover at their parent's
boundary, and unterminated constructs absorb the rest of the input with an
unterminated flag — consumers always receive a tree that covers the whole
input. Verified against the Svelte compiler's own test suite: 4,477
.svelte files (error fixtures included), zero panics, zero span-coverage
violations.
Recovery is reported, not hidden. parse returns a ParseResult whose
recovered flag says whether any of that happened, so a consumer that
rewrites the source can refuse rather than reformat a guess. Measured over
the same 4,477 files against svelte/compiler's own verdict, it flags one
file the compiler accepts. It covers markup structure only — invalid CSS or
JS inside a component belongs to the parsers that own them.
Embedded JavaScript/TypeScript is intentionally not parsed here. The
{…} scan is JS-aware (strings, template literals, comments, bracket
depth) only to find the closing brace — { ok ? "}" : a${'{'}} closes
where JavaScript says it closes. Expressions, patterns, and parameter lists
are exposed as spans into the source; consumers hand them to a JS/TS parser
(e.g. oxc_parser) in whatever mode they need.
Spans are oxc_span::Span. The one dependency. Byte-offset spans that
plug directly into oxc's diagnostics, semantic analysis, and fixers with
zero conversion. parse_markup takes a base_offset added to every span,
so spans are file-relative by construction.
use svelte_markup_parser::{parse_markup, ast::{Node, BlockKind}};
let source = std::fs::read_to_string("Component.svelte")?;
for node in parse_markup(&source, 0) {
if let Node::Block(block) = node
&& let BlockKind::Each(each) = &block.kind
&& each.key.is_none()
{
println!("unkeyed each at {:?}", each.header_span);
}
}There is also a corpus checker that parses every .svelte file under a
directory and verifies the structural invariants:
cargo run --example parse_corpus -- path/to/svelte/project
Built for pitis/oxc, a fork of oxc, as the
foundation for native svelte/* template lint rules in oxlint — the same
role vue_sfc_parser plays for the fork's 56 vue/* rules.
The API is young (0.1.x) and may move; the never-failing span contract
will not.