Add concurrent assertions, sequence and property as verbatim items
assert/assume/cover/restrict property (...) [action block], optionally labeled, at item and statement position; sequence...endsequence and property...endproperty declarations. Verbatim pass-through like constraint/covergroup; verify.rs untouched, attack tests added. Coverage loop round 1, cluster 4: 699 -> 712 / 820 (85.2% -> 86.8%). Feature README: docs/features/assertions/README.md Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
f7c66c79bf
commit
e24d33afab
9 files changed
+938
-7
No files matched your search
@@ -1055,6 +1055,12 @@ impl<'a> Parser<'a> {
|
||||
TokenKind::Directive => Ok(ItemKind::Directive(self.take_raw_text())),
|
||||
TokenKind::Keyword => self.parse_keyword_item(),
|
||||
TokenKind::Ident if self.at_bind_directive() => self.parse_bind().map(ItemKind::Bind),
|
||||
// `NoNMI: assume property (...)` and `restrict property (...)`:
|
||||
// a label, or the non-keyword `restrict`, in front of a
|
||||
// concurrent assertion.
|
||||
TokenKind::Ident if self.at_concurrent_assertion() => {
|
||||
self.parse_concurrent_assertion().map(ItemKind::Verbatim)
|
||||
}
|
||||
TokenKind::Ident => self.parse_ident_item(),
|
||||
TokenKind::SystemIdent if ELAB_TASKS.contains(&t.text.as_str()) => {
|
||||
let call = self.parse_expr()?;
|
||||
@@ -1139,6 +1145,10 @@ impl<'a> Parser<'a> {
|
||||
"class" => self.parse_class().map(ItemKind::Class),
|
||||
"constraint" => self.parse_constraint().map(ItemKind::Verbatim),
|
||||
"covergroup" => self.parse_covergroup().map(ItemKind::Verbatim),
|
||||
"assert" | "assume" | "cover" if self.at_concurrent_assertion() => {
|
||||
self.parse_concurrent_assertion().map(ItemKind::Verbatim)
|
||||
}
|
||||
"property" | "sequence" => self.parse_property_decl().map(ItemKind::Verbatim),
|
||||
// A run of member qualifiers (`virtual`, `local rand`, `static`,
|
||||
// `pure virtual`, ...) does not say what it qualifies; the first
|
||||
// keyword past the run does. Nothing is consumed here, so each
|
||||
@@ -1504,6 +1514,125 @@ impl<'a> Parser<'a> {
|
||||
Ok(self.verbatim_since(start, col))
|
||||
}
|
||||
|
||||
/// True when the cursor opens a concurrent assertion (IEEE 1800 §16.14):
|
||||
/// `[label :] assert|assume|cover|restrict property ( ... )`.
|
||||
///
|
||||
/// The `property` keyword is what makes this unambiguous, and demanding
|
||||
/// it is what leaves the *immediate* assertion path (`assert (expr) ...`,
|
||||
/// which is a real statement node) exactly as it was. `restrict` is not a
|
||||
/// lexer keyword — it is an ordinary identifier everywhere else — so it is
|
||||
/// matched by text, which is safe for the same reason: no type or instance
|
||||
/// name can be followed by the `property` keyword.
|
||||
fn at_concurrent_assertion(&self) -> bool {
|
||||
let mut i = 0;
|
||||
if self.cur().kind == TokenKind::Ident && self.nth(1).is_sym(":") {
|
||||
i = 2;
|
||||
}
|
||||
let head = self.nth(i);
|
||||
let opens = head.is_kw("assert")
|
||||
|| head.is_kw("assume")
|
||||
|| head.is_kw("cover")
|
||||
|| (head.kind == TokenKind::Ident && head.text == "restrict");
|
||||
opens && self.nth(i + 1).is_kw("property")
|
||||
}
|
||||
|
||||
/// A concurrent assertion, kept as verbatim source.
|
||||
///
|
||||
/// `assert property (@(posedge clk) a |-> ##[1:3] b) else $error("...");`
|
||||
/// — the property expression is a grammar of its own (`|->`, `##[1:3]`,
|
||||
/// `throughout`, `disable iff`, `$rose`) that vfmt models none of, and
|
||||
/// there is no layout convention worth inventing for it, so the whole
|
||||
/// item is copied out of the source by span exactly as a `constraint` is.
|
||||
fn parse_concurrent_assertion(&mut self) -> PResult<RawText> {
|
||||
let start = self.here();
|
||||
let col = self.cur().col;
|
||||
if self.cur().kind == TokenKind::Ident && self.nth(1).is_sym(":") {
|
||||
self.bump();
|
||||
self.bump();
|
||||
}
|
||||
// `assert` / `assume` / `cover` / `restrict`.
|
||||
self.bump();
|
||||
self.expect_kw("property")?;
|
||||
self.expect_sym("(")?;
|
||||
self.skip_balanced("(", ")")?;
|
||||
self.skip_action_block()?;
|
||||
Ok(self.verbatim_since(start, col))
|
||||
}
|
||||
|
||||
/// The action block after a concurrent assertion's property spec: a bare
|
||||
/// `;`, a pass statement, an `else` fail statement, or both.
|
||||
fn skip_action_block(&mut self) -> PResult<()> {
|
||||
if self.eat_kw("else") {
|
||||
return self.skip_action_stmt();
|
||||
}
|
||||
self.skip_action_stmt()?;
|
||||
if self.eat_kw("else") {
|
||||
self.skip_action_stmt()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// One `statement_or_null` of an action block, skipped verbatim: a
|
||||
/// `begin ... end` block, or everything through the next `;`. Brackets are
|
||||
/// skipped as units so a `;` written inside one cannot end the scan early.
|
||||
fn skip_action_stmt(&mut self) -> PResult<()> {
|
||||
if self.cur().is_kw("begin") {
|
||||
self.bump();
|
||||
let mut depth = 1usize;
|
||||
while depth > 0 {
|
||||
if self.at_eof() {
|
||||
return self.error("expected `end`");
|
||||
}
|
||||
let t = self.cur();
|
||||
if t.is_kw("begin") {
|
||||
depth += 1;
|
||||
} else if t.is_kw("end") {
|
||||
depth -= 1;
|
||||
}
|
||||
self.bump();
|
||||
}
|
||||
self.parse_label()?;
|
||||
return Ok(());
|
||||
}
|
||||
loop {
|
||||
if self.at_eof() {
|
||||
return self.error("expected `;`");
|
||||
}
|
||||
let t = self.cur();
|
||||
if t.is_sym("(") {
|
||||
self.bump();
|
||||
self.skip_balanced("(", ")")?;
|
||||
} else if t.is_sym("[") {
|
||||
self.bump();
|
||||
self.skip_balanced("[", "]")?;
|
||||
} else if t.is_sym("{") {
|
||||
self.bump();
|
||||
self.skip_balanced("{", "}")?;
|
||||
} else if self.eat_sym(";") {
|
||||
return Ok(());
|
||||
} else {
|
||||
self.bump();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `property name [( ... )]; ... endproperty [: name]`, and its
|
||||
/// `sequence` / `endsequence` twin, kept as verbatim source for the reason
|
||||
/// [`Parser::parse_concurrent_assertion`] gives — the body is written in
|
||||
/// the same second grammar, with local variable declarations mixed in.
|
||||
fn parse_property_decl(&mut self) -> PResult<RawText> {
|
||||
let start = self.here();
|
||||
let col = self.cur().col;
|
||||
let end_kw = if self.cur().is_kw("sequence") { "endsequence" } else { "endproperty" };
|
||||
self.bump();
|
||||
while !self.at_eof() && !self.cur().is_kw(end_kw) {
|
||||
self.bump();
|
||||
}
|
||||
self.expect_closer(end_kw)?;
|
||||
self.parse_label()?;
|
||||
Ok(self.verbatim_since(start, col))
|
||||
}
|
||||
|
||||
/// True when the cursor opens a parameterized type's `#( ... )`.
|
||||
fn at_type_params(&self) -> bool {
|
||||
self.cur().is_sym("#") && self.nth(1).is_sym("(")
|
||||
@@ -2773,6 +2902,12 @@ impl<'a> Parser<'a> {
|
||||
if self.at_macro_item() || self.at_spaced_macro_call() {
|
||||
return Ok(StmtKind::MacroStmt(self.parse_macro_call()?));
|
||||
}
|
||||
// A concurrent assertion is legal wherever a statement is, and the
|
||||
// `property` keyword is what tells it apart from the immediate
|
||||
// `assert (expr)` handled below.
|
||||
if self.at_concurrent_assertion() {
|
||||
return Ok(StmtKind::Verbatim(self.parse_concurrent_assertion()?));
|
||||
}
|
||||
let t = self.cur();
|
||||
if t.kind == TokenKind::Directive {
|
||||
return Ok(StmtKind::Directive(self.take_raw_text()));
|
||||
@@ -4848,6 +4983,120 @@ mod tests {
|
||||
assert_eq!(items[0].meta.trailing.as_ref().expect("the comment survived").text, "// why");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Concurrent assertions (`docs/features/assertions/README.md`)
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn a_concurrent_assertion_is_captured_verbatim() {
|
||||
let src = "module m;\n assert property (@(posedge clk) a |-> ##[1:3] b);\nendmodule\n";
|
||||
let f = parse_ok(src);
|
||||
let ItemKind::Verbatim(raw) = &module_of(&f).items[0].kind else {
|
||||
panic!("expected a verbatim block")
|
||||
};
|
||||
assert_eq!(raw.text, "assert property (@(posedge clk) a |-> ##[1:3] b);");
|
||||
assert_eq!(raw.text, src[raw.span.as_range()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_concurrent_assertion_keyword_parses_at_item_position() {
|
||||
// `restrict` is not a lexer keyword — it reaches the item dispatch as
|
||||
// an identifier — so it is worth pinning next to the other three.
|
||||
for kw in ["assert", "assume", "cover", "restrict"] {
|
||||
let f = parse_ok(&format!("module m;\n {kw} property (a |-> b);\nendmodule\n"));
|
||||
assert!(
|
||||
matches!(module_of(&f).items[0].kind, ItemKind::Verbatim(_)),
|
||||
"`{kw} property` did not parse as a verbatim item"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_label_before_a_concurrent_assertion_is_part_of_the_capture() {
|
||||
let f = parse_ok("NoNMI: assume property (~irq_nm_i);\n");
|
||||
let ItemKind::Verbatim(raw) = &f.items[0].kind else { panic!("expected a verbatim block") };
|
||||
assert_eq!(raw.text, "NoNMI: assume property (~irq_nm_i);");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_assertion_action_block_is_part_of_the_capture() {
|
||||
let f = parse_ok("module m;\n assert property (a) else $error(\"no\");\nendmodule\n");
|
||||
let ItemKind::Verbatim(raw) = &module_of(&f).items[0].kind else {
|
||||
panic!("expected a verbatim block")
|
||||
};
|
||||
assert_eq!(raw.text, "assert property (a) else $error(\"no\");");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_assertion_action_block_may_be_a_begin_end_block() {
|
||||
let src = "module m;\n assert property (a) else begin\n $error(\"no\");\n end\n wire w;\nendmodule\n";
|
||||
let f = parse_ok(src);
|
||||
let items = &module_of(&f).items;
|
||||
let ItemKind::Verbatim(raw) = &items[0].kind else { panic!("expected a verbatim block") };
|
||||
assert_eq!(raw.text, "assert property (a) else begin\n $error(\"no\");\n end");
|
||||
assert!(matches!(items[1].kind, ItemKind::DataDecl(_)), "the next item was swallowed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn both_halves_of_an_action_block_are_captured() {
|
||||
let f = parse_ok(
|
||||
"module m;\n assert property (a) $info(\"y\"); else $error(\"n\");\n wire w;\nendmodule\n",
|
||||
);
|
||||
let items = &module_of(&f).items;
|
||||
let ItemKind::Verbatim(raw) = &items[0].kind else { panic!("expected a verbatim block") };
|
||||
assert_eq!(raw.text, "assert property (a) $info(\"y\"); else $error(\"n\");");
|
||||
assert!(matches!(items[1].kind, ItemKind::DataDecl(_)), "the next item was swallowed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_concurrent_assertion_also_parses_in_statement_position() {
|
||||
let f = parse_ok(
|
||||
"module m;\n initial begin\n assert property (@(posedge clk) a |-> b);\n x = 1;\n end\nendmodule\n",
|
||||
);
|
||||
let ItemKind::Initial(stmt) = &module_of(&f).items[0].kind else {
|
||||
panic!("expected an initial block")
|
||||
};
|
||||
let StmtKind::Block { stmts, .. } = &stmt.kind else { panic!("expected a block") };
|
||||
let StmtKind::Verbatim(raw) = &stmts[0].kind else {
|
||||
panic!("expected a verbatim statement, got {:?}", stmts[0].kind)
|
||||
};
|
||||
assert_eq!(raw.text, "assert property (@(posedge clk) a |-> b);");
|
||||
assert!(matches!(stmts[1].kind, StmtKind::Assign { .. }), "the next statement was lost");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_immediate_assertion_still_takes_the_statement_path() {
|
||||
// No `property` keyword, so nothing about the verbatim capture may
|
||||
// pre-empt the structural immediate-assertion node.
|
||||
let f = parse_ok("module m;\n initial assert (x == 1) else $error(\"no\");\nendmodule\n");
|
||||
let ItemKind::Initial(stmt) = &module_of(&f).items[0].kind else {
|
||||
panic!("expected an initial block")
|
||||
};
|
||||
assert!(matches!(stmt.kind, StmtKind::Assert { .. }), "got {:?}", stmt.kind);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn property_and_sequence_declarations_are_captured_verbatim() {
|
||||
let src =
|
||||
"module m;\n property p(int w);\n realtime t;\n a |-> b;\n endproperty : p\n \
|
||||
sequence s;\n a ##1 b;\n endsequence\nendmodule\n";
|
||||
let f = parse_ok(src);
|
||||
let items = &module_of(&f).items;
|
||||
let ItemKind::Verbatim(p) = &items[0].kind else { panic!("expected a verbatim block") };
|
||||
assert_eq!(p.text, "property p(int w);\n realtime t;\n a |-> b;\n endproperty : p");
|
||||
let ItemKind::Verbatim(s) = &items[1].kind else { panic!("expected a verbatim block") };
|
||||
assert_eq!(s.text, "sequence s;\n a ##1 b;\n endsequence");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unclosed_property_declaration_is_an_error_not_a_swallowed_file() {
|
||||
assert!(
|
||||
parse_err("module m;\n property p;\n a |-> b;\nendmodule\n")
|
||||
.contains("expected `endproperty`"),
|
||||
"an unterminated property must be reported"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_parameterized_type_is_a_declaration_and_a_parameterized_module_is_an_instance() {
|
||||
let f =
|
||||
|
||||
Reference in new issue
Block a user