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
Noneif unset.doc – Extracted doc comment, or
Noneif undocumented.type_ref – Fully-qualified
"package::type"name if resolved cross-file viasvdoc.parser.resolve_types();Noneotherwise.
- 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
Noneif undocumented.type_ref – Fully-qualified
"package::type"name if resolved cross-file viasvdoc.parser.resolve_types();Noneotherwise.modport_preview – For an interface-typed port (
direction == "interface"), the resolvedModportfrom the interface file, if it was found among the files passed tosvdoc.parser.resolve_types();Noneotherwise.
- 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
modportdeclaration 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
enumtypedef.
- class svdoc.ir.StructField(name: str, type: str, doc: str | None)[source]¶
A single field within a packed
structtypedef.
- 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
typedefdeclared in a package: enum, packed struct, or alias.- Variables:
kind – One of
"enum","struct", or"alias"— determines which ofbase_type/values,fields, oralias_typeis 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
functionortaskdeclared in a package.- Variables:
kind –
"function"or"task".return_type – Declared return type; always
Nonefor 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"), orNone.is_virtual –
Trueforvirtual class.params – Class parameters, value and
typeparameters alike.properties – Data members in declaration order.
methods – Functions/tasks (including
externprototypes); thequalifiersfield carriesvirtual/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.
Nonefor interface-typed ports (seeinterface_instance/modportinstead) or unconnected ports.interface_instance – For an interface-typed port, the name of the sibling interface instance it’s bound to (e.g.
"hs");Nonefor plain data-type ports.modport – For an interface-typed port, the modport name used (e.g.
"producer");Nonefor 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 –
Trueif this instance is of aninterfacedefinition rather than amodule.
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
.svfile containing a single module declaration.- Parameters:
path – Path to the
.svfile. Only the first module found in the file is parsed.include_dirs – Optional directories to search for
`includetargets that don’t live alongsidepath(e.g. a sharedinclude/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
.svfile containing a single module, interface, or package.Dispatches to
parse_module(),parse_interface(), orparse_package()based on the syntax kind of the first top-level declaration found.- Parameters:
path – Path to the
.svfile.include_dirs – Optional directories to search for
`includetargets that don’t live alongsidepath.
- Returns:
A
ModuleDoc,InterfaceDoc, orPackageDoc, 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(orInterfaceDoc) already parsed from a single file viaparse_module()/parse_file(), and the full list of files it should be elaborated alongside (e.g. packages it imports types from), patches eachPort’stype_refwith the fully-qualified"package::type"name for any port whose type resolves to a type defined in another file. Mutatesdocin place.Only meaningful for modules today: a bare interface never becomes an elaborated top-level instance, so this is a no-op when
doc.namecan’t be found among the compilation’s top instances.- Parameters:
doc – An already-parsed module (or interface) doc to patch in place.
paths – All
.svfiles needed to elaboratedoc, including the file it was originally parsed from.include_dirs – Optional directories to search for
`includetargets that don’t live alongside any file inpaths(e.g. a sharedinclude/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_nameas top and build its instance hierarchy.Walks the elaborated instance tree (via
Symbol.visit), including instances expanded fromgenerateblocks, 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
.svfiles needed to elaborate it (the module itself plus anything it instantiates, directly or transitively).include_dirs – Optional directories to search for
`includetargets that don’t live alongside any file inpaths.
- Returns:
The root
Instance, withchildrennested to match the real instantiation tree.- Raises:
ValueError – If any of the given files fails to parse cleanly.
LookupError – If
module_nameisn’t found as a top instance after elaboration.
- svdoc.parser.parse_interface(path: str, include_dirs: list | None = None) InterfaceDoc[source]¶
Parse a
.svfile containing a single interface declaration.- Parameters:
path – Path to the
.svfile. Only the first interface found in the file is parsed.include_dirs – Optional directories to search for
`includetargets that don’t live alongsidepath.
- 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
.svfile containing a single package declaration.- Parameters:
path – Path to the
.svfile. Only the first package found in the file is parsed.include_dirs – Optional directories to search for
`includetargets that don’t live alongsidepath.
- 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
.svfile whose first top-level declaration is a class.- Parameters:
path – Path to the
.svfile. Only the first class is parsed.include_dirs – Optional
`includesearch 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
ModuleDocto an Obsidian-friendly Markdown string.- Parameters:
diagrams – Embed the module symbol diagram (and
hierarchyif given and it has children) as Mermaid fenced blocks.hierarchy – Root
Instancefrombuild_hierarchy.
- svdoc.render_md.render_interface(iface: InterfaceDoc) str[source]¶
Render an
InterfaceDocto an Obsidian-friendly Markdown string.
- svdoc.render_md.render_package(pkg: PackageDoc) str[source]¶
Render a
PackageDocto 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 forsvdoc.build, which uses it to render the site’sindex.html.
- svdoc.render_html.render(mod: ModuleDoc, diagrams: bool = False, hierarchy: Instance | None = None) str[source]¶
Render a
ModuleDocto a self-contained HTML page.- Parameters:
diagrams – Embed the module symbol diagram (and
hierarchyif given and it has children) as<pre class="mermaid">blocks plus a pinned Mermaid CDN script.hierarchy – Root
Instancefrombuild_hierarchy.
- svdoc.render_html.render_interface(iface: InterfaceDoc) str[source]¶
Render an
InterfaceDocto a self-contained HTML page.
- svdoc.render_html.render_package(pkg: PackageDoc) str[source]¶
Render a
PackageDocto 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.
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
///< TODOnext 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
.svfile to fix in place.- Returns:
Trueif the file was modified,Falseif 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
pathsand write one HTML page per construct into a single flatout_dir, plus anindex.htmllinking to all of them. Because every page lands in the same directory, the convention-based cross-links insvdoc.render_html(pkg_name.html#member) always resolve correctly, regardless of where the source.svfiles live.- Parameters:
paths – All
.svfiles to document. Each is parsed independently (one construct per file, same assvdoc.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
`includetargets that don’t live alongside any file inpaths(e.g. a sharedinclude/directory separate from per-module source dirs).fmt –
"html"(default) or"md". Markdown mode writes<name>.mdpages cross-linked by relative links plus anindex.mdof[[wikilinks]](Obsidian-friendly).diagrams – Embed a module symbol diagram in every module page, plus an instance-hierarchy diagram for modules that instantiate others (Mermaid;
Falseto 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
strictand 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
pathsorder) 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
svdoccommand (registered viapyproject.toml).Two modes, dispatched on whether the first argument is the literal
buildsubcommand (argparse subparsers don’t mix cleanly with a same-position positional argument, so this is checked manually rather than viaadd_subparsers):svdoc <file.sv> [more_files...]— document a single module/interface/ package, optionally resolving cross-file types againstmore_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 anindex.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 tosys.argv[1:]whenNone.