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.4.tar.gz (5.6 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.4-cp39-abi3-win_arm64.whl (747.8 kB view details)

Uploaded CPython 3.9+Windows ARM64

tree_sitter_postgres-1.1.4-cp39-abi3-win_amd64.whl (764.4 kB view details)

Uploaded CPython 3.9+Windows x86-64

tree_sitter_postgres-1.1.4-cp39-abi3-musllinux_1_2_x86_64.whl (823.3 kB view details)

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

tree_sitter_postgres-1.1.4-cp39-abi3-musllinux_1_2_aarch64.whl (832.8 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_postgres-1.1.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (831.8 kB view details)

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

tree_sitter_postgres-1.1.4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (824.7 kB view details)

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

tree_sitter_postgres-1.1.4-cp39-abi3-macosx_11_0_arm64.whl (841.3 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

tree_sitter_postgres-1.1.4-cp39-abi3-macosx_10_9_x86_64.whl (733.8 kB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_postgres-1.1.4.tar.gz
  • Upload date:
  • Size: 5.6 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.4.tar.gz
Algorithm Hash digest
SHA256 0867d87f01292ac6ecec783da1f975f48750dada6d83d8970ca962633721f28b
MD5 353a2fce7870a1843e67284c25234956
BLAKE2b-256 ffbd78e4c604f05ab78dcb4f99800d0612ad562a7300ae729e7436c525eb53be

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.4-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 cc2b64b253eef568b101ceb42f9ce6056cf930ea87ee01e4a35115ed5795cde2
MD5 4f0119852d6f6f0d3ba2602a5391ec05
BLAKE2b-256 b395a9a4ef19af920c8c20af22772c58711e76a9d2ae9773d6d33807d1064734

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.4-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 e139259c2440fec543f6d14bf9e8414b18a0c05275ae681cde2af7a1cb12ac86
MD5 e11c4fffe33dd57176af3cfad5b80f31
BLAKE2b-256 0f8aec0e50d8d050c9bef439a201e4634a8683765929dfd63a79084eebaedf13

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.4-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 dca4eec216fdc50458ca0766859b2fdd8dcd1a12bf74e7117eda23f65247f2b6
MD5 ecce5f0a1f2d5ec0253aff3bea3fe146
BLAKE2b-256 c28dac6c5c747843520c98318eb688c2f94fd5681aef394200092913c638bf75

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.4-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 e85d3eca33c74a50f693dd7cd3a2779fae7102d5ffc2821aea5906cce7e18e6c
MD5 578213571a1e07c7e353a446fc3ff228
BLAKE2b-256 594fca2d405e4b480e969ad0a179ee26ea521b6cb1c63247c8f5172649c2da05

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4c96fee861764d97d00c126261b273050a4aad0dcb7e7be20ec5762b3fb4d991
MD5 0e9e8e7495e1cf1655f7f08c7f8b5637
BLAKE2b-256 6fe37ce355b3e0fe015445a4997e140ed1a09961b68655120eba2e7c06167e4b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.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.1.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.1.4-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 90ce231647e5501b9a28e94a958aa32ce18d9b412c9853484021334d204bc321
MD5 e46613bb179dbe303a4e5ad64e009b6e
BLAKE2b-256 ec1e11136fabfcb4ee11684a618b9320babd7be69c64c7d4dbc0af18325e5eb2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.4-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8adbc7334019bd114d726ce34a93c8e23d7a15878ca2d0ee87728e2632a01781
MD5 c5c14592b7fe0bef1859f7d5593e8d80
BLAKE2b-256 1b7a982c1ac0006bfc4985cd9e711cd2b64dd6ff55ec8f40a68ec41f97612c10

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.4-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b63eef6f0c626ea1940ec2e98d69617b5cc540199ba4338c771d1d4057b001a0
MD5 be313b24dc5f48d3cb39333563d9953a
BLAKE2b-256 bfc3f07df1e7ff007e125f67476a2a52961d58dbe172c3fe068c8344a558de74

See more details on using hashes here.

Provenance

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