diff --git a/crates/celox-sv-syntax/src/parser/expressions.rs b/crates/celox-sv-syntax/src/parser/expressions.rs index f9b967a3034..5c0dce906c0 100644 --- a/crates/celox-sv-syntax/src/parser/expressions.rs +++ b/crates/celox-sv-syntax/src/parser/expressions.rs @@ -671,6 +671,33 @@ impl Parser<'_> { self.at += 1; } } + // A macro can supply the digits of a based literal + // (`5'd`WIDTH`), leaving them in separate tokens; a + // body such as `0f` lexes as `0` and `f`, adjacent + // in their spelling. + if let ExprKind::Literal(value) = &mut kind + && numeric + && based_without_digits(value) + { + let mut previous: Option = None; + while let Some(next) = self.token() + && matches!( + next.kind, + TokenKind::Number | TokenKind::Identifier + ) + && next.text.bytes().all(|b| { + b.is_ascii_alphanumeric() || matches!(b, b'_' | b'?') + }) + && previous.is_none_or(|span| { + span.file == next.origin.spelling.file + && span.end == next.origin.spelling.start + }) + { + value.push_str(&next.text); + previous = Some(next.origin.spelling); + self.at += 1; + } + } if numeric && matches!(&kind, ExprKind::Literal(value) if !super::numbers::literal(value)) { @@ -1591,6 +1618,17 @@ pub(super) fn builtin(word: &str) -> bool { ) } +/// A sized or unsized based literal spelled up to its base, such as `5'd`. +fn based_without_digits(value: &str) -> bool { + let based = value + .trim_start_matches(|c: char| c.is_ascii_digit() || c == '_' || c.is_ascii_whitespace()); + let Some(base) = based.strip_prefix('\'') else { + return false; + }; + let base = base.strip_prefix(['s', 'S']).unwrap_or(base); + matches!(base, "b" | "B" | "o" | "O" | "d" | "D" | "h" | "H") +} + fn constant_dimension_operands(dimension: &Dimension, pending: &mut Vec) { match dimension.kind { DimensionKind::Index(value) => pending.push(value), diff --git a/crates/celox-sv-syntax/src/parser/tests.rs b/crates/celox-sv-syntax/src/parser/tests.rs index d66393e16f5..b71b04b93dc 100644 --- a/crates/celox-sv-syntax/src/parser/tests.rs +++ b/crates/celox-sv-syntax/src/parser/tests.rs @@ -2245,6 +2245,16 @@ fn numeric_literal_grammar_keeps_integer_real_time_and_macro_base_spellings() { parse(&format!("module M #(parameter P={literal}); endmodule")); } parse("`define BASE 'h ff\nmodule M #(parameter P=8 `BASE); endmodule"); + for (definitions, literal) in [ + ("`define W 4", "5'd`W"), + ("`define V ff", "8'h`V"), + ("`define V 1x", "'sb`V"), + ("`define V 0f", "8'h`V"), + ] { + parse(&format!( + "{definitions}\nmodule M #(parameter P={literal}); endmodule" + )); + } } #[test] @@ -2699,6 +2709,12 @@ fn resetall_is_only_legal_outside_design_elements() { #[test] fn based_literal_expression_values_are_compact_without_losing_source_tokens() { + let native = parse("`define W 4\nmodule M; initial x=5'd`W; endmodule"); + assert!( + native.expressions.iter().any( + |expression| matches!(&expression.kind, ExprKind::Literal(text) if text == "5'd4") + ) + ); for (authored, value) in [("'h x", "'hx"), ("8'd 6", "8'd6"), ("4 'h f", "4'hf")] { let source = format!("module M; initial x={authored}; endmodule"); let native = parse(&source);