Muex.Language behaviour (Muex v0.3.0)

View Source

Behaviour for language adapters that provide AST parsing, unparsing, and compilation.

This behaviour defines the interface for supporting different programming languages in mutation testing. Each language adapter implements the callbacks to handle language-specific AST operations.

Example

defmodule Muex.Language.MyLanguage do
  @behaviour Muex.Language

  @impl true
  def parse(source), do: {:ok, parse_to_ast(source)}

  @impl true
  def unparse(ast), do: {:ok, ast_to_string(ast)}

  @impl true
  def compile(source, module_name), do: {:ok, compiled_module}

  @impl true
  def file_extensions, do: [".my"]

  @impl true
  def test_file_pattern, do: ~r/_test.my$/
end

Summary

Callbacks

Compiles source code into a module that can be loaded into the BEAM.

Returns the list of file extensions for this language.

Parses source code into an Abstract Syntax Tree (AST).

Returns a regex pattern to identify test files.

Converts an AST back into source code string.

Callbacks

compile(source, module_name)

@callback compile(source :: String.t(), module_name :: atom()) ::
  {:ok, module()} | {:error, term()}

Compiles source code into a module that can be loaded into the BEAM.

Parameters

  • source - String containing the source code to compile
  • module_name - Atom representing the module name

Returns

  • {:ok, module} - Successfully compiled module
  • {:error, reason} - Compilation failed with error details

file_extensions()

@callback file_extensions() :: [String.t()]

Returns the list of file extensions for this language.

Returns

List of file extensions including the dot (e.g., [".ex", ".exs"])

parse(source)

@callback parse(source :: String.t()) :: {:ok, ast :: term()} | {:error, term()}

Parses source code into an Abstract Syntax Tree (AST).

Parameters

  • source - String containing the source code to parse

Returns

  • {:ok, ast} - Successfully parsed AST (term structure depends on language)
  • {:error, reason} - Parsing failed with error details

test_file_pattern()

@callback test_file_pattern() :: Regex.t()

Returns a regex pattern to identify test files.

Returns

Regex that matches test file paths

unparse(ast)

@callback unparse(ast :: term()) :: {:ok, String.t()} | {:error, term()}

Converts an AST back into source code string.

Parameters

  • ast - The AST to convert back to source code

Returns

  • {:ok, source} - Successfully generated source code
  • {:error, reason} - Unparsing failed with error details