Skip to content

Commit a1264e7

Browse files
committed
Switch Python parser to AST
1 parent 58ffa4b commit a1264e7

9 files changed

Lines changed: 433 additions & 223 deletions

File tree

ARCHITECTURE.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ This allows adding new languages without changing core graph orchestration.
4747

4848
#### Python Parser (`codegraph/parsers/python_parser.py`)
4949

50-
Uses the legacy token-based parser in `codegraph/parser.py` to extract classes,
51-
functions, imports, and line ranges.
52-
53-
The parser uses Python's `tokenize` module to extract code structure from source files.
50+
Uses Python's `ast` (and `typed_ast` for Python 2.x) to extract classes, functions,
51+
imports, and line ranges.
5452

5553
#### Rust Parser (`codegraph/parsers/rust_parser.py`)
5654

codegraph/core.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class CodeGraph:
88
def __init__(self, args: Namespace):
99
language = getattr(args, "language", "python")
10-
self.parser = get_parser(language)
10+
self.parser = get_parser(language, args=args)
1111
self.paths_list = self.parser.get_source_files(args.paths)
1212
self.modules_data = self.parser.parse_files(self.paths_list)
1313

@@ -22,10 +22,11 @@ def get_lines_numbers(self):
2222
first number in tuple - start line, second - last line
2323
"""
2424
data = {}
25-
for module in self.modules_data:
25+
metadata = self.get_entity_metadata()
26+
for module, entities in metadata.items():
2627
data[module] = {}
27-
for func in self.modules_data[module]:
28-
data[module][func.name] = (func.lineno, func.endno)
28+
for name, info in entities.items():
29+
data[module][name] = (info.get("lineno"), info.get("endno"))
2930
return data
3031

3132
def get_entity_metadata(self) -> Dict:

codegraph/main.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@
4848
show_default=True,
4949
help="Language parser to use",
5050
)
51-
def cli(paths, object_only, file_path, distance, matplotlib, output, csv, language):
51+
@click.option(
52+
"--python-version",
53+
help="Target Python version for parsing (e.g. 2.7, 3.8, 3.10)",
54+
)
55+
def cli(paths, object_only, file_path, distance, matplotlib, output, csv, language, python_version):
5256
"""
5357
Tool that creates a graph of code to show dependencies between code entities (methods, classes, etc.).
5458
CodeGraph does not execute code, it is based only on lex and syntax parsing.
@@ -71,6 +75,7 @@ def cli(paths, object_only, file_path, distance, matplotlib, output, csv, langua
7175
output=output,
7276
csv=csv,
7377
language=language,
78+
python_version=python_version,
7479
)
7580
main(args)
7681

0 commit comments

Comments
 (0)