Fix timescale CRLF // comments leaving CR in the token. TIMESCALE_DIRECTIVE used yyless(yyleng-1), so CRLF comments kept `\r` and the formatter emitted `\r\r\n` (issue #2605).
diff --git a/verible/verilog/parser/verilog-lexer_test.cc b/verible/verilog/parser/verilog-lexer_test.cc index b1f3788..cc18f98 100644 --- a/verible/verilog/parser/verilog-lexer_test.cc +++ b/verible/verilog/parser/verilog-lexer_test.cc
@@ -2035,6 +2035,23 @@ {'/', "/"}, {TK_TimeLiteral, "1ps"}, {TK_NEWLINE, "\n"}}, + // Issue #2605: CRLF `//` comments in TIMESCALE_DIRECTIVE must not keep `\r`. + {{DR_timescale, "`timescale"}, + {TK_SPACE, " "}, + {TK_TimeLiteral, "1ps"}, + {'/', "/"}, + {TK_TimeLiteral, "1ps"}, + {TK_NEWLINE, "\r\n"}, + {TK_EOL_COMMENT, "// hello"}, + {TK_NEWLINE, "\r\n"}}, + {{DR_timescale, "`timescale"}, + {TK_SPACE, " "}, + {TK_TimeLiteral, "1ps"}, + {'/', "/"}, + {TK_TimeLiteral, "1ps"}, + {TK_SPACE, " "}, + {TK_EOL_COMMENT, "// hello"}, + {TK_NEWLINE, "\r\n"}}, // TODO(b/134180314): lexer current drops tokens from pragma sections, // but instead emit the tokens and filter them out. // {{DR_pragma, "`pragma"}, " fragma", {TK_NEWLINE, "\n"}},
diff --git a/verible/verilog/parser/verilog.lex b/verible/verilog/parser/verilog.lex index 759fcc2..5c4cf3b 100644 --- a/verible/verilog/parser/verilog.lex +++ b/verible/verilog/parser/verilog.lex
@@ -990,7 +990,13 @@ return TK_COMMENT_BLOCK; } {EndOfLineComment} { - yyless(yyleng-1); /* return \n to input stream */ + // Match IN_EOL_COMMENT: CRLF is a two-character terminator. Using + // yyless(yyleng-1) left `\r` in the comment token, so the formatter + // emitted `\r\r\n` after `timescale comments (issue #2605, PR #2371). + if (yyleng >= 2 && yytext[yyleng - 2] == '\r') + yyless(yyleng - 2); /* return \r\n to input stream */ + else + yyless(yyleng - 1); /* return \n to input stream */ UpdateLocation(); return TK_EOL_COMMENT; }