The two §11 parser skips: rival unit headers across an ifdef/else split as one verbatim unit, a net delay accepted after the type

TODO §38 (docs/features/parser-skips). Closes the jtag_if.sv and
SimDTM.sv skips from the §11 sweep.

38.1 `Parser::at_rival_header`: in a module/interface/program body, a
continuing directive at the unit's own entry level followed by the same
keyword and name is a header written twice across the arms, not a
nested unit. The error routes into the existing conditional split
(`at_split_conditional` now fires on a rival as well as an open
bracket; `scan_split_extent` runs to the unit's real closer, counting
nested units of the same kind), so the whole unit is one Verbatim item
in both modes. jtag_if.sv now formats (body verbatim, the standing
directive-split trade).

38.2 `DataDecl::delay: Option<NetDelay { expr, after_type }>`: a net
delay is accepted before or after the type (IEEE 1800 §6.7 puts it
after), never both, and printed where it was written. SimDTM.sv now
formats; its four `#0.1` lines come back byte-identical.

Byte-identity vs 0.41.0: 0 of 820 corpus files differ; over 2 554
opentitan files exactly the two named files change outcome.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
lucioandClaude Fable 5.1 committed 2026-09-04 23:06:37 -05:00
1 parent 97aa2fefd8
commit d5abf81563
13 files changed
+1557 -38

No files matched your search

@@ -628,3 +628,158 @@ fn an_opening_split_that_is_the_last_item_does_not_eat_the_closing_keyword() {
"a directive starting a statement list is pass-through, not a split"
);
}
// ------------------------------------------------------------------
// Rival unit headers written across the arms of one region
// (`docs/features/parser-skips/README.md`)
// ------------------------------------------------------------------
/// The `jtag_if.sv` shape, in the four spellings that differ only in the
/// header: `interface`/`module`, with and without `#( ... )`, with `()` and
/// with a real port list. Every one of them is one verbatim item running
/// from the first header to the unit's own closing keyword.
#[test]
fn rival_unit_headers_across_one_region_become_one_verbatim_item() {
for (why, src, ends) in [
(
"interface, `#( ... ) ()` — hw/dv/sv/jtag_agent/jtag_if.sv:6",
"`ifdef GATE_LEVEL\n interface jtag_if #(parameter int unsigned P = 41_664) \
();\n`else\n interface jtag_if #(parameter int unsigned P = 20_000) \
();\n`endif\n wire tck;\nendinterface\nparameter int AFTER = 1;\n",
"endinterface",
),
(
"interface, no parameter list",
"`ifdef A\ninterface i ();\n`else\ninterface i ();\n`endif\n wire \
tck;\nendinterface\nparameter int AFTER = 1;\n",
"endinterface",
),
(
"module, a real port list",
"`ifdef A\nmodule m (input a, output b);\n`else\nmodule m (input a, output \
b);\n`endif\n wire x;\nendmodule\nparameter int AFTER = 1;\n",
"endmodule",
),
(
"module, a closing label",
"`ifdef A\nmodule m #(parameter int W = 1) ();\n`else\nmodule m #(parameter int W = \
2) ();\n`endif\n wire x;\nendmodule : m\nparameter int AFTER = 1;\n",
"endmodule : m",
),
] {
let f = parse_ok(src);
let kinds = split_items(&f);
assert_eq!(kinds.len(), 3, "{why}: {kinds:?}");
assert!(kinds[0].starts_with("directive:`ifdef"), "{why}: {kinds:?}");
assert_eq!(kinds[2], "param", "{why}: the item after the unit was not swallowed");
let ItemKind::Verbatim(raw) = &f.items[1].kind else { panic!("{why}: expected verbatim") };
assert_eq!(raw.text, src[raw.span.as_range()], "{why}");
assert!(raw.text.ends_with(ends), "{why}: {:?}", raw.text);
assert!(raw.text.contains("`else") && raw.text.contains("`endif"), "{why}: {:?}", raw.text);
// The whole unit is inside the block, its body included.
assert!(raw.text.contains("wire"), "{why}: {:?}", raw.text);
assert_eq!(open_conds_after(src), 0, "{why}: the region's own `endif was accounted for");
}
}
#[test]
fn rival_headers_produce_the_same_tree_in_both_modes() {
let src = "`ifdef GATE_LEVEL\n interface jtag_if #(parameter int unsigned P = 41_664) \
();\n`else\n interface jtag_if #(parameter int unsigned P = 20_000) \
();\n`endif\n wire tck;\nendinterface\n";
let strict = parse_ok(src);
let toks = tokenize(src).expect("lex error");
let (tolerant, diags) = parse_tolerant(toks, src);
assert!(diags.is_empty(), "{diags:?}");
assert_eq!(split_items(&strict), split_items(&tolerant));
}
#[test]
fn comments_written_around_a_rival_header_are_kept() {
// The real file writes a comment inside each arm and one before the
// region; none of them may be dropped.
let src = "// head\n`ifdef GATE_LEVEL // which\n // 24MHz for GLS\n interface jtag_if \
();\n`else\n // 50MHz for DV\n interface jtag_if ();\n`endif\n // pins\n wire \
tck;\nendinterface // done\n";
let f = parse_ok(src);
assert_eq!(texts(&f.items[0].meta.leading), ["// head"]);
assert_eq!(f.items[0].meta.trailing.as_ref().expect("the `ifdef's comment").text, "// which");
// The capture starts at the item's *first token*, so the comment written
// in front of the header is the item's own leading comment — the same
// place every other split puts it — and the rest are inside the block.
assert_eq!(texts(&f.items[1].meta.leading), ["// 24MHz for GLS"]);
let ItemKind::Verbatim(raw) = &f.items[1].kind else { panic!("expected a verbatim block") };
for c in ["// 50MHz for DV", "// pins"] {
assert!(raw.text.contains(c), "{c} was dropped from {:?}", raw.text);
}
assert!(!raw.text.contains("// done"), "the trailing comment was swallowed");
assert_eq!(f.items[1].meta.trailing.as_ref().expect("the comment survived").text, "// done");
}
#[test]
fn a_unit_nested_in_a_captured_body_does_not_end_the_scan_early() {
// The extent scan counts units of the head's kind, so the *inner*
// `endmodule` cannot be mistaken for the rival unit's own.
let src = "`ifdef A\nmodule m ();\n`else\nmodule m ();\n`endif\n module inner ();\n \
endmodule\n wire x;\nendmodule\nparameter int AFTER = 1;\n";
let f = parse_ok(src);
let kinds = split_items(&f);
assert_eq!(kinds.len(), 3, "{kinds:?}");
let ItemKind::Verbatim(raw) = &f.items[1].kind else { panic!("expected a verbatim block") };
assert!(raw.text.contains("module inner"), "{:?}", raw.text);
assert!(raw.text.ends_with("endmodule"), "{:?}", raw.text);
assert_eq!(kinds[2], "param", "{kinds:?}");
}
// -- the three negatives ----------------------------------------------
#[test]
fn a_second_unit_in_an_arm_of_a_region_opened_inside_the_body_is_still_an_error() {
// Two nested units, which is illegal: the region the `else belongs to
// was opened *by the body*, not before the header, so the arm is not a
// rival spelling of anything.
let msg = parse_err(
"`ifdef OUTER\nmodule m;\n`ifdef A\n wire a;\n`else\nmodule m;\n`endif\n wire \
b;\nendmodule\n",
);
assert!(msg.contains("endmodule"), "{msg}");
// The same shape with no region open at all around the outer unit.
let msg = parse_err(
"module m;\n`ifdef A\n wire a;\n`else\nmodule m;\n wire \
b;\n`endif\nendmodule\n",
);
assert!(msg.contains("endmodule"), "{msg}");
}
#[test]
fn a_different_name_after_the_else_is_not_a_rival_header() {
let msg = parse_err(
"`ifdef A\ninterface i ();\n`else\ninterface j ();\n`endif\n wire \
x;\nendinterface\n",
);
assert!(msg.contains("endinterface"), "{msg}");
// Nor is a different keyword for the same name.
let msg = parse_err(
"`ifdef A\ninterface i ();\n`else\nmodule i ();\n`endif\n wire x;\nendinterface\n",
);
assert!(msg.contains("endinterface"), "{msg}");
}
#[test]
fn a_region_closed_before_the_header_leaves_the_else_stray() {
// The region opened at the top of the file is closed before the
// interface begins, so the `else below closes nothing: broken input,
// and the parse error stands.
let msg = parse_err(
"`ifdef A\n wire a;\n`endif\ninterface i ();\n`else\n interface i ();\n`endif\n wire \
x;\nendinterface\n",
);
assert!(msg.contains("endinterface"), "{msg}");
// And a region closed *inside* the body before the `else: the header's
// region is gone, so the live one is not the one it was written under.
let msg = parse_err(
"`ifdef OUTER\ninterface i ();\n`endif\n`ifdef B\n wire a;\n`else\n interface i \
();\n`endif\nendinterface\n",
);
assert!(msg.contains("endinterface"), "{msg}");
}