Skip to main content

GLSL grammar for tree-sitter

Project description

tree-sitter-glsl

A specification-compliant GLSL and ESSL parser for tree-sitter covering the full GLSL 4.60 core grammar, all current Khronos extensions, and common real-world macro patterns.

Features

  • Full GLSL 4.60 and ESSL 3.20 grammar
  • All Khronos extension keywords, types, built-in functions, and variables
  • Structured preprocessor parsing (#define, #if/#ifdef/#elif/#else)
  • Macro expansion support for real-world shaders (COMPAT_PRECISION, MYTYPE(args))
  • Multilingual header support (#ifdef __cplusplus language injection)
  • Extension-defined grammar rules (demote, terminateInvocation)

Queries

File Purpose
highlights.scm Syntax highlighting with built-in function/variable/constant recognition
injections.scm Language injection for #ifdef __cplusplus / __STDC__ guards
locals.scm Scope-aware local variable tracking
tags.scm Symbol indexing for code navigation
- -
version-tags.scm #version-dependent construct validation
constructor-heuristics.scm Opt-in heuristic for capitalized constructor calls

version-tags.scm and constructor-heuristics.scm are custom selectors, that might be useful in some cases, but you likely don't need them.

Usage

Node.js

const Parser = require('tree-sitter');
const GLSL = require('tree-sitter-glsl-spec');

const parser = new Parser();
parser.setLanguage(GLSL);
const tree = parser.parse('void main() { gl_FragColor = vec4(1.0); }');

Rust

let mut parser = tree_sitter::Parser::new();
parser.set_language(&tree_sitter_glsl_spec::LANGUAGE.into()).unwrap();
let tree = parser.parse("void main() { gl_FragColor = vec4(1.0); }", None).unwrap();

Python

import tree_sitter_glsl_spec as glsl
from tree_sitter import Parser

parser = Parser(glsl.language())
tree = parser.parse(b"void main() { gl_FragColor = vec4(1.0); }")

Development

npm install                                   # Install dependencies
npx tree-sitter generate                      # Regenerate parser from grammar.js
npx tree-sitter test                          # Run corpus tests
npx eslint                                    # Lint
npx tsc --noEmit                              # Type check
node scripts/audit_node_types.js --coverage   # Grammar coverage analysis

The specification is included as a git submodule. Run git submodule update --init to fetch it for spec-scraping scripts.

References

Background

This project began as a fork of tree-sitter-glsl by Stephan Seitz, which inherited its grammar from tree-sitter-c. After running into too many quirks from the C grammar inheritance, I rewrote the grammar from scratch against the GLSL 4.60 specification.

Why not inherit from tree-sitter-c?

GLSL's syntax is C-like but substantially simpler. A dedicated grammar provides:

  • Fewer ambiguities — 1 GLR conflict (without macros) vs C's 16. No pointers, no K&R functions, no multi-word types, no sizeof/typeof, no casts.
  • Flat expression trees — identifiers and literals appear directly in the tree without the unary_expression > postfix_expression > primary_expression wrapper chain that a C-inherited grammar produces.
  • GLSL-native nodes — layout qualifiers with name:/value: fields, precision qualifiers, interface blocks, demote/terminateInvocation extension statements.
  • Specification fidelity — every BNF production from the spec exists as a (hidden or visible) rule, documented with the original grammar notation.

License

This project is licensed under the MIT license.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

tree_sitter_glsl_spec-1.0.0-cp38-abi3-win_amd64.whl (100.2 kB view details)

Uploaded CPython 3.8+Windows x86-64

tree_sitter_glsl_spec-1.0.0-cp38-abi3-win32.whl (92.4 kB view details)

Uploaded CPython 3.8+Windows x86

tree_sitter_glsl_spec-1.0.0-cp38-abi3-musllinux_1_2_x86_64.whl (137.1 kB view details)

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

tree_sitter_glsl_spec-1.0.0-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (137.1 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

tree_sitter_glsl_spec-1.0.0-cp38-abi3-macosx_11_0_arm64.whl (90.3 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file tree_sitter_glsl_spec-1.0.0-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_glsl_spec-1.0.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 a33b2499b30e43c6fca141daf621b174f5ec5f6d46b7c5d83029eed2ba34318f
MD5 176b267aa6f9150cfc010d00b7b678bb
BLAKE2b-256 db49d5d1b86029c56a6d4573013d077715227c67647c266a9e2e68766ae5d972

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_glsl_spec-1.0.0-cp38-abi3-win_amd64.whl:

Publisher: publish-pypi.yml on Caellian/tree-sitter-glsl

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_glsl_spec-1.0.0-cp38-abi3-win32.whl.

File metadata

File hashes

Hashes for tree_sitter_glsl_spec-1.0.0-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 ea40b699843538901d85b8f41bb85794036e2a2eb76c470b8d0745d03151a9f4
MD5 71f8f62b1741998b95a205ccbca1ba76
BLAKE2b-256 739bac7489b6723ca4443d3dc7588d5501c7978499b380e06489d31b6dbb834f

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_glsl_spec-1.0.0-cp38-abi3-win32.whl:

Publisher: publish-pypi.yml on Caellian/tree-sitter-glsl

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_glsl_spec-1.0.0-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_glsl_spec-1.0.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 24fef4cf8cf36225a45e28bd67d2c81988e62f6687517d52963c2daca4bd75cd
MD5 eb97b9d205c02635e5bb1cb329fcadaa
BLAKE2b-256 88dc5a5d38d9439a31f62adadfb138de51e6119c0a6bb4ff089b67b93b04befc

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_glsl_spec-1.0.0-cp38-abi3-musllinux_1_2_x86_64.whl:

Publisher: publish-pypi.yml on Caellian/tree-sitter-glsl

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_glsl_spec-1.0.0-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_glsl_spec-1.0.0-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 f1b4a2fd9f509b531feb2d7f4a463924cecfb76153bff940743daa233139e690
MD5 20b335bf6075ac4f6cc0c85ee080a04c
BLAKE2b-256 0e8cdac15fcb68384a7a77406f10df5069ab92c4b6e33b8b6e55843ab3a52b5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_glsl_spec-1.0.0-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publish-pypi.yml on Caellian/tree-sitter-glsl

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_glsl_spec-1.0.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_glsl_spec-1.0.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9db41331bd0fed94df326583fc027b662410fee1d917767fe71f1db1fe493574
MD5 94c8c00943c48eec0b3772f44e4184ec
BLAKE2b-256 2659698d28e8b7b4d256e5fa70f87aef4329ee8b355e6b0171ccad654c2705ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_glsl_spec-1.0.0-cp38-abi3-macosx_11_0_arm64.whl:

Publisher: publish-pypi.yml on Caellian/tree-sitter-glsl

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