Ragex.LanguageSupport (Ragex v0.12.0)

View Source

Shared language detection, adapter resolution, and parsing utilities.

Consolidates duplicated detect_language/1, get_adapter/1, parse_document/3, and find_source_files/2 helpers that were previously copy-pasted across Security, Smells, BusinessLogic, MetastaticBridge, Semantic, and other modules.

Supported Languages

  • :elixir -- .ex, .exs
  • :erlang -- .erl, .hrl
  • :python -- .py
  • :ruby -- .rb
  • :haskell -- .hs
  • :javascript -- .js, .jsx, .ts, .tsx, .mjs, .cjs

Note: JavaScript has no Metastatic adapter yet; get_adapter/1 returns an error for it.

Usage

alias Ragex.LanguageSupport

language = LanguageSupport.detect_language("lib/my_module.ex")
# => :elixir

{:ok, adapter} = LanguageSupport.get_adapter(:elixir)
# => {:ok, Metastatic.Adapters.Elixir}

{:ok, doc} = LanguageSupport.parse_document(source, :elixir)

Summary

Functions

Detects language from a file path extension.

Finds supported source files in a directory.

Returns the Metastatic adapter module for a language.

Returns whether a language has a Metastatic adapter.

Returns the list of file extensions with Metastatic adapter support.

Parses source code into a Metastatic.Document via the appropriate adapter.

Returns the list of all supported file extensions.

Types

language()

@type language() ::
  :elixir | :erlang | :python | :ruby | :haskell | :javascript | :unknown

Functions

detect_language(path)

@spec detect_language(String.t()) :: language()

Detects language from a file path extension.

Examples

iex> Ragex.LanguageSupport.detect_language("lib/my_module.ex")
:elixir

iex> Ragex.LanguageSupport.detect_language("script.py")
:python

iex> Ragex.LanguageSupport.detect_language("unknown.xyz")
:unknown

find_source_files(path, opts \\ [])

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

Finds supported source files in a directory.

Options

  • :recursive -- recursively search subdirectories (default: true)
  • :metastatic_only -- only include languages with Metastatic adapters (default: false)

Examples

{:ok, files} = LanguageSupport.find_source_files("lib/")
{:ok, files} = LanguageSupport.find_source_files("lib/", metastatic_only: true)

get_adapter(language)

@spec get_adapter(language()) ::
  {:ok, module()} | {:error, {:unsupported_language, language()}}

Returns the Metastatic adapter module for a language.

Examples

iex> Ragex.LanguageSupport.get_adapter(:elixir)
{:ok, Metastatic.Adapters.Elixir}

iex> Ragex.LanguageSupport.get_adapter(:javascript)
{:error, {:unsupported_language, :javascript}}

has_adapter?(language)

@spec has_adapter?(language()) :: boolean()

Returns whether a language has a Metastatic adapter.

metastatic_extensions()

@spec metastatic_extensions() :: [String.t()]

Returns the list of file extensions with Metastatic adapter support.

parse_document(content, language, opts \\ [])

@spec parse_document(String.t(), language(), keyword()) ::
  {:ok, Metastatic.Document.t()} | {:error, term()}

Parses source code into a Metastatic.Document via the appropriate adapter.

Parameters

  • content -- source code string
  • language -- language atom (or auto-detect via :path option)
  • opts -- keyword options
    • :path -- file path for auto-detection (used only if language is not provided)

Examples

{:ok, doc} = LanguageSupport.parse_document(source, :elixir)

parse_file(path, opts \\ [])

@spec parse_file(
  String.t(),
  keyword()
) :: {:ok, Metastatic.Document.t()} | {:error, term()}

Parses a file into a Metastatic.Document.

Reads the file, detects the language, and parses.

Options

  • :language -- override language detection (default: auto-detect from extension)

Examples

{:ok, doc} = LanguageSupport.parse_file("lib/my_module.ex")

supported_extensions()

@spec supported_extensions() :: [String.t()]

Returns the list of all supported file extensions.