API Reference

Doc IR

Doc IR (v1): module-level only. See PLAN.md Phase 1.

class svdoc.ir.Param(name: str, type: str, default: str | None, doc: str | None, type_ref: str | None = None)[source]

A single module/interface/subroutine parameter.

Variables:
  • name – Parameter identifier.

  • type – Declared type as written in source (e.g. "int").

  • default – Default value expression, or None if unset.

  • doc – Extracted doc comment, or None if undocumented.

  • type_ref – Fully-qualified "package::type" name if resolved cross-file via svdoc.parser.resolve_types(); None otherwise.

class svdoc.ir.Port(name: str, direction: str, type: str, doc: str | None, type_ref: str | None = None, modport_preview: Modport | None = None)[source]

A single module/interface/subroutine port or argument.

Also reused for subroutine arguments (see Subroutine.args), since the shape (name, direction, type, doc) is identical.

Variables:
  • name – Port identifier.

  • direction – "input", "output", "inout", etc.

  • type – Declared type as written in source, including any packed dimensions (e.g. "logic [7:0]").

  • doc – Extracted doc comment, or None if undocumented.

  • type_ref – Fully-qualified "package::type" name if resolved cross-file via svdoc.parser.resolve_types(); None otherwise.

  • modport_preview – For an interface-typed port (direction == "interface"), the resolved Modport from the interface file, if it was found among the files passed to svdoc.parser.resolve_types(); None otherwise.

class svdoc.ir.ModuleDoc(name: str, doc: str | None, params: list[Param] = <factory>, ports: list[Port] = <factory>)[source]

Doc IR for a single SystemVerilog module.

class svdoc.ir.Signal(name: str, type: str, doc: str | None)[source]

A variable declared in an interface body (outside any modport).

class svdoc.ir.ModportPortGroup(direction: str, signals: list[str], doc: str | None)[source]

One direction-grouped clause within a modport, e.g. output valid, data.

class svdoc.ir.Modport(name: str, doc: str | None, port_groups: list[ModportPortGroup] = <factory>)[source]

A single modport declaration inside an interface.

class svdoc.ir.InterfaceDoc(name: str, doc: str | None, params: list[Param] = <factory>, ports: list[Port] = <factory>, signals: list[Signal] = <factory>, modports: list[Modport] = <factory>)[source]

Doc IR for a single SystemVerilog interface, including its modports.

class svdoc.ir.EnumValue(name: str, value: str | None, doc: str | None)[source]

A single named value within an enum typedef.

class svdoc.ir.StructField(name: str, type: str, doc: str | None)[source]

A single field within a packed struct typedef.

class svdoc.ir.Typedef(name: str, doc: str | None, kind: str, base_type: str | None = None, alias_type: str | None = None, values: list[EnumValue] = <factory>, fields: list[StructField] = <factory>)[source]

A single typedef declared in a package: enum, packed struct, or alias.

Variables:
  • kind – One of "enum", "struct", or "alias" — determines which of base_type/values, fields, or alias_type is populated.

  • base_type – For kind="enum", the underlying integer type (e.g. "logic [1:0]").

  • alias_type – For kind="alias", the type being aliased.

  • values – For kind="enum", the enum’s named values in declaration order.

  • fields – For kind="struct", the struct’s fields in declaration order.

class svdoc.ir.Subroutine(name: str, doc: str | None, kind: str, return_type: str | None = None, args: list[Port] = <factory>, qualifiers: list[str] = <factory>)[source]

A function or task declared in a package.

Variables:
  • kind – "function" or "task".

  • return_type – Declared return type; always None for tasks.

  • args – Arguments, reusing Port’s name/direction/type/doc shape.

class svdoc.ir.ClassProperty(name: str, type: str, doc: str | None, qualifiers: list[str] = <factory>)[source]

A data member of a class.

Variables:

qualifiers – Leading qualifiers as written ("rand", "local", "static", …), in source order.

class svdoc.ir.ClassDoc(name: str, doc: str | None, extends: str | None = None, is_virtual: bool = False, params: list[Param] = <factory>, properties: list[ClassProperty] = <factory>, methods: list[Subroutine] = <factory>)[source]

Doc IR for a SystemVerilog class (standalone file or inside a package).

Variables:
  • extends – Base class as written (e.g. "base"), or None.

  • is_virtual – True for virtual class.

  • params – Class parameters, value and type parameters alike.

  • properties – Data members in declaration order.

  • methods – Functions/tasks (including extern prototypes); the qualifiers field carries virtual/static/extern/etc.

class svdoc.ir.PackageDoc(name: str, doc: str | None, typedefs: list[Typedef] = <factory>, subroutines: list[Subroutine] = <factory>, classes: list[ClassDoc] = <factory>)[source]

Doc IR for a single SystemVerilog package: typedefs, subroutines and classes.

class svdoc.ir.ParamValue(name: str, value: str)[source]

A single parameter’s resolved (post-override) value on one instance.

class svdoc.ir.PortConnection(name: str, expr: str | None, interface_instance: str | None = None, modport: str | None = None)[source]

A single port’s connected expression on one instance.

Variables:
  • expr – Connected expression text, for plain data-type ports. None for interface-typed ports (see interface_instance/ modport instead) or unconnected ports.

  • interface_instance – For an interface-typed port, the name of the sibling interface instance it’s bound to (e.g. "hs"); None for plain data-type ports.

  • modport – For an interface-typed port, the modport name used (e.g. "producer"); None for plain data-type ports.

class svdoc.ir.Instance(path: str, name: str, module: str, params: list[ParamValue] = <factory>, connections: list[PortConnection] = <factory>, children: list[Instance] = <factory>, is_interface: bool = False)[source]

One elaborated instance in a module hierarchy.

Variables:
  • path – Full hierarchical path (e.g. "top.g[0].u_leaf2"), unique within the hierarchy even under generate-block array expansion.

  • name – Instance name as written (e.g. "u_leaf2").

  • module – Name of the module/interface definition being instantiated.

  • params – Resolved parameter values (post any #(...) overrides).

  • connections – Port name -> connected expression, in port-list order.

  • children – Instances directly nested inside this one.

  • is_interface – True if this instance is of an interface definition rather than a module.

Parser

AST walker: pyslang SyntaxTree -> Doc IR. See PLAN.md Phase 1 (modules) and Phase 2 (interfaces/modports).

svdoc.parser.parse_module(path: str, include_dirs: list | None = None) → ModuleDoc[source]

Parse a .sv file containing a single module declaration.

Parameters:
  • path – Path to the .sv file. Only the first module found in the file is parsed.

  • include_dirs – Optional directories to search for `include targets that don’t live alongside path (e.g. a shared include/ directory). `include``s resolve automatically without this when the included file is in the same directory as ``path.

Returns:

The module’s ModuleDoc.

Raises:

ValueError – If the file fails to parse cleanly.

svdoc.parser.parse_file(path: str, include_dirs: list | None = None)[source]

Parse a .sv file containing a single module, interface, or package.

Dispatches to parse_module(), parse_interface(), or parse_package() based on the syntax kind of the first top-level declaration found.

Parameters:
  • path – Path to the .sv file.

  • include_dirs – Optional directories to search for `include targets that don’t live alongside path.

Returns:

A ModuleDoc, InterfaceDoc, or PackageDoc, matching whichever construct was found.

Raises:

ValueError – If the file fails to parse cleanly.

svdoc.parser.resolve_types(doc, paths: list, include_dirs: list | None = None) → None[source]

Resolve cross-file port types by elaborating a full Compilation.

Given a ModuleDoc (or InterfaceDoc) already parsed from a single file via parse_module() / parse_file(), and the full list of files it should be elaborated alongside (e.g. packages it imports types from), patches each Port’s type_ref with the fully-qualified "package::type" name for any port whose type resolves to a type defined in another file. Mutates doc in place.

Only meaningful for modules today: a bare interface never becomes an elaborated top-level instance, so this is a no-op when doc.name can’t be found among the compilation’s top instances.

Parameters:
  • doc – An already-parsed module (or interface) doc to patch in place.

  • paths – All .sv files needed to elaborate doc, including the file it was originally parsed from.

  • include_dirs – Optional directories to search for `include targets that don’t live alongside any file in paths (e.g. a shared include/ directory separate from per-module source dirs).

Raises:

ValueError – If any of the given files fails to parse cleanly.

svdoc.parser.build_hierarchy(module_name: str, paths: list, include_dirs: list | None = None) → Instance[source]

Elaborate module_name as top and build its instance hierarchy.

Walks the elaborated instance tree (via Symbol.visit), including instances expanded from generate blocks, and records each instance’s resolved (post-override) parameter values and port connection expressions.

Parameters:
  • module_name – Name of the module to elaborate as the hierarchy root.

  • paths – All .sv files needed to elaborate it (the module itself plus anything it instantiates, directly or transitively).

  • include_dirs – Optional directories to search for `include targets that don’t live alongside any file in paths.

Returns:

The root Instance, with children nested to match the real instantiation tree.

Raises:
  • ValueError – If any of the given files fails to parse cleanly.

  • LookupError – If module_name isn’t found as a top instance after elaboration.

svdoc.parser.parse_interface(path: str, include_dirs: list | None = None) → InterfaceDoc[source]

Parse a .sv file containing a single interface declaration.

Parameters:
  • path – Path to the .sv file. Only the first interface found in the file is parsed.

  • include_dirs – Optional directories to search for `include targets that don’t live alongside path.

Returns:

The interface’s InterfaceDoc, including its signals and modports.

Raises:

ValueError – If the file fails to parse cleanly.

svdoc.parser.parse_package(path: str, include_dirs: list | None = None) → PackageDoc[source]

Parse a .sv file containing a single package declaration.

Parameters:
  • path – Path to the .sv file. Only the first package found in the file is parsed.

  • include_dirs – Optional directories to search for `include targets that don’t live alongside path.

Returns:

The package’s PackageDoc, including its typedefs (enums, structs, aliases) and subroutines (functions, tasks).

Raises:

ValueError – If the file fails to parse cleanly.

svdoc.parser.parse_class(path: str, include_dirs: list | None = None) → ClassDoc[source]

Parse a .sv file whose first top-level declaration is a class.

Parameters:
  • path – Path to the .sv file. Only the first class is parsed.

  • include_dirs – Optional `include search directories.

Returns:

The class’s ClassDoc (parameters, properties, methods, base class).

Raises:

ValueError – If the file fails to parse cleanly.

Markdown renderer

Markdown renderer: Doc IR -> Obsidian-friendly .md string.

svdoc.render_md.render(mod: ModuleDoc, diagrams: bool = False, hierarchy: Instance | None = None) → str[source]

Render a ModuleDoc to an Obsidian-friendly Markdown string.

Parameters:
  • diagrams – Embed the module symbol diagram (and hierarchy if given and it has children) as Mermaid fenced blocks.

  • hierarchy – Root Instance from build_hierarchy.

svdoc.render_md.render_interface(iface: InterfaceDoc) → str[source]

Render an InterfaceDoc to an Obsidian-friendly Markdown string.

svdoc.render_md.render_package(pkg: PackageDoc) → str[source]

Render a PackageDoc to an Obsidian-friendly Markdown string.

svdoc.render_md.render_class(c: ClassDoc) → str[source]

Render a ClassDoc to an Obsidian-friendly Markdown string.

HTML renderer

HTML renderer: Doc IR -> a single self-contained .html page per construct.

Cross-links are convention-based, not verified: a Port/Param whose type_ref is “pkg_name::type_name” links to “pkg_name.html#type_name”, assuming that page was (or will be) generated by rendering pkg_name’s own package file the same way. Nothing here checks that the target page or anchor actually exists.

svdoc.render_html.page(title: str, body_lines: list) → str[source]

Wrap body_lines (raw HTML fragments) in a self-contained HTML page with the shared inline stylesheet. Exposed for svdoc.build, which uses it to render the site’s index.html.

svdoc.render_html.render(mod: ModuleDoc, diagrams: bool = False, hierarchy: Instance | None = None) → str[source]

Render a ModuleDoc to a self-contained HTML page.

Parameters:
  • diagrams – Embed the module symbol diagram (and hierarchy if given and it has children) as <pre class="mermaid"> blocks plus a pinned Mermaid CDN script.

  • hierarchy – Root Instance from build_hierarchy.

svdoc.render_html.render_interface(iface: InterfaceDoc) → str[source]

Render an InterfaceDoc to a self-contained HTML page.

svdoc.render_html.render_package(pkg: PackageDoc) → str[source]

Render a PackageDoc to a self-contained HTML page.

svdoc.render_html.render_class(c: ClassDoc) → str[source]

Render a ClassDoc to a self-contained HTML page.

Diagram renderer

Diagram renderers: Doc/hierarchy IR -> Mermaid or Graphviz dot text.

Both formats are plain text – no graphviz binding or dot binary dependency, matching the existing md/html renderers’ IR-only contract.

svdoc.render_diagram.render_module_symbol_mermaid(mod: ModuleDoc) → str[source]

Single-module box + ports, as a Mermaid flowchart.

svdoc.render_diagram.render_module_symbol_dot(mod: ModuleDoc) → str[source]

Single-module box + ports, as Graphviz dot source.

svdoc.render_diagram.render_hierarchy_mermaid(root: Instance, max_depth: int = 4) → str[source]

Hierarchical instance tree as a Mermaid flowchart, depth-limited.

svdoc.render_diagram.render_hierarchy_dot(root: Instance, max_depth: int = 4) → str[source]

Hierarchical instance tree as Graphviz dot source, depth-limited.

Fixer

svdoc –fix: insert ///< TODO next to undocumented ports/params, and a /** @brief TODO */ stub above an undocumented module, in place. Mirrors ruff –fix — scaffolds missing docs rather than guessing their content.

svdoc.fixer.fix_file(path: str, include_dirs: list | None = None) → bool[source]

Scaffold missing doc comments in place, in path, and save the result.

Inserts ///< TODO next to any undocumented port/parameter, and a /** @brief TODO */ stub above the module/interface/package itself if it has no doc comment. In packages, also scaffolds typedefs (enum values, struct fields) and functions/tasks (and their arguments). Idempotent: re-running on an already-documented file makes no changes.

Parameters:

path – Path to the .sv file to fix in place.

Returns:

True if the file was modified, False if it was already fully documented.

Raises:

ValueError – If the file fails to parse cleanly.

Site builder

svdoc build: a flat multi-page HTML site (one page per construct) with a navigable index, so cross-links between pages always resolve correctly – avoids the relative-path problem of generating standalone pages into whatever directories the source files happen to live in.

svdoc.build.build_site(paths: list, out_dir: str, include_dirs: list | None = None, fmt: str = 'html', diagrams: bool = True, strict: bool = False) → str[source]

Parse every file in paths and write one HTML page per construct into a single flat out_dir, plus an index.html linking to all of them. Because every page lands in the same directory, the convention-based cross-links in svdoc.render_html (pkg_name.html#member) always resolve correctly, regardless of where the source .sv files live.

Parameters:
  • paths – All .sv files to document. Each is parsed independently (one construct per file, same as svdoc.parser.parse_file()); cross-file type resolution runs against the full set for every module.

  • out_dir – Directory to write the site into. Created if missing.

  • include_dirs – Optional directories to search for `include targets that don’t live alongside any file in paths (e.g. a shared include/ directory separate from per-module source dirs).

  • fmt – "html" (default) or "md". Markdown mode writes <name>.md pages cross-linked by relative links plus an index.md of [[wikilinks]] (Obsidian-friendly).

  • diagrams – Embed a module symbol diagram in every module page, plus an instance-hierarchy diagram for modules that instantiate others (Mermaid; False to opt out).

  • strict – Raise on the first file that fails to parse. By default such files (parse errors, or no module/interface/package/class, e.g. program-only files) are skipped with a UserWarning.

Returns:

Path to the written index file.

Raises:

ValueError – If strict and any file fails to parse cleanly.

Pages are named after the construct, so two constructs with the same name (even in different directories) would overwrite each other. The first one (in paths order) wins; later ones are skipped with a warning.

CLI

svdoc CLI (v1): svdoc <file.sv> prints to terminal; –out md/–out html writes a file; svdoc build <files…> writes a multi-page linked HTML site.

svdoc.cli.main(argv=None)[source]

Entry point for the svdoc command (registered via pyproject.toml).

Two modes, dispatched on whether the first argument is the literal build subcommand (argparse subparsers don’t mix cleanly with a same-position positional argument, so this is checked manually rather than via add_subparsers):

  • svdoc <file.sv> [more_files...] — document a single module/interface/ package, optionally resolving cross-file types against more_files, printing Markdown to stdout or writing a single Markdown/HTML file (--out), or scaffolding missing doc comments in place (--fix).

  • svdoc build <files...> --out-dir <dir> — document every given file into a single flat multi-page HTML site (one page per construct plus an index.html), so cross-links between pages always resolve correctly regardless of where the source files live on disk.

Parameters:

argv – Argument list to parse (as sys.argv()[1:] would appear). Defaults to sys.argv[1:] when None.