From f34eb7d9f3b32b04a7a4e4da36ff48abcbc6da66 Mon Sep 17 00:00:00 2001 From: Aaron <2858172+lith-x@users.noreply.github.com> Date: Mon, 23 Dec 2024 14:23:46 -0600 Subject: [PATCH] Prevent trailing _ for all numbers (#1706) * moved _ check to scan_number_suffix * Skipping underscore on list-operators command * Disallow # from operator, update release notes --- releasenotes.md | 2 ++ src/compiler/compiler.c | 2 +- src/compiler/lexer.c | 16 +++++----------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/releasenotes.md b/releasenotes.md index a867b1ac1..f9f302d6f 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -17,6 +17,8 @@ - Fix bug outputting exported functions without predefined extname. - Fix problem where crt1 was linked for dynamic libraries on Linux and BSD. #1710 - Fix CRT detection on Arch Linux. +- Fix lexer allowing a trailing underscore (_) with hex and binary literals. +- Fix `--list-operators` CLI command printing underscore (_) and hash (#). ### Stdlib changes - Increase BitWriter.write_bits limit up to 32 bits. diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index ec532bfcf..0170235b0 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -939,7 +939,7 @@ void print_syntax(BuildOptions *options) if (i == TOKEN_DOCS_START || i == TOKEN_DOCS_END) continue; const char *name = token_type_to_string((TokenType)i); char first_char = name[0]; - if (first_char == '$' || first_char == '@' + if (first_char == '$' || first_char == '@' || first_char == '_' || first_char == '#' || (first_char >= 'a' && first_char <= 'z') || (first_char >= 'A' && first_char <= 'Z')) { diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c index 3fb0e36fe..e1659f6da 100644 --- a/src/compiler/lexer.c +++ b/src/compiler/lexer.c @@ -370,6 +370,11 @@ static inline bool scan_ident(Lexer *lexer, TokenType normal, TokenType const_to */ static bool scan_number_suffix(Lexer *lexer, bool *is_float) { + if (prev(lexer) == '_') + { + backtrack(lexer); + return add_error_token_at_current(lexer, "The number ended with '_', which isn't allowed, please remove it."); + } char c = peek(lexer); if (!char_is_alphanum_(c)) return true; switch (c | 32) @@ -535,11 +540,6 @@ static inline bool scan_hex(Lexer *lexer) is_float = true; if (!scan_exponent(lexer)) return false; } - if (prev(lexer) == '_') - { - backtrack(lexer); - return add_error_token_at_current(lexer, "The number ended with '_', which isn't allowed, please remove it."); - } if (!scan_number_suffix(lexer, &is_float)) return false; return new_token(lexer, is_float ? TOKEN_REAL : TOKEN_INTEGER, lexer->lexing_start); } @@ -581,12 +581,6 @@ static inline bool scan_dec(Lexer *lexer) is_float = true; if (!scan_exponent(lexer)) return false; } - - if (prev(lexer) == '_') - { - backtrack(lexer); - return add_error_token_at_current(lexer, "The number ended with '_', which isn't allowed, please remove it."); - } if (!scan_number_suffix(lexer, &is_float)) return false; return new_token(lexer, is_float ? TOKEN_REAL : TOKEN_INTEGER, lexer->lexing_start); }