Nim Compiler Tools Reference

Nim's compiler provides built-in tooling for documentation generation, code analysis, and module discovery. These are more reliable than web scraping or manual parsing.

JSON Documentation Generation (nim jsondoc)

Generate structured JSON documentation directly from Nim source files.

# Generate JSON docs for a standard library module
nim jsondoc stdlib/system.nim
nim jsondoc stdlib/strutils.nim

# Generate JSON docs for a local module
nim jsondoc src/mymodule.nim

Output is machine-readable JSON containing procedure signatures, type definitions, documentation comments, and source locations.

Index File Generation (nim doc --index:only)

Generate structured .idx index files for module contents.

nim doc --index:only stdlib/system.nim
nim doc --index:only src/mymodule.nim

Produces index files useful for search and cross-referencing across modules.

Syntax and Semantic Checking (nim check)

Validate Nim source without producing a binary. Catches syntax errors, type mismatches, and undeclared identifiers.

nim check src/mymodule.nim
nim check --hints:off src/mymodule.nim

Faster than a full compile — useful for editor integration and CI lint steps.

Module Discovery (nim dump)

List all available standard library modules and compiler paths.

nim dump

Useful for discovering what modules are available in the current Nim installation.

Compiler Module Imports (Advanced)

For deep AST analysis, Nim's own compiler modules can be imported directly:

import compiler/ast       # AST node types and traversal
import compiler/parser    # Nim source parser
import compiler/lexer     # Tokenizer
import compiler/idents    # Identifier cache
import compiler/options   # Compiler configuration
import compiler/docgen    # Documentation generator

# Also available:
import packages/docutils/rstgen  # RST/documentation rendering

Example: Parse and Analyze a Nim Source File

import compiler/[ast, parser, lexer, idents, options]
import json

let cache = newIdentCache()
let conf = newConfigRef()
let ast = parseFile("src/mymodule.nim", cache, conf)

# Extract and process exported symbols from the AST

This approach lets you build custom analysis tools that leverage the same parser the Nim compiler uses internally.