Typing stubs for antlr4-python3-runtime
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
- breaking The `antlr4-python3-runtime` library, which these stubs target, has known incompatibilities with specific Python versions. Notably, `antlr4-python3-runtime==4.11.1` is incompatible with Python 3.13+ due to changes in CPython's internal APIs. Users of Python 3.13 or newer should ensure they are using `antlr4-python3-runtime>=4.13.0`. Always check the compatibility matrix for the underlying runtime.
- gotcha This package provides *only* type stubs. It does not include the actual `antlr4-python3-runtime` library. For your code to run, you must install `antlr4-python3-runtime` separately. Additionally, the ANTLR parser generator tool itself (which converts `.g4` grammars to Python source files) requires a Java Runtime Environment (JRE) to operate, which can be a point of confusion for Python-only developers.
- gotcha As a type stub package part of the `typeshed` project, fixes or improvements to the type annotations should be contributed directly to the `typeshed` GitHub repository, not to this `types-` package directly.
Install
-
pip install types-antlr4-python3-runtime -
pip install antlr4-python3-runtime
Imports
- InputStream
from antlr4 import InputStream
- CommonTokenStream
from antlr4 import CommonTokenStream
- Lexer
from antlr4 import Lexer
- Parser
from antlr4 import Parser
- ParseTreeWalker
from antlr4.tree.Tree import ParseTreeWalker
- GeneratedLexer
from MyGrammarLexer import MyGrammarLexer
Quickstart
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.")