Skip to main content

Convert ABNF grammars to Python regular expressions.

Project description

Check on push to main Test coverage PyPI - version PyPI - Python Version

The program abnf-to-regexp converts augmented Backus-Naur form (ABNF) to a regular expression.

Motivation

For a lot of string matching problems, it is easier to maintain an ABNF grammar instead of a regular expression. However, many programming languages do not provide parsing and matching of ABNFs in their standard libraries. This tool allows you to write your grammars in ABNF and convert it to a regular expression which you then include in your source code.

It is based on abnf Python module, which is used to parse the ABNFs.

After the parsing, we apply a series of optimizations to make the regular expression a bit more readable. For example, the alternations of character classes are taken together to form a single character class.

--help

usage: abnf-to-regexp [-h] -i INPUT [-o OUTPUT]

Convert ABNF grammars to Python regular expressions.

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT, --input INPUT
                        path to the ABNF file
  -o OUTPUT, --output OUTPUT
                        path to the file where regular expression is stored;
                        if not specified, writes to STDOUT

Example Conversion

Please see tests/data/rfc3987/grammar.abnf for an example grammar.

The corresponding generated regular expression is stored at tests/data/rfc3987/expected.out.

Installation

You can install the tool with pip in your virtual environment:

pip3 install abnf-to-regexp

Development

  • Check out the repository.

  • In the repository root, create the virtual environment:

python3 -m venv venv3
  • Activate the virtual environment (in this case, on Linux):

source venv3/bin/activate
  • Install the development dependencies:

pip3 install -e .[dev]
  • Run the pre-commit checks:

python precommit.py

Versioning

We follow Semantic Versioning. The version X.Y.Z indicates:

  • X is the major version (backward-incompatible),

  • Y is the minor version (backward-compatible), and

  • Z is the patch version (backward-compatible bug fix).

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

abnf-to-regexp-1.0.0.tar.gz (9.4 kB view details)

Uploaded Source

File details

Details for the file abnf-to-regexp-1.0.0.tar.gz.

File metadata

  • Download URL: abnf-to-regexp-1.0.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.2.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.5

File hashes

Hashes for abnf-to-regexp-1.0.0.tar.gz
Algorithm Hash digest
SHA256 aeb8cd5fddda87cb92357a66dd048d70ead72465bc8a7a5f02a3b0daa2cdd72b
MD5 a02ea38fa9a828f4285ebdbd18015722
BLAKE2b-256 49e30aab3d2b27e8dc3b8a689a907e64c48aa8fcccf975bb73a778339265dc0c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page