ASTTokens
ASTTokens is a Python library that annotates Abstract Syntax Trees (ASTs) with the positions of tokens and text in the source code that generated them. This functionality is essential for tools that perform source code transformations, such as automated refactoring or syntax highlighting. The current version is 3.0.1, released on November 15, 2025. The library is actively maintained with a stable release cadence, ensuring ongoing support and updates.
Warnings
- breaking ASTTokens requires Python 3.8 or higher. Ensure your environment meets this requirement to avoid compatibility issues.
- gotcha ASTTokens may not support certain node types in Python 3.8 due to known issues. Refer to the documentation for a list of unsupported node types.
Install
-
pip install asttokens
Imports
- ASTTokens
from asttokens import ASTTokens
- ASTText
from asttokens import ASTText
- LineNumbers
from asttokens import LineNumbers
Quickstart
import asttokens
import ast
# Sample source code
source = "Robot('blue').walk(steps=10*n)"
# Parse and annotate the source code
atok = asttokens.ASTTokens(source, parse=True)
# Access the annotated AST
tree = atok.tree
# Retrieve the first token of the first node
first_token = tree.body[0].first_token
# Print the start position of the first token
print(f"Start position: {first_token.startpos}")