Skip to main content

A Python parser for BlackLab Corpus Query Language

Documentation PyPI - Python Version codecov Interrogate coverage License

A full-coverage Python parser for the BlackLab Corpus Query Language (BCQL) that converts query strings into a Pydantic v2 AST (Abstract Syntax Tree) with lossless round-trip reconstruction and structured error reporting.

To get started, you can check out:

Features

  • Complete BCQL coverage: token queries, sequences, repetitions, spans, lookarounds, captures, global constraints, relations, alignments, and built-in functions.
  • Immutable Pydantic v2 AST: every node is a frozen BaseModel subclass with a node_type discriminator, making inspection and pattern matching straightforward.
  • Lossless BCQL round-trip: to_bcql() reproduces the original query (preserving shorthand forms, quote characters, sensitivity flags, etc.).
  • Position-aware syntax errors: BCQLSyntaxError carries the original query, the 0-based offset, and a caret-annotated message: ready to forward to a user or LLM.
  • Optional semantic validation: a CorpusSpec describes which annotations, span tags, alignment fields, and dependency relations your corpus supports. Pass it as parse(query, spec=spec) to catch typos and unsupported features before they reach the corpus. See the tagset validation guide.
  • Zero runtime dependencies beyond Pydantic.

Installation

pip install bcql_py

Or with uv:

uv add bcql_py

Try the demo

A small Gradio app under app/ lets you paste a BCQL query, pick or build a CorpusSpec, and inspect parse + validation results. The hosted demo runs on Hugging Face Spaces at BramVanroy/bcql_py_validation.

To run it locally:

uv sync --group app
uv run python app/app.py

Development

Clone and set up the project:

git clone https://github.com/BramVanroy/bcql_py.git
cd bcql_py
uv sync --dev

Enable pre-commit hooks:

uv run pre-commit install

After installation, hooks run automatically on every git commit. We do style chechking with ruff and type-checking with mypy. You can also run them manually across the whole repo:

uv run pre-commit run --all-files

To work on documentation locally:

make serve-docs

This rebuilds a fresh local mike preview before serving it, so you do not end up testing stale versioned docs. By default it serves a local 0.3.0 version and latest alias from a temporary docs branch. You can override those values when needed, for example:

DOCS_VERSION=0.4.0 DOCS_SOURCE_REF=v0.4.0 make serve-docs

Open both /latest/ and /<version>/ while testing. If you are checking the GitHub source links as well, set DOCS_SOURCE_REF to the release tag you want to emulate.

You can/should run tests before pushing to the remote, although a Github workflow will run those anyway on push. To run them locally:

make test

ANTLR to generate the needed tools

BlackLab uses ANTLR to generate the parser/lexer in Java based on a g4 file. We could similarly generate Python files. However, after trying it out, I find the files obfuscated and unclear and I'm not fond of requiring an extra external (Java-based) library. That is not a slight to ANTLR; I am simply not familiar with the tool: I am sure it is incredibly powerful and useful if you know how to use it. To keep a clearer view of this library I therefore strive to make a Python-native implementation that is true to spec. It's also just a fun project that I do not wish to "automate away" (though I might regret that later). At a later time (TODO) I might implement functionality to cross-validate our implementation with the generated ANTLR parser and lexer. For now I will be satisfied with high coverage testing. In case of doubt I have followed the Bcql.g4 file.

If you'd like to try the ANTLR route yourself, you can try it as follows:

  1. Install requirements (not included in our pyproject.toml file, you'll need to download these yourself!)

    uv pip install requests antlr4-tools antlr4-python3-runtime
    
  2. Download the BlackLab G4 definition from GitHub. You can optionally specify a --branch or --tag, defaults to --branch dev.

    uv run python scripts/get_bcql_g4.py
    # Saved to parser/Bcql.g4
    cd parser/
    
  3. Run ANTLR (you can update -v to the latest version if needed)

    antlr4 -v 4.13.2 -Dlanguage=Python3 Bcql.g4
    

Acknowledgments

Metadata

Release files for bcql-py 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bcql-py 0.4.1
File Size Uploaded Provenance Actions
bcql_py-0.4.1.tar.gz 262.7 kB With provenance Details for bcql_py-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bcql-py 0.4.1
File Interpreter ABI Platform Actions
bcql_py-0.4.1-py3-none-any.whl Python 3 none any Details for bcql_py-0.4.1-py3-none-any.whl

Total release size: 328.1 kB

Release files / bcql_py-0.4.1.tar.gz

Download URL bcql_py-0.4.1.tar.gz
Size 262.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e8739c775add7b31ba29120aa90926d2068e8691b24368bdaad0eac447b61a64
BLAKE2b-256 checksum
How to use checksums
3253d742f593e863b0989aa9bf42072378f8e34e790839cb159a20a642e381c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Release files / bcql_py-0.4.1-py3-none-any.whl

Download URL bcql_py-0.4.1-py3-none-any.whl
Size 65.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
47649a4b60032f1970d42bfb2b6093d83d4a04495271fe15fc0a70ad2e127f83
BLAKE2b-256 checksum
How to use checksums
31ff672d6c1d4413c15a874c7dd2fff9b91b1df26b3fbef65376b607bcee0a03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Security for bcql-py 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for bcql_py-0.4.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page