Skip to content

feat(css): Parsec had no CSS parser to run css-parsing-tests against - #87

Merged
jamesconwaydev merged 2 commits into
mainfrom
css-syntax
Nov 19, 2026
Merged

jamesconwaydev merged 2 commits into
mainfrom
css-syntax

Conversation

@jamesconwaydev

Copy link
Copy Markdown
Collaborator

Problem

Parsec had no CSS parser, so nothing could run css-parsing-tests, and nothing for #29 and #30 to build on. Lexbor has one, tested only against its own files, with no positions on rules or declarations (lexbor#374).

Change

A new crate, crates/parsec-css (package parsec-html-css, crate parsec_css), written from the CSS Syntax Level 3 editor's draft as of October 2026:

  • The tokenizer, with every token type and a byte span on each. Preprocessing happens where it shows (every newline test takes CR, LF, CR LF and FF; NUL counts as U+FFFD) instead of as a copy. Names and values borrow the input unless an escape or a NUL changes them.
  • Every parser entry point: parse_stylesheet, parse_block_contents (nesting's declarations-then-rules), parse_declaration_list, parse_rule, parse_declaration, parse_component_value, parse_component_values, parse_comma_separated, parse_unicode_range_value, and parse_rule_list for the 2021 draft's CDO/CDC behaviour. A rule's {} block is kept as component values and SimpleBlock::contents parses it when the rule calls for that, with the same result as parsing it at once.
  • Spans on every rule, declaration, block and function; LineIndex gives lines and columns on request.
  • An+B (parse_anb, anb, serialize_anb), <urange> (parse_unicode_ranges), and decode_stylesheet for bytes with @charset, through encoding_rs, which the workspace already has.
  • Serialization (to_css and Display on every type) that parses back to the same values, with /**/ where the spec's table says, plus the two pairs it misses (-- before >, < before !).
  • Nothing recurses on the input's nesting: building, writing and dropping a million nested brackets works on any stack.

Also: a fuzz target in crates/parsec-css/fuzz, added to the nightly matrix; a css mode in bench/ timing tokenizing and parsing against Lexbor's CSS module (through a small C file, since reading a token's type needs Lexbor's struct layout) and Servo's cssparser 0.38 (a bench-only dependency); bench/stylesheets.txt with five MIT stylesheets fetched by hash; NOTICE entries for the vendored test data; README and ROADMAP.

Tests

cargo test -p parsec-html-css runs three suites, pinned with their commit and licence, and writes their counts for CI's summary:

  • css-parsing-tests from CourtBouillon/css-parsing-tests (where SimonSapin's repository now points) at 203ce36: 293 passed, 12 failed, 8,061 skipped. The skipped are the color_* files, CSS Color rather than Syntax. The 12 failures are where the current draft parses differently from the suite's: 9 expect unicode-range tokens outside a unicode-range descriptor, 1 has U+0080 as an ident code point, and 2 are "parse a declaration" before nesting made it stop at ;. Each is listed with its reason, and the run fails on any other failure or if a listed one passes.
  • The <urange> cases from rust-cssparser's copy, at 4765111: 12 of 12.
  • romainmenke/css-tokenizer-tests at 0db1d12: 287 of 287, types, text, positions and values. Its three community stylesheets (53 MB, not vendored) also matched in full, run once locally.

Every suite input is also serialized and parsed back, as values, as a stylesheet and as block contents. There are 12 unit tests and 9 doctests, and the fuzz target ran clean for about seven minutes locally after the two serializer fixes it found.

cargo run --release --manifest-path bench/Cargo.toml -- css, twice on an M2 Max with the load under 2.5, ten real stylesheets (1.87 MB), MB/s for all ten, second run:

Parsec Lexbor 3.0.1 cssparser 0.38
Tokenizing 378 217 291
Parsing 148 115 196

Ahead of Lexbor on every sheet at both (1.2 to 2.7 times tokenizing, 1.1 to 1.65 parsing, and Lexbor's parse does more: selectors and typed values). cssparser parses a third faster, since it builds no tree; Parsec keeps every block with spans and parses contents from it. The first run was within 2% except cssparser tokenizing (321).

cargo fmt --all, cargo clippy --workspace --all-targets -- -D warnings, cargo test --workspace, cargo deny check, and the bench's own clippy and deny all pass.

Closes #28

crates/parsec-css (package parsec-html-css, crate parsec_css) is CSS
Syntax Level 3 as the current editor's draft has it: the tokenizer,
every parser entry point including the nesting-era block contents, the
An+B and <urange> microsyntaxes, the @charset byte-stream decoding, and
serialization back to text that parses to the same values.

Every token, function, block, rule and declaration has a byte span, and
LineIndex turns one into a line and column. Names and values borrow the
input unless an escape changes them. Nothing recurses on the input's
nesting, building, writing or dropping.

cargo test runs css-parsing-tests (CourtBouillon, 203ce36), the urange
cases from rust-cssparser's copy, and Romain Menke's tokenizer tests
(0db1d12), and round-trips every input through the serializer. 293 of
305 syntax cases pass; the 12 others are where the suite predates the
draft, each listed with the reason. All 12 urange and all 287 tokenizer
cases pass.

A cargo-fuzz target joins the nightly fuzz run, and bench/ gains a css
mode timing tokenizing and parsing against Lexbor's CSS module and
Servo's cssparser on real stylesheets.
The CSS Syntax parser moves from planned to done in the README's list
and table, with its css-parsing-tests count and its speed against
Lexbor and cssparser, and M7 in the roadmap records it.
@jamesconwaydev
jamesconwaydev merged commit 606b836 into main Nov 19, 2026
32 checks passed
@jamesconwaydev
jamesconwaydev deleted the css-syntax branch November 19, 2026 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A CSS Syntax parser that passes css-parsing-tests

1 participant