Files
lucisv/crates/vlog_parser/src/parser/keyword_stmt.rs
T
lucioandClaude Fable 5.1 a69a1cb528 Coverage gaps L3, L6, L10: six language shapes, randsequence as real nodes, an opt-in uvm_field_* indent
TODO §54 (docs/features/coverage-gaps-l); closes docs/coverage_gaps.md
L3, L6 and L10. The first dev stalled under machine load; a second
finished from the tree.

54.1 `->>` with optional delay or event control (`StmtKind::EventTrigger`),
stream `with [range]` (`ExprKind::StreamWith`), the LRM's other
`casting_type`s, drive strength on a net declaration (`DataDecl::strength`,
whose comment slots also fix a pre-existing refusal on an `assign` with
a comment before its strength), `let` at statement position
(`StmtKind::Let`), and `#(min:typ:max)` clocking skews — `ClockingSkew`
as the LRM's two alternatives, `edge [delay]` | `delay` (review rework of
QA's HIGH). Comment slots added to `let`, stream concatenations and the
clocking grammar; the RTL gap fuzzer at 505 gaps, zero failing.
54.2 `randsequence` (it was a parse error; the keyword is new):
`RandSequence` → `Production` → `Alternative` → `ProdItem` (ref, block,
if, repeat, case, rand join), code blocks by the statement grammar; one
production per line, alternatives aligned; the LRM's examples as cases.
54.3 `[format] uvm-utils-indent` / `lucisv.format.uvmUtilsIndent`, default
off, tracking nested `_begin`/`_end` pairs; 21 corpus files change with
it on, all idempotent. A short comment-free enum body already one-lines,
so no key was built — the behaviour is pinned. `[format]` table tests
added (review rework). `->>`'s event control is bound through the shared
`bind_timing` (review rework of QA's second HIGH).

0 of 820 corpus files differ from 0.48.x with the key off; the opentitan
sweep is identical; no corpus or opentitan file writes any of the new
shapes.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-09-07 01:12:49 -05:00

379 lines
16 KiB
Rust

//! The keyword → statement dispatch table.
//!
//! One `match` over every keyword a statement may begin with: each arm is
//! either the whole (short) grammar of that statement or a call into the
//! function in [`super::stmt`] that holds it. It is a table, not a set of
//! productions, which is why it stays one function; and it is half the
//! lines of the statement grammar, which is why it has a file of its own —
//! `stmt.rs` describes the statements, this file describes how a keyword
//! reaches one.
use super::stmt::ForeachTarget;
use super::*;
impl<'a> Parser<'a> {
/// The statement that a keyword at the cursor begins.
///
/// A dispatch table over the statement keywords: one arm per keyword,
/// each arm either the whole (short) grammar or a call to the function
/// that holds it. It stays one function on purpose — split by keyword
/// group it would be the same table read across several places.
pub(super) fn parse_keyword_stmt(&mut self) -> PResult<StmtKind> {
let kw = self.cur().text.clone();
match kw.as_str() {
"begin" => {
self.bump();
let (label_comments, label_name) = self.parse_label_with()?;
let (label, label_span) = split_label(label_name);
let mut stmts = Vec::new();
while !self.at_body_end("end") {
stmts.push(self.stmt_or_recover()?);
}
let dangling = self.take_dangling();
self.expect_closer("end")?;
let (end_label_comments, end_name) = self.parse_label_with()?;
let (end_label, end_label_span) = split_label(end_name);
Ok(StmtKind::Block {
label_comments,
label,
label_span,
stmts,
dangling,
end_label_comments,
end_label,
end_label_span,
})
}
"fork" => {
self.bump();
let (label_comments, label_name) = self.parse_label_with()?;
let (label, label_span) = split_label(label_name);
let mut stmts = Vec::new();
while !self.at_eof()
&& !self.at_join_kw()
&& !(self.tolerant && self.at_sync_stop())
{
stmts.push(self.stmt_or_recover()?);
}
let dangling = self.take_dangling();
let join_kw = if self.tolerant && !self.at_join_kw() {
self.record("expected `join`");
"join".to_string()
} else {
self.bump().text
};
let (end_label_comments, end_name) = self.parse_label_with()?;
let (end_label, end_label_span) = split_label(end_name);
Ok(StmtKind::ForkJoin {
label_comments,
label,
label_span,
join_kw,
stmts,
dangling,
end_label_comments,
end_label,
end_label_span,
})
}
"unique" | "unique0" | "priority" => {
let qualifier = Some(self.bump().text);
if self.cur().is_kw("if") {
self.parse_if_stmt(qualifier)
} else {
self.parse_case_stmt(qualifier)
}
}
"if" => self.parse_if_stmt(None),
"case" | "casex" | "casez" => self.parse_case_stmt(None),
"for" => self.parse_for_stmt(),
"while" => {
self.bump();
self.expect_sym("(")?;
let cond = self.cond_or_recover()?;
let recovered = cond.is_error();
self.expect_after_recovery(")", recovered)?;
Ok(StmtKind::While { cond, body: Box::new(self.body_after_recovery(recovered)?) })
}
"do" => {
self.bump();
let body = Box::new(self.parse_stmt()?);
self.expect_kw("while")?;
let mut comments =
HeaderComments { after_kw: self.take_before_open("("), ..Default::default() };
self.expect_sym("(")?;
comments.after_open = self.take_gap();
let cond = self.cond_or_recover()?;
comments.before_close = self.gap_before(")");
self.expect_after_recovery(")", cond.is_error())?;
self.expect_stmt_semi()?;
Ok(StmtKind::DoWhile { body, cond, comments: Box::new(comments) })
}
"repeat" => {
self.bump();
self.expect_sym("(")?;
let count = self.cond_or_recover()?;
let recovered = count.is_error();
self.expect_after_recovery(")", recovered)?;
Ok(StmtKind::Repeat { count, body: Box::new(self.body_after_recovery(recovered)?) })
}
"forever" => {
self.bump();
Ok(StmtKind::Forever { body: Box::new(self.parse_stmt()?) })
}
"foreach" => {
self.bump();
let mut comments =
HeaderComments { after_kw: self.take_gap(), ..Default::default() };
self.expect_sym("(")?;
comments.after_open = self.take_gap();
let ForeachTarget { expr: target, vars, comments: var_comments } =
self.parse_foreach_target()?;
comments.before_close = self.gap_before(")");
self.expect_sym(")")?;
let body = Box::new(self.parse_stmt()?);
Ok(StmtKind::Foreach {
target,
vars,
var_comments,
comments: Box::new(comments),
body,
})
}
"randcase" => {
self.bump();
let (items, dangling) = self.parse_case_body()?;
Ok(StmtKind::RandCase { items, dangling })
}
"return" => {
self.bump();
let after_kw = self.take_gap();
let expr = if self.cur().is_sym(";") { None } else { Some(self.parse_expr()?) };
self.expect_stmt_semi()?;
Ok(StmtKind::Return { after_kw, expr })
}
"break" => {
self.bump();
self.expect_stmt_semi()?;
Ok(StmtKind::Break)
}
"continue" => {
self.bump();
self.expect_stmt_semi()?;
Ok(StmtKind::Continue)
}
"disable" => {
self.bump();
// `disable fork;` kills every process the enclosing `fork`
// started; `fork` is a keyword, not a block name.
if self.cur().is_kw("fork") {
let name_span = self.cur().span;
self.bump();
self.expect_stmt_semi()?;
return Ok(StmtKind::Disable { name: "fork".to_string(), name_span });
}
let start = self.here();
let mut name = self.expect_ident()?;
while self.cur().is_sym(".") {
self.bump();
name.push('.');
name.push_str(&self.expect_ident()?);
}
let name_span = self.span_from(start);
self.expect_stmt_semi()?;
Ok(StmtKind::Disable { name, name_span })
}
"wait" => {
self.bump();
// `wait fork;` waits for the enclosing `fork`'s children
// (IEEE 1800 §9.6.1); `fork` is a keyword, not a condition.
if self.cur().is_kw("fork") {
self.bump();
self.expect_stmt_semi()?;
return Ok(StmtKind::WaitFork);
}
self.expect_sym("(")?;
let cond = self.parse_expr()?;
self.expect_sym(")")?;
let stmt =
if self.eat_stmt_semi() { None } else { Some(Box::new(self.parse_stmt()?)) };
Ok(StmtKind::Wait { cond, stmt })
}
"assert" | "assume" | "cover" => {
let kw = self.bump().text;
self.expect_sym("(")?;
let cond = self.parse_expr()?;
self.expect_sym(")")?;
let (then, els) = if self.eat_stmt_semi() {
(None, None)
} else if self.eat_kw("else") {
(None, Some(Box::new(self.parse_stmt()?)))
} else {
let then = Box::new(self.parse_stmt()?);
let els =
if self.eat_kw("else") { Some(Box::new(self.parse_stmt()?)) } else { None };
(Some(then), els)
};
Ok(StmtKind::Assert { kw, cond, then, els })
}
"force" => {
self.bump();
let lhs = self.parse_unary()?;
self.expect_sym("=")?;
let rhs = self.parse_expr()?;
self.expect_stmt_semi()?;
Ok(StmtKind::ForceRelease { kw: "force".into(), lhs, rhs: Some(rhs) })
}
"release" | "deassign" => {
let kw = self.bump().text;
let lhs = self.parse_unary()?;
self.expect_stmt_semi()?;
Ok(StmtKind::ForceRelease { kw, lhs, rhs: None })
}
"parameter" | "localparam" => {
let d = self.parse_param_decl()?;
self.expect_stmt_semi()?;
Ok(StmtKind::ParamDecl(d))
}
// A package import standing among a block's declarations
// (IEEE 1800 §13.4.2): the same statement the item level takes.
"import" => Ok(StmtKind::Import(self.parse_import_list()?)),
// A block-scoped `let` (IEEE 1800 §11.13). `parse_let` reads the
// `;` itself, which is why `takes_semi()` says this kind takes
// none — exactly as the item-level spelling works.
"let" => Ok(StmtKind::Let(Box::new(self.parse_let()?))),
// `randsequence` reads its own `endsequence`, so like a block it
// takes no `;` of its own (`takes_semi()` says so).
"randsequence" => Ok(StmtKind::RandSequence(Box::new(self.parse_randsequence()?))),
// A block-scoped type (IEEE 1800 §6.18), e.g.
// `spi_host/dv/env/seq_lib/spi_host_base_vseq.sv:577`. Same
// grammar and same layout as the item-level `typedef`.
"typedef" => Ok(StmtKind::Typedef(self.parse_typedef()?)),
"input" | "output" | "inout" => {
let d = self.parse_port_decl()?;
Ok(StmtKind::PortDecl(d))
}
// `virtual` here is a local variable of virtual-interface type
// (`virtual my_if h;`), the only class-member qualifier that can
// also start a statement.
_ if NET_KEYWORDS.contains(&kw.as_str())
|| TYPE_KEYWORDS.contains(&kw.as_str())
|| matches!(
kw.as_str(),
"var"
| "automatic"
| "static"
| "const"
| "virtual"
| "enum"
| "struct"
| "union"
) =>
{
// A type keyword followed by `'` is a cast expression, not a
// declaration.
if self.nth(1).is_sym("'") {
return self.parse_assign_or_expr_stmt();
}
let d = self.parse_data_decl()?;
Ok(StmtKind::VarDecl(d))
}
_ => self.error(format!("unsupported statement starting with keyword `{kw}`")),
}
}
/// `for ( init ; cond ; step ) statement` (IEEE 1800 §12.7.1).
///
/// The three sections live inside one bracket, so in tolerant mode they
/// recover as one: whatever failed is reported once, the rest of the
/// header becomes the loop's [`ExprKind::Error`] condition, and the body
/// is parsed as usual. The init entries read before the failure are kept
/// — they are what declares the loop variable the body uses.
fn parse_for_stmt(&mut self) -> PResult<StmtKind> {
self.bump();
let mut comments = ForComments { after_kw: self.take_gap(), ..Default::default() };
self.expect_sym("(")?;
comments.after_open = self.take_gap();
let mut init = Vec::new();
let mut step = Vec::new();
let start_pos = self.pos;
let start = self.here();
let cond = match self.parse_for_header(&mut comments, &mut init, &mut step) {
Ok(cond) => cond,
Err(e) if self.tolerant => Some(self.error_expr(start_pos, start, e)),
Err(e) => return Err(e),
};
let recovered = cond.as_ref().is_some_and(Expr::is_error);
if !recovered {
comments.before_close = self.gap_before(")");
}
self.expect_after_recovery(")", recovered)?;
let body = Box::new(self.body_after_recovery(recovered)?);
Ok(StmtKind::For { init, cond, step, comments: Box::new(comments), body })
}
/// The three sections between a `for` header's brackets, filling `init`
/// and `step` as it goes so a failure keeps what was read before it.
fn parse_for_header(
&mut self,
comments: &mut ForComments,
init: &mut Vec<ForInit>,
step: &mut Vec<ForStep>,
) -> PResult<Option<Expr>> {
if !self.cur().is_sym(";") {
loop {
// Each init may introduce its own variable, and its type may
// be a keyword or a typedef'd name:
// `for (int i = 0, StateEnumT t = t.first(); ...)`
// (prim_sparse_fsm_flop.sv:48). An identifier only counts as a
// type when another identifier follows it — `for (i = 0; ...)`
// assigns to an existing `i`.
let ty = {
let t = self.cur();
let kw_type =
t.kind == TokenKind::Keyword && TYPE_KEYWORDS.contains(&t.text.as_str());
if kw_type || (t.kind == TokenKind::Ident && self.at_typed_declarator()) {
Some(self.parse_opt_data_type()?)
} else {
None
}
};
let before_name = self.take_gap();
let lhs = self.parse_unary()?;
let mut op_comments =
BinaryComments { before: self.gap_before("="), after: Vec::new() };
self.expect_sym("=")?;
op_comments.after = self.take_gap();
let rhs = self.parse_expr()?;
init.push(ForInit {
ty,
before_name,
assign: SimpleAssign {
lhs,
op: "=".to_string(),
op_comments: Box::new(op_comments),
rhs,
},
});
if !self.eat_sym(",") {
break;
}
}
}
comments.before_first_semi = self.gap_before(";");
self.expect_sym(";")?;
comments.after_first_semi = self.take_gap();
let cond = if self.cur().is_sym(";") { None } else { Some(self.parse_expr()?) };
comments.before_second_semi = self.gap_before(";");
self.expect_sym(";")?;
comments.after_second_semi = self.take_gap();
if !self.cur().is_sym(")") {
step.push(self.parse_for_step()?);
while self.eat_sym(",") {
step.push(self.parse_for_step()?);
}
}
Ok(cond)
}
}