Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions crates/celox-sv-syntax/src/parser/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<crate::source::Span> = 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))
{
Expand Down Expand Up @@ -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<ExprId>) {
match dimension.kind {
DimensionKind::Index(value) => pending.push(value),
Expand Down
16 changes: 16 additions & 0 deletions crates/celox-sv-syntax/src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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);
Expand Down
Loading