tree-sitter-css
Fast, incremental CSS grammar for the Tree-sitter parsing framework.
Repository Health
Technical Analysis
tree-sitter-css is the official CSS grammar for Tree-sitter, the incremental parsing library used throughout modern code editors and tooling. It turns raw CSS source into a concrete syntax tree that can be re-parsed efficiently as the document changes, making it the foundation for syntax highlighting, code folding, structural navigation, and linting.
The grammar ships with prebuilt bindings for Rust, C, Node.js, Python, Go, and Swift, so the same parser can be embedded in editors, language servers, and static-analysis pipelines regardless of host language. It is maintained under the core Tree-sitter organization and tracks the CSS syntax used across stylesheets and embedded style blocks.
What You Get
- A complete CSS grammar (grammar.js) plus the generated C parser (src/parser.c) ready to compile
- Rust bindings exposing the parser through the tree-sitter-language interface
- Additional bindings for C, Node.js, Python, Go, and Swift from a single grammar source
- Highlight and query files (queries/) for syntax highlighting and code navigation
- A corpus test suite covering selectors, at-rules, declarations, and error recovery
Common Use Cases
- Powering syntax highlighting for CSS in code editors and terminal viewers
- Building language servers and linters that need a structured view of stylesheets
- Enabling code folding, structural selection, and outline views for CSS files
- Extracting or transforming CSS rules programmatically via the syntax tree
Under The Hood
Architecture - The grammar is declared in grammar.js using Tree-sitter’s DSL, defining CSS constructs (selectors, at-rules, declarations, values) with extras for whitespace/comments and externals for context-sensitive tokens like the descendant operator and pseudo-class colon. The Tree-sitter CLI compiles this into a deterministic C parser (src/parser.c plus scanner), and thin per-language bindings expose that parser to host code. Tech Stack - The source of truth is JavaScript (the grammar DSL); the generated parser is C. The Rust binding depends on tree-sitter-language and builds src/parser.c via a cc build script, exposing a LANGUAGE constant. Parallel bindings exist for C, Node.js, Python, Go, and Swift, all driven from the same grammar. Code Quality - The repo carries a corpus-based test suite under test/corpus plus highlight fixtures, giving concrete coverage of selectors, at-rules, and error recovery; CI runs on GitHub Actions. The generated parser is not hand-edited, so quality tracks the grammar definition. API Design - Consumers get an idiomatic entry point per language (e.g. the Rust LANGUAGE constant fed to a Tree-sitter parser), so integrating CSS parsing takes only a few lines once the Tree-sitter runtime is present.