Search documentation of Ragex

# Ragex.Analyzers.Metastatic (Ragex v0.4.0)

View Source

Analyzer implementation using Metastatic MetaAST library.

Provides richer semantic analysis compared to native regex-based parsers:

- Cross-language semantic equivalence
- Purity analysis (detects side effects like I/O operations)
- Comprehensive complexity metrics:
  - Cyclomatic complexity (McCabe metric)
  - Cognitive complexity (structural complexity with nesting penalties)
  - Maximum nesting depth
  - Enhanced Halstead metrics (volume, difficulty, effort)
  - Detailed LoC (physical, logical, comments, blank)
  - Function-level metrics (statements, returns, variables, parameters)
- Three-layer MetaAST (M2.1/M2.2/M2.3)

## Hybrid Approach

This analyzer uses a hybrid strategy:

1. Parse source code with Metastatic to get MetaAST representation
2. Use native language analyzers for detailed entity extraction (modules, functions, calls)
3. Enrich function metadata with metrics calculated from MetaAST

This combines the strengths of both approaches:

- Native analyzers provide complete, language-specific entity extraction
- Metastatic provides cross-language semantic analysis and quality metrics

## Enrichment

Each function in the analysis result is enriched with a `:metastatic` key in its metadata:

    %{
      metastatic: %{
        cyclomatic: 3,
        cognitive: 2,
        max_nesting: 1,
        halstead: %{volume: 50.0, difficulty: 2.5, effort: 125.0, ...},
        loc: %{physical: 10, logical: 8, comments: 2, blank: 0},
        function_metrics: %{statement_count: 8, return_points: 1, variable_count: 3}
      }
    }

## Fallback Behavior

If Metastatic parsing fails, the analyzer falls back to native analyzers (if `:fallback_to_native_analyzers` feature flag is enabled). This ensures robustness even when encountering code that Metastatic cannot parse.