From 2aeaaeda346971e12a84f11964baf27cf0ad4dea Mon Sep 17 00:00:00 2001 From: tignear Date: Sat, 24 Oct 2026 13:44:38 +0900 Subject: [PATCH] fix(sv): lex a colon before a comment as a colon The lexer matched the `:/` distribution-weight operator before looking for a comment, so `3'b000://comment` became `:/` followed by `/comment` and failed to parse. XuanTie C906 (openc906) uses this form in case items. A `:/` operator is always followed by an expression, and no expression starts with `/` or `*`, so a `:/` immediately followed by `/` or `*` now lexes as `:` and leaves the comment intact. IEEE 1800-2023 5.4 defines `//` and `/*` as comment starts; previously valid input is unaffected. Validation: - celox-sv-syntax: 122 tests passed, including a new lexer test; Clippy with -D warnings and formatting passed. - No sv-tests case at the pinned revision contains `:/` followed by `/` or `*`, so conformance expectations are unchanged. - The rtlmeter XuanTie-C906 sources now get past this parse error. Change-Id: Ia3b604411f360405318fefce2dede32249f1b8e6 --- crates/celox-sv-syntax/src/lexer.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/celox-sv-syntax/src/lexer.rs b/crates/celox-sv-syntax/src/lexer.rs index e31d0a1db31..fa03703681f 100644 --- a/crates/celox-sv-syntax/src/lexer.rs +++ b/crates/celox-sv-syntax/src/lexer.rs @@ -278,8 +278,12 @@ fn lex_with_context( "::", "+:", "-:", "~&", "~|", "~^", "^~", "->", "=>", "*>", "##", "@@", ":=", ":/", ".*", "(*", "*)", ]; + // `:` followed by a comment (`3'b0://`, `x:/*`) is not `:/`. + let comment_after_colon = source[at..].starts_with(":/") + && matches!(bytes.get(at + 2), Some(b'/' | b'*')); let length = OPERATORS .iter() + .filter(|op| !(comment_after_colon && **op == ":/")) .find(|op| source[at..].starts_with(**op)) .map_or(1, |op| op.len()); at += length; @@ -403,6 +407,20 @@ mod tests { } } + #[test] + fn colon_before_comment_is_not_dist_weight() { + let text = "3'b0://c\n3'b1:/*c*/ x :/ 2"; + let mut sources = SourceMap::default(); + let file = sources.add("colon.sv", text); + let tokens = lex(&sources, file).unwrap(); + let significant = tokens + .iter() + .filter(|token| !token.is_trivia()) + .map(|token| token.text.as_str()) + .collect::>(); + assert_eq!(significant, ["3'b0", ":", "3'b1", ":", "x", ":/", "2"]); + } + #[test] fn strings_and_longest_operators_preserve_lexical_boundaries() { let text = "\"continued\\\r\nstring\" \"continued\\\nstring\" \"\"\"multi\nline\"\"\" <<<= >>>= |-> |=> ->> <->";