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.2.4.tar.gz (6.0 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.2.4-cp39-abi3-win_arm64.whl (799.2 kB view details)

Uploaded CPython 3.9+Windows ARM64

tree_sitter_postgres-1.2.4-cp39-abi3-win_amd64.whl (781.5 kB view details)

Uploaded CPython 3.9+Windows x86-64

tree_sitter_postgres-1.2.4-cp39-abi3-musllinux_1_2_x86_64.whl (883.7 kB view details)

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

tree_sitter_postgres-1.2.4-cp39-abi3-musllinux_1_2_aarch64.whl (893.6 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_postgres-1.2.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (892.6 kB view details)

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

tree_sitter_postgres-1.2.4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (885.4 kB view details)

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

tree_sitter_postgres-1.2.4-cp39-abi3-macosx_11_0_arm64.whl (900.9 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

tree_sitter_postgres-1.2.4-cp39-abi3-macosx_10_9_x86_64.whl (787.5 kB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_postgres-1.2.4.tar.gz
  • Upload date:
  • Size: 6.0 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.2.4.tar.gz
Algorithm Hash digest
SHA256 887ab99369e79af58f6f38b129b9640b94629f22c0e70ee2517049600f41dd38
MD5 46f478aa9c188117ab18ad4da28db6af
BLAKE2b-256 b15f40418ff505f3b531f1809aabbf5154a3cf3a9ed8106e6b3499eb698ad5ec

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.2.4.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.2.4-cp39-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.4-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 ab586f969fb0dba648de49edc8b6473affb2aba8e93e425d39260b059cc4cbd3
MD5 470b4a3dde2c4f54e52cf3c992c8e478
BLAKE2b-256 ea2a898aca7aec34e3ece487010a15fae96830364955cb8b4ce9eec1df7c51f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.2.4-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.2.4-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.4-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0cf15e61fc1cb8d809ae04de81d8c04f77486fb85e8e871826ab0ffd4f3c6bf7
MD5 88d5d25dac2020470ed0213e0f3cd931
BLAKE2b-256 ef67c7711e529914c465501b15b6e95e7c991e31bee6144a09cbd08917b0bf92

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.2.4-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.2.4-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.4-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6c27b0bbb7fd666ce6054ab42e73557b43d1a7f2da432a03ee16d920b727e35c
MD5 9d32df4b44d5e86a91701d5dbfca7347
BLAKE2b-256 08ecbc9ef4a8dc88bfc4a1b0a242d2cce379a009bdf29b8dbe8e870c67b50d4b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.2.4-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.2.4-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.4-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 33a80e82f349b3d7c996aaa86fd3e08add3a30a5d19954574b2343ca5e621be1
MD5 d2d53b66dc8ab00b50836f24e11e4138
BLAKE2b-256 35ddd2947015b586ad4044f5f7115ce13bdb25400e1dc78034833bb118acd97d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.2.4-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.2.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f37ba332618cc41670200a24e440df93a8f3cd189cca00e9f11d25a9bddae7ac
MD5 390c98008b37483e1f8d5e0797c08910
BLAKE2b-256 47c24cdac06437dc00f741d87ede6fcb08d297a5a33095e94e18318db23dcc01

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.2.4-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.2.4-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.2.4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 9354d438477a84bc105c9a022689f0f6e9a1dbd57ccdcc709bf7058bdca7e36c
MD5 7376bf10802943eeb6979674d883dc51
BLAKE2b-256 3e09769a97c9375b6da7b7a9f4e1946df9cf741c14e6a8d907eff0617ef946d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.2.4-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.2.4-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.4-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6be1e8f6c34e23ecb9a3363f56cadba4517191ad22be2ef789ffff27fbe6f50e
MD5 60d7fbdcda2bd5d7ad1da70524fc8f3c
BLAKE2b-256 3355a6fb9e6219b2322a15b31f1066f2bdcd5bd7b09a1c743422b7abf4e18895

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.2.4-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.2.4-cp39-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.4-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7c4cb1230acac921d50f5f9fa62b8b9ee7ad8b8cb1371b12ac43a862e4d7aa1b
MD5 dfdf3066aab9be643df6531771549ca3
BLAKE2b-256 ad5c9d82b495c143969807b9727336491c31eb867f7f792f366e99d57f95dc58

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.2.4-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