Decomposition, lane A: vlog_parser's parser.rs and ast.rs become twenty-seven modules, a pure move
TODO section 28, lane A (the file-size rule in CLAUDE.md).
parser.rs (8 786 production + 5 044 test lines) becomes parser/{mod,
cursor, recover, directive, split, verbatim, item, module, decl, tf,
instance, stmt, keyword_stmt, expr, primary, class, covergroup,
assertion, sva}.rs with the tests regrouped into parser/tests/ (14
files, 309 tests, same names and bodies); ast.rs (3 313) becomes
ast/{mod, module, decl, stmt, expr, class, covergroup, sva}.rs, every
type re-exported so vlog_parser::ast::X is unchanged. Nothing over 800
production lines; the two keyword dispatch tables kept whole and
documented as such, parse_keyword_stmt in a file of its own. Carved by
a line-range manifest that had to concatenate back to the original
byte for byte before writing; 149 methods and 18 items pub(super) as
the compiler demanded. Proof: per-binary test counts identical
(workspace 3 215 / 48), 4 807 corpus + opentitan files byte-identical
to the v0.33.0 binary, cargo doc item list unchanged, and a multiset +
signature census done by the dev, redone by QA, re-run by the lead.
docs/features/decomposition-parser/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
159f6a0bd8
commit
9b93f33ae3
45 files changed
+17999
-17241
No files matched your search
@@ -0,0 +1,445 @@
|
||||
//! Concurrent assertions and the SVA declarations: `assert`/`assume`/
|
||||
//! `cover`/`restrict property` items and statements with their action
|
||||
//! blocks, `property`/`sequence` declarations, `expect`, and
|
||||
//! `default disable iff`. The property-expression grammar these all
|
||||
//! contain is [`super::sva`].
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Keywords that open a statement block [`Parser::skip_action_stmt`] skips as
|
||||
/// one unit. `fork` counts only where it opens a block: `disable fork` and
|
||||
/// `wait fork` name the enclosing one.
|
||||
const BLOCK_OPENERS: &[&str] = &["begin", "fork", "case", "casex", "casez", "randcase"];
|
||||
|
||||
/// Keywords that close one of [`BLOCK_OPENERS`]. Which opener a closer belongs
|
||||
/// to is not checked — the scan only counts — so unbalanced input runs to the
|
||||
/// end of the file and reports a clean error there.
|
||||
const BLOCK_CLOSERS: &[&str] = &["end", "join", "join_any", "join_none", "endcase"];
|
||||
|
||||
impl<'a> Parser<'a> {
|
||||
/// 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.
|
||||
pub(super) 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");
|
||||
// `cover sequence ( ... )` is the one spelling that names `sequence`
|
||||
// rather than `property` (IEEE 1800 §16.14); nothing else may.
|
||||
let second = self.nth(i + 1);
|
||||
opens && (second.is_kw("property") || (head.is_kw("cover") && second.is_kw("sequence")))
|
||||
}
|
||||
|
||||
/// 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 lucisv 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.
|
||||
pub(super) 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 *and* the block keywords ([`BLOCK_OPENERS`] /
|
||||
/// [`BLOCK_CLOSERS`]) are skipped as units, so neither a `;` nor an `end`
|
||||
/// written inside one ends the scan early: the pass statement of
|
||||
/// `assert property (p) if (c) begin x = 1; end else y = 2;` is the whole
|
||||
/// `if`, not `if (c) begin x = 1;`. A statement that ends with a block
|
||||
/// ends *there* — no `;` follows one — unless an `else` continues it.
|
||||
///
|
||||
/// The scan never runs past the end of the file and never stands still:
|
||||
/// every arm consumes at least one token, and `at_eof` is checked on every
|
||||
/// turn, so unbalanced input is a clean parse error.
|
||||
fn skip_action_stmt(&mut self) -> PResult<()> {
|
||||
// How many block keywords are open. Zero means the scan is looking for
|
||||
// the statement's own terminator.
|
||||
let mut depth = 0usize;
|
||||
// True while the next token would be the *first* of a statement — at
|
||||
// the start, and again after an `else` continues one.
|
||||
let mut at_stmt_start = true;
|
||||
loop {
|
||||
if self.at_eof() {
|
||||
return self.error("expected `;`");
|
||||
}
|
||||
// A statement that *is* a macro invocation ends with the
|
||||
// invocation, whether or not a `;` follows it:
|
||||
// `assert property (p) else `ASSERT_ERROR(A)` (OpenTitan
|
||||
// `adc_ctrl/dv/sva/adc_ctrl_fsm_sva_if.sv:35`).
|
||||
//
|
||||
// Whether it is the whole statement is decided by what *follows*
|
||||
// the call, never by the macro standing first — a macro's
|
||||
// expansion is just as often a signal name that the rest of the
|
||||
// statement goes on to assign to or read (`` `SIG = 1'b1; ``,
|
||||
// `` `CNT <= `CNT + 1; ``, `` `MSG(x) + 1; ``). When anything
|
||||
// that could continue a statement follows, this falls through to
|
||||
// the ordinary token-by-token scan, which is what ran before this
|
||||
// case existed (QA finding, `docs/features/clocking-wave`).
|
||||
if depth == 0 && at_stmt_start && self.cur().is_macro() {
|
||||
let call = self.parse_macro_call()?;
|
||||
if call.semi || !self.at_statement_tail() {
|
||||
// Its own `;`, or nothing that could continue it: done,
|
||||
// unless an `else` chains a second action onto it.
|
||||
if !self.eat_kw("else") {
|
||||
return Ok(());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// The macro was a name, not a statement. Scan on.
|
||||
at_stmt_start = false;
|
||||
continue;
|
||||
}
|
||||
at_stmt_start = false;
|
||||
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 (t.is_kw("disable") || t.is_kw("wait")) && self.nth(1).is_kw("fork") {
|
||||
// `disable fork` / `wait fork` name the enclosing block; they
|
||||
// do not open one.
|
||||
self.bump();
|
||||
self.bump();
|
||||
} else if BLOCK_OPENERS.iter().any(|k| t.is_kw(k)) {
|
||||
depth += 1;
|
||||
self.bump();
|
||||
} else if BLOCK_CLOSERS.iter().any(|k| t.is_kw(k)) {
|
||||
if depth == 0 {
|
||||
// A closer with nothing open: the action statement is
|
||||
// missing its terminator and the scan has reached the end
|
||||
// of the block the assertion itself sits in.
|
||||
return self.error("expected `;`");
|
||||
}
|
||||
depth -= 1;
|
||||
self.bump();
|
||||
if depth == 0 {
|
||||
self.parse_label()?;
|
||||
if !self.eat_kw("else") {
|
||||
return Ok(());
|
||||
}
|
||||
at_stmt_start = true;
|
||||
}
|
||||
} else if depth == 0 && self.eat_sym(";") {
|
||||
return Ok(());
|
||||
} else {
|
||||
self.bump();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// True when the token at the cursor could *continue* a statement whose
|
||||
/// first token has already been consumed — an assignment, an operator, a
|
||||
/// postfix segment. Used to tell `` else `ASSERT_ERROR(A) `` (the macro is
|
||||
/// the whole action) from `` else `SIG = 1'b1; `` (the macro was a name).
|
||||
///
|
||||
/// Every such continuation is spelled with a symbol: `=`, `<=`, `+`,
|
||||
/// `++`, `.`, `[`, `::`. What can stand after a *complete* action instead
|
||||
/// is a keyword (`assign`, `end`, `endmodule`), an identifier or another
|
||||
/// macro (the next item or label), a directive, or the end of the file —
|
||||
/// none of which continue an expression.
|
||||
///
|
||||
/// Deliberately generous in one direction: guessing "continues" when the
|
||||
/// statement was in fact complete only falls back to the brute-force scan
|
||||
/// that ran before the macro case existed, while guessing "complete" when
|
||||
/// it was not is the regression this predicate exists to prevent.
|
||||
fn at_statement_tail(&self) -> bool {
|
||||
self.cur().kind == TokenKind::Symbol
|
||||
}
|
||||
|
||||
/// `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))
|
||||
}
|
||||
|
||||
/// `default disable iff <expression_or_dist>;` (IEEE 1800 §16.12), kept as
|
||||
/// verbatim source.
|
||||
///
|
||||
/// It is the standing `disable iff` clause of every concurrent assertion
|
||||
/// in the scope, so what it holds is a property-grammar operand — `dist`
|
||||
/// included — and the assertions it governs are already verbatim. Copying
|
||||
/// it out by span keeps the whole assertion story in one bucket instead of
|
||||
/// modelling half of it. Only the item spelling exists; there is no
|
||||
/// statement form.
|
||||
fn parse_default_disable(&mut self) -> PResult<RawText> {
|
||||
let start = self.here();
|
||||
let col = self.cur().col;
|
||||
self.bump(); // default
|
||||
self.expect_kw("disable")?;
|
||||
self.expect_kw("iff")?;
|
||||
self.skip_to_semi()?;
|
||||
Ok(self.verbatim_since(start, col))
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// The SVA declarations
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
/// `property name [( formals )]; [locals] spec [;] endproperty [: name]`
|
||||
/// and its `sequence` / `endsequence` twin (IEEE 1800 §16.8, §16.12),
|
||||
/// behind the rewind-to-verbatim fallback this round's README describes.
|
||||
pub(super) fn parse_prop_seq_decl(&mut self) -> PResult<ItemKind> {
|
||||
let mark = self.rewind_mark();
|
||||
match self.parse_prop_seq_decl_inner() {
|
||||
Ok(decl) => {
|
||||
self.prop_seq_decls += 1;
|
||||
Ok(ItemKind::PropSeqDecl(Box::new(decl)))
|
||||
}
|
||||
Err(_) => {
|
||||
self.rewind_to(mark);
|
||||
self.prop_seq_fallbacks += 1;
|
||||
self.parse_property_decl().map(ItemKind::Verbatim)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_prop_seq_decl_inner(&mut self) -> PResult<PropSeqDecl> {
|
||||
let kw = self.cur().text.clone();
|
||||
let end_kw = if kw == "sequence" { "endsequence" } else { "endproperty" };
|
||||
self.bump();
|
||||
let before_name = self.take_gap();
|
||||
let name = self.expect_name()?;
|
||||
let before_args = self.take_before_open("(");
|
||||
let args = if self.cur().is_sym("(") {
|
||||
// `property p(sequence a, untyped b);` — the two formal type
|
||||
// spellings only a property or a sequence has.
|
||||
self.sva_formals = true;
|
||||
let out = self.parse_tf_ports();
|
||||
self.sva_formals = false;
|
||||
Some(out?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let before_semi = self.gap_before(";");
|
||||
self.expect_sym(";")?;
|
||||
let mut locals = Vec::new();
|
||||
while self.at_assertion_var_decl() {
|
||||
let start = self.here();
|
||||
let meta = self.take_meta();
|
||||
let decl = self.parse_data_decl()?;
|
||||
locals.push(Item { meta, kind: ItemKind::DataDecl(decl), span: self.span_from(start) });
|
||||
}
|
||||
let spec = self.parse_prop_spec()?;
|
||||
let before_spec_semi = self.gap_before(";");
|
||||
let spec_semi = self.eat_sym(";");
|
||||
let dangling = self.take_dangling();
|
||||
self.expect_kw(end_kw)?;
|
||||
let (end_label_comments, end_label) = self.parse_label_with()?;
|
||||
Ok(PropSeqDecl {
|
||||
kw,
|
||||
before_name,
|
||||
name,
|
||||
before_args,
|
||||
args,
|
||||
before_semi,
|
||||
locals,
|
||||
spec,
|
||||
before_spec_semi,
|
||||
spec_semi,
|
||||
dangling,
|
||||
end_label_comments,
|
||||
end_label,
|
||||
})
|
||||
}
|
||||
|
||||
/// True when an `assertion_variable_declaration` — an ordinary data
|
||||
/// declaration (IEEE 1800 §16.12) — stands at the cursor, rather than the
|
||||
/// property spec that follows the last of them.
|
||||
fn at_assertion_var_decl(&self) -> bool {
|
||||
let t = self.cur();
|
||||
if t.is_kw("var") {
|
||||
return true;
|
||||
}
|
||||
if t.kind == TokenKind::Keyword {
|
||||
return TYPE_KEYWORDS.contains(&t.text.as_str());
|
||||
}
|
||||
t.kind == TokenKind::Ident && self.at_typed_declarator()
|
||||
}
|
||||
|
||||
/// A concurrent assertion at item position, behind the same fallback.
|
||||
pub(super) fn parse_assertion_item(&mut self) -> PResult<ItemKind> {
|
||||
let mark = self.rewind_mark();
|
||||
match self.parse_assertion_decl() {
|
||||
Ok(decl) => {
|
||||
self.assertions += 1;
|
||||
Ok(ItemKind::Assertion(Box::new(decl)))
|
||||
}
|
||||
Err(_) => {
|
||||
self.rewind_to(mark);
|
||||
self.assertion_fallbacks += 1;
|
||||
self.parse_concurrent_assertion().map(ItemKind::Verbatim)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The same at statement position.
|
||||
pub(super) fn parse_assertion_stmt(&mut self) -> PResult<StmtKind> {
|
||||
let mark = self.rewind_mark();
|
||||
match self.parse_assertion_decl() {
|
||||
Ok(decl) => {
|
||||
self.assertions += 1;
|
||||
Ok(StmtKind::Assertion(Box::new(decl)))
|
||||
}
|
||||
Err(_) => {
|
||||
self.rewind_to(mark);
|
||||
self.assertion_fallbacks += 1;
|
||||
self.parse_concurrent_assertion().map(StmtKind::Verbatim)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An `expect ( spec ) action_block` statement (IEEE 1800 §16.17), behind
|
||||
/// the fallback [`Parser::parse_expect_statement`] was.
|
||||
pub(super) fn parse_expect_stmt(&mut self) -> PResult<StmtKind> {
|
||||
let mark = self.rewind_mark();
|
||||
match self.parse_assertion_decl() {
|
||||
Ok(decl) => {
|
||||
self.assertions += 1;
|
||||
Ok(StmtKind::Assertion(Box::new(decl)))
|
||||
}
|
||||
Err(_) => {
|
||||
self.rewind_to(mark);
|
||||
self.assertion_fallbacks += 1;
|
||||
self.parse_expect_statement().map(StmtKind::Verbatim)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `[label :] assert|assume|cover|restrict property ( spec ) action`,
|
||||
/// `cover sequence ( spec ) action` and `expect ( spec ) action`.
|
||||
fn parse_assertion_decl(&mut self) -> PResult<AssertionDecl> {
|
||||
let mut label = None;
|
||||
let mut label_comments: Box<LabelComments> = Box::default();
|
||||
if self.cur().kind == TokenKind::Ident && self.nth(1).is_sym(":") {
|
||||
label = Some(self.expect_name()?);
|
||||
label_comments.before_colon = self.gap_before(":");
|
||||
self.expect_sym(":")?;
|
||||
label_comments.after_colon = self.take_gap();
|
||||
}
|
||||
let kw = self.bump().text;
|
||||
let mut after_kw = Vec::new();
|
||||
let mut kw2 = String::new();
|
||||
if self.cur().is_kw("property") || self.cur().is_kw("sequence") {
|
||||
after_kw = self.take_gap();
|
||||
kw2 = self.bump().text;
|
||||
}
|
||||
let before_open = self.take_before_open("(");
|
||||
self.expect_sym("(")?;
|
||||
let after_open = self.take_gap();
|
||||
let spec = self.parse_prop_spec()?;
|
||||
let before_close = self.gap_before(")");
|
||||
self.expect_sym(")")?;
|
||||
let action = self.parse_action_block()?;
|
||||
Ok(AssertionDecl {
|
||||
label,
|
||||
label_comments,
|
||||
kw,
|
||||
after_kw,
|
||||
kw2,
|
||||
before_open,
|
||||
after_open,
|
||||
spec,
|
||||
before_close,
|
||||
action,
|
||||
})
|
||||
}
|
||||
|
||||
/// `action_block ::= ; | statement_or_null [ else statement_or_null ] |
|
||||
/// else statement_or_null` (IEEE 1800 §16.14). Both halves are the
|
||||
/// ordinary statement the parser already reads.
|
||||
fn parse_action_block(&mut self) -> PResult<ActionBlock> {
|
||||
let mut action = ActionBlock::default();
|
||||
if self.cur().is_sym(";") {
|
||||
action.before = self.take_gap();
|
||||
self.bump();
|
||||
return Ok(action);
|
||||
}
|
||||
if !self.cur().is_kw("else") {
|
||||
action.pass = Some(Box::new(self.parse_stmt()?));
|
||||
}
|
||||
if self.cur().is_kw("else") {
|
||||
action.before_else = self.take_gap();
|
||||
self.bump();
|
||||
action.after_else = self.take_gap();
|
||||
action.fail = Some(Box::new(self.parse_stmt()?));
|
||||
}
|
||||
Ok(action)
|
||||
}
|
||||
|
||||
/// `default disable iff ( expression_or_dist ) ;` (IEEE 1800 §16.15),
|
||||
/// behind the fallback [`Parser::parse_default_disable`] was.
|
||||
pub(super) fn parse_default_disable_item(&mut self) -> PResult<ItemKind> {
|
||||
let mark = self.rewind_mark();
|
||||
match self.parse_default_disable_inner() {
|
||||
Ok(decl) => Ok(ItemKind::DefaultDisable(Box::new(decl))),
|
||||
Err(_) => {
|
||||
self.rewind_to(mark);
|
||||
self.parse_default_disable().map(ItemKind::Verbatim)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_default_disable_inner(&mut self) -> PResult<DisableIff> {
|
||||
self.expect_kw("default")?;
|
||||
let mut decl = self.parse_disable_iff(false)?;
|
||||
decl.before_semi = self.gap_before(";");
|
||||
self.expect_sym(";")?;
|
||||
Ok(decl)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user