Skip to main content

ANTLeRinator

Project description

https://img.shields.io/pypi/v/antlerinator?logo=python&logoColor=white https://img.shields.io/pypi/l/antlerinator?logo=open-source-initiative&logoColor=white https://img.shields.io/github/actions/workflow/status/renatahodovan/antlerinator/main.yml?branch=master&logo=github&logoColor=white https://img.shields.io/coveralls/github/renatahodovan/antlerinator/master?logo=coveralls&logoColor=white

ANTLeRinator is a Python utility package to help keeping components of ANTLR v4 in sync.

Requirements

  • Python >= 3.6

  • Java SE >= 7 JRE or JDK (the latter is optional)

Install

ANTLeRinator has both run-time and build-time components, therefore it can be used both as an install requirement and as a setup requirement.

To use ANTLeRinator at run-time, it can be added to setup.cfg as an install requirement (if using setuptools with declarative config):

[options]
install_requires =
    antlerinator
    antlr4-python3-runtime==4.9.2  # optional

Note that ANTLeRinator has no direct dependency on the ANTLRv4 runtime.

To use ANTLeRinator at build-time, it can be added to pyproject.toml as a build system/setup requirement (if using PEP517 builds):

[build-system]
requires = [
    "antlerinator",
    "setuptools",
]
build-backend = "setuptools.build_meta"

To install ANTLeRinator manually, e.g., into a virtual environment, use pip:

pip install antlerinator

The above approaches install the latest release of ANTLeRinator from PyPI. Alternatively, for the development version, clone the project and perform a local install:

pip install .

Usage

Downloading the ANTLRv4 tool jar file at run-time

If the ANTLRv4 runtime is installed, ANTLeRinator can be used to download the corresponding version of the tool jar file:

import antlerinator

assert antlerinator.__antlr_version__ is not None  # alternatively: import antlr4

path = antlerinator.download(lazy=True)

If the ANTLRv4 runtime is not installed or a different version of the tool jar is needed, the required version must/can be specified:

import antlerinator

path = antlerinator.download(version='4.9.2', lazy=True)

By default, these approaches download files to a ~/.antlerinator directory, and only if necessary (i.e., the jar file has not been downloaded yet).

Downloading the ANTLRv4 tool jar manually

Should there be need for downloading the ANTLR v4 tool jar manually, a helper script is available:

antlerinator-download --help

Adding ANTLRv4 support to the command line interface

If an application has an ArgumentParser-based command line interface, ANTLeRinator can be used to add a CLI argument to specify which ANTLRv4 tool jar to use. The default processing of the argument, also provided by ANTLeRinator, is to download the tool jar version corresponding to the ANTLRv4 runtime if necessary:

import antlerinator
import argparse
import subprocess

assert antlerinator.__antlr_version__ is not None

parser = argparse.ArgumentParser()
antlerinator.add_antlr_argument(parser)
args = parser.parse_args()

antlerinator.process_antlr_argument(args)

subprocess.call(['java', '-jar', args.antlr])

Building lexers/parsers at build-time with ANTLRv4

ANTLeRinator also extends Setuptools to allow building lexers/parsers at build-time from .g4 grammars. It adds two new Setuptools commands, build_antlr and clean_antlr, to perform the building and the cleanup of lexers/parsers, and also ensures that these new commands are invoked by the standard build (install), develop, and clean commands as well as by the Setuptools-internal editable_wheel command as appropriate. The building of lexers/parsers is performed using the ANTLRv4 tool and is controlled by the [build_antlr] section in setup.cfg:

[build_antlr]
commands =
    antlerinator:4.9.2 path/to/Dummy.g4 -Dlanguage=Python2 -o pkg/parser/py2 -Xexact-output-dir
    antlerinator:4.9.2 path/to/Dummy.g4 -Dlanguage=Python3 -o pkg/parser/py3 -Xexact-output-dir
output =
    pkg/parser/py?/Dummy*.py
#java =

The commands option of build_antlr lists the invocations of the ANTLRv4 tool. The first element of each invocation is a so-called provider specification that defines where to get the ANTLRv4 tool jar from. Currently, two providers are supported: antlerinator:N.M uses ANTLeRinator to download the requested version of the tool jar (if necessary), while file:/path/to/antlr.jar uses the explicitly given tool jar. The rest of the elements of each invocation are passed to the tool jar as command line arguments.

The java option can be given to explicitly specify which Java VM to use to run the ANTLRv4 tool (java is used by default).

The output option shall list the file names or glob patterns of the output of the ANTLRv4 tool invocations. The clean_antlr command removes these files on cleanup.

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

antlerinator-1!3.0.0.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

antlerinator-1!3.0.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file antlerinator-1!3.0.0.tar.gz.

File metadata

  • Download URL: antlerinator-1!3.0.0.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for antlerinator-1!3.0.0.tar.gz
Algorithm Hash digest
SHA256 e0820f7b21d01edc5a908f1a2409de7b2b7d1a192f2e6fbd7d92660b40d768e8
MD5 17449caa537449108cda40380ef9dc02
BLAKE2b-256 fcd96d9d255a99a6a7263a171d79e274bfe0d9bbe8ee51d759fafb741208d1be

See more details on using hashes here.

File details

Details for the file antlerinator-1!3.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for antlerinator-1!3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 90e8b6dd5d8b89d7262b83475e99f5182b7ba56de839e1b432f9be405b42c622
MD5 f2a816dc5098d184e362762a5c52d989
BLAKE2b-256 6e7021ba97eb16b8496d491e14a9294f6aa0351d20f93af83d5d60805d8ada4c

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