Skip to main content

Postgres grammar for tree-sitter

Project description

tree-sitter-postgres

A tree-sitter grammar for PostgreSQL, generated directly from PostgreSQL's Bison grammar (gram.y) and keyword list (kwlist.h).

Features

  • Current as of PostgreSQL 18 (generated from REL_18_3)
  • 727 grammar rules covering the full PostgreSQL SQL syntax
  • 494 case-insensitive keywords across all four PG keyword categories
  • Correct operator precedence1 + 2 * 3 parses as 1 + (2 * 3)
  • PL/pgSQL support via a separate grammar with language injection
  • Generated, not hand-written — regenerate for any PostgreSQL version

Quick start

npm install
cd postgres && npx tree-sitter generate && npx tree-sitter test

Regenerating from PostgreSQL source

The grammar is generated from a local PostgreSQL checkout. Set PG_SOURCE_DIR to point at your PostgreSQL source tree:

export PG_SOURCE_DIR=/path/to/postgres

# Using just (recommended)
just generate

# Or run the script directly
node script/generate-grammar.js "$PG_SOURCE_DIR"
cd postgres && npx tree-sitter generate

Input files

File Source
src/backend/parser/gram.y Bison grammar (733 rules, 3236 alternatives)
src/include/parser/kwlist.h Keyword definitions (494 keywords)

Generator scripts

Script Purpose
script/generate-grammar.js Orchestrator — reads PG source, writes postgres/grammar.js
script/parse-gram-y.js Parses Bison grammar: rules, terminals, precedence, %prec annotations
script/parse-kwlist.js Parses keyword list into categories
script/codegen.js Generates tree-sitter grammar with precedence and optional-rule handling
postgres/harvest-conflicts.sh Iteratively discovers GLR conflicts needed by tree-sitter

Repository structure

postgres/               PostgreSQL SQL grammar
  grammar.js            Generated tree-sitter grammar
  src/                  Generated parser (C)
  test/corpus/          Test cases (35 tests)
  known-conflicts.json  GLR conflict pairs

plpgsql/                PL/pgSQL grammar
  grammar.js            Hand-written tree-sitter grammar
  src/scanner.c         External scanner for dollar-quoting and keywords
  test/corpus/          Test cases
  queries/              Highlights and injection queries

script/                 Shared generator code
  generate-grammar.js   SQL grammar orchestrator
  parse-gram-y.js       Bison parser
  parse-kwlist.js       Keyword parser
  codegen.js            Grammar code generator

bindings/               Language bindings (Node, Rust, Python, Go, Swift, C)

Design notes

Empty rule handling

Bison's /* EMPTY */ alternatives cannot be directly translated — tree-sitter forbids non-start rules that match the empty string. The generator propagates optionality upward via a fixpoint loop and wraps references with optional() at call sites.

Operator precedence

Binary operators are split into a separate a_expr_prec rule resolved by static precedence (no GLR), while complex patterns (IS, IN, BETWEEN, LIKE, subquery operators) stay in a_expr with GLR conflict resolution. Both prec.left/prec.right (generation-time) and prec.dynamic (runtime) are emitted.

PL/pgSQL

PL/pgSQL is implemented as a separate hand-written grammar in plpgsql/ with an external scanner for dollar-quoting and context-sensitive keywords. SQL expressions and statements within PL/pgSQL blocks are delegated to the postgres grammar via tree-sitter language injection (plpgsql/queries/injections.scm).

License

BSD 3-Clause

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_postgres-1.1.6.tar.gz (5.8 MB view details)

Uploaded Source

Built Distributions

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

tree_sitter_postgres-1.1.6-cp39-abi3-win_arm64.whl (770.7 kB view details)

Uploaded CPython 3.9+Windows ARM64

tree_sitter_postgres-1.1.6-cp39-abi3-win_amd64.whl (788.7 kB view details)

Uploaded CPython 3.9+Windows x86-64

tree_sitter_postgres-1.1.6-cp39-abi3-musllinux_1_2_x86_64.whl (850.1 kB view details)

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

tree_sitter_postgres-1.1.6-cp39-abi3-musllinux_1_2_aarch64.whl (859.3 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_postgres-1.1.6-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (858.8 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

tree_sitter_postgres-1.1.6-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (851.4 kB view details)

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

tree_sitter_postgres-1.1.6-cp39-abi3-macosx_11_0_arm64.whl (868.5 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

tree_sitter_postgres-1.1.6-cp39-abi3-macosx_10_9_x86_64.whl (758.0 kB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

Details for the file tree_sitter_postgres-1.1.6.tar.gz.

File metadata

  • Download URL: tree_sitter_postgres-1.1.6.tar.gz
  • Upload date:
  • Size: 5.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tree_sitter_postgres-1.1.6.tar.gz
Algorithm Hash digest
SHA256 2a90aef92d52c69ba222caa7d6633ae865af44bcf1b96d226eac9a0bd1361052
MD5 c3d1c23bd4ca92465c5fbcfcd7dfb97f
BLAKE2b-256 b7a4c0e57830a0691355ba4b11d31527cfe294c30608a21724b66e44e92ac77b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.6.tar.gz:

Publisher: publish.yml on gmr/tree-sitter-postgres

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_postgres-1.1.6-cp39-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.6-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 816a0c26978c18b23a08ae0039a100ee13d73232436e6bdddf3111a8d1265a83
MD5 ffd8bc9c8b2239f63ccf40365565baaf
BLAKE2b-256 907bd1e0997e66f21be6395901af2e143ee9a84a7f7a6911640e16ae206fc295

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.6-cp39-abi3-win_arm64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

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_postgres-1.1.6-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.6-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ee49665701855d0e607e12f65b6399fb830b870bca7e734bb0348af35e29cc28
MD5 9faca1d20db45e4ed5456e8e1e049fa6
BLAKE2b-256 47d6f6f99e7b6fa2b34248ae5aee247dfde835283b99097b5bee4a0c5299c401

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.6-cp39-abi3-win_amd64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

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_postgres-1.1.6-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.6-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d87b47ce49425a76b73195a1ac438b1d5495b815f324dd8391ed0be46cf168cc
MD5 30a7e67f43cc5a61b36364383c3542f9
BLAKE2b-256 1289b0a4cc689b3605d6186ea4398393e2c896446bd93960a9d0fa15b01f8a85

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.6-cp39-abi3-musllinux_1_2_x86_64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

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_postgres-1.1.6-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.6-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 6b8bde3e9783cc6eb9de4892577ee910267875f9eb5e44859aca7b55133ce28c
MD5 72ffd4f93673da11c944673c0184bfce
BLAKE2b-256 22dd07b663d55c92f1503ef9c25679233f67c6e7e76b64c1833493522e7dc0d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.6-cp39-abi3-musllinux_1_2_aarch64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

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_postgres-1.1.6-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.6-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f936c8bc0d2aebd9c08208c054cdb59a5aa9eb3b27c58c49a39f704bf697f1ad
MD5 43e3102f0afc40a4be1bc0713837b48d
BLAKE2b-256 383798ef049ffa684ce5e17303f4f95355ba032818d35a960e7f7aac98d2fe2c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.6-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

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_postgres-1.1.6-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.6-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 35ddb7fefeec391ae97fb1ed475b782ba11743a7e1b8d51548dca4cb71acbe05
MD5 c14a08cb5fff35d1350aa5b888fbf503
BLAKE2b-256 5c483fb927ef3cfd1c3112c317bd71127d4c38f3926be782c833f80e5dbde1b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.6-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

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_postgres-1.1.6-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.6-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 82fd6033c0141da6b01bd5bb8629429cdd3ffadf4102aafa873b93d94d741092
MD5 04a5d8654605d884d1ec9eafdef7ca06
BLAKE2b-256 c0a0333316a726af56c0bfc1ab14e34c1fe1be77fd1cb4107a0f37df411afce0

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.6-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

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_postgres-1.1.6-cp39-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.6-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7474a28b4a197eece0d7e1f686cefafe242bc1b5ffc208be974b2bd10050fdc4
MD5 ffa94489dd5e6d5c6a613d8d9c4cad57
BLAKE2b-256 8102b5ee777b3ac599d965a76ca01de1ce9937505aac5e19903e457f35e1f78f

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.6-cp39-abi3-macosx_10_9_x86_64.whl:

Publisher: publish.yml on gmr/tree-sitter-postgres

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