C3nif.Parser (C3nif v0.2.0)

View Source

Parse C3 source code to extract NIF function metadata.

This module analyzes C3 source files to find NIF functions based on their signature pattern and extracts metadata from <* @nif ... *> doc comment annotations.

NIF Detection

NIF functions are detected by their signature pattern:

fn erl_nif::ErlNifTerm function_name(
    erl_nif::ErlNifEnv* env, CInt argc, erl_nif::ErlNifTerm* argv
) { ... }

Annotations

Metadata is extracted from C3's doc comment syntax (<* ... *>). We use nif: prefix (not @nif) to avoid conflicts with C3's contract syntax:

<* nif: arity = 2 *>
fn erl_nif::ErlNifTerm add(...) { ... }

<* nif: arity = 1, dirty = cpu *>
fn erl_nif::ErlNifTerm heavy_compute(...) { ... }

<* nif: name = "custom_name", arity = 2 *>
fn erl_nif::ErlNifTerm internal_name(...) { ... }

Callbacks

The following callbacks are auto-detected by function name and signature:

  • on_load - fn CInt on_load(erl_nif::ErlNifEnv*, void**, erl_nif::ErlNifTerm)
  • on_unload - fn void on_unload(erl_nif::ErlNifEnv*, void*)

Summary

Functions

Parse both NIFs and callbacks from C3 source.

Parse C3 source code and detect callback functions.

Parse C3 source code and extract NIF function metadata.

Functions

parse(c3_source)

Parse both NIFs and callbacks from C3 source.

Returns {nifs, callbacks} tuple.

parse_callbacks(c3_source)

@spec parse_callbacks(String.t()) :: C3nif.Parser.Callbacks.t()

Parse C3 source code and detect callback functions.

Returns a Callbacks struct with detected on_load and on_unload functions.

parse_nifs(c3_source)

@spec parse_nifs(String.t()) :: [C3nif.Parser.NifFunction.t()]

Parse C3 source code and extract NIF function metadata.

Returns a list of NifFunction structs for all detected NIF functions.