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

Uploaded CPython 3.9+Windows ARM64

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

Uploaded CPython 3.9+Windows x86-64

tree_sitter_postgres-1.1.3-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.3-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.3-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.3-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.3-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.3-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.3.tar.gz.

File metadata

  • Download URL: tree_sitter_postgres-1.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 1715d42e053f1ccb03d777ef6d44ab60261edf95f21d96cbc3b1f8fc437e686c
MD5 cbd28028c2251a69b0577b4af5c2bc46
BLAKE2b-256 65290c6dec81c7efce36819aa7bf425522af25be4c2a2c03623aee108d4910c4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.3-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 4ea6fcffdda58892b4653002b650a33be28b444a5ca96dd57b9b36aa54db947d
MD5 70390992992886cd5ee5b3fcf94e7f05
BLAKE2b-256 d2e81c32eba21b893d334f5cfe82d54269c22631af8b2f7bd12a0383fe96e1c8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.3-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 5faa1b5780531e5274b5189628ec32d235092ab5dead526279b66300310bf2e6
MD5 81ce19c7b2da547877f01206a17a06a7
BLAKE2b-256 a5ee2ae1beb1bb278e709a70dfd21bbc735b5d91745327cfcba5bd34277f2996

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.3-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 545e82cab324d47675fb168a5f06bace0b1c811445c19a706336c5cf9f2eea50
MD5 dd4fbe59289daab1f5d092fe8a84c9a1
BLAKE2b-256 1528d9d32100a7e41e8d212e89c76de7f3b49d375c7516be412b31f61c620548

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.3-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 1bbe7b9a466364e9402841e572842a86c9422a4835bb7c1465134a53f1c45549
MD5 fd40874d8170269cea27b1d6e64153a1
BLAKE2b-256 5c37e60a19a83f6e37ac1ff044277da6e5305b4ac8ba65cfe569b5a89f63303e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.3-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a37658ed101c979cddbf243940a6c14896d33234649aec7626350db113baed93
MD5 e68ed5a836f08f37b9ee5ed77dbc7b6a
BLAKE2b-256 09a35c98945736acc3e3fcfc6d61348d161c27cde0d4af3ac89e71d0fb9ba642

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.1.3-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.3-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.3-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 5a4e91f628bd0c593d8279e55376492362fc49fdbf0f3e1c0ac6ee9180fc78c1
MD5 e41728d8c0aa0cb0b67536c4ec08d8a1
BLAKE2b-256 faef1febe6a8c6dcd8beb3022e4e2039a7ab6eab250d7678c2fe31148ce41085

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.3-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e322bed0e68ae034038437cdaa07a889e7ff9cbb69c289301a5a182d7dfe9a4a
MD5 57853bb523ebb329117a423e94f00b09
BLAKE2b-256 0a77b0f6c4ea749d9a66c5a54e52d0f9fb37338bba375b429256efc62b23edc8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.1.3-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 11612707eb07d7ba44288f642a8fe2e746e094629921347d866126055d177e03
MD5 613f460883141f92156f17fed35a9289
BLAKE2b-256 781b605f89bc1a3f659ba5a38fc87ff6fabdab24280bc09bc241df07d35443c9

See more details on using hashes here.

Provenance

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