Extend parse_token to accept an optional validator function

This commit is contained in:
omicron 2025-04-01 21:10:19 +02:00
parent fcbf96af48
commit f4638d43b6
2 changed files with 8 additions and 3 deletions

View File

@ -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, 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,
if (current->token.id != token_id) token_validator_t is_valid) {
if (current->token.id != token_id ||
(is_valid && !is_valid(&current->token)))
return parse_no_match(); return parse_no_match();
ast_node_t *node; ast_node_t *node;

View File

@ -11,11 +11,14 @@ typedef struct parse_result {
ast_node_t *node; ast_node_t *node;
} parse_result_t; } parse_result_t;
typedef bool (*token_validator_t)(lexer_token_t *);
parse_result_t parse_error(error_t *err); parse_result_t parse_error(error_t *err);
parse_result_t parse_no_match(); parse_result_t parse_no_match();
parse_result_t parse_success(ast_node_t *ast, tokenlist_entry_t *next); parse_result_t parse_success(ast_node_t *ast, tokenlist_entry_t *next);
parse_result_t parse_token(tokenlist_entry_t *current, 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 *); tokenlist_entry_t *skip_insignificant(tokenlist_entry_t *);