Backends¶
Parser backends convert C/C++ source code into the headerkit
IR. The ParserBackend protocol defines
the interface that all backends implement.
Backends are accessed through a registry. Use get_backend()
to obtain an instance and list_backends() to discover
what is available.
Available Backends¶
| Backend | Description | C++ Support | Macro Extraction | Languages | Classifications |
|---|---|---|---|---|---|
libclang |
LLVM clang-based parser | Yes | Yes | c, cpp |
header |
tree-sitter |
Zero-dependency parser using tree-sitter-c and tree-sitter-cpp |
Yes | No | c, cpp |
header, source |
nim |
Native Nim 2.x compiler std/macros AST extractor (_nim_ast_helper.nim) and nim c --header C-ABI parser |
No | Yes | nim, c |
source, header |
nimony |
Native Nimony (Nim 3.0) NIF S-expression reader (_nimony_nif_helper.nim) |
No | No | nif, nimony, nim |
interface, source, header |
Protocol¶
See ParserBackend on the IR Types page for the
full protocol definition including parse(), name, supports_macros,
supports_cpp, supported_languages, and supported_classifications.
Exceptions¶
LibclangUnavailableError
¶
Bases: RuntimeError
Raised when libclang shared library cannot be found or loaded.
Registry Functions¶
get_backend
¶
Get a parser backend instance.
Returns a new instance of the requested backend. If no name is provided, returns the default backend (libclang).
::
from headerkit.backends import get_backend
# Get default backend
backend = get_backend()
# Get libclang backend
clang = get_backend("libclang")
# Parse a header
header = backend.parse(code, "myheader.h")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Backend name (e.g., |
None
|
Returns:
| Type | Description |
|---|---|
ParserBackend
|
New instance of the requested backend. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the requested backend is not available. Example¶ |
get_default_backend
¶
Get the name of the default backend.
Returns the name of the currently configured default backend.
::
from headerkit.backends import get_default_backend
default = get_default_backend()
print(f"Default backend: {default}")
Returns:
| Type | Description |
|---|---|
str
|
Backend name (e.g., "libclang"). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no backends are available. Example¶ |
list_backends
¶
List names of all registered backends.
::
from headerkit.backends import list_backends
for name in list_backends():
print(f"Available: {name}")
Returns:
| Type | Description |
|---|---|
list[str]
|
List of backend names that can be passed to :func: Example¶ |
is_backend_available
¶
Check if a backend is available for use.
For backends that require external libraries (e.g. libclang), this
performs a real load test -- not just a registry lookup. The result
is not cached on failure so that a subsequent auto_install()
can make the library appear.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Backend name to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the backend is registered and its underlying library is loadable. |
register_backend
¶
Register a parser backend.
Called by backend modules during import to add themselves to the registry.
The first registered backend becomes the default unless is_default is
explicitly set on a later registration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique name for the backend (e.g., |
required |
backend_class
|
type[ParserBackend]
|
Class implementing the :class: |
required |
is_default
|
bool
|
If True, this becomes the default backend for :func: |
False
|
priority
|
int
|
Execution priority for hook dispatch. |
STANDARD
|
get_backend_info
¶
Get information about all known backends.
Returns:
| Type | Description |
|---|---|
list[dict[str, str | bool]]
|
List of dicts with name, available, default, and description. |
Nim & Nimony Backends¶
NimBackend
¶
NimBackend(*, c_backend='auto', doc_format='auto', keep_nim_main=False, filtered_functions=None, include_unexported=False)
First-class HeaderKit parser backend for Nim .nim sources and nim c --header headers.
c_backend_options
classmethod
¶
Return C parser backend options required for Nim --header C files.
prepare_c_parse_args
classmethod
¶
Resolve nimbase.h include path, NIM_INTBITS, and CST macro sets before C parsing.
is_available
¶
Return True if the nim compiler is available on PATH or a C backend is available.
parse
¶
parse(code, filename, include_dirs=None, extra_args=None, *, use_default_includes=True, recursive_includes=True, max_depth=10, project_prefixes=None, allowlist=None, denylist=None, nim_source_path=None, jsondoc_path=None, jsondoc_entries=None, keep_nim_main=None, filtered_functions=None, include_unexported=None, doc_format=None, **kwargs)
Parse either a .nim source file or a Nim --header .h file into HeaderKit IR.
extract_nim_ast_data
¶
Extract structured Nim compiler AST metadata from a .nim source file or code string.
Compiles and invokes HeaderKit's _nim_ast_helper.nim compiler AST extractor
(using compiler/[ast, parser, idents, options, lineinfos, renderer]) and returns
the parsed JSON dictionary with schema:
"path":str— target.nimfile path."included_files":list[str]— recursively resolvedincludefile paths."declarations":list[dict[str, Any]]— extracted declaration items with"kind"in{"routine", "object", "enum", "typedef", "const", "var"}(representing procs/funcs/routines, object/tuple types, enums, typedefs/distinct types, and constants/vars), includingname,c_name,has_exportc,exported,is_deprecated,attributes,parameters,return_type,doc,file,line, andcolumn.
Returns None if the nim compiler is unavailable or AST extraction fails.
clean_nim_c_unit
¶
clean_nim_c_unit(unit, *, filtered_functions=None, keep_nim_main=False, nim_source_path=None, nim_source_code=None, jsondoc_path=None, jsondoc_entries=None, doc_format='auto')
Clean and enrich a SourceUnit parsed from a Nim --header C file.
- Strips Nim runtime internals (
NimMain,signalHandler,nimFrame,eqdestroy_, etc.). - Normalizes
nimbase.hscalar types (NI,NU,NF,NCSTRING,NIM_BOOL), unmanglestyObject_<Name>__<hash>andtyEnum_<Name>__<hash>, strips_p0/`gensym0/_colon_suffixes, and inlinestyProc__<hash>callbacks. - Correlates declarations with
_nim_ast_helper.nimand/orjsondocmetadata to restore exact parameter names, docstrings, deprecation flags, andSourceMeta. - Runs
DocParserTransformto populateparsed_docwhen docstrings exist.
NimonyBackend
¶
HeaderKit parser backend for Nimony NIF (.nif) S-expression AST files and Nimony sources.