hydra.serialization module

Utilities for constructing generic program code ASTs, used for the serialization phase of source code generation.

hydra.serialization.angle_braces_list(style: BlockStyle, els: Sequence[Expr]) Expr

Comma-separate the elements inside angle brackets in the given block style; renders as <> when empty.

hydra.serialization.braces_list_adaptive(els: Sequence[Expr]) Expr

Produce a bracketed list which separates elements by spaces or newlines depending on the estimated width of the expression.

hydra.serialization.bracket_list(style: BlockStyle, els: Sequence[Expr]) Expr

Comma-separate the elements inside square brackets in the given block style; renders as [] when empty.

hydra.serialization.bracket_list_adaptive(els: Sequence[Expr]) Expr

Produce a bracketed list which separates elements by spaces or newlines depending on the estimated width of the expression.

hydra.serialization.brackets(br: Brackets, style: BlockStyle, e: Expr) Expr

Wrap an expression in the given bracket pair using the given block style.

hydra.serialization.choose_layout(threshold: int, inline_expr: Expr, block_expr: Expr) Expr

Pick between an inline and a multi-line layout for the same logical expression. Returns the inline form if its estimated width does not exceed the threshold, otherwise the block form. Note: the threshold is measured against the inline expression in isolation; surrounding indentation may push content beyond the threshold at print time. Callers that know they are nested can pass a smaller threshold to compensate.

hydra.serialization.comma_sep(v1: BlockStyle, v2: Sequence[Expr]) Expr

Separate elements with a comma, using the given block style.

hydra.serialization.comma_sep_adaptive(els: Sequence[Expr]) Expr

Comma-separate elements inline if the joined width fits, otherwise break onto separate lines.

hydra.serialization.cst(s: str) Expr

Construct a constant expression from a literal string.

hydra.serialization.curly_block(style: BlockStyle, e: Expr) Expr

Wrap a single expression in curly braces using the given block style.

hydra.serialization.curly_braces_list(msymb: object, style: BlockStyle, els: Sequence[Expr]) Expr

Separate the elements inside curly braces using the given symbol (default ,) in the given block style; renders as {} when empty.

hydra.serialization.custom_indent(idt: str, s: str) str

Indent every non-empty line of s by idt. Empty lines stay empty (no trailing whitespace) so downstream byte-identity checks don’t care about indent depth.

hydra.serialization.custom_indent_block(idt: str, els: Sequence[Expr]) Expr

Render a list of expressions as a block whose first element starts where the surrounding context placed it and subsequent elements break onto fresh lines indented by the given prefix.

hydra.serialization.dot_sep(v1: Sequence[Expr]) Expr

Separate elements with a dot and no surrounding whitespace.

hydra.serialization.double_newline_sep(v1: Sequence[Expr]) Expr

Separate elements with a blank line between them.

hydra.serialization.expression_length(e: Expr) int

Find the approximate length (number of characters, including spaces and newlines) of an expression without actually printing it.

hydra.serialization.ifx(op: Op, lhs: Expr, rhs: Expr) Expr

Build an infix expression with the given operator and two operands.

hydra.serialization.indent(v1: str) str

Indent every non-empty line of a string by doubleSpace.

hydra.serialization.indent_block(v1: Sequence[Expr]) Expr

Like customIndentBlock, but using doubleSpace as the indent prefix.

hydra.serialization.indent_subsequent_lines(idt: str, e: Expr) Expr

Indent every line of an expression except the first by the given prefix.

hydra.serialization.infix_ws(op: str, l: Expr, r: Expr) Expr

Space-separate three elements: left operand, operator string, right operand.

hydra.serialization.infix_ws_list(op: str, opers: Sequence[Expr]) Expr

Space-separate operands interleaved with the given operator string.

hydra.serialization.newline_sep(v1: Sequence[Expr]) Expr

Separate elements by a newline (no leading whitespace).

hydra.serialization.no_sep(v1: Sequence[Expr]) Expr

Concatenate elements with no separator and no whitespace.

hydra.serialization.num(i: int) Expr

Construct a constant expression from an int32.

hydra.serialization.op(s: str, p: int, assoc: Associativity) Op

Build an op with single-space padding from a symbol, precedence, and associativity.

hydra.serialization.or_op(newlines: bool) Op

Build the | alternative-separator operator. The flag selects whether the right side breaks onto a new line.

hydra.serialization.or_sep(style: BlockStyle, l: Sequence[Expr]) Expr

Separate alternatives with |, breaking onto separate lines when the block style requests a newline before content.

hydra.serialization.paren_list(newlines: bool, els: Sequence[Expr]) Expr

Comma-separate the elements inside parentheses; switches to a half-block style when newlines are requested and there is more than one element. Renders as () when empty.

hydra.serialization.paren_list_adaptive(els: Sequence[Expr]) Expr

Produce a parenthesized list which separates elements by spaces or newlines depending on the estimated width of the expression.

hydra.serialization.parens(v1: Expr) Expr

Wrap an expression in inline parentheses.

hydra.serialization.parenthesize(exp: Expr) Expr

Recursively insert parentheses around subexpressions where required by operator precedence and associativity. The traversal descends into bracket, indent, sequence, and operator expressions and parenthesizes left or right operands whose binding is weaker than the surrounding operator.

hydra.serialization.prefix(p: str, expr: Expr) Expr

Prepend a prefix string to an expression with no surrounding whitespace.

hydra.serialization.print_expr(e: Expr) str

Render an expression to a string, expanding bracket pairs, block styles, indents, and operator chains.

hydra.serialization.semicolon_sep(v1: Sequence[Expr]) Expr

Separate elements with ; inline.

hydra.serialization.sep(op: Op, els: Sequence[Expr]) Expr

Combine a list of expressions into a single OpExpr chain using the given operator. Returns the empty constant for an empty list.

hydra.serialization.space_sep(v1: Sequence[Expr]) Expr

Space-separate elements (no break between them).

hydra.serialization.space_sep_adaptive(els: Sequence[Expr]) Expr

Space-separate elements inline if the joined width fits, otherwise newline-separate them. Useful for long signatures, type chains, and other space-joined sequences.

hydra.serialization.structural_sep(op: Op, els: Sequence[Expr]) Expr

Like sep, but produces a SeqExpr instead of an OpExpr chain. SeqExpr is treated as structural layout and is not subject to parenthesization.

hydra.serialization.structural_space_sep(v1: Sequence[Expr]) Expr

Like spaceSep, but produces a SeqExpr. Use for structural layout that should not trigger parenthesization of children.

hydra.serialization.suffix(s: str, expr: Expr) Expr

Append a suffix string to an expression.

hydra.serialization.sym(s: str) Symbol

Construct a Symbol from a string.

hydra.serialization.symbol_sep(symb: str, style: BlockStyle, l: Sequence[Expr]) Expr

Separate elements with the given symbol; trailing whitespace per element is determined by the block style’s newline-before/after flags (space, single break, or double break).

hydra.serialization.tab_indent(e: Expr) Expr

Indent every line of an expression by four spaces.

hydra.serialization.tab_indent_double_space(exprs: Sequence[Expr]) Expr

Tab-indent a list of expressions, separated by blank lines.

hydra.serialization.tab_indent_single_space(exprs: Sequence[Expr]) Expr

Tab-indent a list of expressions, separated by single newlines.

hydra.serialization.unsupported_type(label: str) Expr

Render a placeholder for a type the writer does not yet handle, e.g. [bigint].

hydra.serialization.unsupported_variant(label: str, obj: str) Expr

Render a placeholder for an unsupported variant, including a string representation of the offending value.

hydra.serialization.with_comma(e: Expr) Expr

Append a trailing comma to an expression.

hydra.serialization.with_semi(e: Expr) Expr

Append a trailing semicolon to an expression.