Skip to content

yasithrashan/basic-python-ast-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Simple Python AST Parser

A lightweight Python tool for analyzing Python code structure using Abstract Syntax Trees (AST). This parser can extract functions and classes from Python files and provide search functionality.

Features

  • Parse individual Python files or entire directories
  • Extract function and class definitions with line numbers
  • Search for functions and classes by name (case-insensitive)
  • Save and load parsing results to/from JSON files
  • Clear results to start fresh analysis
  • Interactive command-line interface

Requirements

  • Python 3.6 or higher
  • No external dependencies (uses only Python standard library)

Installation

  1. Clone or download this repository
  2. Navigate to the project directory
  3. Run the parser:
python parser.py

Usage

Interactive Mode

Run the parser and follow the menu prompts:

python parser.py

Menu Options

  1. Parse a directory - Analyze all Python files in a specified directory
  2. Search for a function - Find functions by name (partial matching)
  3. Search for a class - Find classes by name (partial matching)
  4. Show results - Display all parsed functions and classes
  5. Save results to file - Export results to JSON format
  6. Load results from file - Import previously saved results
  7. Clear all parsed results - Reset the parser state
  8. Exit - Quit the application

Example Workflow

  1. Start the parser: python parser.py
  2. Choose option 1 to parse a directory
  3. Enter directory path (or press Enter for current directory)
  4. Choose option 4 to view results
  5. Choose option 2 to search for specific functions
  6. Choose option 5 to save results for later use

File Structure

project/
├── parser.py          # Main parser application
├── tests/            # Sample Python files for testing
│   ├── sample1.py
│   └── sample2.py
├── results.json      # Generated results file (after saving)
└── README.md

Output Format

The parser extracts the following information:

Functions

  • Name
  • Line number
  • File path

Classes

  • Name
  • Line number
  • Methods list
  • File path

Sample Output

SUMMARY:
   Functions found: 5
   Classes found: 2

FUNCTIONS:
   • add (line 3 in tests/sample1.py)
   • subtract (line 7 in tests/sample1.py)
   • multiply (line 11 in tests/sample1.py)

CLASSES:
   • Calculator (calculate, get_history) (line 15 in tests/sample1.py)
   • TodoList (add_task, mark_done, get_tasks) (line 3 in tests/sample2.py)

Technical Details

AST Parsing Process

  1. File Reading - Read Python source code as text
  2. AST Generation - Convert code to Abstract Syntax Tree using ast.parse()
  3. Tree Traversal - Walk through all nodes using ast.walk()
  4. Node Identification - Identify FunctionDef and ClassDef nodes
  5. Data Extraction - Extract relevant information (name, line number, etc.)
  6. Storage - Store results in structured dictionary format

Supported Python Constructs

  • Function definitions (def)
  • Class definitions (class)
  • Methods within classes
  • Nested functions and classes

Limitations

  • Does not analyze function parameters or return types
  • Does not extract docstrings or comments
  • Does not track variable usage or imports
  • Limited to syntactically valid Python files

Future Enhancements

  • Function parameter extraction
  • Docstring analysis
  • Import statement tracking
  • Variable usage analysis
  • Code complexity metrics
  • Export to different formats (CSV, XML)

Error Handling

The parser includes error handling for:

  • File not found errors
  • Syntax errors in Python files
  • JSON serialization/deserialization errors
  • Invalid directory paths

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. Submit a pull request

License

This project is open source and available under the MIT License.

Author

Created as a learning project for understanding Python AST parsing and code analysis techniques.

About

A simple Python AST parser for analyzing code structure and finding functions/classes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages