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.1.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.1-cp310-abi3-win_amd64.whl (31.1 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_jinja_dialects-0.1.1-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.1-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.1-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.1.tar.gz.

File metadata

File hashes

Hashes for tree_sitter_jinja_dialects-0.1.1.tar.gz
Algorithm Hash digest
SHA256 27d021277ca7ac71aafabcdbe1c7f272a8966b84464bc5827fc2e26a92c0ff06
MD5 4b45f94ee3bb6e413a5236b95e7f1133
BLAKE2b-256 ea54799d0e14d033ac116317b7c5aca62d7de2f8ed41f863cc1ef76ccd36c1e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_jinja_dialects-0.1.1.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.1-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_jinja_dialects-0.1.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 338839c1229d8d78099c5e9d967b4e8186e8a30b06195cc4afa2a861ca487c99
MD5 1ac274fdfd209a3b80d2301c8b6220c3
BLAKE2b-256 98a8084331241572d79a4af8da6293dfab058d43f46cfd7f11683c010ee1c54c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_jinja_dialects-0.1.1-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.1-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_jinja_dialects-0.1.1-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e7bff8077e4c74975128d357d63938e68b62b3683ffaa56138f4cc9172ea0e6c
MD5 097e0ce5a560757bbecad8ad1f958895
BLAKE2b-256 d61d4a1c6c7bf11d7b1cfdbda428d44effbcb014d8479cb6b9a2e679fa929801

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_jinja_dialects-0.1.1-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.1-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.1-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 86088edee16d7dee4679db24f8626091a799882dc7a7dbb5b6079ca73314951b
MD5 dea92d59d2a9ff685453843e681b484c
BLAKE2b-256 1a97442df61a1815598beb581ff617e4912708993724ad7be41aaf8ea926cf50

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_jinja_dialects-0.1.1-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.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_jinja_dialects-0.1.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e53c23593e10bbb3ffa39c8a958e4643d112c01e2ab82ba1b8488ce5ea8dcb89
MD5 91f2ef008ad4f2baad6fcb0a6915eac4
BLAKE2b-256 4f90c2e7b5359311fc8db812ce0d20b72e9fe78149602fbaf87c54121d601fda

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_jinja_dialects-0.1.1-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