From f9e9cac6e8be8771f9e42e559692f102651a070f Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sat, 16 Nov 2024 00:02:03 +0100 Subject: [PATCH] Cleanup and better contract error messages. --- src/compiler/enums.h | 4 ++-- src/compiler/lexer.c | 15 --------------- src/compiler/parse_global.c | 2 +- src/utils/lib.h | 1 + src/utils/stringutils.c | 16 ++++++++++++++++ 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/compiler/enums.h b/src/compiler/enums.h index 48b26482b..ab88f8e21 100644 --- a/src/compiler/enums.h +++ b/src/compiler/enums.h @@ -1240,8 +1240,8 @@ typedef enum TOKEN_CT_VAEXPR, // $vaexpr, TOKEN_CT_VASPLAT, // $vasplat, TOKEN_LAST_KEYWORD = TOKEN_CT_VASPLAT, - TOKEN_DOCS_START, // /** - TOKEN_DOCS_END, // */ (may start with an arbitrary number of `*` + TOKEN_DOCS_START, // <* + TOKEN_DOCS_END, // *> TOKEN_DOCS_EOL, TOKEN_EOF, // \n - SHOULD ALWAYS BE THE LAST TOKEN. diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c index 1cabdd1ac..42ad93a06 100644 --- a/src/compiler/lexer.c +++ b/src/compiler/lexer.c @@ -1159,21 +1159,6 @@ static inline bool scan_base64(Lexer *lexer) // --- Lexer doc lexing - -INLINE void skip_to_doc_line_end(Lexer *lexer) -{ - // Let's skip to either EOF, EOL or */ - char c = peek(lexer); - while (1) - { - if (reached_end(lexer)) return; - if (c == '\n') return; - if (c == '*' && peek_next(lexer) == '/') return; - c = next(lexer); - } -} - - /** * Parse the <* *> directives comments **/ diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index 8ceb8ca24..0fd0e30b2 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -2526,7 +2526,7 @@ static inline bool parse_doc_contract(ParseContext *c, AstId *docs, AstId **docs scratch_buffer_append("@require \""); break; } - scratch_buffer_append_len(start, end - start); + scratch_buffer_append_remove_space(start, end - start); scratch_buffer_append("\" violated"); if (try_consume(c, TOKEN_COLON)) { diff --git a/src/utils/lib.h b/src/utils/lib.h index ae2e56fe3..c131638a7 100644 --- a/src/utils/lib.h +++ b/src/utils/lib.h @@ -161,6 +161,7 @@ void scratch_buffer_append_len(const char *string, size_t len); void scratch_buffer_append_char(char c); void scratch_buffer_append_in_quote(const char *string); void scratch_buffer_append_char_repeat(char c, size_t count); +void scratch_buffer_append_remove_space(const char *start, int len); void scratch_buffer_append_signed_int(int64_t i); void scratch_buffer_append_double(double d); void scratch_buffer_append_shell_escaped(const char *string); diff --git a/src/utils/stringutils.c b/src/utils/stringutils.c index 48a9af821..dd46ce498 100644 --- a/src/utils/stringutils.c +++ b/src/utils/stringutils.c @@ -479,6 +479,22 @@ void scratch_buffer_append_in_quote(const char *string) } } +void scratch_buffer_append_remove_space(const char *start, int len) +{ + char clast = ' '; + int printed = 0; + for (int i = 0; i < len; i++) + { + char ch = start[i]; + if (ch == '\n' || ch == '\t') ch = ' '; + if (ch == ' ' && clast == ch) continue; + scratch_buffer_append_char(ch); + clast = ch; + printed++; + } + if (clast == ' ' && printed > 0) scratch_buffer.len--; +} + void scratch_buffer_append_char(char c) { if (scratch_buffer.len + 1 > MAX_STRING_BUFFER - 1)