{"id":1750,"library":"tree-sitter-yaml","title":"YAML Grammar for Tree-sitter","description":"tree-sitter-yaml provides a pre-compiled Tree-sitter grammar for parsing YAML files within Python applications. It enables detailed syntax analysis and manipulation of YAML structures. Currently at version 0.7.2, the library is actively maintained and receives regular updates, often in conjunction with upstream Tree-sitter changes.","status":"active","version":"0.7.2","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter-grammars/tree-sitter-yaml","tags":["tree-sitter","parsing","yaml","grammar","syntax-tree","linter","static-analysis"],"install":[{"cmd":"pip install tree-sitter-yaml","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"This is the core Python binding library for Tree-sitter, required to load and use the grammar for parsing.","package":"tree-sitter","optional":false},{"reason":"Provides pre-compiled binary wheels for many Tree-sitter grammars, simplifying loading and avoiding manual compilation steps, though tree-sitter-yaml can be loaded directly.","package":"tree-sitter-languages","optional":true}],"imports":[{"note":"Imports the pre-compiled YAML grammar and initializes it with the core Tree-sitter Language object.","symbol":"language","correct":"import tree_sitter_yaml\nfrom tree_sitter import Language, Parser\n\nYAML_LANGUAGE = Language(tree_sitter_yaml.language())"},{"note":"If using the 'tree-sitter-languages' package, grammars (including YAML) can be loaded conveniently by name. Requires 'tree-sitter-languages' to be installed.","symbol":"get_language, get_parser","correct":"from tree_sitter_languages import get_language, get_parser\n\n# Or, for YAML specifically if available via this package\nlanguage = get_language('yaml')\nparser = get_parser('yaml')"}],"quickstart":{"code":"import tree_sitter_yaml\nfrom tree_sitter import Language, Parser\n\n# Load the YAML language grammar\nYAML_LANGUAGE = Language(tree_sitter_yaml.language())\n\n# Initialize the parser with the YAML language\nparser = Parser()\nparser.set_language(YAML_LANGUAGE)\n\n# Example YAML content\nyaml_code = b\"\"\"\nname: John Doe\nage: 30\ncities:\n  - New York\n  - London\n\"\"\"\n\n# Parse the YAML code\ntree = parser.parse(yaml_code)\n\n# Get the root node of the syntax tree\nroot_node = tree.root_node\n\n# Print the tree structure (simplified for quickstart)\ndef print_node(node, indent=0):\n    print('  ' * indent + f\"Type: {node.type}, Text: {node.text.decode('utf8')}\")\n    for child in node.children:\n        print_node(child, indent + 1)\n\nprint_node(root_node)\n\n# Example: Find a specific node type (e.g., 'pair')\nquery = YAML_LANGUAGE.query(\"\"\"\n(pair (key) @key (value) @value)\n\"\"\")\ncaptures = query.captures(root_node)\n\nprint(\"\\nKey-Value Pairs:\")\nfor node, name in captures:\n    if name == 'key':\n        key_text = node.text.decode('utf8')\n    elif name == 'value':\n        value_text = node.text.decode('utf8')\n        print(f\"  Key: {key_text}, Value: {value_text}\")","lang":"python","description":"This quickstart demonstrates how to initialize the Tree-sitter parser with the `tree-sitter-yaml` grammar, parse a YAML byte string, traverse the resulting syntax tree, and execute a basic Tree-sitter query to extract key-value pairs."},"warnings":[{"fix":"Ensure `tree-sitter-yaml` and the core `tree-sitter` library are compatible. If you encounter this error, try updating both packages to their latest versions or consult release notes for compatibility information.","message":"Tree-sitter grammars are compiled against a specific ABI version of the core Tree-sitter library. Updating the `tree-sitter` Python package (or underlying C library) without a corresponding `tree-sitter-yaml` update can lead to 'Incompatible language version' errors.","severity":"breaking","affected_versions":"All versions of `tree-sitter-yaml` when `tree-sitter` core ABI changes."},{"fix":"For `tree-sitter-yaml`, simply load the `language()` function directly. If using `tree-sitter-languages`, use `get_language()` and `get_parser()`. Only manually compile if you are working with a grammar that does not provide pre-built Python wheels.","message":"Directly using the `tree-sitter` library often involves compiling grammars. However, `tree-sitter-yaml` (and `tree-sitter-languages`) provides pre-compiled binary wheels. Attempting manual compilation (`Language.build_library()`) for these packages is usually unnecessary and can lead to errors or confusion if the package already supplies a pre-built grammar.","severity":"gotcha","affected_versions":"All versions."},{"fix":"If experiencing memory issues with reused parsers or large YAML files, consider re-initializing the parser for new files, ensuring the correct language name is passed, or investigating if the issue lies within the integrating application (e.g., editor plugin). Consult the `tree-sitter` core library issues for potential upstream fixes or workarounds.","message":"Some users have reported memory leaks or unexpected highlighting issues when reusing Tree-sitter YAML parsers, especially with very large files or specific text editing patterns in integrated environments (e.g., Neovim). This can manifest as broken highlighting or increased memory consumption.","severity":"gotcha","affected_versions":"Potentially all versions, depending on usage context and core `tree-sitter` interactions."},{"fix":"Update the consuming application or its configuration (e.g., Neovim plugins, color schemes, custom queries) to align with the new Tree-sitter capture group names. Refer to the specific application's documentation or migration guides for details on affected groups (e.g., `@parameter` to `@variable.parameter`).","message":"Downstream tools relying on Tree-sitter grammars (e.g., `nvim-treesitter` for syntax highlighting) may experience breaking changes if Tree-sitter's internal capture group naming conventions are updated. This requires corresponding updates in client applications' configurations, queries, or color schemes.","severity":"breaking","affected_versions":"Relevant for integrations like `nvim-treesitter` from ~v0.9.x to v0.10.x and newer, specifically for highlighting groups."}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}