Skip to main content

A Python package for parsing Cypher queries using ANTLR4.

Project description

antlr4-cypher

antlr4-cypher is a Python package that provides the Lexer and Parser for the Cypher query language, generated using ANTLR with the command java -jar antlr-4.13.2-complete.jar -Dlanguage=Python3 -listener -visitor -o ./antlr4_cypher CypherLexer.g4 CypherParser.g4. The project includes the necessary grammar, lexer, and parser files, as well as empty listener and visitor classes that serve as a reference for implementing custom logic.

Installation

You can install the package using pip:

pip install antlr4-cypher

Grammar Source

The grammar files (CypherLexer.g4 and CypherParser.g4) are sourced from the ANTLR grammars-v4 repository. These files define the syntax and structure of the Cypher query language.

Usage

To use the lexer and parser in your project, you can import them as follows:

from antlr4 import InputStream, CommonTokenStream
from antlr4_cypher import CypherLexer, CypherParser

# Example input
input_query = "MATCH (n) RETURN n"

# Create a lexer and parser
input_stream = InputStream(input_query)
lexer = CypherLexer(input_stream)
token_stream = CommonTokenStream(lexer)
parser = CypherParser(token_stream)

# Parse the input
tree = parser.script()

# Use the tree for further processing

Custom Listeners and Visitors

The package includes empty listener and visitor classes that you can extend to implement custom logic for traversing the parse tree. For example:

from antlr4_cypher import CypherParserListener

class MyCypherListener(CypherParserListener):
    def enterMatchSt(self, ctx):
        print("Entering MATCH clause")

    def exitMatchSt(self, ctx):
        print("Exiting MATCH clause")


from antlr4_cypher import CypherParserVisitor

class CustomCypherParserVisitor(CypherParserVisitor):
    def __init__(self, mapping: dict):
        super().__init__()
        self.mapping = mapping

    def visit(self, tree):
        print("Visiting node:", tree.getText())
        return tree.accept(self)

    # Example of overriding a specific visit method
    def visitMatchSt(self, ctx):
        print("Processing MATCH clause with mapping:", self.mapping)
        return self.visitChildren(ctx)

This README provides a basic overview of the antlr4-cypher project. For more detailed information, please refer to the source code and the ANTLR documentation.

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

antlr4_cypher-0.1.2.tar.gz (56.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

antlr4_cypher-0.1.2-py3-none-any.whl (57.5 kB view details)

Uploaded Python 3

File details

Details for the file antlr4_cypher-0.1.2.tar.gz.

File metadata

  • Download URL: antlr4_cypher-0.1.2.tar.gz
  • Upload date:
  • Size: 56.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for antlr4_cypher-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7216ab43cd6ddbedc330fde3e4e0b319b33b0c8e1be7d799c907c90d884d3fb8
MD5 ff1ef94846b663e82ea6884a5e843d8e
BLAKE2b-256 7022e3e94ff1a4642f458365cc8e8b3a7b538fc165628a4c21d1fe3f8981b0a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for antlr4_cypher-0.1.2.tar.gz:

Publisher: publish-to-pypi.yml on qi-hua/antlr4-cypher

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file antlr4_cypher-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: antlr4_cypher-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 57.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for antlr4_cypher-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 79d5df69caead19e8ecb56c59fcdbae64d5853c6f8100700afecf73b050a962b
MD5 3942216005b6848e03000e0baf8181c7
BLAKE2b-256 fe7fca16aed1613472e092df12a94c8b59f9d271193a4efbb1c6f6808fe1f41c

See more details on using hashes here.

Provenance

The following attestation bundles were made for antlr4_cypher-0.1.2-py3-none-any.whl:

Publisher: publish-to-pypi.yml on qi-hua/antlr4-cypher

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