randomize()/std::randomize() with { ... } (block verbatim), array-method
with (expr), foreach over scoped/hierarchical/indexed arrays, randcase,
wait fork, import as a statement, let declarations, empty call arguments,
general statement labels, and endtask: name; (stray semicolon kept).
Also fixes a pre-existing verification failure for a comment between a
then-branch and its else.
Coverage loop round 1, cluster 8: 772 -> 796 / 820 (94.1% -> 97.1%).
Feature README: docs/features/dv-statements/README.md
Co-Authored-By: Claude Fable 5 <[email protected]>
71 lines
2.5 KiB
Rust
71 lines
2.5 KiB
Rust
//! Wadler-style document IR, the same core idea as `ruff_formatter` (and
|
|
//! prettier): build a tree of texts, line-break opportunities, groups, and
|
|
//! indents; the printer then picks flat or broken layouts based on the
|
|
//! configured line width.
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum Doc {
|
|
/// Verbatim text. Must not contain newlines (use `HardLine`).
|
|
Text(String),
|
|
/// Verbatim text spliced into the *middle* of a line, newlines and all:
|
|
/// the `{ ... }` of a `randomize() with { ... }` constraint block. It
|
|
/// carries its own indentation, so the printer copies it through
|
|
/// untouched and resumes measuring from its last line. Unlike
|
|
/// [`Doc::Verbatim`] it does not force enclosing groups to break — the
|
|
/// text starts on the current line, and what follows it continues on the
|
|
/// line the text ends on.
|
|
Inline(String),
|
|
/// A space when flat, a newline when the enclosing group breaks.
|
|
Line,
|
|
/// Nothing when flat, a newline when the enclosing group breaks.
|
|
SoftLine,
|
|
/// Always a newline. Forces every enclosing group to break.
|
|
HardLine,
|
|
/// Two newlines (one blank line).
|
|
BlankLine,
|
|
/// Content that the printer tries to fit on one line; if it does not
|
|
/// fit (or contains a hard line), every `Line`/`SoftLine` directly
|
|
/// inside becomes a newline.
|
|
Group(Vec<Doc>),
|
|
/// Increase indentation for any line breaks inside.
|
|
Indent(Vec<Doc>),
|
|
/// Sequence with no other effect.
|
|
Concat(Vec<Doc>),
|
|
/// Emitted only when the enclosing group is broken.
|
|
IfBreak(String),
|
|
/// Source bytes copied through untouched, newlines and all, starting at
|
|
/// column 0: the indentation the preceding line break wrote is stripped
|
|
/// first, so the text keeps its own. Always forces enclosing groups to
|
|
/// break. Only valid right after a line break or at the start of the
|
|
/// output — see `docs/features/format-fences/README.md`.
|
|
Verbatim(String),
|
|
}
|
|
|
|
pub fn text(s: impl Into<String>) -> Doc {
|
|
Doc::Text(s.into())
|
|
}
|
|
|
|
pub fn group(docs: Vec<Doc>) -> Doc {
|
|
Doc::Group(docs)
|
|
}
|
|
|
|
pub fn indent(docs: Vec<Doc>) -> Doc {
|
|
Doc::Indent(docs)
|
|
}
|
|
|
|
pub fn concat(docs: Vec<Doc>) -> Doc {
|
|
Doc::Concat(docs)
|
|
}
|
|
|
|
/// Join docs with a separator.
|
|
pub fn join(items: Vec<Doc>, sep: Doc) -> Vec<Doc> {
|
|
let mut out = Vec::new();
|
|
for (i, item) in items.into_iter().enumerate() {
|
|
if i > 0 {
|
|
out.push(sep.clone());
|
|
}
|
|
out.push(item);
|
|
}
|
|
out
|
|
}
|