From 8ba87e01bd9a26ffd391d2ad69ff0eb6a023d1cf Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 24 Aug 2024 13:56:30 +0100 Subject: [PATCH] Improve `parse_enforce_tabs` usage --- src/parser.cpp | 22 +++++++++++++++++----- src/parser.hpp | 5 ++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/parser.cpp b/src/parser.cpp index 1a45c4ea5..b250a3163 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1921,6 +1921,9 @@ gb_internal Array parse_enum_field_list(AstFile *f) { f->curr_token.kind != Token_EOF) { CommentGroup *docs = f->lead_comment; CommentGroup *comment = nullptr; + + parse_enforce_tabs(f); + Ast *name = parse_value(f); Ast *value = nullptr; if (f->curr_token.kind == Token_Eq) { @@ -2259,6 +2262,7 @@ gb_internal Array parse_union_variant_list(AstFile *f) { auto variants = array_make(ast_allocator(f)); while (f->curr_token.kind != Token_CloseBrace && f->curr_token.kind != Token_EOF) { + parse_enforce_tabs(f); Ast *type = parse_type(f); if (type->kind != Ast_BadExpr) { array_add(&variants, type); @@ -4274,6 +4278,7 @@ gb_internal Ast *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_fl while (f->curr_token.kind != follow && f->curr_token.kind != Token_Colon && f->curr_token.kind != Token_EOF) { + if (!is_signature) parse_enforce_tabs(f); u32 flags = parse_field_prefixes(f); Ast *param = parse_var_type(f, allow_ellipsis, allow_typeid_token); if (param->kind == Ast_Ellipsis) { @@ -4363,6 +4368,8 @@ gb_internal Ast *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_fl f->curr_token.kind != Token_EOF && f->curr_token.kind != Token_Semicolon) { CommentGroup *docs = f->lead_comment; + + if (!is_signature) parse_enforce_tabs(f); u32 set_flags = parse_field_prefixes(f); Token tag = {}; Array names = parse_ident_list(f, allow_poly_names); @@ -5375,6 +5382,11 @@ gb_internal u64 check_vet_flags(AstFile *file) { gb_internal void parse_enforce_tabs(AstFile *f) { + // Checks to see if tabs have been used for indentation + if ((check_vet_flags(f) & VetFlag_Tabs) == 0) { + return; + } + Token prev = f->prev_token; Token curr = f->curr_token; if (prev.pos.line < curr.pos.line) { @@ -5391,6 +5403,10 @@ gb_internal void parse_enforce_tabs(AstFile *f) { isize len = end-it; for (isize i = 0; i < len; i++) { + if (it[i] == '/') { + // ignore comments + break; + } if (it[i] == ' ') { syntax_error(curr, "With '-vet-tabs', tabs must be used for indentation"); break; @@ -5405,11 +5421,7 @@ gb_internal Array parse_stmt_list(AstFile *f) { while (f->curr_token.kind != Token_case && f->curr_token.kind != Token_CloseBrace && f->curr_token.kind != Token_EOF) { - - // Checks to see if tabs have been used for indentation - if (check_vet_flags(f) & VetFlag_Tabs) { - parse_enforce_tabs(f); - } + parse_enforce_tabs(f); Ast *stmt = parse_stmt(f); if (stmt && stmt->kind != Ast_EmptyStmt) { diff --git a/src/parser.hpp b/src/parser.hpp index 565a8e621..f1794f79a 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -885,4 +885,7 @@ gb_internal gb_inline gbAllocator ast_allocator(AstFile *f) { gb_internal Ast *alloc_ast_node(AstFile *f, AstKind kind); gb_internal gbString expr_to_string(Ast *expression); -gb_internal bool allow_field_separator(AstFile *f); \ No newline at end of file +gb_internal bool allow_field_separator(AstFile *f); + + +gb_internal void parse_enforce_tabs(AstFile *f); \ No newline at end of file