// Author: Guoliang Chen, a Senior Digital Design Engineer //! The null item — a lone `;` where an item belongs (IEEE 1800-2017 A.1.4 //! `package_or_generate_item_declaration ::= … | ;` and `class_item ::= … | ;`). //! See `docs/features/null-items/README.md`. //! //! Every item list in the parser reaches `parse_item_kind` through //! `item_or_recover`, so one branch is meant to cover all eight scopes. That //! claim is checked here by parsing one source per scope rather than by //! reading the parser. use vlog_parser::ast::*; use vlog_parser::{parse_source, parse_source_tolerant}; /// Parses `src` strictly and returns the tree; panics with the parse error. fn tree(src: &str) -> SourceFile { parse_source(src).unwrap_or_else(|e| panic!("{src}\n=> {e}")) } /// Every null item in `file`, as its source slice — which must be `";"`. fn null_item_slices(src: &str, file: &SourceFile) -> Vec { let mut out = Vec::new(); walk(&file.items, &mut |item| { if matches!(item.kind, ItemKind::Empty) { out.push(src[item.span.as_range()].to_string()); } }); out } fn walk(items: &[Item], f: &mut dyn FnMut(&Item)) { for item in items { f(item); match &item.kind { ItemKind::Module(m) => walk(&m.items, f), ItemKind::Package { items, .. } => walk(items, f), ItemKind::Class(c) => walk(&c.items, f), ItemKind::GenerateRegion { items, .. } => walk(items, f), ItemKind::GenBlock(b) => walk(&b.items, f), ItemKind::GenFor(g) => walk(&g.body.items, f), ItemKind::GenIf(g) => { walk(&g.then.items, f); let mut els = g.els.as_deref(); while let Some(branch) = els { match branch { GenElse::If(_, nested) => { walk(&nested.then.items, f); els = nested.els.as_deref(); } GenElse::Block(block) => { walk(&block.items, f); els = None; } } } } ItemKind::GenCase(g) => { for case_item in &g.items { walk(&case_item.body.items, f); } } _ => {} } } } /// One `;` per scope, and every one of them is an `ItemKind::Empty` whose span /// is exactly the semicolon. #[test] fn a_null_item_parses_in_every_item_scope() { let scopes = [ ("file scope", "module m;\nendmodule;\n"), ("module body", "module m;\n wire a;\n ;\nendmodule\n"), ("interface body", "interface ifc;\n logic a;\n ;\nendinterface\n"), ("program body", "program p;\n ;\nendprogram\n"), ("package body", "package p;\n localparam int W = 1;\n ;\nendpackage\n"), ("class body", "class c;\n int x;\n ;\nendclass\n"), ("generate region", "module m;\n generate\n ;\n endgenerate\nendmodule\n"), ( "generate-for body", "module m;\n for (genvar i = 0; i < 2; i++) begin : g\n ;\n end\nendmodule\n", ), ( "generate-if body", "module m;\n if (A) begin : g\n ;\n end else begin : h\n ;\n end\nendmodule\n", ), ( "generate-case body", "module m;\n case (A)\n 1: begin : g\n ;\n end\n endcase\nendmodule\n", ), ]; for (what, src) in scopes { let file = tree(src); let found = null_item_slices(src, &file); assert!(!found.is_empty(), "{what}: no null item parsed from\n{src}"); assert!(found.iter().all(|s| s == ";"), "{what}: span is not the `;`: {found:?}"); } } /// The shape the owner reported, and its neighbours: a `;` after a closing /// keyword rather than on a line of its own. #[test] fn a_semicolon_after_a_closing_keyword_is_a_null_item() { let sources = [ "module m;\n generate\n endgenerate;\nendmodule\n", "module m;\n generate\n if (A) begin : g\n wire x;\n end;\n endgenerate\nendmodule\n", "module m;\n if (A) begin : g\n wire x;\n end;\nendmodule\n", "module m;\nendmodule;\n", ]; for src in sources { let file = tree(src); assert_eq!(null_item_slices(src, &file), vec![";"], "{src}"); } } /// `;;` is two of them, not one. #[test] fn repeated_semicolons_are_one_null_item_each() { let src = "module m;\n ;;;\nendmodule\n"; let file = tree(src); assert_eq!(null_item_slices(src, &file).len(), 3); } /// Comments ride the ordinary `Meta`, like any other item's. #[test] fn comments_around_a_null_item_are_kept() { let src = "module m;\n // lead\n ; // trail\nendmodule\n"; let file = tree(src); let ItemKind::Module(m) = &file.items[0].kind else { panic!("expected a module") }; let item = m.items.iter().find(|i| matches!(i.kind, ItemKind::Empty)).expect("a null item"); assert_eq!(item.meta.leading.iter().map(|c| c.text.as_str()).collect::>(), ["// lead"]); assert_eq!(item.meta.trailing.as_ref().map(|c| c.text.as_str()), Some("// trail")); } /// Tolerant mode is silent about a null item: it is a successful parse, not a /// recovery, so an editor shows the `empty-item` lint and no parse error. #[test] fn tolerant_parsing_reports_no_diagnostic() { let sources = [ "module m;\n generate\n endgenerate;\nendmodule\n", "module m;\n ;;\nendmodule\n", "package p;\n ;\nendpackage\n", "class c;\n ;\nendclass\n", "module m;\nendmodule;\n", ]; for src in sources { let (file, diagnostics) = parse_source_tolerant(src); assert!(diagnostics.is_empty(), "{src}=> {diagnostics:?}"); assert!(!null_item_slices(src, &file).is_empty(), "{src}"); assert!( null_item_slices(src, &file).iter().all(|s| s == ";"), "an error leaf, not a null item: {src}" ); } } /// The `;` after `endfunction`/`endtask` stays where it always was — on /// `FunctionDecl::end_semi` / `TaskDecl::end_semi`, consumed inside the /// declaration — so the two mechanisms do not both claim it. #[test] fn endfunction_and_endtask_keep_their_own_semicolon_flag() { let src = "module m;\n function int f();\n return 1;\n endfunction;\n task t();\n endtask;\nendmodule\n"; let file = tree(src); assert!(null_item_slices(src, &file).is_empty(), "the `;` must not become a null item"); let ItemKind::Module(m) = &file.items[0].kind else { panic!("expected a module") }; let mut seen = 0; for item in &m.items { match &item.kind { ItemKind::Function(f) => { assert!(f.end_semi); seen += 1; } ItemKind::Task(t) => { assert!(t.end_semi); seen += 1; } _ => panic!("unexpected item"), } } assert_eq!(seen, 2); } // Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com