Skip to main content

Parser for Jinja2 and daughter languages: nunjucks, twig

Project description

tree-sitter-jinja-dialects

A tree-sitter grammar for the Jinja template language family.

Supported languages

This grammar parses the union of syntax from:

  • Jinja2 (Python) - the original
  • Nunjucks (JavaScript) - Mozilla's JS port, used by 11ty
  • Twig (PHP) - Symfony's template engine
  • Tera (Rust) - Rust port
  • Inja (C++) - C++ port
  • Django Templates (Python) - predecessor, subset syntax

Valid syntax from any of these languages is accepted. Both true and True parse as booleans; both none and null parse as null literals; both key=val and key: val work in keyword arguments.

Architecture

The grammar follows a template-first approach: it parses template delimiters and structure, exposing text nodes for HTML injection via queries/injections.scm.

source_file := repeat(choice(statement, output, comment, text))

Expressions

Full expression parsing with 13-level precedence, including:

  • Arithmetic, comparison, logical, membership (in/not in), identity (is/is not)
  • Filter chains (x | lower | truncate(50))
  • Member access (obj.prop, obj?.prop), subscript (items[0])
  • Function calls with keyword arguments and spread
  • Conditional expressions (Jinja2 x if c else y and Twig c ? x : y)
  • Twig operators: ===, !==, ??, b-and/b-or/b-xor

Statements

All standard block and inline statements:

  • if/elif/else/endif
  • for/else/endfor (with tuple unpacking, condition filter, recursive)
  • block/endblock, macro/endmacro, call/endcall
  • filter/endfilter, with/endwith, autoescape/endautoescape
  • raw/endraw and verbatim/endverbatim (via external scanner)
  • set (inline and block forms), extends, include, import, from, do
  • Catch-all generic_tag for custom/extension tags

Whitespace control

All delimiter pairs support - (strip) and ~ (Twig preserve-newlines):

{{- expr -}}    {%- tag -%}    {#- comment -#}
{{~ expr ~}}    {%~ tag ~%}    {#~ comment ~#}

Queries

File Purpose
highlights.scm Syntax highlighting
injections.scm HTML injection into text nodes
locals.scm Variable definitions and references
tags.scm Symbol tagging (blocks, macros)

Usage

Neovim

-- In your tree-sitter config
require('nvim-treesitter.configs').setup {
  ensure_installed = { 'jinja' },
}

Node.js

const Parser = require('tree-sitter');
const Jinja = require('tree-sitter-jinja-dialects');

const parser = new Parser();
parser.setLanguage(Jinja);

const tree = parser.parse('{{ name | lower }}');

Bindings

Pre-built bindings for C, Go, Node.js, Python, Rust, and Swift.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tree_sitter_jinja_dialects-0.1.0.tar.gz (51.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tree_sitter_jinja_dialects-0.1.0-cp310-abi3-win_amd64.whl (31.1 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_jinja_dialects-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl (45.3 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

tree_sitter_jinja_dialects-0.1.0-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (45.8 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64manylinux: glibc 2.5+ x86-64

tree_sitter_jinja_dialects-0.1.0-cp310-abi3-macosx_11_0_arm64.whl (29.8 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file tree_sitter_jinja_dialects-0.1.0.tar.gz.

File metadata

File hashes

Hashes for tree_sitter_jinja_dialects-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2a8c3deaec7a5e39f6d95979fe60e3f86159e07f7a02f8f84b27fbb92de902e2
MD5 9d2b76768f0943ad3e61db216675e449
BLAKE2b-256 d3cdde4cfa7623de4d070ed69e65834e4bf1f3029c5c7882f4d014db13c53a71

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_jinja_dialects-0.1.0.tar.gz:

Publisher: publish.yml on bennypowers/tree-sitter-jinja-dialects

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_jinja_dialects-0.1.0-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_jinja_dialects-0.1.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b2f3148113a394a293793858c5704ec0f13cf7de47a4bae972d6a149ed6462e0
MD5 2e9ef30d341f1c9736c15c6e7da0d911
BLAKE2b-256 ea0cc361dbaa90829703c61ef2b10767a3ce8e9953e389c88e1e8352d761fb7d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_jinja_dialects-0.1.0-cp310-abi3-win_amd64.whl:

Publisher: publish.yml on bennypowers/tree-sitter-jinja-dialects

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_jinja_dialects-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_jinja_dialects-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 98396005531bdffee6f7dd6415ab57eda60a703e6ac72732171afbce5985847e
MD5 81cc368ce69779012d36e22cb77c951a
BLAKE2b-256 99cdb190a2fba882e633fed7270b117034ff932d0e25d84e2ac29cf22bd6a96b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_jinja_dialects-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl:

Publisher: publish.yml on bennypowers/tree-sitter-jinja-dialects

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_jinja_dialects-0.1.0-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_jinja_dialects-0.1.0-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bf0f961aefd9e346f5a727be9b131e38ad6519605b934aa1575dd703460c62a8
MD5 67b7ef98b0bf67910f01529ba96d3fb5
BLAKE2b-256 44d81f0b59f17bed332868ba34eb5340a93698073957c4db1f376b2062c60e4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_jinja_dialects-0.1.0-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on bennypowers/tree-sitter-jinja-dialects

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_jinja_dialects-0.1.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_jinja_dialects-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9daa970fb6e283d5cd15d832e46fb92464990fa6d9c9b8a1ab84e7d0258b166e
MD5 7fc6f131ea4f66870c0347ff5d704fcf
BLAKE2b-256 4ae19d010550f5755974564aeabab138236230c847039f73daa85d75d94c7b70

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_jinja_dialects-0.1.0-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: publish.yml on bennypowers/tree-sitter-jinja-dialects

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page