{"id":4811,"library":"tree-sitter-css","title":"Tree-sitter CSS Grammar","description":"tree-sitter-css is a Python package that provides the pre-compiled Tree-sitter grammar for CSS. It enables the `tree-sitter` Python library to parse CSS code into concrete syntax trees for various applications like syntax highlighting, code analysis, and refactoring tools. The library is actively maintained, with regular updates often aligning with the core `tree-sitter` project.","status":"active","version":"0.25.0","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-css","tags":["parsing","css","tree-sitter","syntax-tree","grammar"],"install":[{"cmd":"pip install tree-sitter tree-sitter-css","lang":"bash","label":"Install core library and CSS grammar"}],"dependencies":[{"reason":"Provides the core parsing engine and Python bindings necessary to use any Tree-sitter grammar.","package":"tree-sitter"}],"imports":[{"note":"The `tree_sitter_css` package exposes the compiled grammar via `tree_sitter_css.language()`, which is then loaded by the `tree_sitter.Language` class. Do not attempt to import `Parser` directly from `tree_sitter_css`.","symbol":"language","correct":"import tree_sitter_css; from tree_sitter import Language, Parser"}],"quickstart":{"code":"import tree_sitter_css\nfrom tree_sitter import Language, Parser\n\n# Load the CSS language grammar\nCSS_LANGUAGE = Language(tree_sitter_css.language())\n\n# Create a parser and set its language\nparser = Parser()\nparser.set_language(CSS_LANGUAGE)\n\n# Parse some CSS code\ncss_code = b\"\"\"\nbody {\n    font-family: Arial, sans-serif;\n    margin: 0;\n}\n.container {\n    padding: 20px;\n    display: flex;\n}\n\"\"\"\ntree = parser.parse(css_code)\n\n# Access the root node of the syntax tree\nroot_node = tree.root_node\nprint(f\"Root node type: {root_node.type}\")\nprint(f\"Root node children count: {len(root_node.children)}\")\n\n# Example: Find a class selector (basic node access for quickstart)\n# More advanced usage typically involves tree-sitter Query objects.\nfor child in root_node.children:\n    if child.type == 'rule_set':\n        # In Tree-sitter, fields can be accessed by name if defined in grammar\n        selector = child.child_by_field_name('selectors')\n        if selector:\n            print(f\"Selector found: {selector.text.decode('utf8')}\")\n","lang":"python","description":"This quickstart demonstrates how to install the `tree-sitter` core library and `tree-sitter-css` grammar, load the CSS language object, parse a simple CSS string, and access basic properties of the generated syntax tree's root node. For advanced tree traversal and querying using Tree-sitter's pattern matching capabilities, refer to the `tree-sitter` library's official documentation."},"warnings":[{"fix":"Instead of compiling from source, install pre-compiled language packages like `tree-sitter-css` via pip and load the language object using `Language(tree_sitter_css.language())`.","message":"The `tree_sitter.Language.build_library` static method, used for compiling grammars from source, was removed in `tree-sitter` versions 0.22.0 and newer. Attempting to use this method will result in an `AttributeError`.","severity":"breaking","affected_versions":"tree-sitter >= 0.22.0"},{"fix":"Ensure `from tree_sitter import Language, Parser` is used, and then load the CSS grammar with `CSS_LANGUAGE = Language(tree_sitter_css.language())`.","message":"The `tree-sitter-css` package provides the *grammar* but not the *parser* itself. You must always import `Language` and `Parser` from the `tree_sitter` core library. Direct imports like `from tree_sitter_css import Parser` will fail.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always ensure your `tree-sitter` and `tree-sitter-css` packages are kept in sync, ideally installing the latest stable versions of both to avoid compatibility issues.","message":"The core `tree-sitter` library (version 0.25.0+) introduced significant internal ABI bumps and changes to how parsing/querying cancellation is handled. While `tree-sitter-css` at version 0.25.0 should be compatible, older versions of `tree-sitter` (e.g., <0.25.0) might lead to ABI mismatches or require adjusting code using deprecated cancellation patterns.","severity":"gotcha","affected_versions":"tree-sitter < 0.25.0 when used with tree-sitter-css >= 0.25.0"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}