Typing stubs for antlr4-python3-runtime

4.13.0.20260408 · active · verified Tue Apr 14

This package provides static type checking stubs for the `antlr4-python3-runtime` library, enabling type checkers like MyPy and Pyright to analyze code that uses ANTLR 4's Python runtime. It is part of the `typeshed` project and aims to provide accurate annotations for `antlr4-python3-runtime==4.13.*`. Releases are frequent, often incorporating a date suffix, reflecting updates within the `typeshed` project and alignment with the underlying runtime.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the core `antlr4-python3-runtime` components with ANTLR-generated lexer and parser classes. The `types-antlr4-python3-runtime` package provides type hints for all `antlr4` imports and the expected interfaces of generated classes, improving developer experience with static analysis tools. Note that the ANTLR tool (a Java JAR) is required separately to generate `MyGrammarLexer.py` and `MyGrammarParser.py` from your `.g4` grammar file.

import os
from antlr4 import InputStream, CommonTokenStream, ParseTreeWalker

# --- Example: Assumes MyGrammar.g4 was compiled to MyGrammarLexer.py and MyGrammarParser.py ---
# To generate these files, you need the ANTLR Java tool (antlr-VERSION-complete.jar):
# java -jar /path/to/antlr-VERSION-complete.jar MyGrammar.g4 -Dlanguage=Python3

# Placeholder for generated classes (replace with actual generated imports)
# For a real scenario, these would be in files like MyGrammarLexer.py and MyGrammarParser.py
class MyGrammarLexer:
    def __init__(self, input_stream):
        pass # Simplified for quickstart
    def getAllTokens(self):
        return [] # Simplified

class MyGrammarParser:
    def __init__(self, token_stream):
        pass # Simplified
    def myRule(self):
        class Context:
            def accept(self, visitor): pass
            def toStringTree(self, recog=None): return "(myRule)"
        return Context() # Simplified

class MyGrammarListener:
    def enterMyRule(self, ctx): pass
    def exitMyRule(self, ctx): pass

# Example usage of the antlr4-python3-runtime with generated classes
input_text = "hello world"
input_stream = InputStream(input_text)

lexer = MyGrammarLexer(input_stream)
token_stream = CommonTokenStream(lexer)
parser = MyGrammarParser(token_stream)

# Assume 'myRule' is the entry point for your grammar
tree = parser.myRule()

print(f"Parsed tree: {tree.toStringTree(recog=parser)}")

# Using a listener for tree traversal
listener = MyGrammarListener()
walker = ParseTreeWalker()
walker.walk(listener, tree)

print("Type checking enabled for antlr4 classes by types-antlr4-python3-runtime.")

view raw JSON →