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.
/**
* 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:
/**
* 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
| Tag | On | Meaning |
|---|---|---|
@param name {type} desc | func | One per parameter. |
@field name {type} desc | struct | One per field. |
@return {type} desc | func | The return value. |
@throws {type} desc | func | An error the function may throw (repeatable). |
@since version | any | When it was introduced. |
@deprecated [reason] | any | Marks 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 ref | any | A cross-reference (repeatable). |
@example | func | A runnable example; its body is the following lines up to the next tag. |
@internal | any | Not part of the public surface. |
@module name | preamble | Marks the file preamble (module doc). |
@author / @version / @license | preamble | Module 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:
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:
| Struct | Fields |
|---|---|
FileDoc | module, funcs, structs, enums, consts, diagnostics |
ModuleDoc | summary, description, author, version, license, deprecated, see |
FuncDoc | name, exported, summary, description, params, returns, throws, examples, since, deprecated, see, internal |
StructDoc | name, exported, summary, description, fields, since, deprecated, see, internal |
EnumDoc | name, exported, summary, description, since, deprecated, see, internal (variants are described in prose, so there is no field list) |
ConstDoc | name, exported, type, summary, description, since, deprecated, see, internal |
ParamDoc | name, type, description (also used for a struct's fields) |
ReturnDoc, ThrowDoc | type, description |
Diagnostic | severity (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:
| Diagnostic | Meaning |
|---|---|
@param / @field that names nothing real | the documented name is not a parameter / field of the construct |
real parameter / field with no @param / @field | docs drifting behind the code, the commonest doc bug; names are cross-checked against the declaration |
| orphaned doc comment | it precedes neither a documentable construct nor a preamble |
enum doc with @param / @field / @return | see Documenting an enum |
[warning] line 36: @param "typo" is not a param of drifted
[warning] line 36: param "real" of drifted has no @paramWhole-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:
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 greetIt 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:
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 librariesdocblockis built on.html- build HTML, e.g. to render parsed docs (not part ofdocblock).
Academy talks
| Talk | What it shows |
|---|---|
| One CI pipeline for GitHub, Gitea and Forgejo | checking 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.