muskitty-css
A from-scratch implementation of the CSS Syntax Module Level 3
tokenizer and parser in safe Rust. Facade crate that re-exports
muskitty-css-tokenizer and muskitty-css-parser for
convenience. Part of the MusKitty browser-engine project.
Status
| Stage | Spec coverage | Tests |
|---|---|---|
| Tokenizer (§4.3) | §4.3.1–§4.3.13 complete | unit + html5lib-style fixture tests |
| Parser (§5) | §5.2, §5.3, §5.4.1–§5.4.10, §5.5.1–§5.5.11 complete | 74 unit tests |
Install
[]
= "0.4"
MSRV: Rust 1.82+ (uses Option::is_none_or and other recently-stabilized stdlib APIs).
Quick start
use ;
use Rule;
use Token;
// Tokenize only.
let tokens = tokenize;
assert!;
// Parse a full stylesheet.
let ss = parse_stylesheet;
assert_eq!;
// Inspect the first rule's prelude.
if let QualifiedRule = &ss.rules
Other top-level entry points: parse_rule,
parse_declaration,
parse_component_value,
parse_list_of_component_values,
parse_comma_separated_list_of_component_values.
Architecture
The crate follows the two-stage model of CSS Syntax §3.1:
- Tokenization (
tokenizermodule) — consumes a stream of Unicode code points (after §5.3 preprocessing) and emitsTokens per the §4.3 "Tokenizer Algorithms":consume_a_token(§4.3.1) dispatches to sub-algorithms such asconsume_an_ident_like_token(§4.3.4),consume_a_numeric_token(§4.3.3),consume_a_string_token(§4.3.5), andconsume_a_url_token(§4.3.6). - Parsing (
parsermodule) — consumes the token stream and produces CSS objects (Stylesheet, Rule, Declaration, ComponentValue) per §5.parser::types— §5.2 data structures.parser::token_stream— §5.3TokenStreamwith mark/restore for backtracking.parser::algorithms— §5.5 the 11 parser algorithms.parser::entry_points— §5.4 the 9 entry points (§5.4.1 / §5.4.2 grammar hooks deferred).
Spec source
Authoritative spec source for this implementation:
D:\CSSWG\css-syntax-3\Overview.md (Markdown rendering of the editor's
draft). All line numbers in code comments reference this file.
Public spec: https://drafts.csswg.org/css-syntax-3/
License
Apache-2.0, same as the workspace root.