{"id":2322,"library":"tree-sitter-embedded-template","title":"Tree-sitter Embedded Template Grammar","description":"tree-sitter-embedded-template provides a Tree-sitter parser for templating languages such as ERB (Embedded Ruby) and EJS (Embedded JavaScript). It allows for robust parsing of files where scripting code is intertwined with text content using delimiters like `<%` and `%>`. The library is currently at version 0.25.0 and is an actively maintained part of the broader Tree-sitter ecosystem.","status":"active","version":"0.25.0","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-embedded-template","tags":["tree-sitter","parser","grammar","templating","erb","ejs","syntax-tree","parsing"],"install":[{"cmd":"pip install tree-sitter tree-sitter-embedded-template","lang":"bash","label":"Install core library and grammar"}],"dependencies":[{"reason":"This package provides the Python bindings for the core Tree-sitter parsing library, which is required to load and use the grammar.","package":"tree-sitter","optional":false}],"imports":[{"symbol":"Language","correct":"from tree_sitter import Language"},{"symbol":"Parser","correct":"from tree_sitter import Parser"},{"note":"The grammar itself is typically imported as a module and its 'language()' function is called to get the language object, rather than directly importing a 'Language' class from it.","wrong":"from tree_sitter_embedded_template import Language","symbol":"language","correct":"import tree_sitter_embedded_template as ts_embedded_template\n...\nts_embedded_template.language()"}],"quickstart":{"code":"import os\nfrom tree_sitter import Language, Parser\nimport tree_sitter_embedded_template as ts_embedded_template\n\n# Example ERB content\nCODE = b\"\"\"\n<h1>Hello, <%= name %>!</h1>\n<% if items.any? %>\n  <ul>\n    <% items.each do |item| %>\n      <li><%= item %></li>\n    <% end %>\n  </ul>\n<% else %>\n  <p>No items to display.</p>\n<% end %>\n\"\"\"\n\n# Ensure the tree-sitter library is built and loaded\n# For pre-compiled grammars like this, installation handles it.\n# For custom grammars, Language.build_library might be needed (but is deprecated in newer versions).\n\n# Load the embedded template language\nEMBEDDED_TEMPLATE_LANGUAGE = Language(ts_embedded_template.language())\n\n# Create a parser and set its language\nparser = Parser()\nparser.set_language(EMBEDDED_TEMPLATE_LANGUAGE)\n\n# Parse the code\ntree = parser.parse(CODE)\n\n# Get the root node of the syntax tree\nroot_node = tree.root_node\n\n# Print basic information about the root node\nprint(f\"Root Node Type: {root_node.type}\")\nprint(f\"Root Node Text: {root_node.text.decode('utf8')}\")\nprint(f\"Number of children: {len(root_node.children)}\")\n\n# Example: Find all 'erb_interpolation' nodes\n# Note: This is a basic traversal. For complex queries, use Tree-sitter's query API.\nfor child in root_node.children:\n    if child.type == 'template_content':\n        for grandchild in child.children:\n            if grandchild.type == 'erb_interpolation':\n                print(f\"Found ERB Interpolation: {grandchild.text.decode('utf8')} (Type: {grandchild.type})\")\n            elif grandchild.type == 'erb_statement':\n                print(f\"Found ERB Statement: {grandchild.text.decode('utf8')} (Type: {grandchild.type})\")\n","lang":"python","description":"This quickstart demonstrates how to install `tree-sitter` and the `tree-sitter-embedded-template` grammar, load the language, parse an ERB code snippet, and access the resulting syntax tree's root node and its children. It illustrates identifying embedded Ruby interpolation and statement nodes."},"warnings":[{"fix":"For official grammars, `pip install tree-sitter-<language>` is the recommended approach. If you need to compile a custom grammar, you'll need to use the `tree-sitter` CLI or other build tooling directly, and load the `.so` or `.dylib` file using `Language('/path/to/grammar.so', 'language_name')`.","message":"The `Language.build_library` function for compiling grammars from source was removed around `py-tree-sitter` version 0.21.x. Grammars are now primarily consumed via pre-compiled `pip install`able packages.","severity":"breaking","affected_versions":"tree-sitter>=0.21.0 (Python bindings)"},{"fix":"Always test your queries and parsing logic when updating `tree-sitter-embedded-template` or the core `tree-sitter` library. Refer to the grammar's `queries` directory on GitHub for up-to-date query examples, and monitor release notes for breaking changes in the grammar definition.","message":"Grammar updates, particularly minor and major versions, can introduce changes to the Abstract Syntax Tree (AST) node types or structure. This can break existing Tree-sitter queries that rely on specific node names or hierarchies.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your code uses `iter_matches` with the correct, fixed behavior. If you previously relied on the deprecated incorrect behavior, you must update your matching logic.","message":"The behavior of `iter_matches` in the core `tree-sitter` library was corrected, which was a breaking change for code relying on its previously incorrect output. An opt-out flag for the old behavior was provided but has since been removed.","severity":"breaking","affected_versions":"tree-sitter versions before ~0.20.x, potentially affecting 0.21.x-0.23.x if using deprecated flags."},{"fix":"Ensure any custom build scripts or tooling are updated to recognize `tree-sitter.json` for grammar metadata. If consuming the pre-compiled Python package, this is handled automatically.","message":"The `tree-sitter` ecosystem shifted from using `package.json` to `tree-sitter.json` for grammar metadata around October 2024. While this primarily affects grammar maintainers and build systems, it can cause issues if you're attempting to build or integrate the grammar from source using older tooling.","severity":"gotcha","affected_versions":"Prior to 0.25.0 (primarily impacts grammar development/building)"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}