Skip to content
Jennifer Programming Language

docblock - Jennifer doc comments and their parser

import "docblock.j" as docblock;

Two things in one: the standard doc-comment format for Jennifer source, and docblock.parse, which reads source and returns that documentation as structured, typed values. It produces data and does not render. Pure Jennifer over regex and strings; runs on either binary.

The format: Jennifer doc comments

A doc comment opens with exactly /** (a plain /* block comment stays invisible) and closes with */. It immediately precedes the construct it documents (a func, a def struct, a def enum, or a def const) or, when it carries an @module tag, it is the file preamble. Whether a construct is exported is read from its export keyword, never a tag.

jennifer
/**
 * Distance between two points.
 *
 * A longer description may follow, separated from the summary by a blank line.
 * @param ax {float} first x coordinate
 * @param ay {float} first y coordinate
 * @param bx {float} second x coordinate
 * @param by {float} second y coordinate
 * @return {float} the Euclidean distance
 * @since 0.9
 */
export func distance(ax as float, ay as float, bx as float, by as float) {
    return math.sqrt(($bx - $ax) * ($bx - $ax) + ($by - $ay) * ($by - $ay));
}

The body is a summary (the first paragraph, with wrapped lines joined, so it can span several source lines), then an optional description (the remaining paragraphs, separated from the summary by a blank line), then @-tags. Put a blank line where the summary ends and the description begins.

Documenting an enum: variants in prose

An enum's doc comment takes no @field / @param / @return tags: its variants are not fields, and a variant's payload is a mini-struct with no slot for one. Describe the variants in the description instead, one per line:

jennifer
/**
 * A drawable shape.
 *
 * - `Circle` - a circle of radius `r`
 * - `Rect` - an axis-aligned rectangle `w` by `h`
 * - `Empty` - nothing to draw (the zero value)
 */
export def enum Shape { Empty, Circle { r as float }, Rect { w as float, h as float } };

An enum doc comment that does carry @field / @param / @return lines gets the warning "enums document variants in prose; is a struct's doc mis-attached?". Either move the tags into prose as above or, when the comment belongs to a struct that drifted away from it, put it back above that struct.

Types are written in Jennifer syntax, in braces

Every {...} type is written verbatim in Jennifer's own syntax: {int}, {list of int}, {map of string to list of int}, {json.Value}. There is no any / mixed pseudo-type (Jennifer has no top type); an opaque value documents as json.Value or a named struct. The braces make extraction unambiguous.

Tags

TagOnMeaning
@param name {type} descfuncOne per parameter.
@field name {type} descstructOne per field.
@return {type} descfuncThe return value.
@throws {type} descfuncAn error the function may throw (repeatable).
@since versionanyWhen it was introduced.
@deprecated [reason]anyMarks it deprecated, including a whole module when it is in the preamble (the API reference then flags the module page and its index row).
@see refanyA cross-reference (repeatable).
@examplefuncA runnable example; its body is the following lines up to the next tag.
@internalanyNot part of the public surface.
@module namepreambleMarks the file preamble (module doc).
@author / @version / @licensepreambleModule metadata.

The reference layout is jennifer fmt output: fmt preserves doc comments and normalises layout deterministically, so a comment reliably attaches to the construct that follows it.

Parsing: docblock.parse(source)

docblock.parse(source as string) returns the whole document as a FileDoc:

jennifer
use fs;
use io;
import "docblock.j" as docblock;

def doc as docblock.FileDoc init docblock.parse(fs.readString("geometry.j"));
io.printf("%s\n", $doc.module.summary);
for (def f in $doc.funcs) {
    io.printf("%s (%d params)\n", $f.name, len($f.params));
}

The result types

The result is typed data rather than a tag bag: each kind of entry has its own typed list (funcs, structs, enums, consts) of fixed-field structs. All are exported:

StructFields
FileDocmodule, funcs, structs, enums, consts, diagnostics
ModuleDocsummary, description, author, version, license, deprecated, see
FuncDocname, exported, summary, description, params, returns, throws, examples, since, deprecated, see, internal
StructDocname, exported, summary, description, fields, since, deprecated, see, internal
EnumDocname, exported, summary, description, since, deprecated, see, internal (variants are described in prose, so there is no field list)
ConstDocname, exported, type, summary, description, since, deprecated, see, internal
ParamDocname, type, description (also used for a struct's fields)
ReturnDoc, ThrowDoctype, description
Diagnosticseverity (always "warning"), line, message

An absent value is its zero: a function with no @return has a returns whose type is ""; a file with no preamble has an empty module.

Diagnostics: it reports, it never enforces

docblock never fails on a documentation error: problems come back as Diagnostic values and the caller decides what is fatal. Four are reported:

DiagnosticMeaning
@param / @field that names nothing realthe documented name is not a parameter / field of the construct
real parameter / field with no @param / @fielddocs drifting behind the code, the commonest doc bug; names are cross-checked against the declaration
orphaned doc commentit precedes neither a documentable construct nor a preamble
enum doc with @param / @field / @returnsee Documenting an enum
text
[warning] line 36: @param "typo" is not a param of drifted
[warning] line 36: param "real" of drifted has no @param

Whole-body analysis (e.g. "a @return on a function that never returns a value") is out of scope: it needs the AST, and the module has only the text.

Scanner correctness

Comment boundaries are found by a character-level /* */ depth scan that skips string literals and # line comments and nests /* */ correctly, so a /** inside a string literal is not mistaken for a doc comment, and a nested /* ... */ inside a doc body does not close it early.

Checking a file or tree

scripts/docblock-check.sh runs the parser over a .j file or a whole directory and reports each file's doc coverage plus any diagnostics, for use as a pre-commit / CI check:

sh
scripts/docblock-check.sh modules/          # every .j under modules/
scripts/docblock-check.sh myapp.j           # a single file
# ok   modules/web.j  (1 module, 22 func, 3 struct, 0 enum, 0 const)
# WARN app.j  (1 diagnostic)
#        line 12: @param "nmae" is not a param of greet

It finds the interpreter via JENNIFER=/path/to/jennifer, else ./jennifer at the repo root, else jennifer on PATH. It exits 0 when every file is clean and 1 when any file has diagnostics.

Enforced in CI

The project runs this check on every push and pull request (a "Check doc comments" step in .github/workflows/test.yml), over both trees:

sh
scripts/docblock-check.sh examples/
scripts/docblock-check.sh modules/

A drift diagnostic fails the build. A missing doc comment is not an error: docblock flags a doc that is present but wrong, or one that documents nothing.

See also

  • jennifer fmt - the canonical layout normaliser.
  • regex / strings - the libraries docblock is built on.
  • html - build HTML, e.g. to render parsed docs (not part of docblock).

Academy talks

TalkWhat it shows
One CI pipeline for GitHub, Gitea and Forgejochecking docblocks as a CI gate step

API reference

Every exported function, struct and constant of docblock, with its signature, generated from the module's doc comments: the docblock API reference.