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.1.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.1-cp39-abi3-win_arm64.whl (796.8 kB view details)

Uploaded CPython 3.9+Windows ARM64

tree_sitter_postgres-1.2.1-cp39-abi3-win_amd64.whl (779.2 kB view details)

Uploaded CPython 3.9+Windows x86-64

tree_sitter_postgres-1.2.1-cp39-abi3-musllinux_1_2_x86_64.whl (880.7 kB view details)

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

tree_sitter_postgres-1.2.1-cp39-abi3-musllinux_1_2_aarch64.whl (889.8 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_postgres-1.2.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (889.5 kB view details)

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

tree_sitter_postgres-1.2.1-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (882.2 kB view details)

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

tree_sitter_postgres-1.2.1-cp39-abi3-macosx_11_0_arm64.whl (898.0 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

tree_sitter_postgres-1.2.1-cp39-abi3-macosx_10_9_x86_64.whl (784.9 kB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_postgres-1.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 589323b92a3047ec78f2b6a0c00abd3e122a195d4f117358896f8967041d4c9c
MD5 8ba334a97a8befe75451f65ad5333730
BLAKE2b-256 9633ea25136587ed5985f9f61e9866846995bddc2d3fb498b15fe9742cf4c054

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.1-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 9210286d3df31c80173017054e121f238e58f11436e4ac472b3dd44b4246645e
MD5 cb89f774498e0c5a4cc6726efb58a169
BLAKE2b-256 0ea276cc8423d8884b9da3f5728701c51f4f5ef04ea3c3ff2d9ab036456b21e0

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6636e5a8ef227761cbc55be4b65d8eda36c9a7f50c5537bfb842878d850315d2
MD5 37b58eea8c140f1d970751164f6beb2c
BLAKE2b-256 81a2f0e584ca4a206fe58382c0f3c28d1aa6a50cc7b47d6f367cf39ee52a8128

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.1-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 eff508da274a2176309072fb8c6a22cbd892efeec58cdb506b519fa69b248391
MD5 9663d32ac2c0b1d395b5b4bd53255064
BLAKE2b-256 216640c2428bd3d904028fcb84eb883a6607b017afbc261ec784fc452b9b76b0

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.1-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 c410b4457086d50bc2a511826c7aff38437b0148bf84ba83e1f67cb172dab1d7
MD5 45000565c3aa20f90a53f77874b048c6
BLAKE2b-256 6b330ce52fc2224543b7b80b3aac2695bbf90f175bdd564ed8a62e2c2d5fbe98

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.1-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ca6e8c5c4169bbaa3ddebffb5368ba4ee289e2b49f50fe92526b4b9fcf62c797
MD5 86b247ce2b5f53a0361a0daf05a7634a
BLAKE2b-256 151dd66ac2226a47918f78df1b5a887887d207e392754b7540cf96df01f218fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.2.1-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.1-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.1-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 0954c4ebbd744e7d95e6d66db218825b7549866eab8505bff45d918152f22938
MD5 a437df71a21d66240155f90d6d3a31dd
BLAKE2b-256 a751ac82fef138f583b32c30f0a5f99f1f24520632e6f242a7d4ecee93f76050

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bc5ca95c1ef6fee95bb6d2ade12b8e337e19f008759253987958ed6801f7ff21
MD5 f3d9717175e60a24687ad0f15b09b7c0
BLAKE2b-256 bcf9606370ade13c4726bd4eb518352064dda222fc221d30b218c8df56fa1dca

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.2.1-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6f182d625c07d13f10edf209fe85ec0ff0894f3e0f045816d400534db0acc394
MD5 54e4e7a4755682955da261cd261603a5
BLAKE2b-256 f5534e142e235b186532aa86199de5c8c20bbf2148a003881971ed7eae34e784

See more details on using hashes here.

Provenance

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