03 Pipeline
bytes -> kernel.split -> rules.parse -> rules.compile -> body.parse -> AST -> renderers
| | (+imports) | (matcher table) |- html
+- body text +- diagnostics +- origin table |- ast json
|- decorations
+- plain text
1. Stages
#- split Find the rules block and body (01 §1). Cheap, byte-level.
- rules.parse Each rule line or full-form block to a
RuleDeclwith origin{file, line, col}. Directives handled:usecalls a resolver callback (host-supplied, synchronous, receives a canonical key; the engine never touches I/O),scope,set,off.useis expanded in place. - rules.compile Produce a
RuleTableper scope: block rules, line rules, inline rules,word rules. Each sorted longest-pattern-first, last-defined-wins for ties. Overridden rules stay in the table with
overridden_byset. Patterns compile to a literal matcher (a small trie keyed on the first literal byte, then a sequential literal/slot walker). No regex crate in the core.rerules useregex-litebehind a cargo feature. - body.parse
- Walk lines. For each line: record indentation, test block openers, then line rules,
else it is a paragraph line.
- Block bodies are collected until the closing line; verbatim ones are stored as raw text.
A
rewrite blockrule's body is stored rewritten into its target's language, line by line, with the node's spans still on the text as written (02 §8, D255). - Line nodes are post-processed:
groupmerges consecutive siblings,nestusesindentation to build children,
splitproduces cells,foldproduces section ranges. - Text content of non-verbatim nodes is scanned for inline and word rules in a single
left-to-right pass, longest match first, respecting `` escapes. Nested inlines recurse into slot content.
@scopeswaps the activeRuleTablefor the enclosed lines.- URL, attribute and CSS safety checks run here; every diagnostic is produced at parse.
- Plugins run last, as an AST transform: a node whose target is a plugin is replaced by
the plugin's output nodes or by a
Rawnode of trusted HTML (same span and rule).
- Walk lines. For each line: record indentation, test block openers, then line rules,
- render Renderers are pure functions over the AST. They never add diagnostics.
2. AST
#Node {
kind: Element | Text | Raw,
target: "p" | "li" | "mermaid" | ...,
classes: [..], attrs: {..},
children: [Node],
span: { start, end }, // byte offsets into the ORIGINAL source
rule: Option<RuleId>, // which compiled rule produced it
scope: ScopeId,
}
Document { nodes, folds: [{kind, level, span}], diagnostics, settings, rule_table, imports, marks }
Spans are always UTF-8 byte offsets into the original source (the JS package converts to
UTF-16 for CodeMirror). A node's span covers its whole subtree. Nodes produced by include
carry the include line's span. That single property is what makes the
editor's highlighting, folding, hybrid rendering and inspector all consistent.
imports lists every key the resolver was asked for and whether it resolved, so async
hosts can fetch missing files and parse again. The JSON shape is fixed in D52.
3. Outputs
#| Output | Consumer |
HTML string plus one <style> block | preview, CLI, edge workers |
| AST JSON | tooling, tests, other renderers |
Decorations [{span, class, rule}] | editor syntax highlighting, derived from rules, no separate grammar |
| Fold ranges | editor gutter |
| Rule table with origins and overrides | inspector |
Diagnostics [{span, severity, message}] | editor squiggles, CLI exit code |
4. Performance and safety
#- Single pass per stage. Target: 10 000 lines in under 5 ms native, under 20 ms WASM.
- No backtracking beyond slot boundary search inside one line.
- Hard limits: nesting depth 64 (nest, blocks, imports, includes combined), inline recursion
32, pattern length 256, 256 template lines per rewrite and 1 MiB of rewritten text per block (02 §8). Exceeding a limit produces one diagnostic and degrades to text (D55). It never panics and never loops.
- Fuzz target: any input, any rules, must terminate and must not panic.
- Incremental parsing is a v1.1 concern. The v1 API is
parse(source, options) -> Document,with the editor debouncing. Line-level incremental reparse fits later because blocks and lines are already independent units.
5. Public API (one shape for every binding)
#parse(source, { path?, resolver?, plugins?, allow_re?, allow_remote?, unsafe_html?,
forms?, vars?, defaults? }) -> Document
render_html(doc) -> { html, css, blocks: [{span, html}] }
render_ast(doc) -> json
decorations(doc, from?, to?) -> [{span, class, rule, kind}]
Every binding (Rust, WASM, Node, CLI) exposes exactly these four, with the same names.
blocks is each top-level node's HTML, for hybrid mode. With the sourceSpans option
(D101), block-level elements carry data-self-s="A B", their byte span relative to the start of
their top-level node; the option is off by default and never changes any other output.
Diagnostics are
{span, severity, message, file?}; severity is error, warning or info.
Beside the four, bindings may offer render_tree(doc) (D114): the same rendering as data, JSON
{format: "selfscribe-tree", version: 1, css, blocks: [{span, nodes}]} where a node is a string
(text) or JsonML [tag, {attrs}, ...children]. It is read back from render_html's output, so
the two never disagree; hosts build their page from it with DOM calls instead of HTML strings.