Skip to content

Latest commit

 

History

History
83 lines (67 loc) · 2 KB

AST_REFERENCE.md

File metadata and controls

83 lines (67 loc) · 2 KB

Tree Interval AST Reference

Related Documentation

Overview

Tree Interval provides comprehensive AST (Abstract Syntax Tree) analysis capabilities through the AstTreeBuilder class and related utilities.

AstTreeBuilder

The main class for converting Python code into Tree Interval's tree structure.

Constructor

AstTreeBuilder(source: Union[FrameType, str])

Methods

  • build() -> Tree[str]
    • Converts source code into a tree structure
    • Returns: Tree representation of the AST

Node Information

Each AST node contains:

  • type: The AST node type (e.g., "Module", "FunctionDef", "If")
  • fields: Dictionary of node-specific attributes
  • _fields: Raw AST node fields data
  • ast_node: Direct access to underlying AST node Common fields include:
    • name: For function and class definitions
    • args: For function arguments
    • body: For statement blocks
    • test: For conditional statements

AST Node Types

Common node types and their fields:

Module

Root node representing the entire source file

{"type": "Module", "fields": {"body": "List[n]"}}

FunctionDef

Function definition nodes

{"type": "FunctionDef", "fields": {"name": "function_name", "args": "Arguments"}}

If

Conditional statements

{"type": "If", "fields": {"test": "Compare", "body": "List[n]", "orelse": "List[n]"}}

Call

Function calls

{"type": "Call", "fields": {"func": "Name", "args": "List[n]"}}

Example Usage

from tree_interval import AstTreeBuilder

# Parse Python code
code = """
def example(x):
    if x > 0:
        return x * 2
    return 0
"""

builder = AstTreeBuilder(code)
ast_tree = builder.build()
ast_tree.visualize()

Related Documentation