YAML Grammar for Tree-sitter

0.7.2 · active · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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.

import tree_sitter_yaml
from tree_sitter import Language, Parser

# Load the YAML language grammar
YAML_LANGUAGE = Language(tree_sitter_yaml.language())

# Initialize the parser with the YAML language
parser = Parser()
parser.set_language(YAML_LANGUAGE)

# Example YAML content
yaml_code = b"""
name: John Doe
age: 30
cities:
  - New York
  - London
"""

# Parse the YAML code
tree = parser.parse(yaml_code)

# Get the root node of the syntax tree
root_node = tree.root_node

# Print the tree structure (simplified for quickstart)
def print_node(node, indent=0):
    print('  ' * indent + f"Type: {node.type}, Text: {node.text.decode('utf8')}")
    for child in node.children:
        print_node(child, indent + 1)

print_node(root_node)

# Example: Find a specific node type (e.g., 'pair')
query = YAML_LANGUAGE.query("""
(pair (key) @key (value) @value)
""")
captures = query.captures(root_node)

print("\nKey-Value Pairs:")
for node, name in captures:
    if name == 'key':
        key_text = node.text.decode('utf8')
    elif name == 'value':
        value_text = node.text.decode('utf8')
        print(f"  Key: {key_text}, Value: {value_text}")

view raw JSON →