Skip to content

Verilog

See Formatting Verilog for the command-line tool. digsim.verilog only uses the Python standard library.

VerilogFormatter

VerilogFormatter(indent: int = 4, blank_lines: int = 1)

Formats Verilog and SystemVerilog. Only the whitespace changes: the indentation of each line, whitespace at the end of lines, and blank lines.

  • A block (module, begin, case, function and so on, to its end keyword) and a bracket that is open at the end of a line indent the lines in it one step.
  • A line that doesn't end a statement (like if (a) or assign y = a &) indents the next line one step.
  • Tabs at the start of a line become spaces, there is at most blank_lines empty lines in a row, and the text ends with one newline.

Comments and strings are not changed, and lines that a `define continues are kept as they are. The lines of a block comment move with its first line.

Parameters:

Name Type Description Default
indent int

The number of spaces for one step of indentation.

4
blank_lines int

The most empty lines in a row.

1

format

format(text: str) -> str

The formatted text. The newlines are the ones of the text (\n or \r\n).

Raises:

Type Description
VerilogFormatError

If formatting would change the tokens of the text. That is a bug in the formatter, and the text is not formatted.

VerilogFormatError

Bases: ValueError

The formatted text would not have the same tokens, so it is not formatted

tokenize

tokenize(text: str) -> list[Token]

The tokens of a Verilog text, line by line. A block comment over several lines is a comment token on each line. The newlines are not tokens.

tokenize_line

tokenize_line(
    text: str, state: LexState = CODE, line: int = 0
) -> tuple[list[Token], LexState]

The tokens of one line (without its newline), and the state the next line starts in.

Parameters:

Name Type Description Default
text str

The line.

required
state LexState

The state the line starts in, from the line before it.

CODE
line int

The line number for the tokens.

0

Token dataclass

Token(kind: TokenKind, text: str, line: int, column: int)

A token of a line.

Parameters:

Name Type Description Default
kind TokenKind

The kind of token.

required
text str

The text of the token.

required
line int

The line number, from 0.

required
column int

The column in the line, from 0.

required

TokenKind

Bases: StrEnum

The kind of a token

LexState

Bases: Enum

What the next line starts in