From b1cc97cc77d6c4fd679316945a08c45061033e13 Mon Sep 17 00:00:00 2001 From: hl-valdemar Date: Fri, 30 Oct 2026 12:39:18 +0100 Subject: [PATCH] refactor: use grouped matches for parser token membership --- docs/style.md | 1 + source/parser/parse.hon | 32 ++++++++++++-------------------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/docs/style.md b/docs/style.md index 4353097..efd1281 100644 --- a/docs/style.md +++ b/docs/style.md @@ -44,6 +44,7 @@ while ( ``` - use early returns for terminal cases so the main path stays shallow. +- use grouped `match` arms for boolean enum membership; reserve compile-time enum maps for enum-to-value data mappings. - register `defer` cleanup immediately after acquiring a resource. use `errdefer` for cleanup needed only until ownership is transferred successfully. ## allocation and ownership diff --git a/source/parser/parse.hon b/source/parser/parse.hon index 8a33746..e5826d5 100644 --- a/source/parser/parse.hon +++ b/source/parser/parse.hon @@ -941,32 +941,24 @@ parse_function_type proc(state @mut State, start ast.Span, invalid bool) ast.Nod return id } -@hide:file -type_starters :: $enummap.init_default(lexer.TokenKind, bool, false, { - ident = true, open_bracket = true, struct = true, c_struct = true, opaque = true, - enum = true, union = true, distinct = true, alias = true, match = true, if = true, - at = true, star = true, question = true, open_paren = true, proc = true, c_func = true, -}) - @hide:file starts_type proc(kind lexer.TokenKind) bool { - return enummap.get(&type_starters, kind) != null + return match kind { + .ident, .open_bracket, .struct, .c_struct, .opaque, .enum, .union, .distinct, + .alias, .match, .if, .at, .star, .question, .open_paren, .proc, .c_func: true + else: false + } } -@hide:file -member_names :: $enummap.init_default(lexer.TokenKind, bool, false, { - ident = true, if = true, else = true, for = true, inline = true, while = true, - proc = true, return = true, yield = true, break = true, continue = true, - true = true, false = true, and = true, or = true, xor = true, mut = true, - struct = true, c_struct = true, opaque = true, enum = true, union = true, - distinct = true, alias = true, match = true, import = true, test = true, - c_func = true, null = true, undefined = true, try = true, catch = true, - defer = true, errdefer = true, unreachable = true, -}) - @hide:file member_name proc(kind lexer.TokenKind) bool { - return enummap.get(&member_names, kind) != null + return match kind { + .ident, .if, .else, .for, .inline, .while, .proc, .return, .yield, .break, + .continue, .true, .false, .and, .or, .xor, .mut, .struct, .c_struct, .opaque, + .enum, .union, .distinct, .alias, .match, .import, .test, .c_func, .null, + .undefined, .try, .catch, .defer, .errdefer, .unreachable: true + else: false + } } @hide:file