From 5fb6ebef2832669dbe38cd4f014a3c348be76640 Mon Sep 17 00:00:00 2001 From: omicron Date: Tue, 1 Apr 2025 19:55:00 +0200 Subject: [PATCH] Add functions to skip over trivia in a tokenlist --- src/tokenlist.c | 23 +++++++++++++++++++++++ src/tokenlist.h | 10 ++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/tokenlist.c b/src/tokenlist.c index 0a102b1..84c2c39 100644 --- a/src/tokenlist.c +++ b/src/tokenlist.c @@ -81,3 +81,26 @@ error_t *tokenlist_fill(tokenlist_t *list, lexer_t *lex) { return err; return nullptr; } + +bool is_trivia(tokenlist_entry_t *trivia) { + switch (trivia->token.id) { + case TOKEN_WHITESPACE: + case TOKEN_COMMENT: + case TOKEN_NEWLINE: + return true; + default: + return false; + } +} + +tokenlist_entry_t *tokenlist_skip_trivia(tokenlist_entry_t *current) { + while (current && is_trivia(current)) + current = current->next; + return current; +} + +tokenlist_entry_t *tokenlist_next(tokenlist_entry_t *current) { + if (!current) + return nullptr; + return tokenlist_skip_trivia(current->next); +} diff --git a/src/tokenlist.h b/src/tokenlist.h index 25d75e3..22ac5ca 100644 --- a/src/tokenlist.h +++ b/src/tokenlist.h @@ -27,4 +27,14 @@ error_t *tokenlist_fill(tokenlist_t *list, lexer_t *lex); void tokenlist_free(tokenlist_t *list); +/** + * Return the first token entry that isn't whitespace, newline or comment + */ +tokenlist_entry_t *tokenlist_skip_trivia(tokenlist_entry_t *current); + +/** + * Return the next token entry that isn't whitespace, newline or comment + */ +tokenlist_entry_t *tokenlist_next(tokenlist_entry_t *current); + #endif // INCLUDE_SRC_TOKENLIST_H_