Pipeline.Codebase.ASTParser (pipeline v0.0.1)

View Source

Unified AST parsing utilities for different programming languages.

Provides a consistent interface for parsing and analyzing code across multiple languages, extracting functions, classes, imports, and other code constructs.

⚠️ IMPORTANT WARNING ⚠️

This is a PROTOTYPE implementation using simplified regex-based parsing.

For production use, this module should be replaced with proper language parsers:

  • JavaScript/TypeScript: Babylon, Acorn, or ESTree-based parsers
  • Python: Built-in ast module or parso library
  • Rust: syn crate or rustc_parse
  • Go: go/parser and go/ast from Go standard library
  • Elixir: Enhanced AST traversal using Code.string_to_quoted/2

The current regex patterns are fragile and will fail on complex code patterns.

Summary

Functions

Calculate cyclomatic complexity for a file.

Find class definitions in a file.

Find function definitions in a file.

Find import/require statements in a file.

Parse a file and extract code information.

Types

class_info()

@type class_info() :: %{
  name: String.t(),
  line: non_neg_integer(),
  end_line: non_neg_integer() | nil,
  methods: [function_info()],
  properties: [String.t()],
  inheritance: [String.t()]
}

comment_info()

@type comment_info() :: %{
  line: non_neg_integer(),
  type: String.t(),
  content: String.t()
}

function_info()

@type function_info() :: %{
  name: String.t(),
  type: String.t(),
  line: non_neg_integer(),
  end_line: non_neg_integer() | nil,
  parameters: [String.t()],
  visibility: String.t(),
  signature: String.t()
}

parse_result()

@type parse_result() :: %{
  functions: [function_info()],
  classes: [class_info()],
  imports: [String.t()],
  exports: [String.t()],
  comments: [comment_info()],
  complexity: non_neg_integer()
}

Functions

calculate_complexity(file_path)

@spec calculate_complexity(String.t()) ::
  {:ok, non_neg_integer()} | {:error, String.t()}

Calculate cyclomatic complexity for a file.

find_classes(file_path, opts \\ [])

@spec find_classes(
  String.t(),
  keyword()
) :: {:ok, [class_info()]} | {:error, String.t()}

Find class definitions in a file.

find_functions(file_path, opts \\ [])

@spec find_functions(
  String.t(),
  keyword()
) :: {:ok, [function_info()]} | {:error, String.t()}

Find function definitions in a file.

find_imports(file_path)

@spec find_imports(String.t()) :: {:ok, [String.t()]} | {:error, String.t()}

Find import/require statements in a file.

parse_file(file_path)

@spec parse_file(String.t()) :: {:ok, parse_result()} | {:error, String.t()}

Parse a file and extract code information.

Returns structured information about functions, classes, imports, and other code constructs based on the file's language.