Parser
parser
Tree-sitter based Python source parser.
This module provides deterministic, fast parsing of Python source files using tree-sitter. It extracts structured information (functions, classes, imports, variables, docstrings) from the concrete syntax tree.
Example
from pathlib import Path from axm_ast.core.parser import extract_module_info mod = extract_module_info(Path("my_module.py")) [f.name for f in mod.functions]['main', 'helper']
extract_module_info(path)
Extract full module information from a Python file.
Parses the file using tree-sitter and extracts all functions, classes, imports, variables, and the module docstring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to a .py file. |
required |
Returns:
| Type | Description |
|---|---|
ModuleInfo
|
ModuleInfo with all extracted metadata. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
ValueError
|
If the file is not a .py file. |
Example
from pathlib import Path mod = extract_module_info(Path("my_module.py")) mod.path.name 'my_module.py'
Source code in packages/axm-ast/src/axm_ast/core/parser.py
parse_file(path)
Parse a Python file into a tree-sitter Tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to a .py file. |
required |
Returns:
| Type | Description |
|---|---|
Tree
|
Parsed tree-sitter Tree. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
ValueError
|
If the file is not a .py file. |
Example
from pathlib import Path tree = parse_file(Path("setup.py")) tree.root_node.type 'module'
Source code in packages/axm-ast/src/axm_ast/core/parser.py
parse_source(source)
Parse a Python source string into a tree-sitter Tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
Python source code as string. |
required |
Returns:
| Type | Description |
|---|---|
Tree
|
Parsed tree-sitter Tree. |
Example
tree = parse_source("def foo(): pass") tree.root_node.type 'module'