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:

# Default: ~/Source/gmr/postgres
node script/generate-grammar.js

# Or specify the path
node script/generate-grammar.js /path/to/postgres

# Then build the parser
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.0.0.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.0.0-cp39-abi3-win_arm64.whl (746.6 kB view details)

Uploaded CPython 3.9+Windows ARM64

tree_sitter_postgres-1.0.0-cp39-abi3-win_amd64.whl (763.2 kB view details)

Uploaded CPython 3.9+Windows x86-64

tree_sitter_postgres-1.0.0-cp39-abi3-musllinux_1_2_x86_64.whl (822.0 kB view details)

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

tree_sitter_postgres-1.0.0-cp39-abi3-musllinux_1_2_aarch64.whl (831.4 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_postgres-1.0.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (831.0 kB view details)

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

tree_sitter_postgres-1.0.0-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (824.3 kB view details)

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

tree_sitter_postgres-1.0.0-cp39-abi3-macosx_11_0_arm64.whl (840.2 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

tree_sitter_postgres-1.0.0-cp39-abi3-macosx_10_9_x86_64.whl (732.8 kB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for tree_sitter_postgres-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a3cc024002390df60975bb4b3144dd208d388eac37d64d91701c67fef225ceb2
MD5 852542297889c0c979dd861a393a08b1
BLAKE2b-256 42393a0e82921f7299ee6dbf34c4585e2429041aa5e2152c1d555fc0a9cf1b0e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.0.0-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 f4fec1080aa5428a3432a6902d207a629cd1a31b67acbb8271cb15eb49a94f56
MD5 874f27350463d72523e899fc852ea82b
BLAKE2b-256 1e67d5612bf562ccb375105008be52f6bfc1cdcadf9d78aea3afcc736aaed26a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.0.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f0c14deb85b8f222b9a6a41253ac741aa4cf59e291eecc4b0356fa2b3014612a
MD5 6769b7f4b98c7de788f426cfefd5af4a
BLAKE2b-256 77568b7ceb0f8d5e826d018750019af9c353b42e4b8add43e94f7abee10fe1b1

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.0.0-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 202575bfbacc574d6b2e158e18fbd28e8bf7b782779739d61c980e8110b5006f
MD5 b47286ad0362ffa9f0e9025bbd73b984
BLAKE2b-256 646b8567d32ca85573876680a9ef3657d09b9a80f72cedd4aeb2030a23589923

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.0.0-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 17e4e10183af568c078d06d788c25cde5f7c6842b268582c7f092ac4a5936697
MD5 6baf9a9b22f016b03ae43a57c587a968
BLAKE2b-256 80aaac028f5438af95c593e0c57b32b6ab55fc6857c553a0309fda8c5fcc1f3b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.0.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a8b319d0db5d1264f4b8faed793f272cfb1bab197d6392d867c9d351a7acdce8
MD5 38fd1153a5c463afe4db50fdea33d427
BLAKE2b-256 2a22b0857096a710a8f1845c19049c233272d96dd16c336dced14e346ba56e6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_postgres-1.0.0-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.0.0-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.0.0-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 687b6e3593244543ee790ed450253b38b250e2291c6581891d1a465001f221fd
MD5 b9077bf7516497c834e4ced45d865f99
BLAKE2b-256 74a815667558312fcb7f1b81f837061bb66d816fc05d34b84be9062ca6475849

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.0.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 511d0019ae171fd5161dd62749df56f932fc82cd1afcbc072c1a3e798aac5757
MD5 8ed375d25323f8106d69255d05a2fc28
BLAKE2b-256 d88b3139236a1dbef631b217c8cbdbf3e893238ebe59c32d48c330f4dd0bda69

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_postgres-1.0.0-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 55362bbfd9d2d631433e3c911eff03e5af6af1c0d042bef445f92f25ecfc822e
MD5 4a70c41ed47684189b5263e67f706cbb
BLAKE2b-256 94d861225d1d705c5835428a7fff7aaa6851b1a17d1bc03aecabec8e71478309

See more details on using hashes here.

Provenance

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