From f4638d43b614a159472a6b48cc4ad146b2528485 Mon Sep 17 00:00:00 2001 From: omicron Date: Tue, 1 Apr 2025 21:10:19 +0200 Subject: [PATCH] Extend parse_token to accept an optional validator function --- src/parser_util.c | 6 ++++-- src/parser_util.h | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/parser_util.c b/src/parser_util.c index 1ece241..17e3027 100644 --- a/src/parser_util.c +++ b/src/parser_util.c @@ -16,8 +16,10 @@ parse_result_t parse_success(ast_node_t *ast, tokenlist_entry_t *next) { } parse_result_t parse_token(tokenlist_entry_t *current, - lexer_token_id_t token_id, node_id_t ast_id) { - if (current->token.id != token_id) + lexer_token_id_t token_id, node_id_t ast_id, + token_validator_t is_valid) { + if (current->token.id != token_id || + (is_valid && !is_valid(¤t->token))) return parse_no_match(); ast_node_t *node; diff --git a/src/parser_util.h b/src/parser_util.h index f21af58..8d7936a 100644 --- a/src/parser_util.h +++ b/src/parser_util.h @@ -11,11 +11,14 @@ typedef struct parse_result { ast_node_t *node; } parse_result_t; +typedef bool (*token_validator_t)(lexer_token_t *); + parse_result_t parse_error(error_t *err); parse_result_t parse_no_match(); parse_result_t parse_success(ast_node_t *ast, tokenlist_entry_t *next); parse_result_t parse_token(tokenlist_entry_t *current, - lexer_token_id_t token_id, node_id_t ast_id); + lexer_token_id_t token_id, node_id_t ast_id, + token_validator_t is_valid); tokenlist_entry_t *skip_insignificant(tokenlist_entry_t *);