Expand description
CSS formatting, and parsing to the wire JSON of Svelte’s parseCss — part of
tsv.
format() formats one stylesheet in one call; a Formatter formats many on one
thread, keeping its arenas warm between calls. The formatter is non-configurable:
Prettier’s style with a print width of 100, tabs and single quotes. Formatting output
is not frozen — it changes between releases as formatting bugs are fixed.
§Usage
use tsv_css::{Formatter, format};
assert_eq!(format("a{color:red}")?, "a {\n\tcolor: red;\n}\n");
let mut formatter = Formatter::new();
assert_eq!(formatter.format("b{}")?, "b {\n}\n");
// Parse to the wire JSON AST (the `json` feature, on by default)
let json = tsv_css::parse_to_json("a{}")?;
assert!(json.starts_with(r#"{"type":"StyleSheetFile""#));The wire JSON is the AST as JSON in the canonical parser’s shape — Svelte’s parseCss’s
here. Its start / end are UTF-16 offsets, and the default wire is span-only: no loc
(the locations feature adds it). Error::span, unlike the wire, is a UTF-8 byte range.
§Errors
A source that does not parse is an Error:
use tsv_css::{ErrorKind, format};
let err = format("a{").unwrap_err();
assert_eq!(err.kind(), ErrorKind::Syntax);
assert_eq!(err.span(), Some(2..2));Every tsv language crate returns the same Error: its Error::kind, its
Error::span in the input’s UTF-8 bytes, and Error::to_report — how tsv’s bindings
(its npm packages) report it — in the wire’s UTF-16 offsets and lines; one line under
{}, the tsv CLI’s frame under {:#}.
§Deep input
The parser refuses a tree past its depth limits with ErrorKind::TooDeep, so every
walk of an accepted tree fits a 2 MiB thread in an optimized build and a 16 MiB one in a
debug build. A stack overflow aborts the process rather than panicking, so a debug
build handling input it does not control — cargo test runs each test on a 2 MiB thread —
wraps the call in run_with_stack, which runs it on a thread reserving STACK_SIZE.
§Features
json(default) —parse_to_json,parse_to_json_withandJsonOptions. Turn default features off for a format-only build.locations—JsonOptions::locations, adding aloc(line and column) to every positioned object of the wire.
The _internal_* features are tsv’s own diagnostics, not for consumers
(Stability).
§Stability
This release exposes no AST: parsing returns the wire JSON, in parseCss’s shape, which
@fuzdev/tsv-parse-wasm’s tsv_ast.d.ts types.
Items hidden from these docs (__private, ast and every other #[doc(hidden)] item)
and the _internal_* features are tsv’s own plumbing for its sibling crates, which pin
each other exactly; they change in any release, outside semver.
Structs§
- Error
- Why a parse or format produced no output — a syntax error, a depth limit, a refusal of
a source that parses, or a source over the size cap (
ErrorKind): what went wrong (Error::kind,Error::message), where (Error::span, UTF-8 byte offsets), and the pointed report tsv’s bindings (its npm packages) render (Error::to_report). One pointer wide, so aResultcarrying it costs a successful call almost nothing. - Error
Report - A pointed error as tsv’s bindings (its npm packages) report it: the message and the frame
apart, and the span in the document’s wire coordinates (
Error::to_report). The error’s full text (its{:#}form) is exactlymessage, a\n, thenframe. - Formatter
- Formats many CSS sources on one thread, reusing its allocations from one source to the next.
- Json
Options - What
parse_to_json_with’s wire carries. The default isparse_to_json’s: the span-only wire. Its one option,locations, requires thelocationsfeature. - Wire
Point - One position in a language’s wire coordinates, the positions its wire JSON (the parse
output: the AST as JSON in the canonical parser’s shape) carries. It holds the offset a
wire node starting at that byte would carry as its
start, and thelocpointtsv parse --locations(and the npm packages’create_locator(…).position_at(start)) gives it. The coordinates are the language’s canonical parser’s: a leading byte-order mark counted for TypeScript (acorn) and elided for Svelte and CSS, and lines ended by ECMAScript’s terminators for TypeScript and by\nalone for Svelte and CSS.
Enums§
- Error
Kind - What kind of failure an
Erroris (Error::kind).
Constants§
- STACK_
SIZE - Stack reserved by
run_with_stack: 32 MiB, twice the debug build’s contract.
Functions§
- format
- Parse and format
sourcein one call. - parse_
to_ json - Parse
sourceand return its wire JSON AST: compact JSON in Svelte’sparseCss()shape (a standaloneStyleSheetFile), every node’sstart/enda UTF-16 offset and noloc. - parse_
to_ json_ with parse_to_jsonunderoptions: whether the wire carriesloc.- run_
with_ stack - Run
fon a thread reservingSTACK_SIZEand return its value.